3

I have several repositories that have been converted from SVN and moving forward we want make sure when people push to the repository they can't create additional heads. there are several hooks in the TipsAndTricks wiki page that prevent pushing if there are multiple heads, but how I can build a hook that prevents pushing new heads?

It seems like the right way to do this would be to compare the number of heads from revision 0:parent and then compare 0:tip, but I can't seem to find a way to do that. hg heads -r $HG_NODE only shows me the number of heads after the user's first commit being pushed.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Asa Ayers
  • 4,854
  • 6
  • 40
  • 57

5 Answers5

5

By default, hg push prevents pushing of additional heads, requiring the -f flag. You could write a Mercurial extension to make it ignore that flag, effectively disabling pushing of new heads.

That said, I would question the wisdom of mechanically disallowing new heads. Instead, I would train your team to merge properly before pushing, while still allowing them to do it on the rare occasion that it makes sense, especially since Mercurial already warns you.

tghw
  • 25,208
  • 13
  • 70
  • 96
  • I've seen preventing multiple haeads presented as being a common hook in multiple places. Its in the tips and tricks in the wiki and is used by mozilla and netbeans. This seems like evidence that its not a bad idea. If I were starting a new project I would use one of those. But they don't handle my converted repos the way I need. – Asa Ayers Dec 05 '10 at 00:13
  • Training people to merge before pushing doesn't fix the problem. Someone else could always have pushed something new between your merge and your push. – Kylotan Dec 06 '10 at 16:55
  • @Kylotan: Then you would get the warning that you were pushing a new head, and hg would recommend that you pull and merge before pushing. You'd still have to use the `-f` flag. – tghw Dec 07 '10 at 19:01
3

Mercurial wiki - Tips and Tricks - Prevent a push that would create multiple heads

Points to several existing hooks which may be useful to copy and adapt.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
alexandrul
  • 12,856
  • 13
  • 72
  • 99
1

This is the best: https://www.mercurial-scm.org/wiki/TipsAndTricks#Prevent_a_push_that_would_create_multiple_heads

I personally use: http://hg.python.org/hooks/file/default/checkheads.py

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
gavenkoa
  • 45,285
  • 19
  • 251
  • 303
  • I found some limitation of `cheackheads.py`, see http://stackoverflow.com/questions/32402715/merges-between-two-branches-in-two-directions-any-good-reason-or-completely-fo/ – gavenkoa Sep 07 '15 at 20:02
1

Check this link. It automates the process of allowing multiple heads

Mercurial Hook: forbid multiple heads

Luis Lobo Borobia
  • 994
  • 11
  • 25
0

Use hg ci -m 'Closed branch feature-x' --close-branch to close all but one head and then apply a normal single-head hook.

Asa Ayers
  • 4,854
  • 6
  • 40
  • 57