5 if [ "$#" -lt 1 ]; then
6 printf 'Usage: %s <commit range>\n', "$0" 1>&2
10 commits=$(git rev-list --no-merges -i --grep='^[[:space:]]*Fixes:' "$@")
11 if [ -z "$commits" ]; then
15 # This should be a git tree that contains *only* Linus' tree
16 Linus_tree="${HOME}/kernels/linus.git"
18 split_re='^([Cc][Oo][Mm][Mm][Ii][Tt])?[[:space:]]*([[:xdigit:]]{5,})([[:space:]]*)(.*)$'
22 # Strip the leading and training spaces from a string
25 local str="${1##*([[:space:]])}"
26 str="${str%%*([[:space:]])}"
32 printf -v commit_msg 'In commit\n\n %s\n\n' \
33 "$(git log -1 --format='%h ("%s")' "$c")"
35 readarray -t fixes_lines < <(git log -1 --format='%B' "$c" |
36 grep -i '^[[:space:]]*Fixes:')
37 fixes_lines=( "${fixes_lines[@]##*([[:space:]])}" )
38 fixes_lines=( "${fixes_lines[@]%%*([[:space:]])}" )
40 for fline in "${fixes_lines[@]}"; do
41 f="${fline##[Ff][Ii][Xx][Ee][Ss]:*([[:space:]])}"
42 printf -v fixes_msg 'Fixes tag\n\n %s\n\nhas these problem(s):\n\n' "$fline"
46 if [[ "$f" =~ $split_re ]]; then
47 first="${BASH_REMATCH[1]}"
48 sha="${BASH_REMATCH[2]}"
49 spaces="${BASH_REMATCH[3]}"
50 subject="${BASH_REMATCH[4]}"
52 msg="${msg:+${msg}${nl}} - leading word '$first' unexpected"
54 if [ -z "$subject" ]; then
55 msg="${msg:+${msg}${nl}} - missing subject"
56 elif [ -z "$spaces" ]; then
57 msg="${msg:+${msg}${nl}} - missing space between the SHA1 and the subject"
60 printf '%s%s - %s\n' "$commit_msg" "$fixes_msg" 'No SHA1 recognised'
64 if ! git rev-parse -q --verify "$sha" >/dev/null; then
65 printf '%s%s - %s\n' "$commit_msg" "$fixes_msg" 'Target SHA1 does not exist'
70 if [ "${#sha}" -lt 12 ]; then
71 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\")."
73 # reduce the subject to the part between () if there
74 if [[ "$subject" =~ ^\((.*)\) ]]; then
75 subject="${BASH_REMATCH[1]}"
76 elif [[ "$subject" =~ ^\((.*) ]]; then
77 subject="${BASH_REMATCH[1]}"
78 msg="${msg:+${msg}${nl}} - Subject has leading but no trailing parentheses"
81 # strip matching quotes at the start and end of the subject
82 # the unicode characters in the classes are
83 # U+201C LEFT DOUBLE QUOTATION MARK
84 # U+201D RIGHT DOUBLE QUOTATION MARK
85 # U+2018 LEFT SINGLE QUOTATION MARK
86 # U+2019 RIGHT SINGLE QUOTATION MARK
87 re1=$'^[\"\u201C](.*)[\"\u201D]$'
88 re2=$'^[\'\u2018](.*)[\'\u2019]$'
89 re3=$'^[\"\'\u201C\u2018](.*)$'
90 if [[ "$subject" =~ $re1 ]]; then
91 subject="${BASH_REMATCH[1]}"
92 elif [[ "$subject" =~ $re2 ]]; then
93 subject="${BASH_REMATCH[1]}"
94 elif [[ "$subject" =~ $re3 ]]; then
95 subject="${BASH_REMATCH[1]}"
96 msg="${msg:+${msg}${nl}} - Subject has leading but no trailing quotes"
99 subject=$(strip_spaces "$subject")
101 target_subject=$(git log -1 --format='%s' "$sha")
102 target_subject=$(strip_spaces "$target_subject")
104 # match with ellipses
106 *...) subject="${subject%...}"
107 target_subject="${target_subject:0:${#subject}}"
109 ...*) subject="${subject#...}"
110 target_subject="${target_subject: -${#subject}}"
113 s1="${subject% ... *}"
114 s2="${subject#* ... }"
116 t1="${target_subject:0:${#s1}}"
117 t2="${target_subject: -${#s2}}"
118 target_subject="$t1 $t2"
121 subject=$(strip_spaces "$subject")
122 target_subject=$(strip_spaces "$target_subject")
124 if [ "$subject" != "${target_subject:0:${#subject}}" ]; then
125 msg="${msg:+${msg}${nl}} - Subject does not match target commit subject${nl} Just use${nl}${tab}git log -1 --format='Fixes: %h (\"%s\")'"
127 lsha=$(cd "$Linus_tree" && git rev-parse -q --verify "$sha")
128 if [ -z "$lsha" ]; then
129 count=$(git rev-list --count "$sha".."$c")
130 if [ "$count" -eq 0 ]; then
131 msg="${msg:+${msg}${nl}} - Target is not an ancestor of this commit"
135 printf '%s%s%s\n' "$commit_msg" "$fixes_msg" "$msg"