Branch
Hash :
89d313b1
Author :
Date :
2020-05-27T20:33:25
checkpatch.sh: fix clang-format-diff usage - strip 1 path component - it does not accept file name anymore, only stdin Refs: #1029
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
#!/usr/bin/env bash
# TODO:
# - inline replace
# - clang-format-diff replacement
# - uncrustify for patches (not git refs)
# - maybe integrate into travis-ci?
function usage()
{
cat <<EOL
$0 [ OPTS ] [ file-or-gitref [ ... ] ]
Example:
# Chech HEAD git ref
$ $0 -r
$ $0 -r HEAD
# Check patch
$ git format-patch --stdout -1 | $0 -p
$ git show -1 | $0 -p
# Or via regular files
$ git format-patch --stdout -2
$ $0 *.patch
# Over a file
$ $0 -d event.c
$ $0 -d < event.c
# And print the whole file not only summary
$ $0 -f event.c
$ $0 -f < event.c
OPTS:
-p - treat as patch
-f - treat as regular file
-d - treat as regular file and print diff
-r - treat as git revision (default)
-C - check using clang-format (default)
-U - check with uncrustify
-c - config for clang-format/uncrustify
-h - print this message
EOL
}
function cfg()
{
[ -z "${options[cfg]}" ] || {
echo "${options[cfg]}"
return
}
local dir="$(dirname "${BASH_SOURCE[0]}")"
[ "${options[clang]}" -eq 0 ] || {
echo "$dir/.clang-format"
return
}
[ "${options[uncrustify]}" -eq 0 ] || {
echo "$dir/.uncrustify"
return
}
}
function abort()
{
local msg="$1"
shift
printf "$msg\n" "$@" >&2
exit 1
}
function default_arg()
{
if [ "${options[ref]}" -eq 1 ]; then
echo "HEAD"
else
[ ! -t 0 ] || abort "<stdin> is a tty"
echo "/dev/stdin"
fi
}
function parse_options()
{
options[patch]=0
options[file]=0
options[file_diff]=0
options[ref]=1
options[clang]=1
options[uncrustify]=0
options[cfg]=
local OPTARG OPTIND c
while getopts "pfrdCUc:h?" c; do
case "$c" in
p)
options[patch]=1
options[ref]=0
options[file]=0
options[file_diff]=0
;;
f)
options[file]=1
options[ref]=0
options[patch]=0
options[file_diff]=0
;;
r)
options[ref]=1
options[file]=0
options[patch]=0
options[file_diff]=0
;;
d)
options[file_diff]=1
options[file]=0
options[patch]=0
options[ref]=0
;;
C)
options[clang]=1
options[uncrustify]=0
;;
U)
options[uncrustify]=1
options[clang]=0
;;
c) options[cfg]="$OPTIND" ;;
?|h)
usage
exit 0
;;
*)
usage
exit 1
;;
esac
done
options[cfg]="$(cfg)"
[ -f "${options[cfg]}" ] || \
abort "Config '%s' does not exist" "${options[cfg]}"
shift $((OPTIND - 1))
args=( "$@" )
if [ ${#args[@]} -eq 0 ]; then
# exit on error globally, not only in subshell
default_arg > /dev/null
args=( "$(default_arg)" )
fi
if [ "${args[0]}" = "/dev/stdin" ]; then
TMP_FILE="/tmp/libevent.checkpatch.$RANDOM"
cat > "$TMP_FILE"
trap "rm '$TMP_FILE'" EXIT
args[0]="$TMP_FILE"
fi
}
function diff() { command diff --color=always "$@"; }
function clang_style()
{
local c="${options[cfg]}"
echo "{ $(sed -e 's/#.*//' -e '/---/d' -e '/\.\.\./d' "$c" | tr $'\n' ,) }"
}
function clang_format() { clang-format -style="$(clang_style)" "$@"; }
function clang_format_diff() { cat "$@" | clang-format-diff -p1 -style="$(clang_style)"; }
# for non-bare repo will work
function clang_format_git()
{ git format-patch --stdout "$@" -1 | clang_format_diff; }
function uncrustify() { command uncrustify -c "${options[cfg]}" "$@"; }
function uncrustify_frag() { uncrustify -l C --frag "$@"; }
function uncrustify_indent_off() { echo '/* *INDENT-OFF* */'; }
function uncrustify_indent_on() { echo '/* *INDENT-ON* */'; }
function git_hunk()
{
local ref=$1 f=$2
shift 2
git cat-file -p $ref:$f
}
function uncrustify_git_indent_hunk()
{
local start=$1 end=$2
shift 2
# Will be beatier with tee(1), but doh bash async substitution
{ uncrustify_indent_off; git_hunk "$@" | head -n$((start - 1)); }
{ uncrustify_indent_on; git_hunk "$@" | head -n$((end - 1)) | tail -n+$start; }
{ uncrustify_indent_off; git_hunk "$@" | tail -n+$((end + 1)); }
}
function strip()
{
local start=$1 end=$2
shift 2
# seek indent_{on,off}()
let start+=2
head -n$end | tail -n+$start
}
function patch_ranges()
{
egrep -o '^@@ -[0-9]+(,[0-9]+|) \+[0-9]+(,[0-9]+|) @@' | \
cut -d' ' -f3
}
function git_ranges()
{
local ref=$1 f=$2
shift 2
git diff -W $ref^..$ref -- $f | patch_ranges
}
function diff_substitute()
{
local f="$1"
shift
sed \
-e "s#^--- /dev/fd.*\$#--- a/$f#" \
-e "s#^+++ /dev/fd.*\$#+++ b/$f#"
}
function uncrustify_git()
{
local ref=$1 r f start end length
shift
local files=( $(git diff --name-only $ref^..$ref | egrep "\.(c|h)$") )
for f in "${files[@]}"; do
local ranges=( $(git_ranges $ref "$f") )
for r in "${ranges[@]}"; do
[[ ! "$r" =~ ^\+([0-9]+)(,([0-9]+)|)$ ]] && continue
start=${BASH_REMATCH[1]}
[ -n "${BASH_REMATCH[3]}" ] && \
length=${BASH_REMATCH[3]} || \
length=1
end=$((start + length))
echo "Range: $start:$end ($length)" >&2
diff -u \
<(uncrustify_git_indent_hunk $start $end $ref "$f" | strip $start $end) \
<(uncrustify_git_indent_hunk $start $end $ref "$f" | uncrustify_frag | strip $start $end) \
| diff_substitute "$f"
done
done
}
function uncrustify_diff() { abort "Not implemented"; }
function uncrustify_file() { uncrustify -f "$@"; }
function checker()
{
local c=$1 u=$2
shift 2
[ "${options[clang]}" -eq 0 ] || {
$c "$@"
return
}
[ "${options[uncrustify]}" -eq 0 ] || {
$u "$@"
return
}
}
function check_patch() { checker clang_format_diff uncrustify_diff "$@"; }
function check_file() { checker clang_format uncrustify_file "$@"; }
function check_ref() { checker clang_format_git uncrustify_git "$@"; }
function check_arg()
{
[ "${options[patch]}" -eq 0 ] || {
check_patch "$@"
return
}
[ "${options[file]}" -eq 0 ] || {
check_file "$@"
return
}
[ "${options[file_diff]}" -eq 0 ] || {
diff -u "$@" <(check_file "$@") | diff_substitute "$@"
return
}
[ "${options[ref]}" -eq 0 ] || {
check_ref "$@"
return
}
}
function main()
{
local a
for a in "${args}"; do
check_arg "$a"
done
}
declare -A options
parse_options "$@"
main "$@" | less -FRSX