contrib/python-hook-examples.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Mon, 25 Sep 2023 12:14:38 +0200
changeset 51013 93a44c1ba0c6
parent 48875 6000f5b25c9b
permissions -rw-r--r--
revlog: adapt the `reading` check for `unionrepo` We cannot just rely on the length check for the `unionrepo` as the local revlog might be empty while the other revlog contains data. In addition, we need to also open the second revlog for reading when needed.

'''
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)))