Marco Andreolli
Marco Andreolli

Reputation: 376

Get the refname in post-receive hooks

I use git under window and I want do some operation after each push, so I use post-receive hook but when I try to get the refname to know the branch that is pushed I give anything.

Why? (I can't give also the other parameters: oldrev and newrev)

This is my post-receive file, the email are send correctly but there isn't the refname in the subject (it's the same if i put $3 in the body)

#!/bin/sh
#
# An example hook script for the "post-receive" event.
#
# 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
#
# see contrib/hooks/ for a sample, or uncomment the next line and
# rename the file to "post-receive".

#. /usr/share/doc/git-core/contrib/hooks/post-receive-email    

# send mail
last_comment=$(git log -n 1 HEAD --format=format:%s%n%b)
last_change=$(git log -1 --name-status)
msmtp  $(git config hooks.mailinglist) <<EOI
Subject: [GIT] ($3) Sources update
$last_change
EOI

Upvotes: 0

Views: 4055

Answers (1)

manojlds
manojlds

Reputation: 301177

The comments at the top of your script shows what is being passed and how:

It is passed arguments in through
# stdin in the form
#  <oldrev> <newrev> <refname>

keyword being stdin. These are not passed as arguments to the script.

You can read from the stdin using something like below:

while read oldrev newrev refname
do
  # Do what you want with $oldrev $newrev $refname

done

Upvotes: 2

Related Questions