Subject: Re: [PATCH v2 1/3] stash bug: stash can lose data in a file removed from the index

From: Charles Bailey <[email-masked]>
Date: Mon, 19 Apr 2010 08:45:20 +0100

On Sun, Apr 18, 2010 at 04:11:15PM -0700, Junio C Hamano wrote:
> Charles Bailey <[email-masked]> writes:
> 
> It is likely that this needs to be protected with SYMLINKS prerequisite.
> Also I am a bit unhappy about the use of "readlink" which is not even in
> POSIX.1 here.  We already have one use of it in the tests but that only
> happens while doing valgrind.  Traditionally this has been more portably
> done by reading from "ls -l file", like so:
> 
> 	case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac

I don't have access to many flavours of unix at the moment and I
failed to look up the POSIXness of readlink. I'll re-roll with greater
portability.

> Also, whether "readlink file" or "ls -l file" is used to check the result,
> the "test -f file" is redundant.

Yes, of course. Not sure what I was thinking.

What about "test -h"? Is this sufficiently portable
for use in our tests? I understand that it's supposed to be POSIX and
available on Solaris sh.

> 
> > +test_expect_failure 'stash directory to file' '
> > +	git reset --hard &&
> > +	mkdir dir &&
> > +	echo foo >dir/file &&
> > +	git add dir/file &&
> > +	git commit -m "Add file in dir" &&
> > +	rm dir/file &&
> > +	rmdir dir &&
> > +	echo bar >dir &&
> > +	git stash save "directory to file" &&
> > +	test -d dir &&
> > +	test foo = "$(cat dir/file)" &&
> > +	test_must_fail git stash apply &&
> > +	test bar = "$(cat dir)" &&
> > +	git reset --soft HEAD^
> > +'
> 
> I have a feeling that this test is being a bit unfair.
> 
> What should a successful invocation of "stash apply" leave in the working
> tree in this case, especially when you consider that in a real life use
> case you may have other files in "dir" directory or changes to "dir/file"?

Actually I now think that this is completely wrong. There's no reason
that stash apply shouldn't succeed. If we managed to save the new file
where the directory was in the stash then why shouldn't apply be able
to at least attempt to remove the tracked files in the directory that
were originally removed and replace them with the stashed file?

Even if we decide that it can't or shouldn't, we should expect a
failing stash apply to leave the tree as it currently is. That does
leave the question of how the user is supposed to get stuff out of his
stash. After all, he's trying to apply the stash on exactly the state
that stash left him in.

Is it sensible to be guided by these two principles: git stash should
be safe, i.e. it should never remove content that it doesn't save in
the database. git stash && git stash apply should leave the working
tree exactly as it was before the git stash invocation (if the stash
succeeds it may be equivalent to a git reset)?

If so we definitely need to fix the behaviour where git stash
vaporizes local changes when there's a file <-> directory change
in the working tree. Even if we cop out and make git stash fail if it
determines that it wouldn't restore the changes.

Charles.