sapling/edenscm/hgext/myparent.py
Jun Wu 9dc21f8d0b codemod: import from the edenscm package
Summary:
D13853115 adds `edenscm/` to `sys.path` and code still uses `import mercurial`.
That has nasty problems if both `import mercurial` and
`import edenscm.mercurial` are used, because Python would think `mercurial.foo`
and `edenscm.mercurial.foo` are different modules so code like
`try: ... except mercurial.error.Foo: ...`, or `isinstance(x, mercurial.foo.Bar)`
would fail to handle the `edenscm.mercurial` version. There are also some
module-level states (ex. `extensions._extensions`) that would cause trouble if
they have multiple versions in a single process.

Change imports to use the `edenscm` so ideally the `mercurial` is no longer
imported at all. Add checks in extensions.py to catch unexpected extensions
importing modules from the old (wrong) locations when running tests.

Reviewed By: phillco

Differential Revision: D13868981

fbshipit-source-id: f4e2513766957fd81d85407994f7521a08e4de48
2019-01-29 17:25:32 -08:00

105 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).
"""
import re
from edenscm.mercurial import registrar
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()