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