2

所以,我想做一个基本的 mercurial 扩展,将一个字符串附加到提交消息中。下面显示的代码放置在一个文件中 -myextension.py并包含在.hgrc.

当我运行hg commit -m "A message"时,提交编辑器将打开,"A message APPENDED"并按预期显示消息。但是,如果我尝试通过按 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)
4

1 回答 1

1

我认为这是正确的行为,因为 mercurial 传递给您的编辑器的临时文件已经包含提交消息。您可以通过猴子修补cmdutil.commitforceeditor 函数在某种程度上覆盖它:

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)  
于 2012-09-14T10:42:14.517 回答