threads / bug / 22047

Possible bug in 1.6.6 with reset --hard and $GIT_WORK_TREE

Subject: Possible bug in 1.6.6 with reset --hard and $GIT_WORK_TREE

## tl;dr

9 messages between Dec 29, 2009 and Dec 30, 2009.

replies: 8people: 6as markdown or json

Fyn Fynn· Dec 29, 2009, 06:47 UTC · lore

The exact same git reset command that works in 1.6.4, fails to work under 1.6.6:

$ GIT_WORK_TREE=$HOME/rawdata/ GIT_DIR=$HOME/rawdata/.git /usr/local/git-1.6.6/bin/git reset --hard fatal: hard reset requires a work tree $ GIT_WORK_TREE=$HOME/rawdata/ GIT_DIR=$HOME/rawdata/.git /usr/local/git-1.6.4/bin/git reset --hard HEAD is now at 77ec73f...

What gives?
Tay Ray Chuan· Dec 29, 2009, 08:12 UTC · re: Fyn Fynn · lore

Re: Possible bug in 1.6.6 with reset --hard and $GIT_WORK_TREE

Hi,
On Tue, Dec 29, 2009 at 2:47 PM, Fyn Fynn <fynfynn@gmail.com> wrote:
Show 11 quoted lines
> The exact same git reset command that works in 1.6.4, fails to work
> under 1.6.6:
>
> $ GIT_WORK_TREE=$HOME/rawdata/ GIT_DIR=$HOME/rawdata/.git
> /usr/local/git-1.6.6/bin/git reset --hard
> fatal: hard reset requires a work tree
> $ GIT_WORK_TREE=$HOME/rawdata/ GIT_DIR=$HOME/rawdata/.git
> /usr/local/git-1.6.4/bin/git reset --hard
> HEAD is now at 77ec73f...
>
> What gives?

perhaps you should also specify the --exec-path option or GIT_EXEC_PATH environment variable.

Assuming a full installation in /usr/local/git-1.6.6/ and thus that the git commands are installed at /usr/local/git-1.6.6/libexec/git-core/, the reset for 1.6.6 would look like this:

$ GIT_WORK_TREE=$HOME/rawdata/ \ GIT_DIR=$HOME/rawdata/.git \ GIT_EXEC_PATH=/usr/local/git-1.6.6/libexec/git-core/ \ /usr/local/git-1.6.6/bin/git reset --hard

-- 
Cheers,
Ray Chuan
Nguyen Thai Ngoc Duy· Dec 29, 2009, 08:38 UTC · re: Fyn Fynn · lore

Re: Possible bug in 1.6.6 with reset --hard and $GIT_WORK_TREE

On 12/29/09, Fyn Fynn <fynfynn@gmail.com> wrote:
Show 11 quoted lines
> The exact same git reset command that works in 1.6.4, fails to work
>  under 1.6.6:
>
>  $ GIT_WORK_TREE=$HOME/rawdata/ GIT_DIR=$HOME/rawdata/.git
>  /usr/local/git-1.6.6/bin/git reset --hard
>  fatal: hard reset requires a work tree
>  $ GIT_WORK_TREE=$HOME/rawdata/ GIT_DIR=$HOME/rawdata/.git
>  /usr/local/git-1.6.4/bin/git reset --hard
>  HEAD is now at 77ec73f...
>
>  What gives?
A recent patch by Jeff (952dfc6 (reset: improve worktree safety valves
- 2009-12-04)) makes sure that "git reset --hard" will not work
outside worktree (which is right). Where was your current directory,
inside $GIT_WORK_TREE or outside?
-- 
Duy
Nanako Shiraishi· Dec 29, 2009, 11:04 UTC · re: Nguyen Thai Ngoc Duy · lore

Re: Possible bug in 1.6.6 with reset --hard and $GIT_WORK_TREE

Quoting Nguyen Thai Ngoc Duy <pclouds@gmail.com>:
Show 16 quoted lines
> On 12/29/09, Fyn Fynn <fynfynn@gmail.com> wrote:
>> The exact same git reset command that works in 1.6.4, fails to work
>>  under 1.6.6:
>>
>>  $ GIT_WORK_TREE=$HOME/rawdata/ GIT_DIR=$HOME/rawdata/.git
>>  /usr/local/git-1.6.6/bin/git reset --hard
>>  fatal: hard reset requires a work tree
>>  $ GIT_WORK_TREE=$HOME/rawdata/ GIT_DIR=$HOME/rawdata/.git
>>  /usr/local/git-1.6.4/bin/git reset --hard
>>  HEAD is now at 77ec73f...
>>
>>  What gives?
>
> A recent patch by Jeff (952dfc6 (reset: improve worktree safety valves
> - 2009-12-04)) makes sure that "git reset --hard" will not work
> outside worktree (which is right).
Sorry, but I don't understand why it is *right*. Isn't 'git reset --hard' supposed to make all the files in the working tree match the HEAD, no matter where you start from?
Jeff's commit message says:
        make sure we are in a worktree. Otherwise, we can end up
	munging files inside of '.git'
But if you have ~/myproject/.git project, whose working tree is ~/myproject, and if you run
        % cd ~/myproject/.git
	% git reset --hard
the code mistakenly overwrote files in ~/myproject/.git directory before Jeff's patch, and I agree it was a bug.
But shouldn't the correct fix be to go to ~/myproject, the obvious root level of the working tree, and check out the files in that directory?
-- 
Nanako Shiraishi
http://ivory.ap.teacup.com/nanako3/
Nguyen Thai Ngoc Duy· Dec 29, 2009, 11:36 UTC · re: Nanako Shiraishi · lore

Re: Possible bug in 1.6.6 with reset --hard and $GIT_WORK_TREE

On 12/29/09, Nanako Shiraishi <nanako3@lavabit.com> wrote:
Show 21 quoted lines
> Quoting Nguyen Thai Ngoc Duy <pclouds@gmail.com>:
>
>  > On 12/29/09, Fyn Fynn <fynfynn@gmail.com> wrote:
>  >> The exact same git reset command that works in 1.6.4, fails to work
>  >>  under 1.6.6:
>  >>
>  >>  $ GIT_WORK_TREE=$HOME/rawdata/ GIT_DIR=$HOME/rawdata/.git
>  >>  /usr/local/git-1.6.6/bin/git reset --hard
>  >>  fatal: hard reset requires a work tree
>  >>  $ GIT_WORK_TREE=$HOME/rawdata/ GIT_DIR=$HOME/rawdata/.git
>  >>  /usr/local/git-1.6.4/bin/git reset --hard
>  >>  HEAD is now at 77ec73f...
>  >>
>  >>  What gives?
>  >
>  > A recent patch by Jeff (952dfc6 (reset: improve worktree safety valves
>  > - 2009-12-04)) makes sure that "git reset --hard" will not work
>  > outside worktree (which is right).
>
>
> Sorry, but I don't understand why it is *right*. Isn't 'git reset --hard' supposed to make all the files in the working tree match the HEAD, no matter where you start from?

It is generally "right" to work from inside worktree, the way Git worked before GIT_WORK_TREE came. In case of "git reset --hard", yes it'd be best if Git could just go to worktree and reset it. I forgot that "git reset --hard" does not take pathspec. The situation may be a bit more complicated with "git status" (which also handles worktree as a whole) because you may need to represent the filename output to be relative to current working directory, not the GIT_WORK_TREE. Using GIT_WORK_TREE from outside worktree is imo stretching git to its limits.

-- 
Duy
Fyn Fynn· Dec 29, 2009, 21:09 UTC · re: Nguyen Thai Ngoc Duy · lore

Re: Possible bug in 1.6.6 with reset --hard and $GIT_WORK_TREE

I went over the 952dfc6 patch, and it seems like it was designed to fix a specific bug - the one demonstrated by Nanako Shiraishi above.

Nowhere there do I see an intention to prevent the action described in the original post. And even if there was such intention, it should have been made explicit by providing a proper error message: the current one simply ignores the fact that we provided GIT_WORK_TREE, and is thus incorrect since the work tree can be found.

Moreover, if there is an intention to prevent resetting from outside the worktree (why?), then probably "checkout -f" should be limited as well.

But it is more likely that the breaking of the original action between 1.6.4 and 1.6.6 came about as an untended consequence of 952dfc6, which oversimplified by assuming that the worktree can only be found if we're inside it, ignoring the possibility that GIT_WORK_TREE was provided.

Thus, the current state should be considered a bug.
--
Fynn
On Tue, Dec 29, 2009 at 3:36 AM, Nguyen Thai Ngoc Duy <pclouds@gmail.com> wrote:
Show 35 quoted lines
> On 12/29/09, Nanako Shiraishi <nanako3@lavabit.com> wrote:
>> Quoting Nguyen Thai Ngoc Duy <pclouds@gmail.com>:
>>
>>  > On 12/29/09, Fyn Fynn <fynfynn@gmail.com> wrote:
>>  >> The exact same git reset command that works in 1.6.4, fails to work
>>  >>  under 1.6.6:
>>  >>
>>  >>  $ GIT_WORK_TREE=$HOME/rawdata/ GIT_DIR=$HOME/rawdata/.git
>>  >>  /usr/local/git-1.6.6/bin/git reset --hard
>>  >>  fatal: hard reset requires a work tree
>>  >>  $ GIT_WORK_TREE=$HOME/rawdata/ GIT_DIR=$HOME/rawdata/.git
>>  >>  /usr/local/git-1.6.4/bin/git reset --hard
>>  >>  HEAD is now at 77ec73f...
>>  >>
>>  >>  What gives?
>>  >
>>  > A recent patch by Jeff (952dfc6 (reset: improve worktree safety valves
>>  > - 2009-12-04)) makes sure that "git reset --hard" will not work
>>  > outside worktree (which is right).
>>
>>
>> Sorry, but I don't understand why it is *right*. Isn't 'git reset --hard' supposed to make all the files in the working tree match the HEAD, no matter where you start from?
>
> It is generally "right" to work from inside worktree, the way Git
> worked before GIT_WORK_TREE came. In case of "git reset --hard", yes
> it'd be best if Git could just go to worktree and reset it. I forgot
> that "git reset --hard" does not take pathspec. The situation may be a
> bit more complicated with "git status" (which also handles worktree as
> a whole) because you may need to represent the filename output to be
> relative to current working directory, not the GIT_WORK_TREE. Using
> GIT_WORK_TREE from outside worktree is imo stretching git to its
> limits.
> --
> Duy
>
Jeff King· Dec 29, 2009, 21:50 UTC · re: Fyn Fynn · lore

Re: Possible bug in 1.6.6 with reset --hard and $GIT_WORK_TREE

On Tue, Dec 29, 2009 at 01:09:20PM -0800, Fyn Fynn wrote:
Show 5 quoted lines
> But it is more likely that the breaking of the original action between
> 1.6.4 and 1.6.6 came about as an untended consequence of 952dfc6,
> which oversimplified by assuming that the worktree can only be found
> if we're inside it, ignoring the possibility that GIT_WORK_TREE was
> provided.

Yes, it is an unintended breakage. We discussed the possibility of automatically moving to the work tree when inside the repo, but decided it could be done later, as other builtins which use NEEDS_WORK_TREE (e.g., clean) already fail in that instance.

However, I did not take into account that those commands fail only with automatic repo detection; they _do_ work with GIT_WORK_TREE. So instead of just dying, we need to be emulating the code in git.c which sets up the work tree (and which will die itself if we run into an error).

So I think what we really want is this:
diff --git a/builtin-reset.c b/builtin-reset.c
index 11d1c6e..e4418bc 100644
--- a/builtin-reset.c
+++ b/builtin-reset.c
@@ -286,10 +286,8 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
 	if (reset_type == NONE)
 		reset_type = MIXED; /* by default */
 
-	if ((reset_type == HARD || reset_type == MERGE)
-	    && !is_inside_work_tree())
-		die("%s reset requires a work tree",
-		    reset_type_names[reset_type]);
+	if (reset_type == HARD || reset_type == MERGE)
+		setup_work_tree();
 
 	/* Soft reset does not touch the index file nor the working tree
 	 * at all, but requires them in a good order.  Other resets reset

I'll try to write a few tests and make sure that's sane.

-Peff
Jeff King· Dec 30, 2009, 08:47 UTC · re: Jeff King · lore

[PATCH] reset: unbreak hard resets with GIT_WORK_TREE

Commit 952dfc6 tried to tighten the safety valves for doing a "reset --hard" in a bare repository or outside the work tree, but accidentally broke the case for GIT_WORK_TREE. This patch unbreaks it.

Most git commands which need a work tree simply use NEED_WORK_TREE in git.c to die before they get to their cmd_* function. Reset, however, only needs a work tree in some cases, and so must handle the work tree itself. The error that 952dfc6 made was to simply forbid certain operations if the work tree was not set up; instead, we need to do the same thing that NEED_WORK_TREE does, which is to call setup_work_tree(). We no longer have to worry about dying in the non-worktree case, as setup_work_tree handles that for us.

Signed-off-by: Jeff King <peff@peff.net>
---
Junio, this should probably go onto maint. I based it directly on
952dfc6, which is in v1.6.6 and v1.6.5.5.
 builtin-reset.c       |    6 ++----
 t/t7103-reset-bare.sh |    6 ++++++
 2 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/builtin-reset.c b/builtin-reset.c
index 11d1c6e..e4418bc 100644
--- a/builtin-reset.c
+++ b/builtin-reset.c
@@ -286,10 +286,8 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
 	if (reset_type == NONE)
 		reset_type = MIXED; /* by default */
 
-	if ((reset_type == HARD || reset_type == MERGE)
-	    && !is_inside_work_tree())
-		die("%s reset requires a work tree",
-		    reset_type_names[reset_type]);
+	if (reset_type == HARD || reset_type == MERGE)
+		setup_work_tree();
 
 	/* Soft reset does not touch the index file nor the working tree
 	 * at all, but requires them in a good order.  Other resets reset
diff --git a/t/t7103-reset-bare.sh b/t/t7103-reset-bare.sh
index 68041df..afb55b3 100755
--- a/t/t7103-reset-bare.sh
+++ b/t/t7103-reset-bare.sh
@@ -29,6 +29,12 @@ test_expect_success 'soft reset is ok' '
 	(cd .git && git reset --soft)
 '
 
+test_expect_success 'hard reset works with GIT_WORK_TREE' '
+	mkdir worktree &&
+	GIT_WORK_TREE=$PWD/worktree GIT_DIR=$PWD/.git git reset --hard &&
+	test_cmp file worktree/file
+'
+
 test_expect_success 'setup bare' '
 	git clone --bare . bare.git &&
 	cd bare.git
-- 
1.6.6.320.g7c9b3
Junio C Hamano· Dec 30, 2009, 17:54 UTC · re: Jeff King · lore

Re: [PATCH] reset: unbreak hard resets with GIT_WORK_TREE

Jeff King <peff@peff.net> writes:
Show 15 quoted lines
> Commit 952dfc6 tried to tighten the safety valves for doing
> a "reset --hard" in a bare repository or outside the work
> tree, but accidentally broke the case for GIT_WORK_TREE.
> This patch unbreaks it.
>
> Most git commands which need a work tree simply use
> NEED_WORK_TREE in git.c to die before they get to their
> cmd_* function. Reset, however, only needs a work tree in
> some cases, and so must handle the work tree itself. The
> error that 952dfc6 made was to simply forbid certain
> operations if the work tree was not set up; instead, we need
> to do the same thing that NEED_WORK_TREE does, which is to
> call setup_work_tree(). We no longer have to worry about dying
> in the non-worktree case, as setup_work_tree handles that
> for us.
Sounds very sane to me.  Thanks.

← back to recent threads