contrib/python-hook-examples.py
author Alexander Solovyov <piranha@piranha.org.ua>
Thu, 26 Mar 2009 16:49:47 +0200
changeset 7917 5a5396f49420
child 7918 62f11ef0df5b
permissions -rw-r--r--
diffstat hook example
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7917
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
     1
'''
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
     2
These are examples of useful hooks in Python for Mercurial.
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
     3
'''
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
     4
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
     5
from mercurial import patch, util
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
     6
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
     7
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
     8
def diffstat(ui, repo, **kwargs):
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
     9
    '''Use it like:
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    10
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    11
    [hooks]
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    12
    commit.diffstat = python:/path/to/this/file.py:diffstat
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    13
    changegroup.diffstat = python:/path/to/this/file.py:diffstat
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    14
    '''
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    15
    if kwargs.get('parent2'):
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    16
        return
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    17
    node = kwargs['node']
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    18
    first = repo[node].parents()[0].node()
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    19
    if 'url' in kwargs:
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    20
        last = repo['tip'].node()
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    21
    else:
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    22
        last = node
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    23
    diff = patch.diff(repo, first, last)
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
    24
    ui.write(patch.diffstat(util.iterlines(diff)))