1、
Handling repository events with hooks
可以通过Mercurial版本管理工具提供的hooks机制来处理repo的各种事件,从而实现对Mercurial的扩展,实现我们的特定需求。
2、
常用的hooks event事件:
摘自:http://hgbook.red-bean.com/read/handling-repository-events-with-hooks.html#sec:hook:precommit
changegroup: This is run after a group of changesets has been brought into the repository from elsewhere. 5 comments
commit: This is run after a new changeset has been created in the local repository. No comments
incoming: This is run once for each new changeset that is brought into the repository from elsewhere. Notice the difference from changegroup, which is run once per group of changesets brought in. No comments
outgoing: This is run after a group of changesets has been transmitted from this repository. No comments
prechangegroup: This is run before starting to bring a group of changesets into the repository. 2 comments
precommit: Controlling. This is run before starting a commit. 2 comments
preoutgoing: Controlling. This is run before starting to transmit a group of changesets from this repository. No comments
pretag: Controlling. This is run before creating a tag. No comments
pretxnchangegroup: Controlling. This is run after a group of changesets has been brought into the local repository from another, but before the transaction completes that will make the changes permanent in the repository. No comments
pretxncommit: Controlling. This is run after a new changeset has been created in the local repository, but before the transaction completes that will make it permanent. One comment
preupdate: Controlling. This is run before starting an update or merge of the working directory. One comment
tag: This is run after a tag is created. No comments
update: This is run after an update or merge of the working directory has finished. No comments
3、
hook配置:
可以修改本地仓库下面.hg/hgrc配置文件,语法规则:
The syntax for Python hooks is as follows:
hookname = python:modulename.submodule.callable
hookname = python:/path/to/python/module.py:callable
例:
[hooks]
precommit = python:.hg/signoff.py:sign_commit_message
4、函数参数:
all hooks will take ui, repo,hooktype -- that's a very common pattern in Mercurial code (core, extensions, hooks, whatever)
例:我们一般可以这样定义函数:
import re def precommit_badbranch(ui, repo, **kwargs): branch = repo[None].branch() branch_re = re.compile(r'\d+\.\d+-branch$') if not branch_re.match(branch): ui.warn('invalid branch name %r (use <major>.<minor>-branch)\n') return True return False
5、返回值:
Hooks can be implemented as either external programs, or internal python calls. The meaning of the return value in both cases is based on the convention for external executables; in other words, a value of 0 means "success". For hooks implemented in python this can be a bit misleading, since it means you return "False" to indicate success and "True" (or throw an exception) to indicate failure.
return True 表明失败, 则此命令执行会失败
return False表明成功,此命令可以执行
6、
The Mercurial API
在写hook的时候,我们可以使用很多Mercurial提供的api,具体参见:
https://www.mercurial-scm.org/wiki/MercurialApi
参考:
http://hgbook.red-bean.com/ --重要
https://www.mercurial-scm.org/wiki/HookExamples
https://www.mercurial-scm.org/wiki/MercurialApi
https://www.mercurial-scm.org/wiki/Hook
https://selenic.com/hg/help/hgrc