sapling/hgext3rd/gitrevset.py
Adam Simpkins 63b7bb19c5 templates: fix help messages for template keywords
Summary:
Many of the template keywords in our extensions were being registered
incorrectly, causing their help output to be rendered incorrectly in the
"hg help templates" output.  The ones in smartlog.py were particularly bad, as
most of them showed only their description, without displaying the name of the
template.  In smartlog.py only singlepublicsuccessor was being displayed
correctly, because it's docstring explicitly included it's own name at the
start.

This fixes all of our extensions to consistently use the
registrar.templatekeyword() decorator to register the keywords.  This decorator
automatically prefixes the help message with the keyword name.  The
mercurial/extensions.py code will explicitly check to see if an extension
contains an "templatekeyword" attribute, and if so it will register any
keywords contained in this registry after calling extsetup().

Test Plan:
Added new unit tests to check the output of "hg help templates" for the
affected keywords.

Reviewers: #sourcecontrol, kulshrax, ikostia, rmcelroy

Reviewed By: rmcelroy

Subscribers: rmcelroy, net-systems-diffs@, yogeshwer, mjpieters

Differential Revision: https://phabricator.intern.facebook.com/D4427729

Signature: t1:4427729:1484831476:17b478a5e867dfc3f85402588c381bf8b1831107
2017-01-19 12:52:54 -08:00

77 lines
2.4 KiB
Python

# gitrevset.py
#
# Copyright 2014 Facebook, Inc.
"""map a git hash to a Mercurial hash:
$ hg log -r 'gitnode($HASH)'
$ hg id -r 'gitnode($HASH)'
shortversion:
$ hg log -r 'g$HASH'
$ hg id -r 'g$HASH'
"""
from mercurial import extensions
from mercurial import error
from mercurial import hg
from mercurial import registrar
from mercurial import revset
from mercurial.i18n import _
import re
githashre = re.compile('g([0-9a-fA-F]{40,40})')
templatekeyword = registrar.templatekeyword()
@templatekeyword("gitnode")
def showgitnode(repo, ctx, templ, **args):
"""Return the git revision corresponding to a given hg rev"""
peerpath = repo.ui.expandpath('default')
# sshing can cause junk 'remote: ...' output to stdout, so we need to
# redirect it temporarily so automation can parse the result easily.
oldfout = repo.ui.fout
try:
repo.baseui.fout = repo.ui.ferr
remoterepo = hg.peer(repo, {}, peerpath)
remoterev = remoterepo.lookup('_gitlookup_hg_%s' % ctx.hex())
except error.RepoError:
# templates are expected to return an empty string when no data exists
return ''
finally:
repo.baseui.fout = oldfout
return remoterev.encode('hex')
def gitnode(repo, subset, x):
"""``gitnode(id)``
Return the hg revision corresponding to a given git rev."""
l = revset.getargs(x, 1, 1, _("id requires one argument"))
n = revset.getstring(l[0], _("id requires a string"))
peerpath = repo.ui.expandpath('default')
# sshing can cause junk 'remote: ...' output to stdout, so we need to
# redirect it temporarily so automation can parse the result easily.
oldfout = repo.ui.fout
try:
repo.baseui.fout = repo.ui.ferr
remoterepo = hg.peer(repo, {}, peerpath)
remoterev = remoterepo.lookup('_gitlookup_git_%s' % n)
finally:
repo.baseui.fout = oldfout
rn = repo[remoterev].rev()
return subset.filter(lambda r: r == rn)
def overridestringset(orig, repo, subset, x):
m = githashre.match(x)
if m is not None:
return gitnode(repo, subset, ('string', m.group(1)))
return orig(repo, subset, x)
def extsetup(ui):
revset.symbols['gitnode'] = gitnode
extensions.wrapfunction(revset, 'stringset', overridestringset)
revset.symbols['stringset'] = revset.stringset
revset.methods['string'] = revset.stringset
revset.methods['symbol'] = revset.stringset