threads / bug / 46567

Bug when stashing previously-ignored file plus associated .gitignore change

Subject: Bug when stashing previously-ignored file plus associated .gitignore change

## tl;dr

5 messages between Aug 11, 2017 and Aug 14, 2017.

replies: 4people: 4as markdown or json

Sam Partington· Aug 11, 2017, 15:55 UTC · lore
Hi there,

I'm running git 2.7.4 on Ubuntu 16.04. I've found a couple of problems when "un-ignoring" files in tandem with git stash.

Here's how to reproduce:

Say you have a project using git, with a .gitignore file which contains the following line:

bin/*
You can then see the problems by doing this:
$ touch bin/mynewfile # this file will be ignored at this point
and then updating .gitignore to look like this (adding that second line):

bin/* !bin/mynewfile

So far, so good; the new file is no longer ignored.
But now, try stashing the changes and including untracked files in the stash:
$ git stash save -u
Here's the first problem, bin/mynewfile is still there:

$ ls bin/mynewfile bin/mynewfile

But you'd expect it to not be there and be in the stash, I think. This is what would normally happen with the untracked-files option for git stash.

This leads to the second problem - you can't now pop the stash:

$ git stash pop bin/mynewfile already exists, no checkout Could not restore untracked files from stash

If you want to apply the stash, you have to remove the file:

$ rm bin/mynewfile $ git stash pop # this works, and re-creates bin/mynewfile

This is quite an unusual edge case, but I have hit it two or three times now and so thought it worth reporting, but I'll understand if it's deemed not worth fixing!

Do let me know if you need any more information from me here.

Thanks Sam

PS Sorry for the lack of formatting - I'm sending this as plain text as my original HTML emails was rejected as possible spam by your mailserver.

Sam Partington Senior Developer

www.whiteoctober.co.uk
Office: +44 (0)1865 920 707

This email message and any attachments are confidential and solely for the use of the intended recipient. If you are not the intended recipient, you have received this message in error. Please notify us immediately and delete the message from your computer. You should not distribute, copy or disclose its contents to any other person. Any views or opinions expressed in this email are solely those of the author and do not necessarily represent those of White October Limited. White October is a private limited company registered in England & Wales under registration number 3982889. The company’s registered office is at 264 Banbury Road, Oxford, OX2 7DY.

Nicolas Morey-Chaisemartin· Aug 11, 2017, 17:14 UTC · re: Sam Partington · lore

[PATCH] stash: clean untracked files before reset

If calling git stash -u on a repo that contains a file that is not ignored any more due to a current modification of the gitignore file, this file is stashed but not remove from the working tree. This is due to git-stash first doing a reset --hard which clears the .gitignore file modification and the call git clean, leaving the file untouched. This causes git stash pop to fail due to the file existing.

This patch simply switches the order between cleaning and resetting and adds a test for this usecase.

Signed-off-by: Nicolas Morey-Chaisemartin <nicolas@morey-chaisemartin.com>
Reported-by: Sam Partington <sam@whiteoctober.co.uk>
---
 git-stash.sh                       | 11 ++++++-----
 t/t3905-stash-include-untracked.sh | 18 ++++++++++++++++++
 2 files changed, 24 insertions(+), 5 deletions(-)
diff --git a/git-stash.sh b/git-stash.sh
index 9b6c2da7b..39083b4d9 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -300,6 +300,12 @@ push_stash () {
 
 	if test -z "$patch_mode"
 	then
+		test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
+		if test -n "$untracked"
+		then
+			git clean --force --quiet -d $CLEAN_X_OPTION -- "$@"
+		fi
+
 		if test $# != 0
 		then
 			git reset -q -- "$@"
@@ -309,11 +315,6 @@ push_stash () {
 		else
 			git reset --hard -q
 		fi
-		test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
-		if test -n "$untracked"
-		then
-			git clean --force --quiet -d $CLEAN_X_OPTION -- "$@"
-		fi
 
 		if test "$keep_index" = "t" && test -n "$i_tree"
 		then
diff --git a/t/t3905-stash-include-untracked.sh b/t/t3905-stash-include-untracked.sh
index 193adc7b6..c1f84d3d5 100755
--- a/t/t3905-stash-include-untracked.sh
+++ b/t/t3905-stash-include-untracked.sh
@@ -211,4 +211,22 @@ test_expect_success 'stash push with $IFS character' '
 	test_path_is_file bar
 '
 
+cat > .gitignore <<EOF
+ignored
+ignored.d/*
+EOF
+
+test_expect_success 'stash previously ignored file' '
+which git &&
+	git reset HEAD &&
+	git add .gitignore &&
+	git commit -m "Add .gitignore" && 
+	>ignored.d/foo &&
+	echo "!ignored.d/foo" >> .gitignore &&
+	git stash save --include-untracked &&
+	test_path_is_missing ignored.d/foo &&
+	git stash pop &&
+	test_path_is_file ignored.d/foo
+'
+
 test_done
-- 
2.14.0.1.gd9597ce13
Junio C Hamano· Aug 11, 2017, 22:11 UTC · re: Nicolas Morey-Chaisemartin · lore

Re: [PATCH] stash: clean untracked files before reset

Nicolas Morey-Chaisemartin <nicolas@morey-chaisemartin.com> writes:
Show 13 quoted lines
> If calling git stash -u on a repo that contains a file that is not
> ignored any more due to a current modification of the gitignore file,
> this file is stashed but not remove from the working tree.
> This is due to git-stash first doing a reset --hard which clears the
> .gitignore file modification and the call git clean, leaving the file
> untouched.
> This causes git stash pop to fail due to the file existing.
>
> This patch simply switches the order between cleaning and resetting
> and adds a test for this usecase.
>
> Signed-off-by: Nicolas Morey-Chaisemartin <nicolas@morey-chaisemartin.com>
> Reported-by: Sam Partington <sam@whiteoctober.co.uk>

These two lines are the other way around; a report/finding was made and then you wrote a fix, which is signed-off by you.

I tried to think of a scenario where it is more desirable to use the contents of the .gitignore file before modification gets stash away, but I came up empty, so let's hope that this change will not make 50% people happier while making the other 50% sadder.

Show 49 quoted lines
> ---
>  git-stash.sh                       | 11 ++++++-----
>  t/t3905-stash-include-untracked.sh | 18 ++++++++++++++++++
>  2 files changed, 24 insertions(+), 5 deletions(-)
>
> diff --git a/git-stash.sh b/git-stash.sh
> index 9b6c2da7b..39083b4d9 100755
> --- a/git-stash.sh
> +++ b/git-stash.sh
> @@ -300,6 +300,12 @@ push_stash () {
>  
>  	if test -z "$patch_mode"
>  	then
> +		test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
> +		if test -n "$untracked"
> +		then
> +			git clean --force --quiet -d $CLEAN_X_OPTION -- "$@"
> +		fi
> +
>  		if test $# != 0
>  		then
>  			git reset -q -- "$@"
> @@ -309,11 +315,6 @@ push_stash () {
>  		else
>  			git reset --hard -q
>  		fi
> -		test "$untracked" = "all" && CLEAN_X_OPTION=-x || CLEAN_X_OPTION=
> -		if test -n "$untracked"
> -		then
> -			git clean --force --quiet -d $CLEAN_X_OPTION -- "$@"
> -		fi
>  
>  		if test "$keep_index" = "t" && test -n "$i_tree"
>  		then
> diff --git a/t/t3905-stash-include-untracked.sh b/t/t3905-stash-include-untracked.sh
> index 193adc7b6..c1f84d3d5 100755
> --- a/t/t3905-stash-include-untracked.sh
> +++ b/t/t3905-stash-include-untracked.sh
> @@ -211,4 +211,22 @@ test_expect_success 'stash push with $IFS character' '
>  	test_path_is_file bar
>  '
>  
> +cat > .gitignore <<EOF
> +ignored
> +ignored.d/*
> +EOF
> +
> +test_expect_success 'stash previously ignored file' '
> +which git &&
I will remove this line while queuing, though.
Show 12 quoted lines
> +	git reset HEAD &&
> +	git add .gitignore &&
> +	git commit -m "Add .gitignore" && 
> +	>ignored.d/foo &&
> +	echo "!ignored.d/foo" >> .gitignore &&
> +	git stash save --include-untracked &&
> +	test_path_is_missing ignored.d/foo &&
> +	git stash pop &&
> +	test_path_is_file ignored.d/foo
> +'
> +
>  test_done
Will queue.  Thanks.
Kevin Daudt· Aug 14, 2017, 04:47 UTC · re: Sam Partington · lore

Re: Bug when stashing previously-ignored file plus associated .gitignore change

On Fri, Aug 11, 2017 at 04:55:38PM +0100, Sam Partington wrote:
Show 62 quoted lines
> Hi there,
> 
> I'm running git 2.7.4 on Ubuntu 16.04.  I've found a couple of
> problems when "un-ignoring" files in tandem with git stash.
> 
> Here's how to reproduce:
> 
> Say you have a project using git, with a .gitignore file which
> contains the following line:
> 
> bin/*
> 
> You can then see the problems by doing this:
> 
> $ touch bin/mynewfile # this file will be ignored at this point > 
> and then updating .gitignore to look like this (adding that second line):
> 
> bin/*
> !bin/mynewfile
> 
> So far, so good; the new file is no longer ignored.
> 
> But now, try stashing the changes and including untracked files in the stash:
> 
> $ git stash save -u
> 
> Here's the first problem, bin/mynewfile is still there:
> 
> $ ls bin/mynewfile
> bin/mynewfile
> 
> But you'd expect it to not be there and be in the stash, I think.
> This is what would normally happen with the untracked-files option for
> git stash.
> 
> This leads to the second problem - you can't now pop the stash:
> 
> $ git stash pop
> bin/mynewfile already exists, no checkout
> Could not restore untracked files from stash
> 
> If you want to apply the stash, you have to remove the file:
> 
> $ rm bin/mynewfile
> $ git stash pop # this works, and re-creates bin/mynewfile
> 
> This is quite an unusual edge case, but I have hit it two or three
> times now and so thought it worth reporting, but I'll understand if
> it's deemed not worth fixing!
> 
> Do let me know if you need any more information from me here.
> 
> Thanks
> Sam
> 
> PS Sorry for the lack of formatting - I'm sending this as plain text
> as my original HTML emails was rejected as possible spam by your
> mailserver.
> 
> Sam Partington
> Senior Developer
> 
Hello Sam,

Is it the case that you did not commit the addition of '!bin/mynewfile' yet? I suspect that by running git stash save -u, you also are stashing this addition to the .gitigore file. Can you confirm this?

Kevin
Sam Partington· Aug 14, 2017, 07:38 UTC · re: Kevin Daudt · lore

Re: Bug when stashing previously-ignored file plus associated .gitignore change

Hello Kevin,

Yes, you're right - I didn't commit the change to the .gitignore file, so that addition is also being stashed.

Thanks Sam Sam Partington Senior Developer

www.whiteoctober.co.uk
Office: +44 (0)1865 920 707

This email message and any attachments are confidential and solely for the use of the intended recipient. If you are not the intended recipient, you have received this message in error. Please notify us immediately and delete the message from your computer. You should not distribute, copy or disclose its contents to any other person. Any views or opinions expressed in this email are solely those of the author and do not necessarily represent those of White October Limited. White October is a private limited company registered in England & Wales under registration number 3982889. The company’s registered office is at 264 Banbury Road, Oxford, OX2 7DY.

On 14 August 2017 at 05:47, Kevin Daudt <me@ikke.info> wrote:
Show 71 quoted lines
> On Fri, Aug 11, 2017 at 04:55:38PM +0100, Sam Partington wrote:
>> Hi there,
>>
>> I'm running git 2.7.4 on Ubuntu 16.04.  I've found a couple of
>> problems when "un-ignoring" files in tandem with git stash.
>>
>> Here's how to reproduce:
>>
>> Say you have a project using git, with a .gitignore file which
>> contains the following line:
>>
>> bin/*
>>
>> You can then see the problems by doing this:
>>
>> $ touch bin/mynewfile # this file will be ignored at this point >
>> and then updating .gitignore to look like this (adding that second line):
>>
>> bin/*
>> !bin/mynewfile
>>
>> So far, so good; the new file is no longer ignored.
>>
>> But now, try stashing the changes and including untracked files in the stash:
>>
>> $ git stash save -u
>>
>> Here's the first problem, bin/mynewfile is still there:
>>
>> $ ls bin/mynewfile
>> bin/mynewfile
>>
>> But you'd expect it to not be there and be in the stash, I think.
>> This is what would normally happen with the untracked-files option for
>> git stash.
>>
>> This leads to the second problem - you can't now pop the stash:
>>
>> $ git stash pop
>> bin/mynewfile already exists, no checkout
>> Could not restore untracked files from stash
>>
>> If you want to apply the stash, you have to remove the file:
>>
>> $ rm bin/mynewfile
>> $ git stash pop # this works, and re-creates bin/mynewfile
>>
>> This is quite an unusual edge case, but I have hit it two or three
>> times now and so thought it worth reporting, but I'll understand if
>> it's deemed not worth fixing!
>>
>> Do let me know if you need any more information from me here.
>>
>> Thanks
>> Sam
>>
>> PS Sorry for the lack of formatting - I'm sending this as plain text
>> as my original HTML emails was rejected as possible spam by your
>> mailserver.
>>
>> Sam Partington
>> Senior Developer
>>
>
> Hello Sam,
>
> Is it the case that you did not commit the addition of '!bin/mynewfile'
> yet? I suspect that by running git stash save -u, you also are stashing
> this addition to the .gitigore file. Can you confirm this?
>
> Kevin

← back to recent threads