threads / patch / 51916

patchstash apply: report status correctly even in a worktree's subdirectory

Subject: [PATCH 1/1] stash apply: report status correctly even in a worktree's subdirectory

## tl;dr

6 messages between Sep 25, 2019 and Oct 4, 2019. Diffs are folded; open one to read it.

replies: 5people: 2as markdown or json

Johannes Schindelin via GitGitGadget· Sep 25, 2019, 12:45 UTC · lore

[PATCH 0/1] stash apply: be prepared to run in a worktree's subdirectory

I saw this issue a couple times in my setup, and always wondered why nobody else seemed to be hit by this. When I finally found/made some time to investigate, I found out that it really requires a specific setup: I have many worktrees connected to my main git.git clone, often run inside t/ and I do stash quite often (now that git stash's performance is a joy on Windows).

Johannes Schindelin (1):
  stash apply: report status correctly even in a worktree's subdirectory
 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
base-commit: 4c86140027f4a0d2caaa3ab4bd8bfc5ce3c11c8a
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-354%2Fdscho%2Fapply-stash-in-subdirectory-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-354/dscho/apply-stash-in-subdirectory-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/354
-- 
gitgitgadget
Johannes Schindelin via GitGitGadget· Sep 25, 2019, 12:45 UTC · re: Johannes Schindelin via GitGitGadget · lore
From: Johannes Schindelin <johannes.schindelin@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.schindelin@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
Show changes to 2 files +29 −0

builtin/stash.c, 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
Junio C Hamano· Oct 3, 2019, 22:22 UTC · re: Johannes Schindelin via GitGitGadget · lore

Re: [PATCH 1/1] stash apply: report status correctly even in a worktree's subdirectory

"Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com> writes:

Show 13 quoted lines
> 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);
>  	}

Nicely spotted. Exporting GIT_WORK_TREE alone without GIT_DIR feels a bit disturbing, at least to me, though.

I wondered if this misbehaves when the end user has GIT_WORK_TREE environment exported, but in such a case, get_git_work_tree() would return that directory, and by re-exporting it to the child process, we would honor the end user's intention, so all is good, I think.

Thanks.
Show 33 quoted lines
> 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
Johannes Schindelin· Oct 4, 2019, 09:19 UTC · re: Junio C Hamano · lore

Re: [PATCH 1/1] stash apply: report status correctly even in a worktree's subdirectory

Hi Junio,
On Fri, 4 Oct 2019, Junio C Hamano wrote:
Show 19 quoted lines
> "Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
> writes:
>
> > 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);
> >  	}
>
> Nicely spotted.  Exporting GIT_WORK_TREE alone without GIT_DIR feels
> a bit disturbing, at least to me, though.

You're absolutely right! Of course, the problem this patch fixes is _because_ `GIT_DIR` is defined, but I should make sure that I don't introduce the reverse problem ;-)

> I wondered if this misbehaves when the end user has GIT_WORK_TREE
> environment exported, but in such a case, get_git_work_tree() would
> return that directory, and by re-exporting it to the child process,
> we would honor the end user's intention, so all is good, I think.

Yes, that'd my understanding, too. It won't hurt to re-define that variable, but it hurts not to define it at all.

Ciao, Dscho

Show 37 quoted lines
>
> Thanks.
>
> > 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
>
Johannes Schindelin via GitGitGadget· Oct 4, 2019, 12:30 UTC · re: Johannes Schindelin via GitGitGadget · lore

[PATCH v2 0/1] stash apply: be prepared to run in a worktree's subdirectory

I saw this issue a couple times in my setup, and always wondered why nobody else seemed to be hit by this. When I finally found/made some time to investigate, I found out that it really requires a specific setup: I have many worktrees connected to my main git.git clone, often run inside t/ and I do stash quite often (now that git stash's performance is a joy on Windows).

Changes since v1:
 * We now make sure to also set GIT_DIR.
Johannes Schindelin (1):
  stash apply: report status correctly even in a worktree's subdirectory
 builtin/stash.c              |  4 ++++
 t/t3908-stash-in-worktree.sh | 27 +++++++++++++++++++++++++++
 2 files changed, 31 insertions(+)
 create mode 100755 t/t3908-stash-in-worktree.sh
base-commit: 4c86140027f4a0d2caaa3ab4bd8bfc5ce3c11c8a
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-354%2Fdscho%2Fapply-stash-in-subdirectory-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-354/dscho/apply-stash-in-subdirectory-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/354
Range-diff vs v1:
 1:  a687c16b82 ! 1:  4e19436fbf stash apply: report status correctly even in a worktree's subdirectory
     @@ -3,15 +3,20 @@
          stash apply: report status correctly even in a worktree's subdirectory
      
          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`.
     +    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>
      
       diff --git a/builtin/stash.c b/builtin/stash.c
     @@ -23,6 +28,8 @@
       		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);
       	}
-- 
gitgitgadget
Johannes Schindelin via GitGitGadget· Oct 4, 2019, 12:30 UTC · re: Johannes Schindelin via GitGitGadget · lore

[PATCH v2 1/1] stash apply: report status correctly even in a worktree's subdirectory

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

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>
---
 builtin/stash.c              |  4 ++++
 t/t3908-stash-in-worktree.sh | 27 +++++++++++++++++++++++++++
 2 files changed, 31 insertions(+)
 create mode 100755 t/t3908-stash-in-worktree.sh
Show changes to 2 files +31 −0

builtin/stash.c, t/t3908-stash-in-worktree.sh

diff --git a/builtin/stash.c b/builtin/stash.c
index b5a301f24d..b108f099fa 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -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
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

← back to recent threads