aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOliver Smith <osmith@sysmocom.de>2021-06-16 14:18:52 +0200
committerOliver Smith <osmith@sysmocom.de>2021-06-16 16:49:47 +0200
commitd58b999e0f8ea2ae8f744192589f3e3923e1da6d (patch)
tree39179f961c4097cd2a932238cb0580c831e85ba2
parent1e750ed6dcf8d9f576776e5beadb70a6bf5267e2 (diff)
lint: add helper scripts
Add lint_diff.sh, which runs checkpatch on git diff to either HEAD~1 (if the tree is clean) or HEAD. This can be used as pre-commit hook, and it's what jenkins will run. Add lint_all.sh, which runs checkpatch on a whole repository to test if the rules we are checking for make sense in Osmocom context. Related: OS#5087 Change-Id: I1d02c169b05fb05b87209a444a5ddb86efc99d04
-rwxr-xr-xlint/lint_all.sh43
-rwxr-xr-xlint/lint_diff.sh38
2 files changed, 81 insertions, 0 deletions
diff --git a/lint/lint_all.sh b/lint/lint_all.sh
new file mode 100755
index 0000000..1c25578
--- /dev/null
+++ b/lint/lint_all.sh
@@ -0,0 +1,43 @@
+#!/bin/sh -e
+# Script to test if linting is sane by running it on a whole repository
+GIT_DIR="$(git rev-parse --show-toplevel 2>/dev/null || true)"
+SCRIPT_DIR="$(dirname "$(realpath "$0")")"
+OUT=/tmp/lint_all_out
+TYPES="$1"
+
+echo "Running find in $GIT_DIR"
+files=$(find \
+ "$GIT_DIR" \
+ -name '*.c' \
+ -o -name '*.h' \
+ -o -name '*.cpp' \
+ -o -name '*.hpp')
+
+if [ -n "$TYPES" ]; then
+ echo "Running checkpath with --types="$TYPES" in $GIT_DIR"
+
+ "$SCRIPT_DIR"/checkpatch/checkpatch.pl \
+ -f \
+ --color=always \
+ --no-summary \
+ --no-tree \
+ --show-types \
+ --terse \
+ --types="$TYPES" \
+ $files \
+ | tee "$OUT"
+
+else
+ echo "Running checkpath in $GIT_DIR"
+
+ "$SCRIPT_DIR"/checkpatch/checkpatch_osmo.sh \
+ -f \
+ --color=always \
+ --no-summary \
+ --show-types \
+ --terse \
+ $files \
+ | tee "$OUT"
+fi
+
+wc -l "$OUT"
diff --git a/lint/lint_diff.sh b/lint/lint_diff.sh
new file mode 100755
index 0000000..f8daab7
--- /dev/null
+++ b/lint/lint_diff.sh
@@ -0,0 +1,38 @@
+#!/bin/sh -e
+# Jenkins runs this script on submitted gerrit patches. Can be used as git pre-commit hook.
+COMMIT="$1"
+GIT_DIR="$(git rev-parse --show-toplevel 2>/dev/null || true)"
+SCRIPT_DIR="$(dirname "$(realpath "$0")")"
+
+if [ -z "$GIT_DIR" ]; then
+ echo "ERROR: path is not a git repository: $PWD"
+ exit 1
+fi
+
+if [ -z "$COMMIT" ]; then
+ # Clean worktree: diff last commit against the one before
+ COMMIT="HEAD~1"
+
+ if [ -n "$(git status --porcelain)" ]; then
+ # Dirty worktree: diff uncommitted changes against last commit
+ COMMIT="HEAD"
+ fi
+fi
+
+echo "Running checkpatch on 'git diff $COMMIT'..."
+echo
+if git diff -U0 "$COMMIT" | "$SCRIPT_DIR/checkpatch/checkpatch_osmo.sh" - \
+ --color=always \
+ --mailback \
+ --show-types \
+ --showfile \
+ --terse
+then
+ exit 0
+fi
+
+echo
+echo "Please fix the linting errors above. More information:"
+echo "https://osmocom.org/projects/cellular-infrastructure/wiki/Linting"
+echo
+exit 1