0

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@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@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.

Is there any way to ensure a push is done to only one branch at a time, via a remote hook?

Open to any and all suggestions ( re: this workflow :) )

3 Answers3

2

Have you considered just using an alias like hg nudge:

http://hgtip.com/tips/advanced/2009-09-28-nudge-a-gentler-push/

which is just:

[alias]
nudge = push --rev .

That makes sure you only push your current parent revision and its ancestors. which given the assumptions in your script above would probably all be in the same branch (or requires to push that anyway). You need to create a new habit, but it's pretty direct.

Ry4an Brase
  • 77,054
  • 6
  • 143
  • 167
1

$1 in your assignment to $CHANGESETS is not defined when the script is run - replace it with $HG_NODE

http://www.selenic.com/mercurial/hgrc.5.html#hooks

This assumes you are running this as a pretxnchangegroup hook. (Tested with hg 1.8.1, but I'm reasonably certain that hasn't changed lately).

Matt
  • 678
  • 1
  • 6
  • 13
0

turns out i was doing something silly....changeset 58 is the parent of changeset 59.

Thanks guys.