github.com/spg/deis@v1.7.3/builder/rootfs/etc/confd/templates/gitreceive (about) 1 #!/bin/bash 2 set -eo pipefail 3 4 # Places cursor at start of line, so that subsequent text replaces existing text. For example; 5 # "remote: Updated branch 'master' of 'repo'. Deploying to dev." becomes 6 # "Updated branch 'master' of 'repo'. Deploying to dev." 7 strip_remote_prefix() { 8 stdbuf -i0 -o0 -e0 sed "s/^/"$'\e[1G'"/" 9 } 10 11 GITUSER=${GITUSER:-git} 12 GITHOME="/home/$GITUSER" 13 SELF=`which $0` 14 15 case "$1" in 16 17 # called by sshd on each `git push` 18 run) 19 export RECEIVE_USER=$2 20 export RECEIVE_FINGERPRINT=$3 21 # ssh provides the original requested command in $SSH_ORIGINAL_COMMAND 22 SSH_ORIGINAL_COMMAND="$(echo $SSH_ORIGINAL_COMMAND | sed 's/\///g' )" # remove any '/'s 23 export RECEIVE_REPO="$(echo $SSH_ORIGINAL_COMMAND | awk '{print $2}' | sed -e 's/'\''//g')" 24 REPO_PATH="$GITHOME/$RECEIVE_REPO" 25 if [ ! -d $REPO_PATH ]; then 26 mkdir -p $REPO_PATH 27 cd $REPO_PATH 28 git init --bare > /dev/null 29 fi 30 cd $GITHOME 31 PRERECEIVE_HOOK="$REPO_PATH/hooks/pre-receive" 32 # inject a pre-receive hook 33 cat > $PRERECEIVE_HOOK <<EOF 34 #!/bin/bash 35 cat | $SELF pre-receive 36 EOF 37 chmod +x $PRERECEIVE_HOOK 38 # call the original git-shell 39 git-shell -c "$SSH_ORIGINAL_COMMAND" 40 ;; 41 42 pre-receive) 43 while read oldrev newrev refname 44 do 45 LOCKFILE="/tmp/$RECEIVE_REPO.lock" 46 if ( set -o noclobber; echo "$$" > "$LOCKFILE" ) 2> /dev/null; then 47 trap 'rm -f "$LOCKFILE"; exit 1' INT TERM EXIT 48 49 # check for authorization on this repo 50 $GITHOME/receiver "$RECEIVE_REPO" "$newrev" "$RECEIVE_USER" "$RECEIVE_FINGERPRINT" 51 rc=$? 52 if [[ $rc != 0 ]] ; then 53 echo " ERROR: failed on rev $newrev - push denied" 54 exit $rc 55 fi 56 # builder assumes that we are running this script from $GITHOME 57 cd $GITHOME 58 # if we're processing a receive-pack on an existing repo, run a build 59 if [[ $SSH_ORIGINAL_COMMAND == git-receive-pack* ]]; then 60 $GITHOME/builder $RECEIVE_USER $RECEIVE_REPO $newrev 2>&1 | strip_remote_prefix 61 fi 62 63 rm -f "$LOCKFILE" 64 trap - INT TERM EXIT 65 else 66 echo "Another git push is ongoing. Aborting..." 67 exit 1 68 fi 69 done 70 ;; 71 72 *) 73 echo "Usage: gitreceive <command> [options]" 74 ;; 75 esac