开始为 Mercurial 编写 Hook 的最佳方法是什么?
我是 Mercurial 的新手,想编写一些钩子来防止某些分支等之间的合并。我正在寻找某种贯穿整个循环的教程。
我查看了 API 和这些 示例,但我仍然觉得很困惑。与阅读手册页或类似内容相比,我一直更擅长通过教程/研讨会进行学习。
我可以理解示例代码的工作原理,但如何让 Mercurial 识别这些函数?
例如) 我已经写了这个,但我不明白如何让 Mercurial 在提交之前运行代码。
def is_bad_branch_name(ui, repo, **kwargs):
"""
Checks that a commit is always done on a named branch.
This function enforces Projectplace's branching convention.
@return: True if the branch name is invalid.
@returntype: Boolean
"""
branch = repo[None].branch()
branch_names = r'(TT|AZ)(-#)(\d)+(:)[\s\w]*'
acceptable_branch_names = re.compile(branch_names)
if not acceptable_branch_names.match(branch):
ui.warn('invalid branch name %r (use <TT|AZ>-#<number>: <description>)\n')
return True
return False
I'm new to Mercurial and want to write some hooks to prevent merging between certain branches and the like. I'm looking for some kind of tutorial that goes through the whole loop.
I've looked at the API and these examples, but I still find it confusing. I've always been better at learning through a tutorial/workshop than by reading a man page or similar.
I can understand how the example code works, but how do I get Mercurial to recognize the functions?
e.g.)
I've written this, but I don't understand how to make Mercurial run the code before a commit.
def is_bad_branch_name(ui, repo, **kwargs):
"""
Checks that a commit is always done on a named branch.
This function enforces Projectplace's branching convention.
@return: True if the branch name is invalid.
@returntype: Boolean
"""
branch = repo[None].branch()
branch_names = r'(TT|AZ)(-#)(\d)+(:)[\s\w]*'
acceptable_branch_names = re.compile(branch_names)
if not acceptable_branch_names.match(branch):
ui.warn('invalid branch name %r (use <TT|AZ>-#<number>: <description>)\n')
return True
return False
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我指出明显的原因,因为我是一个绝望的代表 wh***:
“BoS 明确的 hg 书中的第 10 章. 使用钩子处理存储库事件”。
如果您无法找出所需的特定挂钩,请提出更具体的问题。
快乐挂钩!
Let me point out the obvious for the sole reason of me being a desperate rep wh***:
"Chapter 10. Handling repository events with hooks" in BoS's definite hg book.
If you can't figure out a particular hook you want, please ask a more specific question.
Happy hooking!
我现在已经成功地编写并使用了钩子。我发现缺少在线文档,但这也可能是我的问题。我所做的是研究示例代码和一些试验和错误编码,并在这里提出一些问题。 :)
编辑
为了将来参考,这是 Mercurial 在提交之前在 hgrc 中运行上述代码所需的行。这是挂钩和代码路径的部分。
对于每个要挂钩的函数,您都需要一行。 “预提交”意味着挂钩将在执行提交之前运行。这样,如果钩子返回 True,提交将被中止。 “bad_branch_name”只是一个标识符,您可以在那里键入任何内容。
I've managed to write and make use of hooks now. I found the online documentation lacking, but that might just as well be a problem on my part. What I did was study example code and some trial and error coding as well as ask some questions here. :)
edit
For future reference this is the kind of line Mercurial needs in your hgrc to run the above code before a commit. That is a section for hooks and the paths to the code.
You will need one line for each function you want to hook. "precommit" means that the hook will be run just before a commit is performed. This way the commit will be aborted if the hook returns True. "bad_branch_name" is just an identifier, you can type anything there.