Python 如何编写基本的mercurial扩展?
因此,我想做一个基本的mercurial扩展,在提交消息中添加一个字符串。下面显示的代码放在文件-Python 如何编写基本的mercurial扩展?,python,mercurial,Python,Mercurial,因此,我想做一个基本的mercurial扩展,在提交消息中添加一个字符串。下面显示的代码放在文件-myextension.py中,并包含在.hgrc中 当我运行hgcommit-m“A message”时,提交编辑器打开,消息“A message added”如预期所示。但是,如果我尝试按CTRL+X中止提交,则提交仍然会出现完整的、现在附加的消息 我做错了什么 from mercurial import commands, extensions def commit(originalcomm
myextension.py
中,并包含在.hgrc
中
当我运行hgcommit-m“A message”
时,提交编辑器打开,消息“A message added”
如预期所示。但是,如果我尝试按CTRL+X中止提交,则提交仍然会出现完整的、现在附加的消息
我做错了什么
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)
我认为这是正确的行为,因为临时文件 传递给编辑器的消息已包含提交消息。您可以覆盖 这在一定程度上是通过猴子修补 功能:
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)