sapling/hgext3rd/myparent.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

94 lines
3.2 KiB
Python

# myparent.py
#
# Copyright 2016 Facebook, Inc.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
'''
myparent.py - Commit template keywords based on your previous commit
If your diff stacks are comprised of related diffs, many commits will share the
same reviewers, tasks and even the title prefix. With this extension, mercurial
can prefill the relevant fields based on your previous commit in the stack.
The extension adds five new keywords:
- *myparentdiff* the diff number of the parent commit
- *myparentreviewers* the reviewers of the parent commit
- *myparentsubscribers* the subscribers of the parent commit
- *myparenttasks* the tasks of the parent commit
- *myparenttitleprefix* the prefix as defined by [] of the parent commit.
E.g. '[e2e automation] foo bar' -> '[e2e automation]'
After enabling the extension, change the default commit template:
[committemplate]
emptymsg={myparenttitleprefix}
Summary: {myparentdiff}
Test Plan:
Reviewers: {myparentreviewers}
Subscribers: {myparentsubscribers}
Tasks: {myparenttasks}
Blame Revision:
In some (all?) repositories at Facebook the commit template is overridden at
the repository level. If that is the case, add the line above to the `.hg/hgrc`
file inside the repository (e.g. ~/www/.hg/hgrc).
'''
from mercurial import registrar
import re
templatekeyword = registrar.templatekeyword()
@templatekeyword("myparentdiff")
def showmyparentdiff(repo, ctx, templ, **args):
"""Show the differential revision of the commit's parent, if it has the
same author as this commit.
"""
return extract_from_parent(ctx, 'Differential Revision:.*/(D\d+)')
@templatekeyword("myparentreviewers")
def showmyparentreviewers(repo, ctx, templ, **args):
"""Show the reviewers of the commit's parent, if it has the
same author as this commit.
"""
return extract_from_parent(ctx, '\s*Reviewers: (.*)')
@templatekeyword("myparentsubscribers")
def showmyparentsubscribers(repo, ctx, templ, **args):
"""Show the subscribers of the commit's parent, if it has the
same author as this commit.
"""
return extract_from_parent(ctx, '\s*Subscribers: (.*)')
@templatekeyword("myparenttasks")
def showmyparenttasks(repo, ctx, templ, **args):
"""Show the tasks from the commit's parent, if it has the
same author as this commit.
"""
return extract_from_parent(ctx, '\s*(?:Tasks|Task ID): (.*)')
@templatekeyword("myparenttitleprefix")
def showmyparenttitleprefix(repo, ctx, templ, **args):
"""Show the title prefix of the commit's parent, if it has the
same author as this commit.
"""
if not p1_is_same_user(ctx):
return ''
descr = ctx.p1().description()
title = descr.splitlines()[0]
prefix_end = title.find(']')
return title[0:prefix_end + 1] if prefix_end > 0 else ''
def extract_from_parent(ctx, pattern):
if not p1_is_same_user(ctx):
return ''
descr = ctx.p1().description()
match = re.search(pattern, descr)
return match.group(1) if match else ''
def p1_is_same_user(ctx):
return ctx.user() == ctx.p1().user()