summaryrefslogtreecommitdiffstats
path: root/util/lint/lint-stable-022-clang-format
blob: 9c94fbc06012578ea6f498cf58d12dbf484299a1 (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
#!/usr/bin/env sh
#
# SPDX-License-Identifier: GPL-2.0-only

# DESCR: Run clang-format on white-listed directories

LC_ALL=C export LC_ALL

# until we require this by default, we need a list of opted-in directories
if [ ! -f .clang-format-scope ]; then
	exit 0
fi

files_to_check=$(git log HEAD~..HEAD --format= --name-only $(cat .clang-format-scope) |grep "\.[ch]$")

# nothing to do
if [ -z "$files_to_check" ]; then
	exit 0
fi

if [ $(clang-format $files_to_check | wc -l) -gt 0 ]; then
	if [ "$(git diff --no-prefix HEAD~..HEAD -- $files_to_check | clang-format-diff)" != "" ]; then
		echo "Coding style mismatch. The following patch fixes it:"
		git diff --no-prefix HEAD~..HEAD -- $files_to_check | clang-format-diff
		exit 0
	fi
fi