diff --git a/.githooks/pre-commit b/.githooks/pre-commit index 47ee87b..42e68d2 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -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