Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

From the post-receive file:

# This 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
#

But when I test it with echo "$1 $2 $3", I get a blank line only. Does any-one know why?

share|improve this question

3 Answers 3

That is because the arguments are passed through the stdin, not through the command line arguments. This is because there can be multiple changes which are then passed to your script as multiple lines. So you can either use the read command or get the input from /dev/stdin.

share|improve this answer

Here's a simple example that confirms koumes21s answer. I made post-receive a Python script with the following code:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import sys

print "ARGS:", sys.argv

a = sys.stdin.read()
(old, new, ref) = a.split()
print "Old: %s" % old
print "New: %s" % new
print "Ref: %s" % ref

Here's the output after a push. Notice that "ARGS" only reports the name of the script and none of the stdin.

inneralienmbp$ git push
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 299 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
remote: ARGS: ['hooks/post-receive']
remote: Old: 5c9f9a43132516040200ae76cc2f4f2cad57d724
remote: New: 95e0e2873eaad2a9befa2dff7e2ce9ffdf3af843
remote: Ref: refs/heads/master
To /Users/tweaver/test2/test.git/
   5c9f9a4..95e0e28  master -> master

Thanks koumes21!

share|improve this answer

Here is a post on stack which solves this problem.

http://stackoverflow.com/a/12367999/1354978

Here is a simple version for what you are trying to get:

read oldrev newrev ref
echo "$oldrev"
echo "$newrev"
echo "$ref"

Here is the version that I use for my CI server and email hook

read oldrev newrev ref
echo "$oldrev" "$newrev" "$ref" | . /usr/share/git-core/contrib/hooks/post-receive-email


if [ "refs/heads/qa" == "$ref" ]; then
  # Big Tuna YO!
  wget -q -O - --connect-timeout=2 http://127.0.0.1:3000/hooks/build/qa_now
fi
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.