From: Johannes Schindelin <johannes.schinde...@gmx.de>

When Git wants to spawn a child Git process inside a worktree's
subdirectory, 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.

Signed-off-by: Johannes Schindelin <johannes.schinde...@gmx.de>
---
 builtin/stash.c              |  2 ++
 t/t3908-stash-in-worktree.sh | 27 +++++++++++++++++++++++++++
 2 files changed, 29 insertions(+)
 create mode 100755 t/t3908-stash-in-worktree.sh

diff --git a/builtin/stash.c b/builtin/stash.c
index b5a301f24d..a1e2e7ae7e 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -497,6 +497,8 @@ 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_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
index 0000000000..2b2b366ef9
--- /dev/null
+++ b/t/t3908-stash-in-worktree.sh
@@ -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
-- 
gitgitgadget

Reply via email to