2011-10-21 44 views
3

我试图修改this Mercurial extension以提示用户添加一个FogBugz案例编号到他们的提交消息。理想情况下,我希望用户在被提示后输入一个数字,并自动附加到提交消息中。如何设置或修改来自mercurial extension的提交消息?

这里是我到目前为止有:

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扩展名有关,它似乎并不能在这里提供帮助。

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

回答

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] 
0

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

我会建议寻找一个precommit钩子拒绝提交如果一个bugid被遗漏了。

+0

我能做到什么,我需要使用extensions.wrapcommand。看看我的回答:) –

相关问题