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 #!/usr/bin/env bash
set -e set -e
filesToFormat=$( STAGED=$(
git --no-pager diff --name-status --no-color --cached | \ git --no-pager diff --name-only --no-color --cached --diff-filter=d |
awk '$1 != "D" && $2 ~ /\.c$|\.h$/ {print $NF}' # 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 do
clang-format -i "$path" if [[ "$FILENAME" =~ .*\.c ]] || [[ "$FILENAME" == .*\.h ]]; then
git add "$path" TARGETS+=("${FILENAME}")
done; fi
done <<< "$STAGED"
if (( ${#TARGETS[@]} )); then
clang-format -i "${TARGETS[@]}"
git add "${TARGETS[@]}"
fi