contrib/python-hook-examples.py
author Jun Wu <quark@fb.com>
Sun, 02 Jul 2017 20:08:09 -0700
changeset 33198 36b3febd739f
parent 28562 2b585677220e
child 39895 1a184b727aff
permissions -rw-r--r--
phabricator: add a contrib script The default Phabricator client arcanist is not friendly to send a stack of changesets. It works better when a feature branch is reviewed as a single review unit. However, we want multiple revisions per feature branch. To be able to have an `hg email`-like UX to send and receive a stack of commits easily, it seems we have to re-invent things. This patch adds `phabricator.py` speaking Conduit API [1] in `contrib` as the first step. This may also be an option for people who don't want to run PHP. Config could be done in `hgrc` (instead of `arcrc` or `arcconfig`): [phabricator] # API token. Get it from https://phab.mercurial-scm.org/conduit/login/ token = cli-xxxxxxxxxxxxxxxxxxxxxxxxxxxx url = https://phab.mercurial-scm.org/ # callsign is used by the next patch callsign = HG This patch only adds a single command: `debugcallconduit` to keep the patch size small. To test it, having the above config, and run: $ hg debugcallconduit diffusion.repository.search <<EOF > {"constraints": {"callsigns": ["HG"]}} > EOF The result will be printed in prettified JSON format. [1]: Conduit APIs are listed at https://phab.mercurial-scm.org/conduit/

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