代码之家  ›  专栏  ›  技术社区  ›  Nathan Lee

有用的汞钩

  •  23
  • Nathan Lee  · 技术社区  · 15 年前

    你遇到的一些有用的反复无常的钩子是什么?

    一些示例挂钩位于 Mercurial book :

    我个人并不觉得这些很有用。我想看看:

    • 拒绝多个磁头
    • 拒绝包含合并的变更组(如果希望用户始终重新设置基码,则很有用)
      • 拒绝包含合并的变更组,除非提交消息具有特殊字符串
    • 自动链接到FogBugz或TFS(类似于Bugzilla Hook)
    • 黑名单会拒绝具有特定变更集ID的推送。(如果使用MQ从其他克隆拉入更改,则很有用)

    请使用有蝙蝠和巴什,或者巨蟒的钩子。这样,NIX和Windows用户都可以使用它们。

    4 回复  |  直到 6 年前
        1
  •  16
  •   Vadim Kotov First Zero    6 年前

    对于正式存储库,我最喜欢的钩子是拒绝多个头的钩子。当你有一个持续的集成系统,需要一个后合并提示来自动构建时,这是很好的。

    以下是一些例子: MercurialWiki: TipsAndTricks - prevent a push that would create multiple heads

    我使用NetBeans的这个版本:

    # This software may be used and distributed according to the terms
    # of the GNU General Public License, incorporated herein by reference.
    #
    # To forbid pushes which creates two or more headss
    #
    # [hooks]
    # pretxnchangegroup.forbid_2heads = python:forbid2_head.forbid_2heads
    
    from mercurial import ui
    from mercurial.i18n import gettext as _
    
    def forbid_2heads(ui, repo, hooktype, node, **kwargs):
        if len(repo.heads()) > 1:
            ui.warn(_('Trying to push more than one head, try run "hg merge" before it.\n'))
            return True
    
        2
  •  8
  •   Macke    13 年前

    我刚刚创建了一个小的pretxncommit钩子,用于检查制表符和尾随空格,并将其很好地报告给用户。它还提供清除这些文件(或所有文件)的命令。

    CheckFiles 延伸。

        3
  •  5
  •   Macke    13 年前

    另一个好钩子是这个。它允许多个头部,但前提是它们位于不同的分支中。

    Single head per branch

    def hook(ui, repo, **kwargs):
        for b in repo.branchtags():
            if len(repo.branchheads(b)) > 1:
                print "Two heads detected on branch '%s'" % b
                print "Only one head per branch is allowed!"
                return 1
        return 0
    
        4
  •  0
  •   Mike The Dinosaur    9 年前

    我喜欢上面提到的每个分支钩的单头;但是, branchtags() 应替换为 branchmap() 因为BranchTags()不再可用。(我不能对那个评论,所以我把它贴在这里)。

    我也喜欢钩子 https://bobhood.wordpress.com/2012/12/14/branch-freezing-with-mercurial/ 冻结的树枝。您在您的hgrc中添加一个部分,如下所示:

    [frozen_branches]
    freeze_list = BranchFoo, BranchBar
    

    加上钩子:

    def frozenbranches(ui, repo, **kwargs):
        hooktype = kwargs['hooktype']
        if hooktype != 'pretxnchangegroup':
            ui.warn('frozenbranches: Only "pretxnchangegroup" hooks are supported by this hook\n')
            return True
        frozen_list = ui.configlist('frozen_branches', 'freeze_list')
        if frozen_list is None:
            # no frozen branches listed; allow all changes
            return False
        try:
            ctx = repo[kwargs['node']]
            start = ctx.rev()
            end = len(repo)
    
            for rev in xrange(start, end):
                node = repo[rev]
                branch = node.branch()
                if branch in frozen_list:
                    ui.warn("abort: %d:%s includes modifications to frozen branch: '%s'!\n" % (rev, node.hex()[:12], branch))
                    # reject the entire changegroup
                    return True
        except:
            e = sys.exc_info()[0]
            ui.warn("\nERROR !!!\n%s" % e)
            return True
    
        # allow the changegroup
        return False
    

    如果有人试图更新冻结的分支(例如BranchFoo、BranchBar),事务将中止。