Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Having trouble accessing the arguments to the git pre-receive hook

I am running vanilla git (git version 2.23.3) as origin master of a bare repository on an AWS instance.

I have created a file repo_dir/hooks/pre-receive with the following contents:

#!/bin/bash -p

echo $1 > pre-receive-old-hash.txt
echo $2 > pre-receive-new-hash.txt
echo $3 > pre-receive-ref.txt

exit 1

I make a test push from my remote. I can confirm that hook is run, as I get the push rejected.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

However, while the pre-receive-x.txt files exist, they are empty. What am I doing wrong?

>Solution :

https://git-scm.com/docs/githooks#pre-receive

It takes no arguments, but for each ref to be updated it receives on standard input a line of the format:

<old-value> SP <new-value> SP <ref-name> LF

(Emphasize mine — phd)

Make your script this:

#!/bin/bash -p

while read old new refname; do
    echo $old >> pre-receive-old-hash.txt
    echo $new >> pre-receive-new-hash.txt
    echo $refname >> pre-receive-ref.txt
done

exit 1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading