precommit mercurial hook to stop commits to the wrong branch

Bob Hood bhood2 at comcast.net
Wed Oct 9 12:19:01 UTC 2013


On 10/9/2013 3:09 AM, Faheem Mitha wrote:
>
> I posted the following question to Stack Overflow.
>
> http://stackoverflow.com/q/19234473/350713
>
> The text of that question is below.
>
> Ry4an kindly replied with a bash solution. I think I would prefer a Python
> version, but that would use the Mercurial "not-an-api", as Ry4an put it.
> Does this look practical? If anyone feels like taking the time to sketch an
> approach, I'd appreciate it. Regardless, any comments would be appreciated,
> Thanks.

Yeah, I think it's always better to do things in-process if an API allows it.

I don't know if this will help you at all, but I wrote a pre-commit hook for
our project that effectively "freezes" named branches after we have made our
GA releases from them, preventing any further modifications to the branch by
developers, accidental or otherwise.  It's fairly short, and it may serve as a
start for what you want to accomplish.

I added the following script as a Python package to the distribution that our
Mercurial server uses so it would be directly accessible by an "import":

    # frozen branches by Bob Hood
    # effectively "freezes" named branches in a repo, preventing further
modifications
    def hook(ui, repo, **kwargs):
        frozen_list = ui.configlist('frozen_branches', 'freeze_list')
        if frozen_list is None:
            return False   # no frozen branches listed; allow all

        hooktype = kwargs['hooktype']
        if hooktype != 'pretxnchangegroup':
            ui.warn('frozen_branches: Only "pretxnchangegroup" hooks are
supported by this script\n')
            return True

        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("Error: %d:%s includes modifications to a frozen
branch '%s'!\n" % (rev, node.hex()[:12], branch))
                return True   # reject the changegroup

        return False   # allow the changegroup

In the .hg/hgrc file for the repository being served, you would activate this
change hook by adding a new entry to the [hooks] section, thus:

    [hooks]
    ...
    pretxnchangegroup.frozen_branches =
python:frozen_branches.frozen_branches.hook

and then add a new section in the same hgrc file called [frozen_branches] that
lists the named branches to be restricted, e.g.:

    [frozen_branches]
    freeze_list = 10.0, 10.next, 10.0_backports, 11.0, 11.0.sp1, 11.0.sp2

I know this isn't quite what you're after, but with some adjustments to the
script, you can make sure an incoming changeset is "pure", and reject it if
not.  Hope that gives you enough to get going.  :)



More information about the Mercurial mailing list