2011-10-21 64 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。看看我的回答:) –

相關問題