A post-receive hook gets its arguments from stdin, in the form:
<oldrev> <newrev> <refname>
Since these arguments are coming from stdin, not from a command line argument, you need to use read
instead of $1 $2 $3
.
The post-receive hook can receive multiple branches at once (for example if someone does a git push --all
), so we also need to wrap the read
in a while
loop.
A working snippet looks something like this:
#!/bin/bash
while read oldrev newrev refname
do
branch=$(git rev-parse --symbolic --abbrev-ref $refname)
if [ "master" = "$branch" ]; then
# Do something
fi
done
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…