]> git.ozlabs.org Git - next-scripts/blob - check_fixes
get_gitid is no longer useful
[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 tab=$'\t'
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_re ]]; then
53                         first="${BASH_REMATCH[1]}"
54                         sha="${BASH_REMATCH[2]}"
55                         spaces="${BASH_REMATCH[3]}"
56                         subject="${BASH_REMATCH[4]}"
57                         if [ "$first" ]; then
58                                 msg="${msg:+${msg}${nl}}  - leading word '$first' unexpected"
59                         fi
60                         if [ -z "$subject" ]; then
61                                 msg="${msg:+${msg}${nl}}  - missing subject"
62                         elif [ -z "$spaces" ]; then
63                                 msg="${msg:+${msg}${nl}}  - missing space between the SHA1 and the subject"
64                         fi
65                 else
66                         printf '%s%s  - %s\n' "$commit_msg" "$fixes_msg" 'No SHA1 recognised'
67                         commit_msg=''
68                         continue
69                 fi
70                 if ! git rev-parse -q --verify "$sha" >/dev/null; then
71                         printf '%s%s  - %s\n' "$commit_msg" "$fixes_msg" 'Target SHA1 does not exist'
72                         commit_msg=''
73                         continue
74                 fi
75
76                 if [ "${#sha}" -lt 12 ]; then
77                         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\")."
78                 fi
79                 # reduce the subject to the part between () if there
80                 if [[ "$subject" =~ ^\((.*)\) ]]; then
81                         subject="${BASH_REMATCH[1]}"
82                 elif [[ "$subject" =~ ^\((.*) ]]; then
83                         subject="${BASH_REMATCH[1]}"
84                         msg="${msg:+${msg}${nl}}  - Subject has leading but no trailing parentheses"
85                 fi
86
87                 # strip matching quotes at the start and end of the subject
88                 # the unicode characters in the classes are
89                 # U+201C LEFT DOUBLE QUOTATION MARK
90                 # U+201D RIGHT DOUBLE QUOTATION MARK
91                 # U+2018 LEFT SINGLE QUOTATION MARK
92                 # U+2019 RIGHT SINGLE QUOTATION MARK
93                 re1=$'^[\"\u201C](.*)[\"\u201D]$'
94                 re2=$'^[\'\u2018](.*)[\'\u2019]$'
95                 re3=$'^[\"\'\u201C\u2018](.*)$'
96                 if [[ "$subject" =~ $re1 ]]; then
97                         subject="${BASH_REMATCH[1]}"
98                 elif [[ "$subject" =~ $re2 ]]; then
99                         subject="${BASH_REMATCH[1]}"
100                 elif [[ "$subject" =~ $re3 ]]; then
101                         subject="${BASH_REMATCH[1]}"
102                         msg="${msg:+${msg}${nl}}  - Subject has leading but no trailing quotes"
103                 fi
104
105                 subject=$(strip_spaces "$subject")
106
107                 target_subject=$(git log -1 --format='%s' "$sha")
108                 target_subject=$(strip_spaces "$target_subject")
109
110                 # match with ellipses
111                 case "$subject" in
112                 *...)   subject="${subject%...}"
113                         target_subject="${target_subject:0:${#subject}}"
114                         ;;
115                 ...*)   subject="${subject#...}"
116                         target_subject="${target_subject: -${#subject}}"
117                         ;;
118                 *\ ...\ *)
119                         s1="${subject% ... *}"
120                         s2="${subject#* ... }"
121                         subject="$s1 $s2"
122                         t1="${target_subject:0:${#s1}}"
123                         t2="${target_subject: -${#s2}}"
124                         target_subject="$t1 $t2"
125                         ;;
126                 esac
127                 subject=$(strip_spaces "$subject")
128                 target_subject=$(strip_spaces "$target_subject")
129
130                 if [ "$subject" != "${target_subject:0:${#subject}}" ]; then
131                         msg="${msg:+${msg}${nl}}  - Subject does not match target commit subject${nl}    Just use${nl}${tab}git log -1 --format='Fixes: %h (\"%s\")'"
132                 fi
133                 lsha=$(cd "$Linus_tree" && git rev-parse -q --verify "$sha")
134                 if [ -z "$lsha" ]; then
135                         count=$(git rev-list --count "$sha".."$c")
136                         if [ "$count" -eq 0 ]; then
137                                 msg="${msg:+${msg}${nl}}  - Target is not an ancestor of this commit"
138                         fi
139                 fi
140                 if [ "$msg" ]; then
141                         printf '%s%s%s\n' "$commit_msg" "$fixes_msg" "$msg"
142                         commit_msg=''
143                 fi
144         done <<< "$fixes_lines"
145 done
146
147 exit 0