7caf8651fced7b400cd7901be5c909f03ad41c0e
[git-central.git] / server / update-stable
1 #!/bin/sh
2
3 #
4 # This enforces stable/candidate/topic patterns.
5 #
6 # Specifically:
7 #
8 # * stable must be moved 1 merge commit at a time (see below)
9 # * candidates/topics are frozen once merged into stable
10 # * topics are frozen once merged into candidates
11 #
12 # For DAG aesthetics, we prefer stable only moving in the approved way,
13 # which is via empty (no change) merge commits. The rationale is that
14 # in the DAG we want a simple, one-commit move from each release to the
15 # next.
16 #
17 # We started out with:
18 #
19 # * -- A                stable
20 #  \    \
21 #   \    * -- * -- B    topic1
22 #    \           /
23 #     * -- * -- *       topic2
24 #
25 # And then publishing stable was a matter of fast-forwarding
26 # from A to B.
27 #
28 # In a complicated (non-rebased) DAG, this becomes hard to follow,
29 # so want we want instead is:
30 #
31 # * -- A ----------- C  stable
32 #  \    \           /
33 #   \    * -- * -- B    topic1
34 #    \           /
35 #     * -- * -- *       topic2
36 #
37 # Where commit C lists as it's first parent the prior stable
38 # commit and as it's second parent the release candidate. No
39 # other parents are allowed (e.g. no octopus merges here, which
40 # would insinuate qa didn't happen on the merged result).
41 #
42 # Also, we want to enforce that C does not actually introduce
43 # any diffs to the files between B and C--as they would be changes
44 # that QA does not see.
45 #
46
47 . $(dirname $0)/functions
48
49 refname="$1"
50 oldrev="$2"
51 newrev="$3"
52
53 case "$refname" in
54         refs/heads/*)
55                 short_refname=${refname##refs/heads/}
56                 ;;
57         *)
58                 exit 0
59                 ;;
60 esac
61
62 set_change_type
63 if [ "$change_type" == "delete" ] ; then
64         exit 0
65 fi
66
67 # create/delete is okay
68 if [ "$change_type" != "update" ] ; then
69         exit 0
70 fi
71
72 if [ "$short_refname" == "stable" ] ; then
73         # Stable enforcement
74
75         # read backwards:
76         # - all commits from old..new
77         # - unless they were already pointed to by a branch
78         # = all new commits on stable
79         count=$(git rev-parse --not --branches | git rev-list --stdin $oldrev..$newrev | wc -l)
80         if [ "$count" -ne "1" ] ; then
81                 display_error_message "Moving stable must entail a single commit"
82                 exit 1
83         fi
84
85         number_of_parents=$(git rev-list --no-walk --parents $newrev | sed 's/ /\n/g' | grep -v $newrev | wc -l)
86         if [ "$number_of_parents" -ne "2" ] ; then
87                 display_error_message "Moving stable must entail a merge commit"
88                 exit 1
89         fi
90
91         first_parent=$(git rev-list --no-walk --parents $newrev | sed 's/ /\n/g' | grep -v $newrev | head --lines=1)
92         if [ "$first_parent" != "$oldrev" ] ; then
93                 display_error_message "Moving stable must have the previous stable as the first parent"
94                 exit 1
95         fi
96
97         second_parent=$(git rev-list --no-walk --parents $newrev | sed 's/ /\n/g' | grep -v $newrev | tail --lines=1)
98         changed_lines=$(git diff $second_parent..$newrev | wc -l)
99         if [ "$changed_lines" -ne "0" ] ; then
100                 display_error_message "Moving stable must not result in any changes from $second_parent"
101                 exit 1
102         fi
103 else
104         # Check if candidate/topic is already in stable
105         git branch --contains "$oldrev" | grep --quiet stable
106         if [ $? -eq 0 ] ; then
107                 # Okay, it's in stable...but was it previously just a 0-commit initial banch?
108                 git rev-list --first-parent stable | grep --quiet "$oldrev"
109                 if [ $? -ne 0 ] ; then
110                         display_error_message "$short_refname has been merged into stable"
111                         exit 1
112                 fi
113         fi
114
115         # For now candidates can mix amongst each other so early exit
116         case "$refname" in
117                 refs/heads/candidate*)
118                         exit 0
119                         ;;
120                 *)
121                         ;;
122         esac
123
124         # Check if topic is already in candidates
125         candidate=$(git branch --contains "$oldrev" | grep -oP "candidate.*" --max-count=1)
126         if [ $? -eq 0 ] ; then
127                 display_error_message "$short_refname has been merged into $candidate"
128                 exit 1
129         fi
130 fi
131