【问题标题】:Useful Mercurial Hooks [closed]有用的 Mercurial Hooks [关闭]
【发布时间】:2010-12-14 22:10:05
【问题描述】:

您遇到过哪些有用的 Mercurial 钩子?

一些示例钩子位于Mercurial book

我个人认为这些不是很有用。我想看看:

  • 拒绝多头
  • 通过合并拒绝更改组(如果您希望用户始终变基,则很有用)
    • 通过合并拒绝更改组,除非提交消息具有特殊字符串
  • 自动链接到 Fogbugz 或 TFS(类似于 bugzilla 挂钩)
  • 黑名单,将拒绝具有特定变更集 ID 的推送。 (如果您使用 MQ 从其他克隆中提取更改,这很有用)

请坚持使用具有 bat 和 bash 或 Python 的钩子。这样,*nix 和 Windows 用户都可以使用它们。

【问题讨论】:

标签: mercurial hook mercurial-hook


【解决方案1】:

我最喜欢的正式存储库钩子是拒绝多头的钩子。如果您有一个需要合并后提示来自动构建的持续集成系统,那就太好了。

这里有几个例子: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

【讨论】:

  • hg.python.org/hooks/file/tips/checkheads.py - 允许分支更好......
  • 确实,有一些不错的变体,每个命名分支只允许一个头(匿名分支也是分支)。
  • @gavenkoa:链接应该是hg.python.org/hooks/file/tip/checkheads.py(不是/tips/)
  • @gavenkoa:您最多只能在创建后 5 或 15 分钟内执行此操作,以避免讨论中的破坏性更改。您也许应该发表您的评论作为答案,因为它比接受的要好得多。
【解决方案2】:

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

请参阅CheckFiles 扩展。

【讨论】:

    【解决方案3】:

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

    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】:

      我喜欢上面提到的 Single Head Per Branch hook;但是,branchtags() 应替换为 branchmap(),因为 branchtags() 不再可用。 (我无法评论那个,所以我把它贴在这里)。

      我也喜欢来自 https://bobhood.wordpress.com/2012/12/14/branch-freezing-with-mercurial/ 的 Frozen Branches 的钩子。您可以像这样在 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),事务将被中止。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多