按照 Martin Geisler 的回答和这篇文章,这里有一个适用于 Windows 的钩子:
在hgrc
:
[hooks]
precommit.bookmark = python:/path/to/hg-hooks.py:prefix_commit_message
并在hg-hooks.py
:
import sys, mercurial
## to be accepted in TortoiseHg, see http://tortoisehg.bitbucket.io/manual/2.9/faq.html
sys.path.append(r'C:\Python27\Lib\site-packages')
import hglib
def _get_active_bookmark(path):
'''Return the active bookmark or None.
'''
client = hglib.open(path)
bookmarks, active = client.bookmarks()
if active == -1:
return None
else:
return bookmarks[active][0]
###
### Available hooks
###
def prefix_commit_message(ui, repo, **kwargs):
'''Prepend [active bookmark name] to commit message.
'''
commitctx = repo.commitctx
def rewrite_ctx(ctx, error):
book = _get_active_bookmark(repo.root)
if book:
old_text = ctx._text
if not old_text.lstrip().startswith("["):
ctx._text = "[" + book + "] "+ old_text
return commitctx(ctx, error)
repo.commitctx = rewrite_ctx