Mercurial Changegroup hook varies based on branches
Asked Answered
B

1

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.

Beach answered 22/1, 2011 at 0:11 Comment(0)
N
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.

Nullipore answered 22/1, 2011 at 20:34 Comment(3)
I guess ${BRANCH:=default} should be BRANCH=${BRANCH:=default}.Brahmanism
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 :=.Nullipore
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.Brahmanism

© 2022 - 2024 — McMap. All rights reserved.