Python 如何从mercurial扩展设置或修改提交消息?
我正在尝试修改以提示用户在提交消息中添加FogBugz案例号。理想情况下,我希望用户在收到提示后键入一个数字,并将其自动附加到提交消息中 以下是到目前为止我得到的信息:Python 如何从mercurial扩展设置或修改提交消息?,python,mercurial,fogbugz,mercurial-extension,Python,Mercurial,Fogbugz,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:
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
def pretxncommit(用户界面、回购、**kwargs):
tip=repo.changectx(repo.changelog.tip())
如果没有,则重新搜索(tip.description())和len(tip.parents())<2:
casenumResponse=ui.prompt(“***请指定案例编号,x将中止,或按enter键忽略:”,“”)
casenum=重新搜索casenum.search(casenumResponse)
如果是casenum:
#这不行!
#tip.description(tip.description()+'(Case'+casenum.group(0)+'))
返回真值
elif(casenumResponse=='x'):
警告(“***用户已中止\n”)
返回真值
返回真值
返回错误
我没有找到编辑提交消息的方法<代码>提示。说明似乎是只读的,我在文档或示例中没有看到任何允许我修改它的内容。我所看到的编辑提交消息的唯一参考与补丁和Mq扩展有关,在这里似乎没有帮助
关于如何设置提交消息,您有什么想法吗?如果不修改变更集,就无法修改提交消息
我建议研究一个预提交钩子,如果bugid被遗漏,它会拒绝提交。我最终没有找到一种使用钩子的方法,但我可以使用扩展名.wrapcommand并修改选项来完成 我在这里包含了结果扩展的源代码 在检测到提交消息中缺少事例时,my version会提示用户输入事例、忽略警告或中止提交 如果用户通过指定案例编号来响应提示,则会将其附加到现有提交消息中 如果用户以“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]
我能够使用extensions.wrapcommand完成我所需要的。看看我的答案:)