2009-11-10 88 views
23

你遇到過什麼有用的水銀掛鉤?有用的水銀掛鉤

幾個例子鉤位於Mercurial book

我個人不覺得這是非常有用的。我想看看:

  • 拒絕多頭
  • 拒絕(如果你希望用戶隨時可以變基有用)與合併Changegroups
    • 拒絕Changegroups與合併,除非提交信息有特殊字符串
  • 自動鏈接到Fogbugz或TFS(類似於bugzilla鉤子)
  • 黑名單,將拒絕推送具有某些changeset id的用戶。 (如果你使用MQ從其他克隆中引入更改,這很有用)

請堅持使用bat或bash或Python。這樣它們可以被* nix和Windows用戶使用。

+0

也許更多的想法可以從這些Subversion問題中收集:http://stackoverflow.com/questions/6155/common-types-of-subversion-hooks和http://stackoverflow.com/questions/884608/share- common-useful-svn-pre-commit-hooks ... – Macke 2010-12-29 10:11:54

回答

16

我最喜歡的形式化存儲庫的鉤子是拒絕多個頭部的鉤子。當你有一個需要合併後提示自動構建的持續集成系統時,這非常棒。

幾個例子在這裏: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 
+4

http://hg.python.org/hooks/file/tips/checkheads.py - 這更好,因爲允許分支.... – gavenkoa 2011-10-20 21:03:50

+0

確實,有一些不錯的變體,每個命名分支只允許一個頭(匿名分支也是分支)。 – 2011-10-21 23:03:37

+1

@gavenkoa:鏈接應該是http://hg.python.org/hooks/file/tip/checkheads.py(不是/ tips /) – Macke 2011-10-23 11:25:45

8

我剛剛創建了一個小pretxncommit掛鉤,檢查標籤和結尾的空白,並將其報告相當好聽給用戶。它還提供了清理這些文件(或所有文件)的命令。

請參閱CheckFiles的擴展。

5

另一個好的鉤子是這個。它允許多個頭,但只有當他們在不同的分支。

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 
0

我喜歡的單頭每如上所述科鉤;但是,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)的交易將被中止。