view contrib/python-hook-examples.py @ 49370:1572f790ee5e stable

convert: remove old ElementTree import cruft from darcs All the `import elementtree` attempts seem to pre-date py2.5, when it was brought into the standard library, and the manual `cElementTree` fast implementation import has been unnecessary and deprecated since py3.3.
author Ian Moody <moz-ian@perix.co.uk>
date Wed, 15 Jun 2022 01:01:02 +0100
parents 6000f5b25c9b
children
line wrap: on
line source

'''
Examples of useful python hooks for Mercurial.
'''
from mercurial import (
    patch,
    util,
)


def diffstat(ui, repo, **kwargs):
    """Example usage:

    [hooks]
    commit.diffstat = python:/path/to/this/file.py:diffstat
    changegroup.diffstat = python:/path/to/this/file.py:diffstat
    """
    if kwargs.get('parent2'):
        return
    node = kwargs['node']
    first = repo[node].p1().node()
    if 'url' in kwargs:
        last = repo.changelog.tip()
    else:
        last = node
    diff = patch.diff(repo, first, last)
    ui.write(patch.diffstat(util.iterlines(diff)))