]> git.proxmox.com Git - mirror_frr.git/blob - tools/checkpatch.sh
Merge pull request #1795 from qlyoung/vtysh-history-q2f
[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 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
53 git -C $tree reset --hard
54 git -C $tree clean -fd
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
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
72 if [ -a /tmp/f2/$(basename $file) ]; then
73 result=$(diff $file /tmp/f2/$(basename $file) | grep -A3 "ERROR\|WARNING" | grep -A2 -B2 '/tmp/f1')
74 else
75 result=$(cat $file | grep -A3 "ERROR\|WARNING" | grep -A2 -B2 '/tmp/f1')
76 fi
77 if [ "$?" -eq "0" ]; then
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
86 fi
87 done
88 fi
89
90 # restore working tree
91 if [ $dirty -eq 1 ]; then
92 git -C $tree read-tree $WORKTREE;
93 git -C $tree checkout-index -af;
94 git -C $tree read-tree $INDEX;
95 if [ -n "$td" ]; then
96 rm $td
97 fi
98 git -C $tree config --unset gc.auto;
99 fi
100
101 exit $stat