Skip to content

Instantly share code, notes, and snippets.

@addfs
Created September 29, 2011 17:55
Show Gist options
  • Save addfs/1251416 to your computer and use it in GitHub Desktop.
Save addfs/1251416 to your computer and use it in GitHub Desktop.
git post-recive hook for pivotal
#!/bin/sh
#
# Alexey Ch <[email protected]>
#
# The "post-receive" script is run after receive-pack has accepted a pack
# and the repository has been updated. It is passed arguments in through
# stdin in the form
# <oldrev> <newrev> <refname>
# For example:
# aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master
#
# Sets: $change_type
# Assumes: $oldrev $newrev
#
# --- Interpret
# 0000->1234 (create)
# 1234->2345 (update)
# 2345->0000 (delete)
set_change_type() {
if [ "$oldrev" == "0000000000000000000000000000000000000000" ] ; then
change_type="create"
else
if [ "$newrev" == "0000000000000000000000000000000000000000" ] ; then
change_type="delete"
else
change_type="update"
fi
fi
}
# Sets: $newrev_type $oldrev_type $rev $rev_type
# Assumes: $newrev $oldrev
# --- Get the revision types
set_rev_types() {
newrev_type=$(git cat-file -t "$newrev" 2> /dev/null)
oldrev_type=$(git cat-file -t "$oldrev" 2> /dev/null)
if [ "$newrev" == "0000000000000000000000000000000000000000" ] ; then
rev_type="$oldrev_type"
rev="$oldrev"
else
rev_type="$newrev_type"
rev="$newrev"
fi
}
pivotal_hook()
{
# --- Arguments
oldrev=$(git rev-parse $1)
newrev=$(git rev-parse $2)
refname="$3"
set_change_type
set_rev_types
case "$refname","$rev_type" in
refs/heads/*,commit)
# branch
refname_type="branch"
function="branch"
short_refname=${refname##refs/heads/}
#sed reverse rev_list
rev_list=$(git rev-list $oldrev..$newrev | sed -n '1!G;h;$p')
while IFS='\n' read -ra ADDR; do
for i in "${ADDR[@]}"; do
author=$(git rev-list -1 --pretty --format=%cn $i | sed '1d')
message=$(git cat-file commit $i | sed '1,/^$/d')
message=${message//&/&amp;}
message=${message//</&lt;}
message=${message//>/&gt;}
curl -H "X-TrackerToken: $pivotal_token" -X POST -H "Content-type: application/xml" \
-d "<source_commit><message>$message</message><author>*$author*</author><commit_id>$i</commit_id></source_commit>" \
http://www.pivotaltracker.com/services/v3/source_commits > /dev/null 2>&1
echo >&2 "*** Comment successfuly added"
done
done <<< "$rev_list"
;;
refs/remotes/*,commit)
# tracking branch
refname_type="tracking branch"
short_refname=${refname##refs/remotes/}
echo >&2 "*** Push-update of tracking branch, $refname"
echo >&2 "*** - no action."
exit 0
;;
*)
# Anything else (is there anything else?)
echo >&2 "*** Unknown type of update to $refname ($rev_type)"
echo >&2 "*** - no action"
exit 1
;;
esac
}
pivotal_token="PUT YOU TOKEN HERE"
while read oldrev newrev refname
do
pivotal_hook $oldrev $newrev $refname
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment