Martin Roth | 0ad5fbd | 2020-12-24 12:06:38 -0700 | [diff] [blame] | 1 | #!/usr/bin/env sh |
Patrick Georgi | f743728 | 2018-05-24 17:36:41 +0200 | [diff] [blame] | 2 | # |
Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 3 | # SPDX-License-Identifier: GPL-2.0-only |
| 4 | |
Patrick Georgi | f743728 | 2018-05-24 17:36:41 +0200 | [diff] [blame] | 5 | # DESCR: Run clang-format on white-listed directories |
| 6 | |
Martin Roth | d81debd | 2022-06-03 00:06:57 -0600 | [diff] [blame] | 7 | LINTDIR="$( |
| 8 | cd -- "$(dirname "$0")" > /dev/null 2>&1 || return |
| 9 | pwd -P |
| 10 | )" |
Patrick Georgi | f743728 | 2018-05-24 17:36:41 +0200 | [diff] [blame] | 11 | |
Martin Roth | d81debd | 2022-06-03 00:06:57 -0600 | [diff] [blame] | 12 | # 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 |
| 17 | if [ ! -f .clang-format-scope ] || [ "${IN_GIT_TREE}" -eq 0 ]; then |
Patrick Georgi | f743728 | 2018-05-24 17:36:41 +0200 | [diff] [blame] | 18 | exit 0 |
| 19 | fi |
| 20 | |
Martin Roth | d81debd | 2022-06-03 00:06:57 -0600 | [diff] [blame] | 21 | files_to_check=$(${GIT} log HEAD~..HEAD --format= --name-only $(cat .clang-format-scope) | grep "\.[ch]$") |
Patrick Georgi | f743728 | 2018-05-24 17:36:41 +0200 | [diff] [blame] | 22 | |
| 23 | # nothing to do |
| 24 | if [ -z "$files_to_check" ]; then |
| 25 | exit 0 |
| 26 | fi |
| 27 | |
| 28 | if [ $(clang-format $files_to_check | wc -l) -gt 0 ]; then |
Martin Roth | d81debd | 2022-06-03 00:06:57 -0600 | [diff] [blame] | 29 | if [ "$(${GIT} diff --no-prefix HEAD~..HEAD -- $files_to_check | clang-format-diff)" != "" ]; then |
Patrick Georgi | 2785290 | 2019-02-27 22:02:04 +0100 | [diff] [blame] | 30 | echo "Coding style mismatch. The following patch fixes it:" |
Martin Roth | d81debd | 2022-06-03 00:06:57 -0600 | [diff] [blame] | 31 | ${GIT} diff --no-prefix HEAD~..HEAD -- $files_to_check | clang-format-diff |
Angel Pons | ce828b6 | 2019-08-31 02:46:42 +0200 | [diff] [blame] | 32 | exit 0 |
Patrick Georgi | 2785290 | 2019-02-27 22:02:04 +0100 | [diff] [blame] | 33 | fi |
Patrick Georgi | f743728 | 2018-05-24 17:36:41 +0200 | [diff] [blame] | 34 | fi |