1#! /bin/sh 2# SPDX-License-Identifier: BSD-3-Clause 3# Copyright 2016 6WIND S.A. 4 5# Check commit logs (headlines and references) 6# 7# If any doubt about the formatting, please check in the most recent history: 8# git log --format='%>|(15)%cr %s' --reverse | grep -i <pattern> 9 10print_usage () { 11 cat <<- END_OF_HELP 12 usage: $(basename $0) [-h] [-nX|-r range] 13 14 Check commit log formatting. 15 The git commits to be checked can be specified as a "git log" option, 16 by latest git commits limited with -n option, or commits in the git 17 range specified with -r option. 18 e.g. To check only the last commit, ‘-n1’ or ‘-r@~..’ is used. 19 If no range provided, default is origin/main..HEAD. 20 END_OF_HELP 21} 22 23selfdir=$(dirname $(readlink -f $0)) 24# The script caters for two formats, the new preferred format, and the old 25# format to ensure backward compatibility. 26# The new format is aligned with the format of the checkpatches script, 27# and allows for specifying the patches to check by passing -nX or -r range. 28# The old format allows for specifying patches by passing -X or range 29# as the first argument. 30range=${1:-origin/main..} 31 32if [ "$range" = '--help' ] ; then 33 print_usage 34 exit 0 35# convert -N to HEAD~N.. in order to comply with git-log-fixes.sh getopts 36elif printf -- "$range" | grep -q '^-[0-9]\+' ; then 37 range="HEAD$(printf -- "$range" | sed 's,^-,~,').." 38else 39 while getopts hr:n: ARG ; do 40 case $ARG in 41 n ) range="HEAD~$OPTARG.." ;; 42 r ) range=$OPTARG ;; 43 h ) print_usage ; exit 0 ;; 44 ? ) print_usage ; exit 1 ;; 45 esac 46 done 47 shift $(($OPTIND - 1)) 48fi 49 50commits=$(git log --format='%h' --reverse $range) 51headlines=$(git log --format='%s' --reverse $range) 52bodylines=$(git log --format='%b' --reverse $range) 53fixes=$(git log --format='%h %s' --reverse $range | grep -i ': *fix' | cut -d' ' -f1) 54stablefixes=$($selfdir/git-log-fixes.sh $range | sed '/(N\/A)$/d' | cut -d' ' -f2) 55tags=$(git log --format='%b' --reverse $range | grep -i -e 'by *:' -e 'fix.*:') 56bytag='\(Reported\|Suggested\|Signed-off\|Acked\|Reviewed\|Tested\)-by:' 57 58failure=false 59 60# check headline format (spacing, no punctuation, no code) 61bad=$(echo "$headlines" | grep --color=always \ 62 -e ' ' \ 63 -e '^ ' \ 64 -e ' $' \ 65 -e '\.$' \ 66 -e '[,;!?&|]' \ 67 -e ':.*_' \ 68 -e '^[^:]\+$' \ 69 -e ':[^ ]' \ 70 -e ' :' \ 71 | sed 's,^,\t,') 72[ -z "$bad" ] || { printf "Wrong headline format:\n$bad\n" && failure=true;} 73 74# check headline prefix when touching only drivers, e.g. net/<driver name> 75bad=$(for commit in $commits ; do 76 headline=$(git log --format='%s' -1 $commit) 77 files=$(git diff-tree --no-commit-id --name-only -r $commit) 78 [ -z "$(echo "$files" | grep -v '^\(drivers\|doc\|config\)/')" ] || 79 continue 80 drv=$(echo "$files" | grep '^drivers/' | cut -d "/" -f 2,3 | sort -u) 81 drvgrp=$(echo "$drv" | cut -d "/" -f 1 | uniq) 82 if [ $(echo "$drvgrp" | wc -l) -gt 1 ] ; then 83 echo "$headline" | grep -v '^drivers:' 84 elif [ $(echo "$drv" | wc -l) -gt 1 ] ; then 85 echo "$headline" | grep -v "^drivers/$drvgrp" 86 else 87 echo "$headline" | grep -v "^$drv" 88 fi 89done | sed 's,^,\t,') 90[ -z "$bad" ] || { printf "Wrong headline prefix:\n$bad\n" && failure=true;} 91 92# check headline prefix for libraries 93bad=$(echo "$headlines" | grep --color=always \ 94 -e '^lib/' \ 95 | sed 's,^,\t,') 96[ -z "$bad" ] || { printf "Wrong headline prefix:\n$bad\n" && failure=true;} 97 98# check headline label for common typos 99bad=$(echo "$headlines" | grep --color=always \ 100 -e '^example[:/]' \ 101 -e '^apps/' \ 102 -e '^testpmd' \ 103 -e 'test-pmd' \ 104 -e '^bond:' \ 105 | sed 's,^,\t,') 106[ -z "$bad" ] || { printf "Wrong headline label:\n$bad\n" && failure=true;} 107 108# check headline lowercase for first words 109bad=$(echo "$headlines" | grep --color=always \ 110 -e '^.*[[:upper:]].*:' \ 111 -e ': *[[:upper:]]' \ 112 | sed 's,^,\t,') 113[ -z "$bad" ] || { printf "Wrong headline uppercase:\n$bad\n" && failure=true;} 114 115# check headline case (Rx/Tx, VF, L2, MAC, Linux ...) 116IFS=' 117' 118words="$selfdir/words-case.txt" 119for word in $(cat $words); do 120 bad=$(echo "$headlines" | grep -iw $word | grep -vw $word) 121 if [ "$word" = "Tx" ]; then 122 bad=$(echo $bad | grep -v 'OCTEON\ TX') 123 fi 124 for bad_line in $bad; do 125 bad_word=$(echo $bad_line | cut -d":" -f2 | grep -iwo $word) 126 [ -z "$bad_word" ] || { printf "Wrong headline case:\n\ 127 \"$bad_line\": $bad_word --> $word\n" && failure=true;} 128 done 129done 130 131# check headline length (60 max) 132bad=$(echo "$headlines" | 133 awk 'length>60 {print}' | 134 sed 's,^,\t,') 135[ -z "$bad" ] || { printf "Headline too long:\n$bad\n" && failure=true;} 136 137# check body lines length (75 max) 138bad=$(echo "$bodylines" | grep -v '^Fixes:' | 139 awk 'length>75 {print}' | 140 sed 's,^,\t,') 141[ -z "$bad" ] || { printf "Line too long:\n$bad\n" && failure=true;} 142 143# check starting commit message with "It" 144bad=$(for commit in $commits ; do 145 firstbodyline=$(git log --format='%b' -1 $commit | head -n1) 146 echo "$firstbodyline" | grep --color=always -ie '^It ' 147done | sed 's,^,\t,') 148[ -z "$bad" ] || { printf "Wrong beginning of commit message:\n$bad\n"\ 149 && failure=true;} 150 151# check tags spelling 152bad=$(echo "$tags" | 153 grep -v "^$bytag [^,]* <.*@.*>$" | 154 grep -v '^Fixes: [0-9a-f]\{7\}[0-9a-f]* (".*")$' | 155 sed 's,^.,\t&,') 156[ -z "$bad" ] || { printf "Wrong tag:\n$bad\n" && failure=true;} 157 158# check missing Coverity issue: tag 159bad=$(for commit in $commits; do 160 body=$(git log --format='%b' -1 $commit) 161 echo "$body" | grep -qi coverity || continue 162 echo "$body" | grep -q '^Coverity issue:' && continue 163 git log --format='\t%s' -1 $commit 164done) 165[ -z "$bad" ] || { printf "Missing 'Coverity issue:' tag:\n$bad\n"\ 166 && failure=true;} 167 168# check missing Bugzilla ID: tag 169bad=$(for commit in $commits; do 170 body=$(git log --format='%b' -1 $commit) 171 echo "$body" | grep -qi bugzilla || continue 172 echo "$body" | grep -q '^Bugzilla ID:' && continue 173 git log --format='\t%s' -1 $commit 174done) 175[ -z "$bad" ] || { printf "Missing 'Bugzilla ID:' tag:\n$bad\n"\ 176 && failure=true;} 177 178# check missing Fixes: tag 179bad=$(for fix in $fixes ; do 180 git log --format='%b' -1 $fix | grep -q '^Fixes: ' || 181 git log --format='\t%s' -1 $fix 182done) 183[ -z "$bad" ] || { printf "Missing 'Fixes' tag:\n$bad\n" && failure=true;} 184 185# check Fixes: reference 186fixtags=$(echo "$tags" | grep '^Fixes: ') 187bad=$(for fixtag in $fixtags ; do 188 hash=$(echo "$fixtag" | sed 's,^Fixes: \([0-9a-f]*\).*,\1,') 189 if git branch --contains $hash 2>&- | grep -q '^\*' ; then 190 good="Fixes: $hash "$(git log --format='("%s")' -1 $hash 2>&-) 191 else 192 good="reference not in current branch" 193 fi 194 printf "$fixtag" | grep -v "^$good$" 195done | sed 's,^,\t,') 196[ -z "$bad" ] || { printf "Wrong 'Fixes' reference:\n$bad\n" && failure=true;} 197 198# check Cc: stable@dpdk.org for fixes 199bad=$(for fix in $stablefixes ; do 200 git log --format='%b' -1 $fix | grep -qi '^Cc: *stable@dpdk.org' || 201 git log --format='\t%s' -1 $fix 202done) 203[ -z "$bad" ] || { printf "Is it candidate for Cc: stable@dpdk.org backport?\n$bad\n"\ 204 && failure=true;} 205 206total=$(echo "$commits" | wc -l) 207if $failure ; then 208 printf "\nInvalid patch(es) found - checked $total patch" 209else 210 printf "\n$total/$total valid patch" 211fi 212[ $total -le 1 ] || printf 'es' 213printf '\n' 214$failure && exit 1 || exit 0 215