2

So I used this method to access the name of a repo within a git serverside hook. Is there a way to access the branch name as well?

The intention is that whenever someone pushes to this bare repo, a hook will trigger that calls another script and passes it the name of the repo (already does this) as well as the branch name of the branch that was just pushed.

Community
  • 1
  • 1
derpy
  • 67
  • 1
  • 6

1 Answers1

1

As mentioned in "how can git post-receive hook get name of repo it is running on?", a post-receive hook can receive commits from multiple branches:

#!/bin/bash
while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    if [ "master" == "$branch" ]; then
        # Call your script
    fi
done

Your script could end up being called multiple times.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Does the $branch variable get the name of the branch? I can't seem to echo it (without the if statement). – derpy Apr 30 '15 at 06:38
  • @derpy yes, it does. It the echo does not work, try and redirect it to a file, for testing. – VonC Apr 30 '15 at 06:40
  • that's what i was trying. if i take out the if and fi lines from the script you posted and replace them with echo $branch > /some/path , i get nothing. – derpy Apr 30 '15 at 06:54
  • @derpy are you pushing commits? (meaning is the script triggered by a push, or do you try to execute the script directly and manually?) – VonC Apr 30 '15 at 07:39
  • Triggered by a push. To prove everything else works, I can use the aforementioned code to output the name of the repo to a file but still fails on the branch. – derpy Apr 30 '15 at 09:03
  • @derpy can you print the `$refname` that you are pushing? – VonC Apr 30 '15 at 09:05