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