Reputation: 42798
So, I would like to make a basic mercurial extension that appends a string to the commit message. Below shown code is placed in a file - myextension.py
and included by .hgrc
.
When I run hg commit -m "A message"
, the commit editor opens with the message "A message APPENDED"
shown as expected. If I however try to abort the commit by pressing CTRL+X, the commit still occurs with the full, now appended message.
What am I doing wrong here?
from mercurial import commands, extensions
def commit(originalcommit, ui, repo, *pats, **opts):
if not opts["message"]:
return originalcommit(ui, repo, *pats, **opts)
else:
opts["force_editor"] = True
opts["message"] += " APPENDED"
return originalcommit(ui, repo, *pats, **opts)
def uisetup(ui):
extensions.wrapcommand(commands.table, 'commit', commit)
Upvotes: 2
Views: 442
Reputation: 4107
I think this is the correct behaviour, since the temporary file that mercurial passes to your editor already contains a commit message. You can override this to some extend by monkey-patching the cmdutil.commitforceeditor function:
from mercurial import commands, extensions, cmdutil, util
cfe = cmdutil.commitforceeditor
def commitforceeditor(repo, ctx, subs):
text = cfe(repo, ctx, subs)
# Do not commit unless the commit message differs from
# the one you specified on the command line
if ctx.description() == text.strip():
raise util.Abort("empty commit message")
else:
return text
def commit(originalcommit, ui, repo, *pats, **opts):
if not opts["message"]:
return originalcommit(ui, repo, *pats, **opts)
else:
opts["force_editor"] = True
opts["message"] += " APPENDED"
# monkey-patch
cmdutil.commitforceeditor = commitforceeditor
return originalcommit(ui, repo, *pats, **opts)
def uisetup(ui):
extensions.wrapcommand(commands.table, 'commit', commit)
Upvotes: 1