]> git.lizzy.rs Git - rust.git/blob - src/ci/scripts/verify-backported-commits.sh
:arrow_up: rust-analyzer
[rust.git] / src / ci / scripts / verify-backported-commits.sh
1 #!/bin/bash
2 # Ensure commits in beta are in master & commits in stable are in beta + master.
3 set -euo pipefail
4 IFS=$'\n\t'
5
6 source "$(cd "$(dirname "$0")" && pwd)/../shared.sh"
7
8 # We don't care about commits that predate this automation check, so we pass a
9 # `<limit>` argument to `git cherry`.
10 BETA_LIMIT="53fd98ca776cb875bc9e5514f56b52eb74f9e7a9"
11 STABLE_LIMIT="a178d0322ce20e33eac124758e837cbd80a6f633"
12
13 verify_backported_commits_main() {
14   ci_base_branch=$(ciBaseBranch)
15
16   if [[ "$ci_base_branch" != "beta" && "$ci_base_branch" != "stable" ]]; then
17     echo 'Skipping. This is only run when merging to the beta or stable branches.'
18     exit 0
19   fi
20
21   if [[ $ci_base_branch == "beta" ]]; then
22     verify_cherries master "$BETA_LIMIT" \
23       || exit 1
24
25   elif [[ $ci_base_branch == "stable" ]]; then
26     (verify_cherries master "$STABLE_LIMIT" \
27       & verify_cherries beta "$STABLE_LIMIT") \
28       || exit 1
29
30   fi
31 }
32
33 # Verify all commits in `HEAD` are backports of a commit in <upstream>. See
34 # https://git-scm.com/docs/git-cherry for an explanation of the arguments.
35 #
36 # $1 = <upstream>
37 # $2 = <limit>
38 verify_cherries() {
39   # commits that lack a `backport-of` comment.
40   local no_backports=()
41   # commits with an incorrect `backport-of` comment.
42   local bad_backports=()
43
44   commits=$(git cherry "origin/$1" HEAD "$2")
45
46   if [[ -z "$commits" ]]; then
47     echo "All commits in \`HEAD\` are present in \`$1\`"
48     return 0
49   fi
50
51   commits=$(echo "$commits" | grep '^\+' | cut -c 3-)
52
53   while read sha; do
54     # Check each commit in <current>..<upstream>
55     backport_sha=$(get_backport "$sha")
56
57     if [[ "$backport_sha" == "nothing" ]]; then
58       echo "✓ \`$sha\` backports nothing"
59       continue
60     fi
61
62     if [[ -z "$backport_sha" ]]; then
63       no_backports+=("$sha")
64       continue
65     fi
66
67     if ! is_in_master "$backport_sha"; then
68       bad_backports+=("$sha")
69       continue
70     fi
71
72     echo "✓ \`$sha\` backports \`$backport_sha\`"
73   done <<< "$commits"
74
75   failure=0
76
77   if [ ${#no_backports[@]} -ne 0 ]; then
78         echo 'Error: Could not find backports for all commits.'
79         echo
80         echo 'All commits in \`HEAD\` are required to have a corresponding upstream commit.'
81         echo 'It looks like the following commits:'
82         echo
83         for commit in "${no_backports[@]}"; do
84           echo "    $commit"
85         done
86         echo
87         echo "do not match any commits in \`$1\`. If this was intended, add the text"
88         echo '\`backport-of: <SHA of a commit already in master>\`'
89         echo 'somewhere in the message of each of these commits.'
90         echo
91         failure=1
92   fi
93
94   if [ ${#bad_backports[@]} -ne 0 ]; then
95         echo 'Error: Found incorrectly marked commits.'
96         echo
97         echo 'The following commits:'
98         echo
99         for commit in "${bad_backports[@]}"; do
100           echo "    $commit"
101         done
102         echo
103         echo 'have commit messages marked \`backport-of: <SHA>\`, but the SHA is not in'
104         echo '\`master\`.'
105         echo
106         failure=1
107   fi
108
109   return $failure
110 }
111
112 # Get the backport of a commit. It echoes one of:
113 #
114 # 1. A SHA of the backported commit
115 # 2. The string "nothing"
116 # 3. An empty string
117 #
118 # $1 = <sha>
119 get_backport() {
120   # This regex is:
121   #
122   # ^.* - throw away any extra starting characters
123   # backport-of: - prefix
124   # \s\? - optional space
125   # \(\) - capture group
126   # [a-f0-9]\+\|nothing - a SHA or the text 'nothing'
127   # .* - throw away any extra ending characters
128   # \1 - replace it with the first match
129   # {s//\1/p;q} - print the first occurrence and quit
130   #
131   git show -s --format=%B "$1" \
132     | sed -n '/^.*backport-of:\s\?\([a-f0-9]\+\|nothing\).*/{s//\1/p;q}'
133 }
134
135 # Check if a commit is in master.
136 #
137 # $1 = <sha>
138 is_in_master() {
139   git merge-base --is-ancestor "$1" origin/master 2> /dev/null
140 }
141
142 verify_backported_commits_main