2020-01-07 22:12:18 +08:00
|
|
|
#!/usr/bin/env bash
|
2020-01-20 01:03:11 +08:00
|
|
|
# tools/checkpatch.sh
|
2020-01-07 22:12:18 +08:00
|
|
|
#
|
2020-08-23 00:36:57 +08:00
|
|
|
# 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
|
2020-01-07 22:12:18 +08:00
|
|
|
#
|
2020-08-23 00:36:57 +08:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2020-01-07 22:12:18 +08:00
|
|
|
#
|
|
|
|
# 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)
|
|
|
|
|
2020-04-18 20:53:21 +08:00
|
|
|
check=check_patch
|
2020-02-09 11:08:26 +08:00
|
|
|
fail=0
|
2020-02-14 19:21:05 +08:00
|
|
|
range=0
|
2020-02-25 17:28:05 +08:00
|
|
|
spell=0
|
2020-02-09 11:08:26 +08:00
|
|
|
|
2020-01-07 22:12:18 +08:00
|
|
|
usage() {
|
|
|
|
echo "USAGE: ${0} [options] [list|-]"
|
|
|
|
echo ""
|
|
|
|
echo "Options:"
|
|
|
|
echo "-h"
|
2020-03-10 11:55:22 +08:00
|
|
|
echo "-c spell check with codespell(install with: pip install codespell)"
|
|
|
|
echo "-r range check only (coupled with -p or -g)"
|
2020-03-11 01:49:40 +08:00
|
|
|
echo "-p <patch file names> (default)"
|
2020-02-25 17:28:05 +08:00
|
|
|
echo "-g <commit list>"
|
2020-01-07 22:12:18 +08:00
|
|
|
echo "-f <file list>"
|
|
|
|
echo "- read standard input mainly used by git pre-commit hook as below:"
|
|
|
|
echo " git diff --cached | ./tools/checkpatch.sh -"
|
2020-03-11 01:49:40 +08:00
|
|
|
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"
|
2020-04-18 20:53:21 +08:00
|
|
|
|
|
|
|
exit $@
|
2020-01-07 22:12:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
check_file() {
|
2020-04-19 16:58:55 +08:00
|
|
|
if ! $TOOLDIR/nxstyle $@ 2>&1; then
|
|
|
|
fail=1
|
2020-02-09 11:08:26 +08:00
|
|
|
fi
|
2020-02-25 17:28:05 +08:00
|
|
|
|
|
|
|
if [ $spell != 0 ]; then
|
2020-04-19 16:58:55 +08:00
|
|
|
if ! codespell -q 7 ${@: -1}; then
|
|
|
|
fail=1
|
2020-02-25 17:28:05 +08:00
|
|
|
fi
|
|
|
|
fi
|
2020-01-07 22:12:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
check_ranges() {
|
|
|
|
while read; do
|
2020-04-16 15:18:24 +08:00
|
|
|
if [[ $REPLY =~ ^(\+\+\+\ (b/)?([^[:blank:]]+).*)$ ]]; then
|
2020-01-07 22:12:18 +08:00
|
|
|
if [ "$ranges" != "" ]; then
|
2020-02-14 19:21:05 +08:00
|
|
|
if [ $range != 0 ]; then
|
2020-04-18 20:53:21 +08:00
|
|
|
check_file $ranges $path
|
2020-02-14 19:21:05 +08:00
|
|
|
else
|
2020-04-18 20:53:21 +08:00
|
|
|
check_file $path
|
2020-02-14 19:21:05 +08:00
|
|
|
fi
|
2020-01-07 22:12:18 +08:00
|
|
|
fi
|
2020-06-01 05:19:21 +08:00
|
|
|
path=$(realpath "${BASH_REMATCH[3]}")
|
2020-01-07 22:12:18 +08:00
|
|
|
ranges=""
|
|
|
|
elif [[ $REPLY =~ @@\ -[0-9]+(,[0-9]+)?\ \+([0-9]+,[0-9]+)?\ @@.* ]]; then
|
|
|
|
ranges+="-r ${BASH_REMATCH[2]} "
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [ "$ranges" != "" ]; then
|
2020-02-14 19:21:05 +08:00
|
|
|
if [ $range != 0 ]; then
|
2020-04-18 20:53:21 +08:00
|
|
|
check_file $ranges $path
|
2020-02-14 19:21:05 +08:00
|
|
|
else
|
2020-04-18 20:53:21 +08:00
|
|
|
check_file $path
|
2020-02-14 19:21:05 +08:00
|
|
|
fi
|
2020-01-07 22:12:18 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
check_patch() {
|
2020-04-19 16:58:55 +08:00
|
|
|
if ! git apply --check $1; then
|
|
|
|
fail=1
|
2020-02-14 19:21:05 +08:00
|
|
|
else
|
|
|
|
git apply $1
|
|
|
|
diffs=`cat $1`
|
|
|
|
check_ranges <<< "$diffs"
|
|
|
|
git apply -R $1
|
2020-01-07 22:12:18 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
check_commit() {
|
2020-09-04 05:14:06 +08:00
|
|
|
diffs=`git diff $1`
|
2020-02-09 11:08:26 +08:00
|
|
|
check_ranges <<< "$diffs"
|
2020-01-07 22:12:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2020-04-18 20:53:21 +08:00
|
|
|
- )
|
|
|
|
check_ranges
|
2020-01-07 22:12:18 +08:00
|
|
|
;;
|
2020-02-25 17:28:05 +08:00
|
|
|
-c )
|
|
|
|
spell=1
|
|
|
|
;;
|
2020-04-18 20:53:21 +08:00
|
|
|
-f )
|
|
|
|
check=check_file
|
2020-01-07 22:12:18 +08:00
|
|
|
;;
|
2020-02-25 17:28:05 +08:00
|
|
|
-g )
|
2020-04-18 20:53:21 +08:00
|
|
|
check=check_commit
|
2020-01-07 22:12:18 +08:00
|
|
|
;;
|
2020-04-18 20:53:21 +08:00
|
|
|
-h )
|
|
|
|
usage 0
|
2020-01-07 22:12:18 +08:00
|
|
|
;;
|
2020-04-18 20:53:21 +08:00
|
|
|
-p )
|
|
|
|
check=check_patch
|
|
|
|
;;
|
|
|
|
-r )
|
|
|
|
range=1
|
|
|
|
;;
|
|
|
|
-* )
|
|
|
|
usage 1
|
2020-01-07 22:12:18 +08:00
|
|
|
;;
|
|
|
|
* )
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
esac
|
2020-02-14 19:21:05 +08:00
|
|
|
shift
|
2020-01-07 22:12:18 +08:00
|
|
|
done
|
|
|
|
|
2020-04-18 20:53:21 +08:00
|
|
|
for arg in $@; do
|
|
|
|
$check $arg
|
2020-01-07 22:12:18 +08:00
|
|
|
done
|
2020-02-09 11:08:26 +08:00
|
|
|
|
|
|
|
exit $fail
|