English 中文(简体)
Useful Mercurial Hooks [closed]
原标题:
Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 2 years ago.

What are some useful Mercurial hooks that you have come across?

A few example hooks are located in the Mercurial book:

I personally don t find these very useful. I would like to see:

  • Reject Multiple Heads
  • Reject Changegroups with merges (useful if you want users to always rebase)
    • Reject Changegroups with merges, unless commit message has special string
  • Automatic links to Fogbugz or TFS (similar to bugzilla hook)
  • Blacklist, would deny pushes that had certain changeset ids. (Useful if you use MQ to pull changes in from other clones)

Please stick to hooks that have either bat and bash, or Python. That way they can be used by both *nix and Windows users.

最佳回答

My favorite hook for formal repositories is the one that refuses multiple heads. It s great when you ve got a continuous integration system that needs a post-merge tip to build automatically.

A few examples are here: MercurialWiki: TipsAndTricks - prevent a push that would create multiple heads

I use this version from 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.
 ))
        return True
问题回答

I ve just created a small pretxncommit hook that checks for tabs and trailing whitespace and reports it rather nicely to the user. It also provides a command for cleaning up those files (or all files).

See the CheckFiles extension.

Another good hook is this one. It allows multiple heads, but only if they are in different branches.

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

I like the Single Head Per Branch hook mentioned above; however, branchtags() should be replaced with branchmap() since branchtags() is no longer available. (I couldn t comment on that one so I stuck it down here).

I also like the hook from https://bobhood.wordpress.com/2012/12/14/branch-freezing-with-mercurial/ for Frozen Branches. You add a section in your hgrc like this:

[frozen_branches]
freeze_list = BranchFoo, BranchBar

and add the hook:

def frozenbranches(ui, repo, **kwargs):
    hooktype = kwargs[ hooktype ]
    if hooktype !=  pretxnchangegroup :
        ui.warn( frozenbranches: Only "pretxnchangegroup" hooks are supported by this hook
 )
        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 !
" % (rev, node.hex()[:12], branch))
                # reject the entire changegroup
                return True
    except:
        e = sys.exc_info()[0]
        ui.warn("
ERROR !!!
%s" % e)
        return True

    # allow the changegroup
    return False

If anyone attempts to update the frozen branches (e.g., BranchFoo, BranchBar) the transaction will be aborted.





相关问题
Can I clone part of a Mercurial repository?

Is it possible to clone part of a Mercurial repository? Let s say the repository is quite large, or contains multiple projects, or multiple branches. Can I clone only part of the repository? E.g. in ...

Sync files in Two Different Repos using HG

I ve got a problem when I try to sync files in two different repos. What I want to do is: I ve got 2 repos A and B, and they share some common files, suppose they lie in A/docs/common/ and B/docs/...

Tips on upgrading CVS to git/hg?

We still use CVS, I use git and hg for my personal use though I m still a novice at both, but I realize they re much more modern and better, faster, distributed, etc. It s just everyone is so ...

git for mercurial like git-svn

Hello is there a tool for git that allow to work with mercurial repositories F.e. for svn there is a git-svn package, that allows to clone/commit/update from svn and work in a git way.. So is there ...

Useful Mercurial Hooks [closed]

What are some useful Mercurial hooks that you have come across? A few example hooks are located in the Mercurial book: acl bugzilla notify check for whitespace I personally don t find these very ...

热门标签