From 271c329677673ca0e713025ccc1ce3858d9bfceb Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Thu, 27 Jan 2022 22:52:27 +0100 Subject: [PATCH] nix: Fix postgrest-git-hooks causing merge conflicts when staging files partially Signed-off-by: Wolfgang Walther --- nix/tools/devTools.nix | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/nix/tools/devTools.nix b/nix/tools/devTools.nix index 491f00cac..f1ef6d64f 100644 --- a/nix/tools/devTools.nix +++ b/nix/tools/devTools.nix @@ -165,6 +165,17 @@ let # The following unsets all GIT_ variables. unset "''${!GIT_@}" + function restore () { + ref="$(git stash list --format=format:%gD --grep "$1" -n1)" + # this will avoid merge conflicts when applying the stash + ${git}/bin/git restore --source="$ref" . + # restore untracked files, too. could fail with no files + if [ "$(git show --numstat --format=oneline "$ref^3" | wc -l)" -gt 1 ]; then + ${git}/bin/git restore --overlay --source="$ref^3" . + fi + ${git}/bin/git stash drop "$ref" + } + case "$_arg_mode" in basic) case "$_arg_hook" in @@ -179,7 +190,7 @@ let if [ "$(git stash list --grep $stash)" ]; then # Only create the stash pop trap, if we actually created a stash. # Otherwise stash pop will cause havoc. - trap '${git}/bin/git stash pop $(git stash list --format=format:%gD --grep "$stash" -n1)' EXIT + trap 'restore "$stash"' EXIT fi ${style}/bin/postgrest-style @@ -204,7 +215,7 @@ let if [ "$(git stash list --grep $stash)" ]; then # Only create the stash pop trap, if we actually created a stash. # Otherwise stash pop will cause havoc. - trap '${git}/bin/git stash pop $(git stash list --format=format:%gD --grep "$stash" -n1)' EXIT + trap 'restore "$stash"' EXIT fi ${style}/bin/postgrest-style