]> git.ozlabs.org Git - next-scripts/blob - check_fixes
add check_fixes script
[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                 re='^(['\''"])(.*)\1$'
77                 if [[ "$subject" =~ $re ]]; then
78                         subject="${BASH_REMATCH[2]}"
79                 elif [[ "$subject" =~ ^[\'\"](.*)$ ]]; then
80                         subject="${BASH_REMATCH[1]}"
81                         msg="${msg:+${msg}${nl}}  - Subject has leading but no trailing quotes"
82                 fi
83
84                 subject=$(strip_spaces "$subject")
85
86                 target_subject=$(git log -1 --format='%s' "$sha")
87                 target_subject=$(strip_spaces "$target_subject")
88
89                 # match with ellipses
90                 case "$subject" in
91                 *...)   subject="${subject%...}"
92                         target_subject="${target_subject:0:${#subject}}"
93                         ;;
94                 ...*)   subject="${subject#...}"
95                         target_subject="${target_subject: -${#subject}}"
96                         ;;
97                 *\ ...\ *)
98                         s1="${subject% ... *}"
99                         s2="${subject#* ... }"
100                         subject="$s1 $s2"
101                         t1="${target_subject:0:${#s1}}"
102                         t2="${target_subject: -${#s2}}"
103                         target_subject="$t1 $t2"
104                         ;;
105                 esac
106                 subject=$(strip_spaces "$subject")
107                 target_subject=$(strip_spaces "$target_subject")
108
109                 if [ "$subject" != "${target_subject:0:${#subject}}" ]; then
110                         msg="${msg:+${msg}${nl}}  - Subject does not match target commit subject"
111                 fi
112                 lsha=$(cd $Linus_tree; git rev-parse -q --verify "$sha")
113                 if [ -z "$lsha" ]; then
114                         count=$(git rev-list --count "$sha".."$c")
115                         if [ "$count" -eq 0 ]; then
116                                 msg="${msg:+${msg}${nl}}  - Target is not an ancestor of this commit"
117                         fi
118                 fi
119                 if [ "$msg" ]; then
120                         printf '%s%s%s\n' "$commit_msg" "$fixes_msg" "$msg"
121                         commit_msg=''
122                 fi
123         done <<< "$fixes_lines"
124 done
125
126 exit 0