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