Skip to content

Instantly share code, notes, and snippets.

@spuder
Last active March 8, 2016 16:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spuder/9795078 to your computer and use it in GitHub Desktop.
Save spuder/9795078 to your computer and use it in GitHub Desktop.
pre-recieve
#!/bin/bash
#I don't understand this behavior, there are 0 parameters passed in, yet I am still able to refer to $oldsha, $refname and $newsha
set -x
echo "oldsha is $oldsha"
echo "refname is $refname"
echo "newsha is $newsha"
echo "Recieved $# parameters"
echo " \$\A is $@"
echo $1
NULL_SHA1="0000000000000000000000000000000000000000" # 40 0's
new_list=
any_deleted=false
while read oldsha newsha refname; do
echo "oldsha is $oldsha"
echo "refname is $refname"
echo "newsha is $newsha"
echo "Recieved $# parameters"
echo " \$\A is $@"
echo $1
case $oldsha,$newsha in
*,$NULL_SHA1) # it's a delete
any_deleted=true;;
$NULL_SHA1,*) # it's a create
new_list="$new_list $newsha";;
*,*) # it's an update
new_list="$new_list $newsha";;
esac
done
$any_deleted && [ -n "$new_list" ] && {
echo 'error: you are deleting some refs and creating/updating others'
echo 'please split your push into separate operations'
exit 1
}
[ -z "$new_list" ] && exit 0
# look at all new objects, and verify them
# let's write the verifier function, including a check_banned function...
check_banned() {
if [ "$1" = root ]; then
echo "################################################################"
echo "Commits from $1 are not allowed"
echo ... rest of message ...
exit 1
fi
}
check_commit() {
check_banned "$(git log -1 --pretty=format:%an $1)"
check_banned "$(git log -1 --pretty=format:%cn $1)"
}
git rev-list $new_list --not --all |
while read sha1; do
objtype=$(git cat-file -t $sha1)
case $objtype in
commit) check_commit $sha1;;
esac
done
#########################
#http://stackoverflow.com/questions/22546393/can-git-pre-receive-hooks-evaulate-the-incoming-commit
Here is what is printed out
Obviously oldsha newsha and refname are not parametrs, so where are they set?
sowen@pv-sowen-nb:/tmp/hooktest3$ git push
If you see an error; 'Permission denied',verify you have added your ssh key
within the web interface, or use https
Counting objects: 11, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (9/9), 731 bytes | 0 bytes/s, done.
Total 9 (delta 3), reused 0 (delta 0)
remote: + echo 'oldsha is '
remote: oldsha is
remote: + echo 'refname is '
remote: refname is
remote: + echo 'newsha is '
remote: newsha is
remote: + echo 'Recieved 0 parameters'
remote: Recieved 0 parameters
remote: + echo ' $\A is '
remote: $\A is
remote: + echo
remote:
remote: + NULL_SHA1=0000000000000000000000000000000000000000
remote: + new_list=
remote: + any_deleted=false
remote: + read oldsha newsha refname
remote: + echo 'oldsha is 82050cbc63eafb4a456fd7b9b8fed4875a049a38'
remote: oldsha is 82050cbc63eafb4a456fd7b9b8fed4875a049a38
remote: + echo 'refname is refs/heads/master'
remote: refname is refs/heads/master
remote: + echo 'newsha is a73dd42e0ea61803118925c25ac173f430f1a1dc'
remote: newsha is a73dd42e0ea61803118925c25ac173f430f1a1dc
remote: + echo 'Recieved 0 parameters'
remote: Recieved 0 parameters
remote: + echo ' $\A is '
remote: $\A is
remote: + echo
remote:
remote: + case $oldsha,$newsha in
remote: + new_list=' a73dd42e0ea61803118925c25ac173f430f1a1dc'
remote: + read oldsha newsha refname
remote: + false
remote: + '[' -z ' a73dd42e0ea61803118925c25ac173f430f1a1dc' ']'
remote: + git rev-list a73dd42e0ea61803118925c25ac173f430f1a1dc --not --all
remote: + read sha1
remote: ++ git cat-file -t a73dd42e0ea61803118925c25ac173f430f1a1dc
remote: + objtype=commit
remote: + case $objtype in
remote: + check_commit a73dd42e0ea61803118925c25ac173f430f1a1dc
remote: ++ git log -1 --pretty=format:%an a73dd42e0ea61803118925c25ac173f430f1a1dc
remote: + check_banned root
remote: + '[' root = root ']'
remote: + echo '################################################################'
remote: ################################################################
remote: + echo 'Commits from root are not allowed'
remote: Commits from root are not allowed
remote: + echo ... rest of message ...
remote: ... rest of message ...
remote: + exit 1
@spuder
Copy link
Author

spuder commented Apr 28, 2014

because git can receive up to ~4000 references in 1 commit, not possible to pass that many parameters into a script. Must read them from stdin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment