3

我正在尝试修改此 Mercurial 扩展,以提示用户将 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 扩展有关,这似乎在这里没有帮助。

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

4

2 回答 2

6

我最终没有找到使用钩子的方法,但我能够使用extensions.wrapcommand和修改选项来做到这一点。

我在这里包含了生成的扩展的来源。

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

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

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

如果用户只是按回车键响应,则提交将继续原始的无案例提交消息。

我还添加了 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]
于 2011-10-21T20:50:22.057 回答
0

您不能在不修改变更集的情况下修改提交消息。

我建议研究一个 precommit 钩子,如果忽略了 bugid,它会拒绝提交。

于 2011-10-21T20:42:13.803 回答