contrib/python-hook-examples.py
author Augie Fackler <augie@google.com>
Mon, 06 Feb 2017 23:57:21 -0500
changeset 31033 861b070d92da
parent 28562 2b585677220e
child 39895 1a184b727aff
permissions -rw-r--r--
qdiff: migrate to modern pager API This results in the default pager-attend list being empty. Sadly, we can't let the code be that way, because some legacy extensions depend on hooking the pager's attend list at import time (and we'd like to not break them), and if the list is actually *empty* that triggers magic behavior in the extension that attends everything. Instead, we put a long, improbable command name as the only entry in the attend list.

'''
Examples of useful python hooks for Mercurial.
'''
from __future__ import absolute_import
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['tip'].node()
    else:
        last = node
    diff = patch.diff(repo, first, last)
    ui.write(patch.diffstat(util.iterlines(diff)))