post-receive-delivery: new script to deliver branches
authorOlivier Matz <zer0@droids-corp.org>
Mon, 18 Feb 2013 20:52:56 +0000 (21:52 +0100)
committerOlivier Matz <zer0@droids-corp.org>
Mon, 18 Feb 2013 21:22:40 +0000 (22:22 +0100)
Signed-off-by: Olivier Matz <zer0@droids-corp.org>
server/post-receive-delivery [new file with mode: 0755]

diff --git a/server/post-receive-delivery b/server/post-receive-delivery
new file mode 100755 (executable)
index 0000000..4726561
--- /dev/null
@@ -0,0 +1,98 @@
+#!/bin/bash
+#
+# Copyright (c) 2007 Andy Parkins
+# Copyright (c) 2008 Stephen Haberman
+# Copyright (c) 2013 Olivier Matz
+#
+# Based on post-receive-email
+#
+# This hook clone and pull the repoditory each time a commit is pushed
+# in the tree. Each branch will be delivered in
+# ${GIT_DELIVER_DIRECTORY}/branch.
+
+
+# ---------------------------- Functions
+
+. $(dirname $0)/functions
+
+generate_delivery()
+{
+       # --- Arguments
+       oldrev=$(git rev-parse $1)
+       newrev=$(git rev-parse $2)
+       refname="$3"
+
+       set_change_type
+       set_rev_types
+       set_describe_tags
+
+       # 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/heads/*,commit)
+                       # branch
+                       refname_type="branch"
+                       function="branch"
+                       branch=${refname##refs/heads/}
+                       ;;
+               *)
+                       # nothing to do
+                       return
+                       ;;
+       esac
+
+       if expr "$newrev" : '0*$' >/dev/null; then
+               # nothing to do, branch is deleted
+               return
+       fi
+
+       dstdir=${GIT_DELIVER_DIRECTORY}/${branch}
+       if [ ! -d ${dstdir} ]; then
+               mkdir -p ${GIT_DELIVER_DIRECTORY} || exit 2
+               cd ${GIT_DELIVER_DIRECTORY}
+               git clone --branch ${branch} ${GIT_DIR} ${branch}
+       else
+               cd ${dstdir}
+               unset GIT_DIR
+               git pull --rebase
+       fi
+}
+
+# ---------------------------- main()
+
+
+# --- Config
+# Set GIT_DIR either from the working directory or the environment variable.
+GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
+if [ -z "$GIT_DIR" ]; then
+       echo >&2 "fatal: post-receive: GIT_DIR not set"
+       exit 1
+fi
+GIT_DIR=$(readlink -e ${GIT_DIR})
+export GIT_DIR
+
+if [ -z "${GIT_DELIVER_DIRECTORY}" ]; then
+       echo >&2 "fatal: GIT_DELIVER_DIRECTORY not set"
+       exit 1
+fi
+
+recipients=$(git config hooks.post-receive-email.mailinglist)
+
+# --- Main loop
+# Allow dual mode: run from the command line just like the update hook, or
+# if no arguments are given then run as a hook script
+if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
+       # Output to the terminal in command line mode - if someone wanted to
+       # resend an email; they could redirect the output to sendmail
+       # themselves
+       PAGER= generate_delivery $2 $3 $1
+else
+       while read oldrev newrev refname
+       do
+               generate_delivery $oldrev $newrev $refname
+       done
+fi