aboutsummaryrefslogtreecommitdiffstats
path: root/lint/lint_diff.sh
blob: 7ae186fb268390ee98082aba96d311dc2f3eef4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/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

ERROR=0

echo "Running docker_run_rm.sh on the whole tree..."
echo
if ! "$SCRIPT_DIR"/docker_run_rm.sh; then
	ERROR=1
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
	ERROR=1
fi


if [ "$ERROR" = 1 ]; then
	echo
	echo "Please fix the linting errors above. More information:"
	echo "https://osmocom.org/projects/cellular-infrastructure/wiki/Linting"
	echo

	if [ -n "$JENKINS_HOME" ]; then
		echo "Leaving review comments in gerrit..."
		set -x

		# Run again, but in the proper format for checkpatch_json.py
		# and store the output in a file
		git diff -U0 "$COMMIT" | "$SCRIPT_DIR/checkpatch/checkpatch_osmo.sh" \
			> ../checkpatch_output || true
		cd ..
		# Convert to gerrit review format
		"$SCRIPT_DIR/checkpatch/checkpatch_json.py" \
			checkpatch_output \
			gerrit_report.json \
			"$BUILD_TAG" \
			"$BUILD_URL"
		# Apply as review in gerrit
		ssh \
			-p "$GERRIT_PORT" \
			-l jenkins \
			"$GERRIT_HOST" \
				gerrit \
					review \
					"$GERRIT_PATCHSET_REVISION" \
					--json \
					< gerrit_report.json
	fi

	exit 1
fi