You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by gn...@apache.org on 2020/01/07 14:12:24 UTC
[incubator-nuttx] branch pr47 updated: Nxstyle checkpatch support
(#47)
This is an automated email from the ASF dual-hosted git repository.
gnutt pushed a commit to branch pr47
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git
The following commit(s) were added to refs/heads/pr47 by this push:
new f6568e6 Nxstyle checkpatch support (#47)
f6568e6 is described below
commit f6568e67a4f06f317157fe3d53e658d7115447e5
Author: liuguo09 <li...@users.noreply.github.com>
AuthorDate: Tue Jan 7 22:12:18 2020 +0800
Nxstyle checkpatch support (#47)
* tools/nxstyle.c: Donot check unknown file extension files
nxstyle only support c soure file and header file check, donot check
other unknown file extension files.
* tools/checkpatch.sh: Add checkpatch.sh script based on nxstyle tool
Usage:
checkpatch.sh patch-list // default as patch list
checkpatch.sh -p patch-list
checkpatch.sh -c commit-list
checkpatch.sh -f file-list
checkpatch.sh - // read from stdin, which used by git pre-commit hook
And git pre-commit hook could use checkpatch.sh as below:
git diff --cached | ./tools/checkpatch.sh -
---
tools/checkpatch.sh | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++
tools/nxstyle.c | 13 ++++--
2 files changed, 137 insertions(+), 3 deletions(-)
diff --git a/tools/checkpatch.sh b/tools/checkpatch.sh
new file mode 100755
index 0000000..4a9f447
--- /dev/null
+++ b/tools/checkpatch.sh
@@ -0,0 +1,127 @@
+#!/usr/bin/env bash
+#
+# Copyright (C) 2019 Xiaomi
+#
+# Licensed 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)
+
+usage() {
+ echo "USAGE: ${0} [options] [list|-]"
+ echo ""
+ echo "Options:"
+ echo "-h"
+ echo "-p <patch list> (default)"
+ echo "-c <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 -"
+}
+
+check_file() {
+ $TOOLDIR/nxstyle -m 86 $@ 2>&1
+}
+
+check_ranges() {
+ local fail=0
+
+ while read; do
+ if [[ $REPLY =~ \+\+\+\ (b/)?([^[:blank:]]+).* ]]; then
+ if [ "$ranges" != "" ]; then
+ check_file $ranges $path 2>&1
+ if [ $? != 0 ]; then
+ fail=1
+ fi
+ fi
+ path=${BASH_REMATCH[2]}
+ ranges=""
+ elif [[ $REPLY =~ @@\ -[0-9]+(,[0-9]+)?\ \+([0-9]+,[0-9]+)?\ @@.* ]]; then
+ ranges+="-r ${BASH_REMATCH[2]} "
+ fi
+ done
+ if [ "$ranges" != "" ]; then
+ check_file $ranges $path 2>&1
+ if [ $? != 0 ]; then
+ fail=1
+ fi
+ fi
+ if [ $fail = 1 ]; then
+ exit 1
+ fi
+}
+
+check_patch() {
+ git apply --check $1
+ if [ $? != 0 ]; then
+ exit 1
+ fi
+ git apply $1
+ cat $1 | check_ranges
+ git apply -R $1
+}
+
+check_commit() {
+ git show $1 | check_ranges
+}
+
+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
+ -h )
+ usage
+ exit 0
+ ;;
+ -p )
+ shift
+ patches=$@
+ break
+ ;;
+ -c )
+ shift
+ commits=$@
+ break
+ ;;
+ -f )
+ shift
+ files=$@
+ break
+ ;;
+ - )
+ check_ranges
+ exit 0
+ ;;
+ * )
+ patches=$@
+ break
+ ;;
+ esac
+done
+
+for patch in $patches; do
+ check_patch $patch
+done
+
+for commit in $commits; do
+ check_commit $commit
+done
+
+for file in $files; do
+ check_file $file
+done
diff --git a/tools/nxstyle.c b/tools/nxstyle.c
index 1ccbb75..2a109ea 100644
--- a/tools/nxstyle.c
+++ b/tools/nxstyle.c
@@ -60,6 +60,7 @@
#define WARN(m, l, o) message(WARN, (m), (l), (o))
#define ERROR(m, l, o) message(ERROR, (m), (l), (o))
#define INFO(m, l, o) message(INFO, (m), (l), (o))
+#define INFOFL(m,s) message(INFO, (m), -1, -1)
/****************************************************************************
* Private types
@@ -157,7 +158,7 @@ static int message(enum class_e class, const char *text, int lineno, int ndx)
{
if (lineno == -1 && ndx == -1)
{
- fprintf(out, "%s:%s: %s\n", class_text[class], text, g_file_name);
+ fprintf(out, "%s: %s: %s\n", g_file_name, class_text[class], text);
}
else
{
@@ -301,11 +302,11 @@ int main(int argc, char **argv, char **envp)
/* Are we parsing a header file? */
- ext = strrchr(g_file_name, '.');
+ ext = strrchr(g_file_name, '.');
if (ext == 0)
{
- WARN("No file extension", 0 , 0);
+ INFOFL("No file extension", g_file_name);
}
else if (strcmp(ext, ".h") == 0)
{
@@ -316,6 +317,12 @@ int main(int argc, char **argv, char **envp)
g_file_type = C_SOURCE;
}
+ if (g_file_type == UNKNOWN)
+ {
+ INFOFL("Unknown file extension", g_file_name);
+ return 0;
+ }
+
btabs = false; /* True: TAB characters found on the line */
bcrs = false; /* True: Carriable return found on the line */
bfunctions = false; /* True: In private or public functions */