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