简体   繁体   中英

Mercurial: enforce “hg pull -u” before “hg commit”

I have in some cases a need to enforce that Mercurial-users have run hg pull -u before any hg commit can be allowed, ie, hg pull will mean that the incoming queue is empty — and furthermore I also want that the person is using the head version of the branch.

How can I set up such a restriction?

(I am fully aware that this goes against parts of the DVCS design core)

You could ask your developers to install

[hooks]
pre-commit = hg pull -u

in their config files (it should probably be installed in the per-repository .hg/hgrc file since this workflow is repository specific).

This makes Mercurial a little Subversion-like: your developers will only have one outstanding changeset. But note as soon as someone pushes to the server, hg pull -u cannot update to the new branch tip since it will cross branches (topological branches) to do so. So a proper merge will be needed at that point (or a rebase , see hg pull --rebase ).

Normally mercurial will NOT let you push an open head to the server without using the -f flag (force). You can write a hook to pull automatically but that can not be enforced server side due to the server not knowing what you have. There is an article on mercurial's website about this scenario: https://www.mercurial-scm.org/wiki/TipsAndTricks?highlight=%28heads%29#Prevent_a_push_that_would_create_multiple_heads

As Adam says, perhaps what you really need to do is prevent multiple heads (per branch). This is what we do, using the 'forbid_2head' hook from Netbeans (linked from here https://www.mercurial-scm.org/wiki/TipsAndTricks#Prevent_a_push_that_would_create_multiple_heads )

The result is that the hook prevents any push that creates multiple heads on a branch (so one on the anonymous/default branch plus one each on named branches). This effectively forces a pull before commit because you have to pull, get the two heads locally, then merge or rebase to remove it.

note, the hook is on the server/master repo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM