extension to print commit message that would be supplied to editor

Summary:
the nuclide team would like to be able to get the commit template
message without actually invoking a commit. This small extensions allows them
to do this.

Caveats: ignores subrepositories.

Test Plan: new test

Reviewers: #mercurial, most

Subscribers: mjpieters

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

Tasks: 12771006
This commit is contained in:
Ryan McElroy 2016-10-03 04:39:51 -07:00
parent db64f55505
commit 78f0c73d4b
2 changed files with 78 additions and 0 deletions

View File

@ -0,0 +1,43 @@
from mercurial import (
cmdutil,
context,
error,
)
from mercurial.i18n import _
cmdtable = {}
command = cmdutil.command(cmdtable)
@command('debugcommitmessage', [], _('FORM'))
def debugcommitmessage(ui, repo, *args):
form = None
if len(args) > 1:
raise error.Abort(_('provide at most one form'))
elif len(args) > 0:
form = args[0]
status = repo.status()
text = ''
user = None
date = None
extra = None
ctx = context.workingcommitctx(repo, status, text, user, date, extra)
subs = []
editform = form or 'commit.normal.normal'
extramsg = _('Leave message empty to abort commit.')
forms = [e for e in editform.split('.') if e]
forms.insert(0, 'changeset')
while forms:
tmpl = repo.ui.config('committemplate', '.'.join(forms))
if tmpl:
committext = cmdutil.buildcommittemplate(
repo, ctx, subs, extramsg, tmpl)
break
forms.pop()
else:
committext = cmdutil.buildcommittext(repo, ctx, subs, extramsg)
ui.status(committext)

View File

@ -0,0 +1,35 @@
Set up extension
$ cat >> $HGRCPATH <<EOF
> [extensions]
> debugcommitmessage = $TESTDIR/../hgext3rd/debugcommitmessage.py
> EOF
Set up repo
$ hg init repo
$ cd repo
Test extension
$ hg debugcommitmessage
HG: Enter commit message. Lines beginning with 'HG:' are removed.
HG: Leave message empty to abort commit.
HG: --
HG: user: test
HG: branch 'default'
HG: no files changed
$ hg debugcommitmessage --config committemplate.changeset.commit.normal.normal="Test Specific Message\n"
Test Specific Message
$ hg debugcommitmessage --config committemplate.changeset.commit="Test Generic Message\n"
Test Generic Message
$ hg debugcommitmessage commit.amend.normal --config committemplate.changeset.commit="Test Generic Message\n"
Test Generic Message
$ hg debugcommitmessage randomform --config committemplate.changeset.commit="Test Generic Message\n"
HG: Enter commit message. Lines beginning with 'HG:' are removed.
HG: Leave message empty to abort commit.
HG: --
HG: user: test
HG: branch 'default'
HG: no files changed