python - 如何设置或修改来自 Mercurial 扩展的提交消息?

标签 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 扩展有关,但似乎对这里没有帮助。

关于如何设置提交消息有什么想法吗?

最佳答案

我最终没有找到使用 Hook 的方法,但我能够使用 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]

关于python - 如何设置或修改来自 Mercurial 扩展的提交消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7854041/

相关文章:

fogbugz - 以编程方式分配 Fogbugz 案例

python - pip (1.3.1) 在删除本地缓存之前不升级包

python - Python 中每个值有多个键的字典

python - 这在逻辑上是一样的吗?

git - 是否有可以使用白名单而不是黑名单的(开源)VCS?

shell - 默认使用 less 查看 Mercurial 日志/状态

mercurial - 在 Mercurial 中拆分变更集的最简单方法

fogbugz - 为什么 Fogbugz API 不允许我登录?

bug-tracking - 自动将意外错误/堆栈跟踪记录到错误跟踪器的建议

python - 如何从 python 中尾部上升的频谱中减去基线?