Filling in docs.
[git-central.git] / server / update-ensure-follows
1 #!/bin/sh
2 #
3 # When updating a branch, ensure it has the latest changes
4 # from other branches, e.g. stable.
5 #
6 # While this forces merging sooner than devs may like, it
7 # assures deployment and qa staff that the latest revisions
8 # they are qa'ing will always have the last stable release
9 # in it.
10 #
11 # Config
12 # ------
13 # hooks.update-ensure-follows.branches
14 #  Space-separated list of branches that other branches must merge with
15 # hooks.update-ensure-follows.excused
16 #  Space-separated list of branches that are excused from following (e.g. gitconfig)
17 #
18
19 . $(dirname $0)/functions
20
21 # Command line
22 refname="$1"
23 oldrev="$2"
24 newrev="$3"
25
26 # Look up the config variable and exit if not set
27 follows=$(git config hooks.update-ensure-follows.branches)
28 if [[ $? -ne 0 ]] ; then
29         exit 0
30 fi
31
32 # Branch deletions are okay
33 if expr "$newrev" : '0*$' >/dev/null ; then
34         exit 0
35 fi
36
37 # We only care about branches moving--ignore tags.
38 case "$refname" in
39         refs/heads/*)
40                 short_refname=${refname##refs/heads/}
41                 ;;
42         *)
43                 exit 0
44                 ;;
45 esac
46
47 excused=" $(git config hooks.update-ensure-follows.excused) "
48 if [[ $excused =~ " $short_refname " ]] ; then
49         exit 0
50 fi
51
52 follows=($follows)
53 count=${#follows[@]}
54 for ((i = 0 ; i < count ; i++)) do
55         follow="${follows[$i]}"
56         git rev-parse --verify --quiet "$follow"
57         if [ $? -eq 0 ] ; then
58                 missing_commits=$(git log ^$newrev $follow --pretty=oneline | wc -l)
59                 if [ $missing_commits -ne 0 ] ; then
60                         # If for some reason people are stupid and push with a --force flag,
61                         # we should warn them to update first in case one of their teammates
62                         # already merged for them
63                         if [ "0000000000000000000000000000000000000000" != "$oldrev" -a "$(git merge-base $oldrev $newrev)" != "$oldrev" ] ; then
64                                 display_error_message "You need to update your local branch $short_refname"
65                         else
66                                 display_error_message "You need to merge $follow into $short_refname"
67                         fi
68                         exit 1
69                 fi
70         fi
71 done
72
73 exit 0
74