public

function library for git hooks & a post-receive hook for automatic branch building

  • Download Gist
git-hook-functions.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
#!/bin/bash
 
GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
if [ -z "$GIT_DIR" ]; then
echo >&2 "fatal: hooks/functions: GIT_DIR not set"
exit 1
fi
 
read oldrev newrev refname
 
# --- Interpret
# 0000->1234 (create)
# 1234->2345 (update)
# 2345->0000 (delete)
if expr "$oldrev" : '0*$' >/dev/null
then
change_type="create"
else
if expr "$newrev" : '0*$' >/dev/null
then
change_type="delete"
else
change_type="update"
fi
fi
 
# --- Get the revision types
newrev_type=$(git cat-file -t $newrev 2> /dev/null)
oldrev_type=$(git cat-file -t "$oldrev" 2> /dev/null)
case "$change_type" in
create|update)
rev="$newrev"
rev_type="$newrev_type"
;;
delete)
rev="$oldrev"
rev_type="$oldrev_type"
;;
esac
 
# The revision type tells us what type the commit is, combined with
# the location of the ref we can decide between
# - working branch
# - tracking branch
# - unannoted tag
# - annotated tag
case "$refname","$rev_type" in
refs/tags/*,commit)
# un-annotated tag
refname_type="tag"
short_refname=${refname##refs/tags/}
;;
refs/tags/*,tag)
# annotated tag
refname_type="annotated tag"
short_refname=${refname##refs/tags/}
# change recipients
if [ -n "$announcerecipients" ]; then
recipients="$announcerecipients"
fi
;;
refs/heads/*,commit)
# branch
refname_type="branch"
short_refname=${refname##refs/heads/}
;;
refs/remotes/*,commit)
# tracking branch
refname_type="tracking branch"
short_refname=${refname##refs/remotes/}
;;
*)
# Anything else (is there anything else?)
refname_type="unknown"
;;
esac
 
describe=$(git describe $rev 2>/dev/null)
if [ -z "$describe" ]; then
describe=$rev
fi
 
projectdesc=$(sed -ne '1p' "$GIT_DIR/description")
# Check if the description is unchanged from it's default, and shorten it to
# a more manageable length if it is
if expr "$projectdesc" : "Unnamed repository.*$" >/dev/null
then
projectdesc="UNNAMED PROJECT"
fi
 
# some email helpers
recipients=$(git config hooks.mailinglist)
announcerecipients=$(git config hooks.announcelist)
envelopesender=$(git config hooks.envelopesender)
emailprefix=$(git config hooks.emailprefix || echo '[SCM] ')
custom_showrev=$(git config hooks.showrev)
 
# get the name of the target branch for builds
regex='refs/heads/(.*)'
if [[ $refname =~ $regex ]]; then
target_branch="${BASH_REMATCH[1]}"
fi
 
# how to spawn builds in jenkins
jenkins_url="http://jenkins//job"
jenkins_token="SEEEKRIT"
get_autobuild_options() {
configfile_hash=$(git ls-tree $newrev | grep autobuild.cfg | cut -d' ' -f 3 | cut -f 1 )
if [[ "$configfile_hash" != "" ]] ; then
autobuild_options=$( git cat-file blob "$configfile_hash" | egrep -i "^\s*${target_branch}=" | cut -d= -f 2 )
echo "autobuild options detected: '${autobuild_options}'"
autobuild_enabled=$( echo "$autobuild_options" | grep '\<autobuild\>' )
fi
}
post-receive.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
job="MyJenkinsJob"
 
# load the git hook functions library
. git-hook-functions.sh
 
# read the autobuild configuration
get_autobuild_options
 
if [[ $autobuild_enabled ]] ; then
function trigger_build {
echo "Automatically scheduling new build against origin/$target_branch: $jenkins_url/$job"
wget -q -O - "$jenkins_url/$job/buildWithParameters?token=$jenkins_token&BRANCH=$target_branch" > /dev/null
}
 
case "$change_type" in
create)
if expr "$newrev" : '0*$' >/dev/null; then
echo "$refname is newly created, so not triggering a build."
else
trigger_build
fi
;;
update)
trigger_build
;;
delete)
echo "$refname is being deleted, so not triggering a build."
;;
esac
fi

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.