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