pre-commit hook: only format modified lines

instead of entire files
This commit is contained in:
teutat3s 2023-04-26 12:49:44 +02:00
parent a7d4be4af5
commit c65f185773
Signed by: teutat3s
GPG key ID: 4FA1D3FA524F22C1

View file

@ -9,15 +9,25 @@ fi
diff="git diff-index --name-only --cached $against --diff-filter d" diff="git diff-index --name-only --cached $against --diff-filter d"
all_files=($($diff)) mapfile -t all_files < <($diff)
# Format staged files. # Stash only unstaged changes, keeping staged changes
old_stash=$(git rev-parse --quiet --verify refs/stash)
git stash push --quiet --keep-index -m 'Unstaged changes before pre-commit hook'
new_stash=$(git rev-parse --quiet --verify refs/stash)
# Format staged files
if ((${#all_files[@]} != 0)); then if ((${#all_files[@]} != 0)); then
treefmt "${all_files[@]}" && treefmt "${all_files[@]}" &&
git add "${all_files[@]}" git add "${all_files[@]}"
fi fi
# check editorconfig # If unstaged changes were stashed re-apply to working tree
if [ "$old_stash" != "$new_stash" ]; then
git stash pop --quiet
fi
# Check editorconfig
if ((${#all_files[@]} != 0)); then if ((${#all_files[@]} != 0)); then
if ! editorconfig-checker -- "${all_files[@]}"; then if ! editorconfig-checker -- "${all_files[@]}"; then
printf "%b\n" \ printf "%b\n" \