contrib/python-hook-examples.py
author Matt Harbison <matt_harbison@yahoo.com>
Sat, 10 Dec 2022 14:44:46 -0500
changeset 49795 f1e820cda2f5
parent 48875 6000f5b25c9b
permissions -rw-r--r--
typing: add type hints related to message output in mercurial/ui.py This will shake loose some bytes vs str issues in the doc checker.

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