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
This commit is contained in:
Oliver Smith 2021-06-16 14:18:52 +02:00
parent 1e750ed6dc
commit d58b999e0f
2 changed files with 81 additions and 0 deletions

43
lint/lint_all.sh Executable file
View File

@ -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"

38
lint/lint_diff.sh Executable file
View File

@ -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