blob: 4a602f798a1072382b320f3f1f1a181c7c7616ac [file] [log] [blame]
Martin Roth0ad5fbd2020-12-24 12:06:38 -07001#!/usr/bin/env sh
Patrick Georgif7437282018-05-24 17:36:41 +02002#
Patrick Georgi7333a112020-05-08 20:48:04 +02003# SPDX-License-Identifier: GPL-2.0-only
4
Patrick Georgif7437282018-05-24 17:36:41 +02005# DESCR: Run clang-format on white-listed directories
6
Martin Rothd81debd2022-06-03 00:06:57 -06007LINTDIR="$(
8 cd -- "$(dirname "$0")" > /dev/null 2>&1 || return
9 pwd -P
10)"
Patrick Georgif7437282018-05-24 17:36:41 +020011
Martin Rothd81debd2022-06-03 00:06:57 -060012# shellcheck source=helper_functions.sh
13. "${LINTDIR}/helper_functions.sh"
14
15# Until we require this by default, we need a list of opted-in directories
16# If the script isn't looking at a git repository, just exit
17if [ ! -f .clang-format-scope ] || [ "${IN_GIT_TREE}" -eq 0 ]; then
Patrick Georgif7437282018-05-24 17:36:41 +020018 exit 0
19fi
20
Martin Rothd81debd2022-06-03 00:06:57 -060021files_to_check=$(${GIT} log HEAD~..HEAD --format= --name-only $(cat .clang-format-scope) | grep "\.[ch]$")
Patrick Georgif7437282018-05-24 17:36:41 +020022
23# nothing to do
24if [ -z "$files_to_check" ]; then
25 exit 0
26fi
27
28if [ $(clang-format $files_to_check | wc -l) -gt 0 ]; then
Martin Rothd81debd2022-06-03 00:06:57 -060029 if [ "$(${GIT} diff --no-prefix HEAD~..HEAD -- $files_to_check | clang-format-diff)" != "" ]; then
Patrick Georgi27852902019-02-27 22:02:04 +010030 echo "Coding style mismatch. The following patch fixes it:"
Martin Rothd81debd2022-06-03 00:06:57 -060031 ${GIT} diff --no-prefix HEAD~..HEAD -- $files_to_check | clang-format-diff
Angel Ponsce828b62019-08-31 02:46:42 +020032 exit 0
Patrick Georgi27852902019-02-27 22:02:04 +010033 fi
Patrick Georgif7437282018-05-24 17:36:41 +020034fi