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