From c65f1857735905cc52c3d3e2da2c7e63da97021c Mon Sep 17 00:00:00 2001 From: teutat3s Date: Wed, 26 Apr 2023 12:49:44 +0200 Subject: [PATCH] pre-commit hook: only format modified lines instead of entire files --- shell/hooks/pre-commit.sh | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/shell/hooks/pre-commit.sh b/shell/hooks/pre-commit.sh index 4ce4fa13..23776793 100755 --- a/shell/hooks/pre-commit.sh +++ b/shell/hooks/pre-commit.sh @@ -9,15 +9,25 @@ fi 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 treefmt "${all_files[@]}" && git add "${all_files[@]}" 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 ! editorconfig-checker -- "${all_files[@]}"; then printf "%b\n" \