post-receive-delivery: add a deliver command
[git-central.git] / server / post-receive-delivery
1 #!/bin/bash
2 #
3 # Copyright (c) 2007 Andy Parkins
4 # Copyright (c) 2008 Stephen Haberman
5 # Copyright (c) 2013 Olivier Matz
6 #
7 # Based on post-receive-email
8 #
9 # This hook clone and pull the repoditory each time a commit is pushed
10 # in the tree. Each branch will be delivered in
11 # ${GIT_DELIVER_DIRECTORY}/branch. If ${GIT_DELIVER_COMMAND} is set,
12 # this command will be executed in the delivery directory.
13
14
15 # ---------------------------- Functions
16
17 . $(dirname $0)/functions
18
19 generate_delivery()
20 {
21         # --- Arguments
22         oldrev=$(git rev-parse $1)
23         newrev=$(git rev-parse $2)
24         refname="$3"
25
26         set_change_type
27         set_rev_types
28         set_describe_tags
29
30         # The revision type tells us what type the commit is, combined with
31         # the location of the ref we can decide between
32         #  - working branch
33         #  - tracking branch
34         #  - unannoted tag
35         #  - annotated tag
36         case "$refname","$rev_type" in
37                 refs/heads/*,commit)
38                         # branch
39                         refname_type="branch"
40                         function="branch"
41                         branch=${refname##refs/heads/}
42                         ;;
43                 *)
44                         # nothing to do
45                         return
46                         ;;
47         esac
48
49         if expr "$newrev" : '0*$' >/dev/null; then
50                 # nothing to do, branch is deleted
51                 return
52         fi
53
54         dstdir=${GIT_DELIVER_DIRECTORY}/${branch}
55         if [ ! -d ${dstdir} ]; then
56                 mkdir -p ${GIT_DELIVER_DIRECTORY} || exit 2
57                 cd ${GIT_DELIVER_DIRECTORY}
58                 git clone --branch ${branch} ${GIT_DIR} ${branch}
59         else
60                 cd ${dstdir}
61                 unset GIT_DIR
62                 git pull --rebase
63         fi
64
65         if [ ! -z "${GIT_DELIVER_COMMAND}" ]; then
66                 cd ${dstdir}
67                 ${GIT_DELIVER_COMMAND}
68         fi
69 }
70
71 # ---------------------------- main()
72
73
74 # --- Config
75 # Set GIT_DIR either from the working directory or the environment variable.
76 GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
77 if [ -z "$GIT_DIR" ]; then
78         echo >&2 "fatal: post-receive: GIT_DIR not set"
79         exit 1
80 fi
81 GIT_DIR=$(readlink -e ${GIT_DIR})
82 export GIT_DIR
83
84 if [ -z "${GIT_DELIVER_DIRECTORY}" ]; then
85         echo >&2 "fatal: GIT_DELIVER_DIRECTORY not set"
86         exit 1
87 fi
88
89 recipients=$(git config hooks.post-receive-email.mailinglist)
90
91 # --- Main loop
92 # Allow dual mode: run from the command line just like the update hook, or
93 # if no arguments are given then run as a hook script
94 if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
95         # Output to the terminal in command line mode - if someone wanted to
96         # resend an email; they could redirect the output to sendmail
97         # themselves
98         PAGER= generate_delivery $2 $3 $1
99 else
100         while read oldrev newrev refname
101         do
102                 generate_delivery $oldrev $newrev $refname
103         done
104 fi