Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8787875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:00:22+00:00 2026-06-13T22:00:22+00:00

As a followup for Mercurial: enforce "hg pull -u" before "hg commit" I have

  • 0

As a followup for Mercurial: enforce "hg pull -u" before "hg commit"
I have started to use a hook

[hooks]
pretxnchangegroup.forbid_2heads = /usr/local/bin/forbid_2head.sh

where forbid_2head.sh looks like this

#!/bin/bash 
BRANCH=`hg branch`
COUNT=`hg heads --template '{branch}|{rev}\n' | grep ^${BRANCH} | wc -l` 
if [ "$COUNT" -ne "1" ] ; then 
   echo "=========================================================="
   echo "Trying to push more than one head, which is not allowed"
   echo "You seem to try to add changes to an old changeset!"
   echo "==========================================================" 
   exit 1 
fi 
exit 0

It is derivative of the script found at http://tutorials.davidherron.com/2008/10/forbidding-multiple-heads-in-shared.html
where I do allow multiple named branches.

The problem I have now is that

  • it stops hg push -f which is what I wanted
  • it also stops hg pull in case there are incoming changeset and I have commits outgoing. This is indeed bad

Can I in any way reuse the same script but change the hook setup and stop “hg push -f”?
Or can I in forbid_2head.sh know whether this is a push or pull command running?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:00:24+00:00Added an answer on June 13, 2026 at 10:00 pm

    First, the script isn’t completely correct: it just counts the number of heads in the branch currently checked out on the server (the one hg branch) reports. You could improve it by using

    hg heads tip
    

    to get the heads of the branch of tip. But someone might push changesets on more than one branch at a time, so what you really want is

    hg heads --template '{branch}\n' $HG_NODE:tip
    

    to find branch heads for the branches touched by $HG_NODE:tip (the changesets pushed in the not-yet-committed transaction). You can then compare that with

    hg log --template '{branch}\n' -r $HG_NODE:tip | sort -u
    

    which are the branches touched by the changegroup.

    If you don’t want to allow existing multiple heads, then you can simplify the above to just

    $(hg heads --template 'x' | wc -c) -eq $(hg branches | wc -l)
    

    which just tests that the number of branch heads is equal to the number of branches — i.e., that there is exactly one branch head per named branch.

    With that out of the way, let me mention $HG_SOURCE. That environment variable is set by Mercurial when it runs the hook: it has the value push if the changegroup is being pushed into the repository using direct filesystem access, and the value serve if the changegroup is coming in over SSH or HTTP. See the Mercurial book.

    So, to conclude, I believe this is a good “forbid multiple heads” script:

    #!/bin/sh
    HEADS=$(hg heads --template 'x' | wc -c)
    BRANCHES=$(hg branches | wc -l)
    test $HG_SOURCE = 'serve' -a $HEADS -ne $BRANCHES
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

NOTE: This is a followup to my question here. I have a program that
This is a followup to an earlier question ( Help Refining RegEx ("\b\d{6}([ ]{1,15})\d{7}\b")
As a followup to my previous question about determining camera parameters I have formulated
This is a followup/post question to another question; as I have discovered a solution
In followup to Where has "window-based" project gone? , what are the steps needed
I have a followup question to this question . I'm writing a web service
I have a followup question to the one asked here: Java Abstract Class (this
This is a followup to this question. I have lots of structures on my
This is a followup to a question I asked yesterday: Have you ever had
I'm a fan of Mercurial and have been using it on Linux for a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.