threads / patch / 24561

patchrequire_work_tree: Look for top-level instead of is-inside-work-tree

Subject: [PATCH] require_work_tree: Look for top-level instead of is-inside-work-tree

## tl;dr

6 messages between Jul 28, 2010 and Aug 3, 2010. Diffs are folded; open one to read it.

replies: 5people: 3as markdown or json

Tor Arne Vestbø· Jul 28, 2010, 16:47 UTC · lore

The documentation describes require_work_tree as guarding against bare repositories, and that's also the way it's used from porcelain such as git-rebase. When implemented using --is-inside-work-tree the samantics change, causing git-rebase to fail if run from outside GIT_WORK_TREE, even if GIT_WORK_TREE is valid.

Signed-off-by: Tor Arne Vestbø <tor.arne.vestbo@nokia.com>
---
 git-sh-setup.sh     |    2 +-
 t/t1501-worktree.sh |    9 +++++++++
 2 files changed, 10 insertions(+), 1 deletions(-)
Show changes to 2 files +10 −1

git-sh-setup.sh, t/t1501-worktree.sh

diff --git a/git-sh-setup.sh b/git-sh-setup.sh
index 6131670..f8e4428 100644
--- a/git-sh-setup.sh
+++ b/git-sh-setup.sh
@@ -141,7 +141,7 @@ cd_to_toplevel () {
 }
 
 require_work_tree () {
-	test "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = true ||
+	test -n "$(git rev-parse --show-toplevel 2>/dev/null)" ||
 	die "fatal: $0 cannot be used without a working tree."
 }
 
diff --git a/t/t1501-worktree.sh b/t/t1501-worktree.sh
index bd8b607..45b09e7 100755
--- a/t/t1501-worktree.sh
+++ b/t/t1501-worktree.sh
@@ -114,6 +114,15 @@ test_expect_success 'repo finds its work tree from work tree, too' '
 	 test sub/dir/tracked = "$(git ls-files)")
 '
 
+test_expect_success 'require_work_tree finds work tree' '
+	(cd repo.git/work &&
+	. "$(git --exec-path)"/git-sh-setup &&
+	cd .. &&
+	require_work_tree &&
+	cd .. &&
+	require_work_tree)
+'
+
 test_expect_success '_gently() groks relative GIT_DIR & GIT_WORK_TREE' '
 	(cd repo.git/work/sub/dir &&
 	GIT_DIR=../../.. GIT_WORK_TREE=../.. GIT_PAGER= \
-- 
1.7.2.19.g48995
Junio C Hamano· Jul 28, 2010, 23:00 UTC · re: Tor Arne Vestbø · lore

Re: [PATCH] require_work_tree: Look for top-level instead of is-inside-work-tree

Tor Arne Vestbø <tor.arne.vestbo@nokia.com> writes:
Show 8 quoted lines
> The documentation describes require_work_tree as guarding against
> bare repositories, and that's also the way it's used from porcelain
> such as git-rebase. When implemented using --is-inside-work-tree
> the samantics change, causing git-rebase to fail if run from outside
> GIT_WORK_TREE, even if GIT_WORK_TREE is valid.
>
> Signed-off-by: Tor Arne Vestbø <tor.arne.vestbo@nokia.com>
> ---

The "requirement" is that we _have_ work tree somewhere that we can cd-to-toplevel to if we wanted to, not that we _are_ already in the work tree. I can buy that rationale.

However, I notice that "git bisect", "git mergetool" and "git submodule" do not seem to do cd_to_topleve immediately after require_work_tree. The last one has cd_to_toplevel in later parts of the codepath, presumably so that it can collect paths relative to the subdirectory in the work tree. I wonder if all of them actually need to be run from inside a work tree? Don't they need a separate "git rev-parse --is-inside-work-tree || die" check after require_work_tree (or perhaps cd_to_toplevel) if we apply this patch?

"git rebase--interactive" also lacks cd_to_toplevel but that is done by the calling "git rebase" and I think that one is Ok.

Show 39 quoted lines
>  git-sh-setup.sh     |    2 +-
>  t/t1501-worktree.sh |    9 +++++++++
>  2 files changed, 10 insertions(+), 1 deletions(-)
>
> diff --git a/git-sh-setup.sh b/git-sh-setup.sh
> index 6131670..f8e4428 100644
> --- a/git-sh-setup.sh
> +++ b/git-sh-setup.sh
> @@ -141,7 +141,7 @@ cd_to_toplevel () {
>  }
>  
>  require_work_tree () {
> -	test "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = true ||
> +	test -n "$(git rev-parse --show-toplevel 2>/dev/null)" ||
>  	die "fatal: $0 cannot be used without a working tree."
>  }
>  
> diff --git a/t/t1501-worktree.sh b/t/t1501-worktree.sh
> index bd8b607..45b09e7 100755
> --- a/t/t1501-worktree.sh
> +++ b/t/t1501-worktree.sh
> @@ -114,6 +114,15 @@ test_expect_success 'repo finds its work tree from work tree, too' '
>  	 test sub/dir/tracked = "$(git ls-files)")
>  '
>  
> +test_expect_success 'require_work_tree finds work tree' '
> +	(cd repo.git/work &&
> +	. "$(git --exec-path)"/git-sh-setup &&
> +	cd .. &&
> +	require_work_tree &&
> +	cd .. &&
> +	require_work_tree)
> +'
> +
>  test_expect_success '_gently() groks relative GIT_DIR & GIT_WORK_TREE' '
>  	(cd repo.git/work/sub/dir &&
>  	GIT_DIR=../../.. GIT_WORK_TREE=../.. GIT_PAGER= \
> -- 
> 1.7.2.19.g48995
Tor Arne Vestbø· Jul 30, 2010, 11:04 UTC · re: Junio C Hamano · lore

Re: [PATCH] require_work_tree: Look for top-level instead of is-inside-work-tree

Hey, and thanks for your feedback Junio!
On 29.07.10 01.00, ext Junio C Hamano wrote:
Show 14 quoted lines
> Tor Arne Vestbø <tor.arne.vestbo@nokia.com> writes:
>
>  > The documentation describes require_work_tree as guarding against
>  > bare repositories, and that's also the way it's used from porcelain
>  > such as git-rebase. When implemented using --is-inside-work-tree
>  > the samantics change, causing git-rebase to fail if run from outside
>  > GIT_WORK_TREE, even if GIT_WORK_TREE is valid.
>  >
>  > Signed-off-by: Tor Arne Vestbø <tor.arne.vestbo@nokia.com>
>  > ---
>
> The "requirement" is that we _have_ work tree somewhere that we can
> cd-to-toplevel to if we wanted to, not that we _are_ already in the work
> tree. I can buy that rationale.
Right. You put it much nicer than me :)
Show 8 quoted lines
> However, I notice that "git bisect", "git mergetool" and "git submodule"
> do not seem to do cd_to_topleve immediately after require_work_tree. The
> last one has cd_to_toplevel in later parts of the codepath, presumably so
> that it can collect paths relative to the subdirectory in the work tree.
> I wonder if all of them actually need to be run from inside a work tree?
> Don't they need a separate "git rev-parse --is-inside-work-tree || die"
> check after require_work_tree (or perhaps cd_to_toplevel) if we apply this
> patch?

I think if we have a work tree somewhere, we can at least do "git rev-parse --is-inside-work-tree || cd_to_toplevel" instead of dying, unless there's some danger to that (the user running a command from outside GIT_WORK_TREE but expecting GIT_WORK_TREE to not be touched).

For "git bisect" and "git submodule" running them in a sub-directory of the work tree complains about needing to be run from the top-level, so I assume we can do an unconditional cd_to_toplevel after the require_work_tree?

For "git mergetool" we should probably do it conditionally only if the user is not inside a work tree already, so that the behavior of running the tool in a sub-directory is not changed.

Tor Arne
Michael J Gruber· Aug 2, 2010, 14:37 UTC · re: Tor Arne Vestbø · lore

Re: [PATCH] require_work_tree: Look for top-level instead of is-inside-work-tree

Tor Arne Vestbø venit, vidit, dixit 28.07.2010 18:47:
Show 25 quoted lines
> The documentation describes require_work_tree as guarding against
> bare repositories, and that's also the way it's used from porcelain
> such as git-rebase. When implemented using --is-inside-work-tree
> the samantics change, causing git-rebase to fail if run from outside
> GIT_WORK_TREE, even if GIT_WORK_TREE is valid.
> 
> Signed-off-by: Tor Arne Vestbø <tor.arne.vestbo@nokia.com>
> ---
>  git-sh-setup.sh     |    2 +-
>  t/t1501-worktree.sh |    9 +++++++++
>  2 files changed, 10 insertions(+), 1 deletions(-)
> 
> diff --git a/git-sh-setup.sh b/git-sh-setup.sh
> index 6131670..f8e4428 100644
> --- a/git-sh-setup.sh
> +++ b/git-sh-setup.sh
> @@ -141,7 +141,7 @@ cd_to_toplevel () {
>  }
>  
>  require_work_tree () {
> -	test "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = true ||
> +	test -n "$(git rev-parse --show-toplevel 2>/dev/null)" ||
>  	die "fatal: $0 cannot be used without a working tree."
>  }
>  

An alternative which does not change the established behavior of require_work_tree would be changing the order of require_work_tree and cd_to_top_level in the callers where possible along the lines of

http://mid.gmane.org/96abf622ca2cf92998ce4ed393ccaa75d95dd9a8.1279112025.git.git@drmicha.warpmail.net

which got lost somehow. (The other callers, as mentioned by Junio, would need to be changed differently, e.g. by moving cd_to... earlier.)

Another problem I noticed back then (I was away since) was that a relative GIT_WORK_TREE is left in place after a cd_to_top_level and messes things up completely - it does not seem to be relative to GIT_DIR. So, there seems to be more to fix in this area.

Cheers, Michael

Junio C Hamano· Aug 2, 2010, 17:46 UTC · re: Michael J Gruber · lore

Re: [PATCH] require_work_tree: Look for top-level instead of is-inside-work-tree

Michael J Gruber <git@drmicha.warpmail.net> writes:
Show 8 quoted lines
> An alternative which does not change the established behavior of
> require_work_tree would be changing the order of require_work_tree and
> cd_to_top_level in the callers where possible along the lines of
>
> http://mid.gmane.org/96abf622ca2cf92998ce4ed393ccaa75d95dd9a8.1279112025.git.git@drmicha.warpmail.net
>
> which got lost somehow. (The other callers, as mentioned by Junio, would
> need to be changed differently, e.g. by moving cd_to... earlier.)
Doesn't it sound stupid to "cd-to-toplevel" and then "require-work-tree"?

If you can go to the top-level, and once you successfully got there, you already _know_ that you have a work tree (and also you already know at that point you are in the work tree). The reason why "require-work-tree" has been placed before "cd-to-toplevel" is exactly for that purpose, I think. It is possible that some callers wanted to "require-work-tree" to mean "I want you to not just _have_ a work tree, but actually be _in_ it", but I somehow doubt it. It is more like "I am going to ask you to go to the top, but let's make sure that you do have a top before doing so", I think.

I on the other hand do not think it is wrong to lose the existing calls to require-work-tree if you know that you are going to call cd-to-toplevel before doing any git operation that needs to have a work-tree, though.

> Another problem I noticed back then (I was away since) was that a
> relative GIT_WORK_TREE is left in place after a cd_to_top_level and
> messes things up completely - it does not seem to be relative to
> GIT_DIR. So, there seems to be more to fix in this area.

I agree; I don't think GIT_WORK_TREE was designed to be anything but an absolute path to begin with. If a command chdir's around and exports the environment to its hooks and subcommands, it should be prepared to adjust it before doing so.

Michael J Gruber· Aug 3, 2010, 07:57 UTC · re: Junio C Hamano · lore

Re: [PATCH] require_work_tree: Look for top-level instead of is-inside-work-tree

Junio C Hamano venit, vidit, dixit 02.08.2010 19:46:
Show 12 quoted lines
> Michael J Gruber <git@drmicha.warpmail.net> writes:
> 
>> An alternative which does not change the established behavior of
>> require_work_tree would be changing the order of require_work_tree and
>> cd_to_top_level in the callers where possible along the lines of
>>
>> http://mid.gmane.org/96abf622ca2cf92998ce4ed393ccaa75d95dd9a8.1279112025.git.git@drmicha.warpmail.net
>>
>> which got lost somehow. (The other callers, as mentioned by Junio, would
>> need to be changed differently, e.g. by moving cd_to... earlier.)
> 
> Doesn't it sound stupid to "cd-to-toplevel" and then "require-work-tree"?

It sounds outright silly, agreed. Though, unless you know the implementation, "cd_to_toplevel" may succeed cd'ing to what "rev-parse --show-toplevel" returns without require_work_tree being happy.

But don't we try to preserve existing behavior unless it's a bug? We certainly have a mismatch of behavior and documentation here. The question is whether we want to break anyone who relied on "require_work_tree" dieing when cwd is not within the work-tree.

Show 10 quoted lines
> 
> If you can go to the top-level, and once you successfully got there, you
> already _know_ that you have a work tree (and also you already know at
> that point you are in the work tree).  The reason why "require-work-tree"
> has been placed before "cd-to-toplevel" is exactly for that purpose, I
> think.  It is possible that some callers wanted to "require-work-tree" to
> mean "I want you to not just _have_ a work tree, but actually be _in_ it",
> but I somehow doubt it.  It is more like "I am going to ask you to go to
> the top, but let's make sure that you do have a top before doing so", I
> think.

Well, if people relied on current behavior... I didn't, I don't mind changing this, in fact I'm usually in "changing mood" and running into the "preserve behavior" wall ;)

In any case, I think "require_work_tree" should really test whether we can cd into the worktree, i.e. whether a cd_to_toplevel would succeed, and not just whether "rev-parse --show-toplevel" returns a non-empty string.

Show 14 quoted lines
> 
> I on the other hand do not think it is wrong to lose the existing calls to
> require-work-tree if you know that you are going to call cd-to-toplevel
> before doing any git operation that needs to have a work-tree, though.
> 
>> Another problem I noticed back then (I was away since) was that a
>> relative GIT_WORK_TREE is left in place after a cd_to_top_level and
>> messes things up completely - it does not seem to be relative to
>> GIT_DIR. So, there seems to be more to fix in this area.
> 
> I agree; I don't think GIT_WORK_TREE was designed to be anything but an
> absolute path to begin with.  If a command chdir's around and exports the
> environment to its hooks and subcommands, it should be prepared to adjust
> it before doing so.

We do have some magic to re-export a relative GIT_DIR as absolute, and the doc says GIT_WORK_TREE is relative to GIT_DIR. We even have a test which succeeds by pure chance, as playing around with different layouts shows. I'll try to come up at least with tests for this when I get to it.

Cheers, Michael

← back to recent threads