Source

committer / hgcommitter.py

# committer.py - record the committer of other people's patches
#
# Copyright 2011 Eric Eisner
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2, incorporated herein by reference.

'''
Add committer extra to your commits when the supplied user isn't you.


To set another user as author with yourself as committer:

  $ hg commit -u author

To set an author without a committer:

  $ HGUSER=user hg commit

To set the committer and author explicitly:

  $ HGUSER=committer hg commit -u author
'''

import os
from mercurial import encoding, extensions, localrepo, revset, templatekw
from mercurial.i18n import _

def commit(orig, self, text="", user=None, date=None, match=None, force=False,
           editor=False, extra={}):
    # record the committer if the supplied user is different
    committer = os.environ.get("HGUSER")
    if committer is None:
        committer = self.ui.config("ui", "username")
        if committer is not None:
            committer = os.path.expandvars(committer)
    if committer is None:
        committer = os.environ.get("EMAIL")

    if committer and user and user != committer:
        extra = dict(extra)
        extra['committer'] = encoding.fromlocal(committer.strip())
    return orig(self, text, user, date, match, force, editor, extra)

def revsetcommitter(repo, subset, x):
    """``committer([user])``
    Changesets with committer [matching the given user].
    """
    if x is None:
        query = ''
    else:
        query = revset.getstring(x, _('committer requires a string')).lower()
    revs = []
    for r in subset:
        extra = repo[r].extra()
        if 'committer' in extra and query in extra.get('committer').lower():
            revs.append(r)
    return revs

def kwcommitter(repo, ctx, **args):
    """:committer: String. The committer of the changeset if any."""
    if 'committer' in ctx.extra():
        return ctx.extra().get('committer')

def extsetup(ui):
    revset.symbols['committer'] = revsetcommitter
    templatekw.keywords['committer'] = kwcommitter
    extensions.wrapfunction(localrepo.localrepository, 'commit', commit)