2020-01-07 15:12:18 +01:00
|
|
|
#!/usr/bin/env bash
|
2020-01-19 18:03:11 +01:00
|
|
|
# tools/checkpatch.sh
|
2020-01-07 15:12:18 +01:00
|
|
|
#
|
2024-09-10 14:15:58 +02:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
#
|
2020-08-22 18:36:57 +02: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 15:12:18 +01:00
|
|
|
#
|
2020-08-22 18:36:57 +02:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2020-01-07 15:12:18 +01: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 14:53:21 +02:00
|
|
|
check=check_patch
|
2020-02-09 04:08:26 +01:00
|
|
|
fail=0
|
2020-02-14 12:21:05 +01:00
|
|
|
range=0
|
2020-02-25 10:28:05 +01:00
|
|
|
spell=0
|
2023-01-31 13:49:52 +01:00
|
|
|
encoding=0
|
2021-09-18 11:14:33 +02:00
|
|
|
message=0
|
2023-07-20 11:48:27 +02:00
|
|
|
cmake_warning_once=0
|
2020-02-09 04:08:26 +01:00
|
|
|
|
2020-01-07 15:12:18 +01:00
|
|
|
usage() {
|
|
|
|
echo "USAGE: ${0} [options] [list|-]"
|
|
|
|
echo ""
|
|
|
|
echo "Options:"
|
|
|
|
echo "-h"
|
2023-01-31 13:49:52 +01:00
|
|
|
echo "-c spell check with codespell (install with: pip install codespell)"
|
|
|
|
echo "-u encoding check with cvt2utf (install with: pip install cvt2utf)"
|
2020-03-10 04:55:22 +01:00
|
|
|
echo "-r range check only (coupled with -p or -g)"
|
2020-03-10 18:49:40 +01:00
|
|
|
echo "-p <patch file names> (default)"
|
2021-09-18 11:14:33 +02:00
|
|
|
echo "-m Change-Id check in commit message (coupled with -g)"
|
2020-02-25 10:28:05 +01:00
|
|
|
echo "-g <commit list>"
|
2020-01-07 15:12:18 +01: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-10 18:49:40 +01: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 14:53:21 +02:00
|
|
|
|
|
|
|
exit $@
|
2020-01-07 15:12:18 +01:00
|
|
|
}
|
|
|
|
|
2022-02-26 00:06:47 +01:00
|
|
|
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
|
2020-02-09 04:08:26 +01:00
|
|
|
fi
|
2022-02-26 00:06:47 +01:00
|
|
|
}
|
2020-02-25 10:28:05 +01:00
|
|
|
|
2023-07-18 06:01:40 +02:00
|
|
|
is_cmake_file() {
|
|
|
|
file_name=$(basename $@)
|
|
|
|
if [ "$file_name" == "CMakeLists.txt" ] || [[ "$file_name" =~ "cmake" ]]; then
|
|
|
|
echo 1
|
|
|
|
else
|
|
|
|
echo 0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-02-26 00:06:47 +01:00
|
|
|
check_file() {
|
2023-02-01 00:37:10 +01:00
|
|
|
if [ -x $@ ]; then
|
|
|
|
case $@ in
|
|
|
|
*.bat | *.sh | *.py)
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "$@: error: execute permissions detected!"
|
|
|
|
fail=1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
|
2023-09-12 05:27:52 +02:00
|
|
|
if [ ${@##*.} == 'py' ]; then
|
2024-08-09 09:46:16 +02:00
|
|
|
black --check $@ || fail=1
|
|
|
|
flake8 --config ${TOOLDIR}/../.github/linters/setup.cfg $@ || fail=1
|
2024-08-16 03:58:13 +02:00
|
|
|
isort --settings-path ${TOOLDIR}/../.github/linters/setup.cfg $@ || fail=1
|
2023-09-12 05:27:52 +02:00
|
|
|
elif [ "$(is_rust_file $@)" == "1" ]; then
|
2022-02-26 00:06:47 +01:00
|
|
|
if ! command -v rustfmt &> /dev/null; then
|
|
|
|
fail=1
|
2023-02-01 00:22:39 +01:00
|
|
|
elif ! rustfmt --edition 2021 --check $@ 2>&1; then
|
2020-04-19 10:58:55 +02:00
|
|
|
fail=1
|
2020-02-25 10:28:05 +01:00
|
|
|
fi
|
2023-07-18 06:01:40 +02:00
|
|
|
elif [ "$(is_cmake_file $@)" == "1" ]; then
|
|
|
|
if ! command -v cmake-format &> /dev/null; then
|
2023-07-20 11:48:27 +02:00
|
|
|
if [ $cmake_warning_once == 0 ]; then
|
|
|
|
echo -e "\ncmake-format not found, run following command to install:"
|
|
|
|
echo " $ pip install cmake-format"
|
|
|
|
cmake_warning_once=1
|
|
|
|
fi
|
2023-07-18 06:01:40 +02:00
|
|
|
fail=1
|
|
|
|
elif ! cmake-format --check $@ 2>&1; then
|
2023-07-20 11:48:27 +02:00
|
|
|
if [ $cmake_warning_once == 0 ]; then
|
|
|
|
echo -e "\ncmake-format check failed, run following command to update the style:"
|
2024-03-12 10:47:10 +01:00
|
|
|
echo -e " $ cmake-format <src> -o <dst>\n"
|
2023-07-20 11:48:27 +02:00
|
|
|
cmake-format --check $@ 2>&1
|
|
|
|
cmake_warning_once=1
|
|
|
|
fi
|
2023-07-18 06:01:40 +02:00
|
|
|
fail=1
|
|
|
|
fi
|
2023-02-01 00:22:39 +01:00
|
|
|
elif ! $TOOLDIR/nxstyle $@ 2>&1; then
|
|
|
|
fail=1
|
|
|
|
fi
|
2022-02-26 00:06:47 +01:00
|
|
|
|
2023-02-01 00:22:39 +01:00
|
|
|
if [ $spell != 0 ]; then
|
|
|
|
if ! codespell -q 7 ${@: -1}; then
|
|
|
|
fail=1
|
2022-02-26 00:06:47 +01:00
|
|
|
fi
|
2023-02-01 00:22:39 +01:00
|
|
|
fi
|
2023-01-31 13:49:52 +01:00
|
|
|
|
2023-02-01 00:22:39 +01:00
|
|
|
if [ $encoding != 0 ]; then
|
|
|
|
md5="$(md5sum $@)"
|
|
|
|
cvt2utf convert --nobak "$@" &> /dev/null
|
|
|
|
if [ "$md5" != "$(md5sum $@)" ]; then
|
|
|
|
echo "$@: error: Non-UTF8 characters detected!"
|
|
|
|
fail=1
|
2023-01-31 13:49:52 +01:00
|
|
|
fi
|
2020-02-25 10:28:05 +01:00
|
|
|
fi
|
2020-01-07 15:12:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
check_ranges() {
|
|
|
|
while read; do
|
2020-04-16 09:18:24 +02:00
|
|
|
if [[ $REPLY =~ ^(\+\+\+\ (b/)?([^[:blank:]]+).*)$ ]]; then
|
2020-01-07 15:12:18 +01:00
|
|
|
if [ "$ranges" != "" ]; then
|
2020-02-14 12:21:05 +01:00
|
|
|
if [ $range != 0 ]; then
|
2020-04-18 14:53:21 +02:00
|
|
|
check_file $ranges $path
|
2020-02-14 12:21:05 +01:00
|
|
|
else
|
2020-04-18 14:53:21 +02:00
|
|
|
check_file $path
|
2020-02-14 12:21:05 +01:00
|
|
|
fi
|
2020-01-07 15:12:18 +01:00
|
|
|
fi
|
2020-05-31 23:19:21 +02:00
|
|
|
path=$(realpath "${BASH_REMATCH[3]}")
|
2020-01-07 15:12:18 +01: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 12:21:05 +01:00
|
|
|
if [ $range != 0 ]; then
|
2020-04-18 14:53:21 +02:00
|
|
|
check_file $ranges $path
|
2020-02-14 12:21:05 +01:00
|
|
|
else
|
2020-04-18 14:53:21 +02:00
|
|
|
check_file $path
|
2020-02-14 12:21:05 +01:00
|
|
|
fi
|
2020-01-07 15:12:18 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
check_patch() {
|
2020-04-19 10:58:55 +02:00
|
|
|
if ! git apply --check $1; then
|
|
|
|
fail=1
|
2020-02-14 12:21:05 +01:00
|
|
|
else
|
|
|
|
git apply $1
|
|
|
|
diffs=`cat $1`
|
|
|
|
check_ranges <<< "$diffs"
|
|
|
|
git apply -R $1
|
2020-01-07 15:12:18 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-09-07 05:09:02 +02:00
|
|
|
check_msg() {
|
|
|
|
while read; do
|
|
|
|
if [[ $REPLY =~ ^Change-Id ]]; then
|
|
|
|
echo "Remove Gerrit Change-ID's before submitting upstream"
|
|
|
|
fail=1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2020-01-07 15:12:18 +01:00
|
|
|
check_commit() {
|
2021-09-18 11:14:33 +02:00
|
|
|
if [ $message != 0 ]; then
|
|
|
|
msg=`git show -s --format=%B $1`
|
|
|
|
check_msg <<< "$msg"
|
|
|
|
fi
|
2020-09-03 23:14:06 +02:00
|
|
|
diffs=`git diff $1`
|
2020-02-09 04:08:26 +01:00
|
|
|
check_ranges <<< "$diffs"
|
2020-01-07 15:12:18 +01: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 14:53:21 +02:00
|
|
|
- )
|
|
|
|
check_ranges
|
2020-01-07 15:12:18 +01:00
|
|
|
;;
|
2020-02-25 10:28:05 +01:00
|
|
|
-c )
|
|
|
|
spell=1
|
|
|
|
;;
|
2023-01-31 13:49:52 +01:00
|
|
|
-u )
|
|
|
|
encoding=1
|
|
|
|
;;
|
2020-04-18 14:53:21 +02:00
|
|
|
-f )
|
|
|
|
check=check_file
|
2020-01-07 15:12:18 +01:00
|
|
|
;;
|
2021-09-18 11:14:33 +02:00
|
|
|
-m )
|
|
|
|
message=1
|
|
|
|
;;
|
2020-02-25 10:28:05 +01:00
|
|
|
-g )
|
2020-04-18 14:53:21 +02:00
|
|
|
check=check_commit
|
2020-01-07 15:12:18 +01:00
|
|
|
;;
|
2020-04-18 14:53:21 +02:00
|
|
|
-h )
|
|
|
|
usage 0
|
2020-01-07 15:12:18 +01:00
|
|
|
;;
|
2020-04-18 14:53:21 +02:00
|
|
|
-p )
|
|
|
|
check=check_patch
|
|
|
|
;;
|
|
|
|
-r )
|
|
|
|
range=1
|
|
|
|
;;
|
|
|
|
-* )
|
|
|
|
usage 1
|
2020-01-07 15:12:18 +01:00
|
|
|
;;
|
|
|
|
* )
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
esac
|
2020-02-14 12:21:05 +01:00
|
|
|
shift
|
2020-01-07 15:12:18 +01:00
|
|
|
done
|
|
|
|
|
2020-04-18 14:53:21 +02:00
|
|
|
for arg in $@; do
|
|
|
|
$check $arg
|
2020-01-07 15:12:18 +01:00
|
|
|
done
|
2020-02-09 04:08:26 +01:00
|
|
|
|
|
|
|
exit $fail
|