git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Thomas Gummerer <t.gummerer@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: Reid Price <reid.price@gmail.com>, git@vger.kernel.org
Subject: Re: Apparent bug in 'git stash push <subdir>' loses untracked files
Date: Sun, 17 Dec 2017 18:05:11 +0000	[thread overview]
Message-ID: <20171217180511.GA2641@hank> (raw)
In-Reply-To: <xmqq4loqplou.fsf@gitster.mtv.corp.google.com>

On 12/16, Junio C Hamano wrote:
> Thomas Gummerer <t.gummerer@gmail.com> writes:
> 
> > Maybe the best solution would be to introduce 'git reset --hard --
> > <pathspec>', or maybe someone who knows shell programming a little
> > better than me has an idea? 
> >
> > diff --git a/git-stash.sh b/git-stash.sh
> > index 1114005ce2..01bf74015e 100755
> > --- a/git-stash.sh
> > +++ b/git-stash.sh
> > @@ -322,10 +322,15 @@ push_stash () {
> >  
> >                 if test $# != 0
> >                 then
> > +                       git ls-files -z >"$(git rev-parse --git-dir)"/stash-to-remove
> >                         git reset -q -- "$@"
> >                         git ls-files -z --modified -- "$@" |
> >                         git checkout-index -z --force --stdin
> > -                       git clean --force -q -d -- "$@"
> > +                       while read -r -d '' to_delete
> > +                       do
> > +                               git clean --force -q -d -- "$to_delete"
> > +                       done <"$(git rev-parse --git-dir)"/stash-to-remove
> > +                       rm "$(git rev-parse --git-dir)"/stash-to-remove
> >                 else
> >                         git reset --hard -q
> >                 fi
> 
> Hmph.  I personally did not care (nor use) pathspec limited stash
> creation, so I admit that I do not offhand know what this code
> (before or after the above fix) is trying to do, even though it has
> been in our tree for some time X-<.

Yeah unfortunately this has been broken for pathspecs that match
untracked files since I introduced it :(

> But judging from the fact that
> the other side is a mere "git reset --hard" (for the whole tree), I
> guess for each and every path in the index and/or in the HEAD that
> match pathspec "$@", you are trying to
> 
>  - remove it from the index if it is not in HEAD;
> 
>  - add it to the index if it is not in the index but is in HEAD; or
> 
>  - reset the index to the version in HEAD if it is in the index.
> 
> and then match the working tree version to that of the index?

That's correct.  That's how I would have imagined 'git reset --hard --
<pathspec>'.

> I am not convinced that "git reset --hard -- <pathspec>" is a good
> UI [*1*] but I agree that a low level facility that does these would
> be quite helpful to implement the then-clause of the above.

Thanks a lot for spelling this out below.  After reading that I
definitely agree that 'git reset --hard -- <pathspec>' wouldn't be a
good UI, and something like that would belong in checkout.

> But I suspect that you do not have to do shell "logic" nor low-level
> plumbing update in this case.  Would the attached work?

Ah interesting, what you have below looks good to me indeed, it
matches what I'd expect it to do and fixes the bug that was reported.
Thanks! 

I've taken the liberty to take what you have below and turned into a
proper patch, giving you authorship of it, as you wrote the code for
the fix.  Hope that's the appropriate credit for you for this patch.

[...]

--- >8 ---
From: Junio C Hamano <gitster@pobox.com>
Subject: [PATCH] stash: don't delete untracked files that match pathspec

Currently when 'git stash push -- <pathspec>' is used, untracked files
that match the pathspec will be deleted, even though they do not end up
in a stash anywhere.

Untracked files should be left along by 'git stash push -- <pathspec>'
unless the --untracked or --all flags are given.  Fix that.

Reported-by: Reid Price <reid.price@gmail.com>
Test-by: Thomas Gummerer <t.gummerer@gmail.com>
Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 git-stash.sh     |  5 ++---
 t/t3903-stash.sh | 16 ++++++++++++++++
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/git-stash.sh b/git-stash.sh
index 1114005ce2..a979bfb665 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -322,10 +322,9 @@ push_stash () {
 
 		if test $# != 0
 		then
-			git reset -q -- "$@"
-			git ls-files -z --modified -- "$@" |
+			git ls-files -z -- "$@" |
 			git checkout-index -z --force --stdin
-			git clean --force -q -d -- "$@"
+			git diff-index -p HEAD -- "$@" | git apply --index -R
 		else
 			git reset --hard -q
 		fi
diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index 39c7f2ebd7..6952a031b2 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -1064,4 +1064,20 @@ test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
 	test foo,bar = $(cat foo),$(cat bar)
 '
 
+test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' '
+	git reset &&
+	>subdir/untracked &&
+	>subdir/tracked1 &&
+	>subdir/tracked2 &&
+	git add subdir/tracked* &&
+	git stash -- subdir/ &&
+	test_path_is_missing subdir/tracked1 &&
+	test_path_is_missing subdir/tracked2 &&
+	test_path_is_file subdir/untracked &&
+	git stash pop &&
+	test_path_is_file subdir/tracked1 &&
+	test_path_is_file subdir/tracked2 &&
+	test_path_is_file subdir/untracked
+'
+
 test_done
-- 
2.15.1.390.ga48b24a585.dirty


  reply	other threads:[~2017-12-17 18:03 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-13 17:32 Apparent bug in 'git stash push <subdir>' loses untracked files Reid Price
2017-12-13 21:20 ` Igor Djordjevic
2017-12-13 23:14   ` Thomas Gummerer
2017-12-13 23:46     ` Igor Djordjevic
2017-12-13 23:05 ` Thomas Gummerer
2017-12-16 18:33   ` Junio C Hamano
2017-12-17 18:05     ` Thomas Gummerer [this message]
2017-12-18 18:24       ` Junio C Hamano
2018-01-05 20:03         ` Thomas Gummerer
2018-01-06  0:24           ` [PATCH v2] stash: don't delete untracked files that match pathspec Thomas Gummerer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171217180511.GA2641@hank \
    --to=t.gummerer@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=reid.price@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).