]> git.proxmox.com Git - mirror_frr.git/blame - tools/checkpatch.sh
Merge pull request #1884 from donaldsharp/spammy_messages
[mirror_frr.git] / tools / checkpatch.sh
CommitLineData
3e4ae702
QY
1#!/bin/bash
2# Check a patch for style errors.
a162e6a5
QY
3usage="./checkpatch.sh <patch> <tree>"
4patch=$1
5tree=$2
6checkpatch="$tree/tools/checkpatch.pl --no-tree -f"
33fe08f4
QY
7ignore="ldpd\|babeld"
8cwd=${PWD##*/}
9dirty=0
1e1c1e13 10stat=0
3e4ae702 11
a162e6a5
QY
12if [[ -z "$1" || -z "$2" ]]; then
13 echo "$usage"
14 exit 0
3e4ae702
QY
15fi
16
f5fd113c
QY
17# remove temp directories
18rm -rf /tmp/f1 /tmp/f2
19
33fe08f4 20# save working tree
a162e6a5 21if git -C $tree status --porcelain | egrep --silent '^(\?\?|.[DM])'; then
33fe08f4
QY
22 echo "Detected dirty tree, caching state..."
23 dirty=1
a162e6a5
QY
24 git -C $tree config gc.auto 0;
25 td=$(git -C $tree status -z | grep -z "^[ARM]D" | cut -z -d' ' -f2- | tr '\0' '\n')
f5fd113c 26 INDEX=$(git -C $tree write-tree)
a162e6a5 27 git -C $tree add -f .
f5fd113c 28 WORKTREE=$(git -C $tree write-tree)
33fe08f4
QY
29 echo "Saved index to $INDEX"
30 echo "Saved working tree to $WORKTREE"
31fi
32
33# double check
a162e6a5 34if git -C $tree status --porcelain | egrep --silent '^(\?\?|.[DM])'; then
33fe08f4
QY
35 echo "[!] git working directory must be clean."
36 exit 1
37fi
38
a162e6a5
QY
39git -C $tree reset --hard
40git -C $tree apply < $patch
41mkdir -p /tmp/f1 /tmp/f2
42mod=$(git -C $tree ls-files -m | grep ".*\.[ch]" | grep -v $ignore)
f5fd113c
QY
43mod+=" $(git -C $tree ls-files --others --exclude-standard | grep '.*\.[ch]' | grep -v $ignore)"
44echo $mod
a162e6a5
QY
45if [ -z "$mod" ]; then
46 echo "There doesn't seem to be any changes."
47else
86d488ce
QY
48 echo "Copying source to temp directory..."
49 for file in $mod; do
50 echo "$tree/$file --> /tmp/f1/$file"
51 cp $tree/$file /tmp/f1/
52 done
a162e6a5 53 git -C $tree reset --hard
f5fd113c 54 git -C $tree clean -fd
86d488ce
QY
55 for file in $mod; do
56 if [ -f $tree/$file ]; then
57 echo "$tree/$file --> /tmp/f2/$file"
58 cp $tree/$file /tmp/f2/
59 fi
60 done
a162e6a5
QY
61 echo "Running style checks..."
62 for file in /tmp/f1/*; do
63 echo "$checkpatch $file > $file _cp"
64 $checkpatch $file > "$file"_cp 2> /dev/null
65 done
66 for file in /tmp/f2/*; do
67 echo "$checkpatch $file > $file _cp"
68 $checkpatch $file > "$file"_cp 2> /dev/null
69 done
70 echo "Done."
71 for file in /tmp/f1/*_cp; do
a162e6a5 72 if [ -a /tmp/f2/$(basename $file) ]; then
6b1ebf3c 73 result=$(diff $file /tmp/f2/$(basename $file) | grep -A3 "ERROR\|WARNING" | grep -A2 -B2 '/tmp/f1')
a162e6a5 74 else
6b1ebf3c 75 result=$(cat $file | grep -A3 "ERROR\|WARNING" | grep -A2 -B2 '/tmp/f1')
a162e6a5 76 fi
1e1c1e13 77 if [ "$?" -eq "0" ]; then
2580e72f
QY
78 echo "Report for $(basename $file _cp)" 1>&2
79 echo "===============================================" 1>&2
80 echo "$result" 1>&2
81 if echo $result | grep -q "ERROR"; then
82 stat=2
83 elif [ "$stat" -eq "0" ]; then
84 stat=1
85 fi
1e1c1e13 86 fi
a162e6a5 87 done
a162e6a5 88fi
33fe08f4
QY
89
90# restore working tree
91if [ $dirty -eq 1 ]; then
a162e6a5
QY
92 git -C $tree read-tree $WORKTREE;
93 git -C $tree checkout-index -af;
94 git -C $tree read-tree $INDEX;
33fe08f4
QY
95 if [ -n "$td" ]; then
96 rm $td
97 fi
a162e6a5 98 git -C $tree config --unset gc.auto;
33fe08f4 99fi
1e1c1e13
QY
100
101exit $stat