0

As per this link, an update hook should be passed 3 arguments. Using the code in the first answer of that link should then let you ascertain the branch name. However, I am unable to reproduce this and I cannot ascertain branch name.

I can confirm that my workflow is functional. Whenever I push to a repo, it runs a file called "update" which is an update hook. I can confirm that it will write text to a file and does execute.

This is some code that does work for me. It solicits the name of the repo and successfully writes it to ~/name:

#!/bin/bash
if [ $(git rev-parse --is-bare-repository) = true ]
then
    REPOSITORY_BASENAME=$(basename "$PWD")
else
    REPOSITORY_BASENAME=$(basename $(readlink -nf "$PWD"/..))
fi
echo $REPOSITORY_BASENAME > ~/name

This is the code that does not work for me. It fails to write the value of refname and branch variables etc. And while I am pushing to master (should satisfy 'if' condition, I have also tried the code without the if loop:

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

All I am looking for is a way to reference the branch name in an update hook.

Community
  • 1
  • 1
derpy
  • 57
  • 1
  • 6

1 Answers1

1

The documentation (man githooks) says:

   The hook executes once for each ref to be updated, and takes three parameters:

   ·   the name of the ref being updated,

   ·   the old object name stored in the ref,

   ·   and the new object name to be stored in the ref.

You're reading on stdin, you want to look at parameters ($1, $2, $3).

The link you're mentioning talks about post-receive hook, which behaves differently.

Matthieu Moy
  • 11,096
  • 2
  • 35
  • 57