7

Is there an existing hook in Mercurial which, like changegroup, allows actions to take place on a push, but allows me to do multiple actions (or vary them) based on which branches are affected by the changesets therein?

For example, I need to notify a listener at an url when a push is made but ideally it would notify different urls based on which branch is affected without just blanketing them all.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
ziklagx
  • 173
  • 5

1 Answers1

8

There are no branch-specfic hooks, but you can do that logic in the hook itself. For example in your hgrc:

[hooks]
changeset = actions-by-branch.sh

and then in your actions-by-branch.sh you'd do:

#!/bin/bash
BRANCH=$(hg log --template '{branch}' -r $HG_NODE)
BRANCH=${BRANCH:-default}  # set value to 'default' if it was empty

if [ "$BRANCH" == "default" ] ; then
   do something
elif [ "$BRANCH" == "release" ] ; then
   do something else
else
   do a different thing
fi

Notice that I used a changeset rather than changegroup hook. A single changegroup can have changesets on multiple branches, which would complicate the logic. If you do decide to go that route you need to loop from $HG_NODE all the way to tip to act on each changeset in the changegroup.

Ry4an Brase
  • 78,112
  • 7
  • 148
  • 169
  • 1
    I guess `${BRANCH:=default}` should be `BRANCH=${BRANCH:=default}`. – palacsint Jan 15 '17 at 22:51
  • It works as written. From the bash man page "${parameter:=word} Assign Default Values. If parameter is unset or null, the expansion of word is assigned to parameter. The value of param- eter is then substituted. Positional parameters and special parameters may not be assigned to in this way." If we wanted the more verbose assignment statement we'd use `:-` instead of `:=`. – Ry4an Brase Jan 16 '17 at 13:53
  • 1
    Ok, I've figured out the problem. Bash tries to run the value of `$BRANCH` as a command. That's why I got a `mybranchname: command not found` error message. So, you either need the assignment or use a bash no-op (`: ${BRANCH:=default}`) to avoid this error. – palacsint Jan 16 '17 at 22:50