precommit mercurial hook to stop commits to the wrong branch

Faheem Mitha faheem at faheem.info
Fri Oct 11 12:03:46 UTC 2013


On Wed, 09 Oct 2013 06:19:01 -0600, Bob Hood <bhood2 at comcast.net> wrote:
> 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.

Thanks Bob,

That's very helpful. I didn't see your message immediately because you
didn't CC me, and I don't receive messages from the mailing list. So,
please do CC me. Thanks.

I suggest you put your hook on the Mercurial wiki. Possibly
http://mercurial.selenic.com/wiki/HookExamples, though it is targeted
for deletion, so there may be better places.

If you use Stack Overflow you could also add it to
http://stackoverflow.com/questions/1705921/useful-mercurial-hooks

It looks like you are using the Mercurial internal API. Have you had
any issues with it breaking?

Also, what would suggest as a reference for writing in-process hooks,
other than the hooks chapter in hgbook?

> 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

How does the hook know to check this section? Is this based on the
name

    pretxnchangegroup.frozen_branches

? Do you have a reference for this syntax?

> 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.  :)

Thats great, thanks.

     Regards, Faheem




More information about the Mercurial mailing list