[chirp_devel] [PATCH 1 of 2] Add tools/checkpatch.sh for style compliance checking

Dan Smith
Mon Dec 2 13:47:08 PST 2013


# HG changeset patch
# User Dan Smith <dsmith at danplanet.com>
# Date 1386019635 28800
#      Mon Dec 02 13:27:15 2013 -0800
# Node ID e6c6ffd8cae979cbeafa14c5fdfbb80d22333ecd
# Parent  c1271344c63cbba1919fa290b857a33e1bb1bae5
Add tools/checkpatch.sh for style compliance checking

This adds tools/checkpatch.sh which checks a given HG revision
for style compliance. We should call this as part of our tests,
have Jenkins call it, and keep it full of things we'd like to
enforce. Developers should run this on their patches (when
possible) before submitting.

Had this idea after the patch for #1277

diff -r c1271344c63c -r e6c6ffd8cae9 tools/checkpatch.sh
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/checkpatch.sh	Mon Dec 02 13:27:15 2013 -0800
@@ -0,0 +1,69 @@
+#!/bin/bash
+#
+# CHIRP coding standards compliance script
+#
+# To add a test to this file, create a new check_foo() function
+# and then add it to the list of TESTS= below
+#
+
+TESTS="check_long_lines check_bug_number check_commit_message_line_length"
+
+function check_long_lines() {
+    local rev="$1"
+    local files="$2"
+
+    if [ -z "$files" ]; then
+	return
+    fi
+    pep8 --select=E501 $files || \
+	error "Please use <80 columns in source files"
+}
+
+function check_bug_number() {
+    local rev="$1"
+    hg log -vr $rev | grep -qE '#[0-9]+' || \
+	error "A bug number is required like #123"
+	
+}
+
+function _less_than_80() {
+    while true; do
+	read line
+	if [ -z "$line" ]; then
+	    break
+	elif [ $(echo $line | wc -c) -ge 80 ]; then
+	    return 1
+	fi
+    done
+}
+
+function check_commit_message_line_length() {
+    local rev="$1"
+    hg log -vr $rev | (_less_than_80) || \
+	error "Please keep commit message lines to <80 columns"
+}
+
+# --- END OF TEST FUNCTIONS ---
+
+function error() {
+    echo FAIL: $*
+    ERROR=1
+}
+
+function get_touched_files() {
+    local rev="$1"
+    hg status -n --change $rev | grep '\.py$'
+}
+
+files=$(get_touched_files tip)
+rev=${1:-tip}
+
+for testname in $TESTS; do
+    eval "$testname $rev \"$files\""
+done
+
+if [ -z "$ERROR" ]; then
+    echo "Patch '${rev}' is OK"
+else
+    exit 1
+fi



More information about the chirp_devel mailing list