takeshin
takeshin

Reputation: 50638

git: empty arguments in post-receive hook

I'm writing post-receive hook basing on the post-receive-email script from the contrib dir, but it seems that the oldrev and newrev arguments are empty.

The script looks like this:

#!/bin/bash

oldrev=$(git rev-parse $1)
newrev=$(git rev-parse $2)

The script runs on push, but all $1, $2, $oldrev and $newrev are empty. Should I configure something to get it running?

(The repository was created by gitolite if it does matter)

Upvotes: 17

Views: 12683

Answers (5)

François
François

Reputation: 987

I stumbled into this problem while setting up a continuous integration server. Since the arguments are not passed to post-receive via the command line, but are passed over STDIN, you have to use the read command to fetch them. Here is how I did it:

#!/bin/sh
read oldrev newrev refname
BRANCH=${refname#refs/heads/} 
curl --request POST "http://my.ci.server/hooks/build/myproject_$BRANCH"

Upvotes: 54

aanno
aanno

Reputation: 688

A more elaborated version of François script would be

#!/bin/bash

JENKINS_URL="http://192.168.1.116/jenkins"
GIT_URL="[email protected]:nuclos/nuclos.git"

# remove all spaces and newlines from ARG
trim() {
  local ARG="$1"
  shift
  echo -e "$ARG" | tr -d "[:space:]\n" 
}

# unique sort ARG items separated by newlines
unique() {
  local ARG="$1"
  shift
  echo -e "$ARG" | sort -u -i
}

# cut first and last character from ARG
cutfl() {
  local ARG="$1"
  shift
  local LEN="${#ARG}"
  let LEN="$LEN - 2"
  echo "${ARG:1:$LEN}"
}
BRANCHES=""
while read oldrev newrev refname; do
  BRANCH=`trim ${refname#refs/heads/}`
  if [ -n "$BRANCH" ]; then
    BRANCHES+="${BRANCH}\n"
  fi
done

BRANCHES=`unique "$BRANCHES" | tr '\n' ','`
BRANCHES=`cutfl "$BRANCHES"`
echo wget -q -O - "$JENKINS_URL/git/notifyCommit?url=$GIT_URL&branches=$BRANCHES"
at "now + 5 minutes" <<END
wget -q -O - "$JENKINS_URL/git/notifyCommit?url=$GIT_URL&branches=$BRANCHES"
END

This version could cope with more than one branch and only triggers one build for each one.

Upvotes: 3

estani
estani

Reputation: 26477

There are no arguments though the information is passed over STDIN. To read that information from bash simply do this:

read oldrev newrev refname
echo "Old revision: $oldrev"
echo "New revision: $newrev"
echo "Reference name: $refname"

I'm just summarizing the answers already posted.

Upvotes: 13

Dennis
Dennis

Reputation: 4177

Actually, I don't accept the "it takes no arguments", because the sample script post-receive.sample is having the following comment:

# The "post-receive" script is run after receive-pack has accepted a pack
# and the repository has been updated.  It is passed arguments in through
# stdin in the form
#  <oldrev> <newrev> <refname>
# For example:
#  aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master

Upvotes: 1

J&#246;rg W Mittag
J&#246;rg W Mittag

Reputation: 369458

The post-receive hook doesn't take any arguments. Quoth the manual (emphasis added):

This hook is invoked by git-receive-pack on the remote repository, which happens when a git push is done on a local repository. It executes on the remote repository once after all the refs have been updated.

This hook executes once for the receive operation. It takes no arguments, but gets the same information as the pre-receive hook does on its standard input.

This hook does not affect the outcome of git-receive-pack, as it is called after the real work is done.

This supersedes the post-update hook in that it gets both old and new values of all the refs in addition to their names.

Both standard output and standard error output are forwarded to git send-pack on the other end, so you can simply echo messages for the user.

The default post-receive hook is empty, but there is a sample script post-receive-email provided in the contrib/hooks directory in git distribution, which implements sending commit emails.

Upvotes: 9

Related Questions