]> git.ipfire.org Git - thirdparty/git.git/commitdiff
stash apply: report status correctly even in a worktree's subdirectory
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Fri, 4 Oct 2019 12:30:59 +0000 (05:30 -0700)
committerJunio C Hamano <gitster@pobox.com>
Sun, 6 Oct 2019 00:04:56 +0000 (09:04 +0900)
When Git wants to spawn a child Git process inside a worktree's
subdirectory while `GIT_DIR` is set, we need to take care of specifying
the work tree's top-level directory explicitly because it cannot be
discovered: the current directory is _not_ the top-level directory of
the work tree, and neither is it inside the parent directory of
`GIT_DIR`.

This fixes the problem where `git stash apply` would report pretty much
everything deleted or untracked when run inside a worktree's
subdirectory.

To make sure that we do not introduce the "reverse problem", i.e. when
`GIT_WORK_TREE` is defined but `GIT_DIR` is not, we simply make sure
that both are set.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/stash.c
t/t3908-stash-in-worktree.sh [new file with mode: 0755]

index b5a301f24d7a5f8b0a5f7d51703ca4cf38e2109c..b108f099fabf53ecb8f5ae2c6dd2a99a66e42a88 100644 (file)
@@ -497,6 +497,10 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
                 */
                cp.git_cmd = 1;
                cp.dir = prefix;
+               argv_array_pushf(&cp.env_array, GIT_WORK_TREE_ENVIRONMENT"=%s",
+                                absolute_path(get_git_work_tree()));
+               argv_array_pushf(&cp.env_array, GIT_DIR_ENVIRONMENT"=%s",
+                                absolute_path(get_git_dir()));
                argv_array_push(&cp.args, "status");
                run_command(&cp);
        }
diff --git a/t/t3908-stash-in-worktree.sh b/t/t3908-stash-in-worktree.sh
new file mode 100755 (executable)
index 0000000..2b2b366
--- /dev/null
@@ -0,0 +1,27 @@
+#!/bin/sh
+#
+# Copyright (c) 2019 Johannes E Schindelin
+#
+
+test_description='Test git stash in a worktree'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+       test_commit initial &&
+       git worktree add wt &&
+       test_commit -C wt in-worktree
+'
+
+test_expect_success 'apply in subdirectory' '
+       mkdir wt/subdir &&
+       (
+               cd wt/subdir &&
+               echo modified >../initial.t &&
+               git stash &&
+               git stash apply >out
+       ) &&
+       grep "\.\.\/initial\.t" wt/subdir/out
+'
+
+test_done