如何设置或修改mercurial扩展中的提交消息?

时间:2011-10-21 19:00:32

标签: python mercurial fogbugz mercurial-extension

我正在尝试修改this Mercurial extension以提示用户在其提交消息中添加FogBugz案例编号。理想情况下,我希望用户在收到提示后只输入一个数字,并将其自动附加到提交消息中。

这是我到目前为止所得到的:

def pretxncommit(ui, repo, **kwargs):
    tip = repo.changectx(repo.changelog.tip())
    if not RE_CASE.search(tip.description()) and len(tip.parents()) < 2:
        casenumResponse = ui.prompt('*** Please specify a case number, x to abort, or hit enter to ignore:', '')
        casenum = RE_CASENUM.search(casenumResponse)        
        if casenum:
            # this doesn't work!
            # tip.description(tip.description() + ' (Case ' + casenum.group(0) + ')')
            return True
        elif (casenumResponse == 'x'):
            ui.warn('*** User aborted\n')
            return True
        return True
    return False

我无法找到的是编辑提交消息的方法。 tip.description似乎是只读的,我在文档或示例中没有看到任何可以让我修改它的内容。我看到编辑提交消息的唯一引用与补丁和Mq扩展有关,看起来似乎没有帮助。

关于如何设置提交消息的任何想法?

2 个答案:

答案 0 :(得分:6)

我最终没有找到使用钩子的方法,但我能够使用extensions.wrapcommand并修改选项。

我在这里包含了结果扩展的来源。

在提交消息中检测到缺少案例后,我的版本会提示用户输入一个,忽略警告或中止提交。

如果用户通过指定案例编号来响应提示,则会将其附加到现有提交消息中。

如果用户以“x”响应,则提交将中止,并且更改仍然未完成。

如果用户仅通过按Enter键进行响应,则提交将继续进行原始无提示提交消息。

我还添加了nofb选项,如果用户有意提交没有案例编号的提交,则会跳过提示。

这是扩展名:

"""fogbugzreminder

Reminds the user to include a FogBugz case reference in their commit message if none is specified
"""

from mercurial import commands, extensions
import re

RE_CASE = re.compile(r'(case):?\s*\d+', re.IGNORECASE)
RE_CASENUM = re.compile(r'\d+', re.IGNORECASE)

def commit(originalcommit, ui, repo, **opts):

    haschange = False   
    for changetype in repo.status():
        if len(changetype) > 0:
            haschange = True

    if not haschange and ui.config('ui', 'commitsubrepos', default=True):
        ctx = repo['.']
        for subpath in sorted(ctx.substate):
            subrepo = ctx.sub(subpath)
            if subrepo.dirty(): haschange = True

    if haschange and not opts["nofb"] and not RE_CASE.search(opts["message"]):

        casenumResponse = ui.prompt('*** Please specify a case number, x to abort, or hit enter to ignore:', '')
        casenum = RE_CASENUM.search(casenumResponse)        

        if casenum:         
            opts["message"] += ' (Case ' + casenum.group(0) + ')'
            print '*** Continuing with updated commit message: ' + opts["message"]          
        elif (casenumResponse == 'x'):
            ui.warn('*** User aborted\n')
            return False    

    return originalcommit(ui, repo, **opts)

def uisetup(ui):    
    entry = extensions.wrapcommand(commands.table, "commit", commit)
    entry[1].append(('', 'nofb', None, ('suppress the fogbugzreminder warning if no case number is present in the commit message')))

要使用此扩展程序,请将源复制到名为fogbugzreminder.py的文件中。然后在您的Mercurial.ini文件(或hgrc,无论您的偏好是什么)中,将以下行添加到[extensions]部分:

fogbugzreminder=[path to the fogbugzreminder.py file]

答案 1 :(得分:0)

如果不修改变更集,则无法修改提交消息。

我建议调查一个precommit钩子,如果遗漏了一个bugid就会拒绝提交。

相关问题