Use more robust pre-commit hook.

pull/12/head
Joshua Potter 2023-12-03 14:32:16 -07:00
parent 963283b74e
commit 55e1b42a73
1 changed files with 15 additions and 7 deletions

View File

@ -1,13 +1,21 @@
#!/usr/bin/env bash
set -e
filesToFormat=$(
git --no-pager diff --name-status --no-color --cached | \
awk '$1 != "D" && $2 ~ /\.c$|\.h$/ {print $NF}'
STAGED=$(
git --no-pager diff --name-only --no-color --cached --diff-filter=d |
# Remove quotations used to surrounding filenames with special characters.
sed -e "s/^\"//" -e "s/\"$//g"
)
for path in $filesToFormat
TARGETS=()
while IFS= read -r FILENAME
do
clang-format -i "$path"
git add "$path"
done;
if [[ "$FILENAME" =~ .*\.c ]] || [[ "$FILENAME" == .*\.h ]]; then
TARGETS+=("${FILENAME}")
fi
done <<< "$STAGED"
if (( ${#TARGETS[@]} )); then
clang-format -i "${TARGETS[@]}"
git add "${TARGETS[@]}"
fi