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