forked from apache/nuttx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckpatch.sh
executable file
·182 lines (165 loc) · 3.7 KB
/
checkpatch.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env bash
# tools/checkpatch.sh
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to you under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
TOOLDIR=$(dirname $0)
check=check_patch
fail=0
range=0
spell=0
message=0
usage() {
echo "USAGE: ${0} [options] [list|-]"
echo ""
echo "Options:"
echo "-h"
echo "-c spell check with codespell(install with: pip install codespell)"
echo "-r range check only (coupled with -p or -g)"
echo "-p <patch file names> (default)"
echo "-m Change-Id check in commit message (coupled with -g)"
echo "-g <commit list>"
echo "-f <file list>"
echo "- read standard input mainly used by git pre-commit hook as below:"
echo " git diff --cached | ./tools/checkpatch.sh -"
echo "Where a <commit list> is any syntax supported by git for specifying git revision, see GITREVISIONS(7)"
echo "Where a <patch file names> is a space separated list of patch file names or wildcard. or *.patch"
exit $@
}
is_rust_file() {
file_ext=${@##*.}
file_ext_r=${file_ext/R/r}
file_ext_rs=${file_ext_r/S/s}
if [ "$file_ext_rs" == "rs" ]; then
echo 1
else
echo 0
fi
}
check_file() {
if [ "$(is_rust_file $@)" == "1" ]; then
if ! command -v rustfmt &> /dev/null; then
fail=1
else
if ! rustfmt --edition 2021 --check $@ 2>&1; then
fail=1
fi
fi
else
if ! $TOOLDIR/nxstyle $@ 2>&1; then
fail=1
fi
if [ $spell != 0 ]; then
if ! codespell -q 7 ${@: -1}; then
fail=1
fi
fi
fi
}
check_ranges() {
while read; do
if [[ $REPLY =~ ^(\+\+\+\ (b/)?([^[:blank:]]+).*)$ ]]; then
if [ "$ranges" != "" ]; then
if [ $range != 0 ]; then
check_file $ranges $path
else
check_file $path
fi
fi
path=$(realpath "${BASH_REMATCH[3]}")
ranges=""
elif [[ $REPLY =~ @@\ -[0-9]+(,[0-9]+)?\ \+([0-9]+,[0-9]+)?\ @@.* ]]; then
ranges+="-r ${BASH_REMATCH[2]} "
fi
done
if [ "$ranges" != "" ]; then
if [ $range != 0 ]; then
check_file $ranges $path
else
check_file $path
fi
fi
}
check_patch() {
if ! git apply --check $1; then
fail=1
else
git apply $1
diffs=`cat $1`
check_ranges <<< "$diffs"
git apply -R $1
fi
}
check_msg() {
while read; do
if [[ $REPLY =~ ^Change-Id ]]; then
echo "Remove Gerrit Change-ID's before submitting upstream"
fail=1
fi
done
}
check_commit() {
if [ $message != 0 ]; then
msg=`git show -s --format=%B $1`
check_msg <<< "$msg"
fi
diffs=`git diff $1`
check_ranges <<< "$diffs"
}
make -C $TOOLDIR -f Makefile.host nxstyle 1>/dev/null
if [ -z "$1" ]; then
usage
exit 0
fi
while [ ! -z "$1" ]; do
case "$1" in
- )
check_ranges
;;
-c )
spell=1
;;
-f )
check=check_file
;;
-m )
message=1
;;
-g )
check=check_commit
;;
-h )
usage 0
;;
-p )
check=check_patch
;;
-r )
range=1
;;
-* )
usage 1
;;
* )
break
;;
esac
shift
done
for arg in $@; do
$check $arg
done
exit $fail