]> git.ozlabs.org Git - next-scripts/blob - check_fixes
check_fixes: get rid of the last shellcheck warning
[next-scripts] / check_fixes
1 #!/bin/bash
2
3 if [ "$#" -lt 1 ]; then
4         printf 'Usage: %s <commit range>\n', "$0" 1>&2
5         exit 1
6 fi
7
8 commits=$(git rev-list --no-merges -i --grep='^[[:space:]]*Fixes:' "$@")
9 if [ -z "$commits" ]; then
10         exit 0
11 fi
12
13 # This should be a git tree that contains *only* Linus' tree
14 Linus_tree="${HOME}/kernels/linus.git"
15 split_re='^([[:xdigit:]]+)[[:space:]]+(.*)$'
16 nl=$'\n'
17
18 # Strip the leading and training spaces from a string
19 strip_spaces()
20 {
21         [[ "$1" =~ ^[[:space:]]*(.*[^[:space:]])[[:space:]]*$ ]]
22         echo "${BASH_REMATCH[1]}"
23 }
24
25 for c in $commits; do
26
27         commit_log=$(git log -1 --format='%h ("%s")' "$c")
28         commit_msg="In commit
29
30   $commit_log
31
32 "
33
34         fixes_lines=$(git log -1 --format='%B' "$c" |
35                         grep -i '^[[:space:]]*Fixes:')
36
37         while read -r fline; do
38                 [[ "$fline" =~ ^[[:space:]]*[Ff][Ii][Xx][Ee][Ss]:[[:space:]]*(.*)$ ]]
39                 f="${BASH_REMATCH[1]}"
40                 fixes_msg="Fixes tag
41
42   $fline
43
44 has these problem(s):
45
46 "
47                 sha=
48                 subject=
49                 msg=
50                 [[ "$f" =~ $split_re ]]
51                 sha="${BASH_REMATCH[1]}"
52                 subject="${BASH_REMATCH[2]}"
53
54                 if [ -z "$sha" ]; then
55                         printf '%s%s  - %s\n' "$commit_msg" "$fixes_msg" 'No SHA1 recognised'
56                         commit_msg=''
57                         continue
58                 fi
59                 if ! git rev-parse -q --verify "$sha" >/dev/null; then
60                         printf '%s%s  - %s\n' "$commit_msg" "$fixes_msg" 'Target SHA1 does not exist'
61                         commit_msg=''
62                         continue
63                 fi
64
65                 if [ "${#sha}" -lt 12 ]; then
66                         msg="${msg:+${msg}${nl}}  - SHA1 should be at least 12 digits long"
67                 fi
68                 # reduce the subject to the part between () if there
69                 if [[ "$subject" =~ ^\((.*)\) ]]; then
70                         subject="${BASH_REMATCH[1]}"
71                 elif [[ "$subject" =~ ^\((.*) ]]; then
72                         subject="${BASH_REMATCH[1]}"
73                         msg="${msg:+${msg}${nl}}  - Subject has leading but no trailing parentheses"
74                 fi
75
76                 # strip matching quotes at the start and end of the subject
77                 # the unicode characters in the classes are
78                 # U+201C LEFT DOUBLE QUOTATION MARK
79                 # U+201D RIGHT DOUBLE QUOTATION MARK
80                 # U+2018 LEFT SINGLE QUOTATION MARK
81                 # U+2019 RIGHT SINGLE QUOTATION MARK
82                 re1=$'^[\"\u201C](.*)[\"\u201D]$'
83                 re2=$'^[\'\u2018](.*)[\'\u2019]$'
84                 re3=$'^[\"\'\u201C\u2018](.*)$'
85                 if [[ "$subject" =~ $re1 ]]; then
86                         subject="${BASH_REMATCH[1]}"
87                 elif [[ "$subject" =~ $re2 ]]; then
88                         subject="${BASH_REMATCH[1]}"
89                 elif [[ "$subject" =~ $re3 ]]; then
90                         subject="${BASH_REMATCH[1]}"
91                         msg="${msg:+${msg}${nl}}  - Subject has leading but no trailing quotes"
92                 fi
93
94                 subject=$(strip_spaces "$subject")
95
96                 target_subject=$(git log -1 --format='%s' "$sha")
97                 target_subject=$(strip_spaces "$target_subject")
98
99                 # match with ellipses
100                 case "$subject" in
101                 *...)   subject="${subject%...}"
102                         target_subject="${target_subject:0:${#subject}}"
103                         ;;
104                 ...*)   subject="${subject#...}"
105                         target_subject="${target_subject: -${#subject}}"
106                         ;;
107                 *\ ...\ *)
108                         s1="${subject% ... *}"
109                         s2="${subject#* ... }"
110                         subject="$s1 $s2"
111                         t1="${target_subject:0:${#s1}}"
112                         t2="${target_subject: -${#s2}}"
113                         target_subject="$t1 $t2"
114                         ;;
115                 esac
116                 subject=$(strip_spaces "$subject")
117                 target_subject=$(strip_spaces "$target_subject")
118
119                 if [ "$subject" != "${target_subject:0:${#subject}}" ]; then
120                         msg="${msg:+${msg}${nl}}  - Subject does not match target commit subject"
121                 fi
122                 lsha=$(cd "$Linus_tree" && git rev-parse -q --verify "$sha")
123                 if [ -z "$lsha" ]; then
124                         count=$(git rev-list --count "$sha".."$c")
125                         if [ "$count" -eq 0 ]; then
126                                 msg="${msg:+${msg}${nl}}  - Target is not an ancestor of this commit"
127                         fi
128                 fi
129                 if [ "$msg" ]; then
130                         printf '%s%s%s\n' "$commit_msg" "$fixes_msg" "$msg"
131                         commit_msg=''
132                 fi
133         done <<< "$fixes_lines"
134 done
135
136 exit 0