hook to limit push to only one branch at a time

Gilles Moris gilles.moris at free.fr
Sat May 28 01:20:42 CDT 2011


On Friday 27 May 2011 06:02:57 pm Jay Keshur wrote:
> Hi,
>
> I'm trying to create a hook that blocks pushes to a remote repository if
> you are trying to push more than once branch.
>
> Here's the hook:
>
> #!/bin/bash
>
> HG_EXE="/opt/csw/bin/hg"
> CHANGESETS=`${HG_EXE} log -r $1:tip --template '{node} '`
>
> i=0
> for changeset in ${CHANGESETS}
> do
>        BRANCH=`${HG_EXE} log -r ${changeset} --template '{branches}'`
>
>        if [ "${BRANCH}" == "" ]
>        then
>               BRANCH="default"
>        fi
>        BRANCHES[$i]=${BRANCH}
>        i=$i+1
> done
>
> items=${#BRANCHES[*]}
> if [ $items -gt 1 ]
> then
>        i=0
>        while [ "${BRANCHES[${i}+1]}" != "" ]
>        do
>               if [ "${BRANCHES[${i}]}" != "${BRANCHES[${i}+1]}" ]
>               then
>                      echo "ERROR: You are trying to push more than one
> branch, use \"hg push -b [branch_name]\"" exit 1
>               fi
>        i=$i+1
>        done
> fi
>
> The problem:
> If I've committed on two branches:
>
> changeset:   58:8d2bebe08dd9
> user:        keshurj <Jay.Keshur at monitisegroup.com>
> date:        Thu May 26 16:36:49 2011 +0100
> summary:     commit on default
>
> changeset:   59:43be74e39a44
> branch:      branch1
> tag:         tip
> user:        keshurj <Jay.Keshur at monitisegroup.com>
> date:        Thu May 26 16:40:25 2011 +0100
> summary:     commit on branch1
>
> and try to push using hg push -b branch1, the hook still sees ${HG_NODE} as
> 8d2bebe08dd9, which is on default.
>

The script seems correct. From the log file, branch1 seems to branch from 
8d2bebe08dd9, which means that if you want to push 43be74e39a44 (branch1), 
you have to push all ancestors nodes including its parent 8d2bebe08dd9.

If you had independent branches, you would push only branch1 changesets.

Regards.
Gilles.


More information about the Mercurial-devel mailing list