Use more robust pre-commit.

pull/9/head
Joshua Potter 2023-12-03 14:31:36 -07:00
parent b4e42e6de2
commit 9a5274f0dd
1 changed files with 15 additions and 6 deletions

View File

@ -1,12 +1,21 @@
#!/usr/bin/env bash
set -e
FORMAT_FILES=$(
git --no-pager diff --name-status --no-color --cached | \
awk '$1 != "D" && $2 ~ /\.py$/ {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"
)
if [ -n "$FORMAT_FILES" ]; then
black --quiet "$FORMAT_FILES"
git add "$FORMAT_FILES"
TARGETS=()
while IFS= read -r FILENAME
do
if [[ "$FILENAME" =~ .*\.py ]]; then
TARGETS+=("${FILENAME}")
fi
done <<< "$STAGED"
if (( ${#TARGETS[@]} )); then
black --quiet "${TARGETS[@]}"
git add "${TARGETS[@]}"
fi