aboutsummaryrefslogtreecommitdiffstats
path: root/hooks
diff options
context:
space:
mode:
authorFintasticMan <finlay.neon.kid@gmail.com>2023-01-09 23:46:53 +0100
committerFintasticMan <finlay.neon.kid@gmail.com>2024-01-12 14:42:53 +0100
commitc634a4e3b44f0d1b020be2bbb52ac57fb4f8772d (patch)
tree0d598aa8cbefde83d8d3603b5cee0bbd663e5f28 /hooks
parentab8e267e2861f33750a1320bdcf39db03134dd1b (diff)
hook: Use clang-format with highest version
Diffstat (limited to 'hooks')
-rwxr-xr-xhooks/pre-commit25
1 files changed, 22 insertions, 3 deletions
diff --git a/hooks/pre-commit b/hooks/pre-commit
index 9247f8a8..e03b4217 100755
--- a/hooks/pre-commit
+++ b/hooks/pre-commit
@@ -1,8 +1,27 @@
#!/bin/sh
-changedFiles="$(git clang-format --extensions cpp,h --style file --staged -q --diffstat -- ':!src/FreeRTOS' ':!src/libs')"
-git clang-format --extensions cpp,h --style file --staged -q -- ':!src/FreeRTOS' ':!src/libs'
+minVersion="14.0.0"
-echo "$changedFiles" | head -n -1 | cut -d ' ' -f 1 | while read -r file; do
+for file in $(find $(echo "$PATH" | tr ':' ' ') -maxdepth 1 -type f -executable -name 'git-clang-format*'); do
+ curName="$(basename "$file" | sed 's/^git-//')"
+ curVersion="$("$curName" --version | cut -d ' ' -f 3)"
+
+ if [ "$(printf '%s\n' "$curVersion" "$version" "$minVersion" | sort -V | tail -n 1)" = "$curVersion" ]; then
+ name="$curName"
+ version="$curVersion"
+ fi
+done
+
+if [ -z "$name" ]; then
+ echo "Could not find a suitable clang-format installation. Install clang-format that includes the git-clang-format script, with at least version $minVersion"
+ exit 1
+fi
+
+args='-q --extensions cpp,h --style file --staged -- :!src/FreeRTOS :!src/libs'
+
+changedFiles="$(git "$name" --diffstat $args)"
+git "$name" $args
+
+echo "$changedFiles" | head -n -1 | cut -d ' ' -f 2 | while read -r file; do
git add -- "$file"
done