From 3c141e7122bdb7007a3cec0017074618e30bd842 Mon Sep 17 00:00:00 2001 From: teutat3s Date: Sun, 2 Jul 2023 14:49:47 +0200 Subject: [PATCH] devshell: stash only unstaged changes in pre-commit hook --- shell/hooks/pre-commit.sh | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/shell/hooks/pre-commit.sh b/shell/hooks/pre-commit.sh index 45efec25..4e3027e9 100755 --- a/shell/hooks/pre-commit.sh +++ b/shell/hooks/pre-commit.sh @@ -8,8 +8,16 @@ else fi # Stash only unstaged changes, keeping staged changes +# We have to stash two times, because: +# --keep-index also stashes the staged changes. +# The staged changes end up in both the stage AND the stash. +# https://overflow.hostux.net/questions/7650797/how-to-stash-only-unstaged-changes-in-git#60875082 old_stash=$(git rev-parse --quiet --verify refs/stash) -git stash push --quiet --keep-index -m 'Unstaged changes before pre-commit hook' +git stash push --quiet --staged --message "Staged changes before pre-commit hook" +git stash push --quiet --keep-index --message "Unstaged changes before pre-commit hook" +if git stash show "stash@{1}" 2> /dev/null; then + git stash pop --quiet --index "stash@{1}" +fi new_stash=$(git rev-parse --quiet --verify refs/stash) diff="git diff-index --name-only --cached $against --diff-filter d"