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