gitgitgadget / git

GitGitGadget's Git fork. Open Pull Requests here to submit them to the Git mailing list
https://gitgitgadget.github.io/
Other
213 stars 134 forks source link

stash: fix "--staged" with binary files #1722

Closed adamchainz closed 5 months ago

adamchainz commented 6 months ago

CC: Jeff King peff@peff.net, Randall S. Becker randall.becker@nexbridge.ca

gitgitgadget[bot] commented 6 months ago

There are issues in commit 44b91e040ce8c79a3aaf2a9cc08c5bcea362c48b: stash: fix "--staged" with binary files Commit not signed off

adamchainz commented 6 months ago

/submit

gitgitgadget[bot] commented 6 months ago

Submitted as pull.1722.git.1713781694490.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-1722/adamchainz/aj/stash-binary-fix-v1

To fetch this version to local tag pr-1722/adamchainz/aj/stash-binary-fix-v1:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-1722/adamchainz/aj/stash-binary-fix-v1
gitgitgadget[bot] commented 6 months ago

On the Git mailing list, Junio C Hamano wrote (reply to this):

"Adam Johnson via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Adam Johnson <me@adamj.eu>
>
> "git stash --staged" would crash with binary files, after saving the stash.

"would crash with" -> "errors out when given", probably.

We would be better off reserving the word "crash" to refer to an
uncontrolled exit with non-zero status of the command, possibly
killed via system signals like SEGV and SIGBUS.  In this case, I
suspect that you would get an "error:" from underlying apply
machinery, which leads the caller to exit with a non-zero status
due to an error.

> This behaviour dates back to the addition of the feature in 41a28eb6c1
> (stash: implement '--staged' option for 'push' and 'save', 2021-10-18).

When you find the commit that introduced the problem you are fixing,
especially when the author of the commit is still active on the
mailing list, it would be nice to give a carbon-copy of the message
to them.

> Adding the "--binary" option of "diff-tree" fixes this. The "diff-tree" call
> in stash_patch() also omits "--binary", but that is fine since binary files
> cannot be selected interactively.

I love seeing an explanation like this in a proposed commit log
message.  It is concise but very clear at the same time.  It is so
clearly written that anybody who is reasonably familiar with the
code does not even need to look at the patch text itself to see what
the actual fix would look like.

Very nicely explained.

> Helped-By: Jeff King <peff@peff.net>
> Helped-By: Randall S. Becker <randall.becker@nexbridge.ca>
> Signed-off-by: Adam Johnson <me@adamj.eu>
> ---

Will queue.

Thanks.

> diff --git a/builtin/stash.c b/builtin/stash.c
> index 062be1fbc07..7751bca868e 100644
> --- a/builtin/stash.c
> +++ b/builtin/stash.c
> @@ -1205,8 +1205,8 @@ static int stash_staged(struct stash_info *info, struct strbuf *out_patch,
>   }
>  
>   cp_diff_tree.git_cmd = 1;
> - strvec_pushl(&cp_diff_tree.args, "diff-tree", "-p", "-U1", "HEAD",
> -          oid_to_hex(&info->w_tree), "--", NULL);
> + strvec_pushl(&cp_diff_tree.args, "diff-tree", "-p", "--binary",
> +          "-U1", "HEAD", oid_to_hex(&info->w_tree), "--", NULL);
>   if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {
>       ret = -1;
>       goto done;
> diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
> index 00db82fb245..a7f71f8126f 100755
> --- a/t/t3903-stash.sh
> +++ b/t/t3903-stash.sh
> @@ -393,6 +393,15 @@ test_expect_success 'stash --staged' '
>   test bar,bar4 = $(cat file),$(cat file2)
>  '
>  
> +test_expect_success 'stash --staged with binary file' '
> + printf "\0" >file &&
> + git add file &&
> + git stash --staged &&
> + git stash pop &&
> + printf "\0" >expect &&
> + test_cmp expect file
> +'
gitgitgadget[bot] commented 6 months ago

On the Git mailing list, "Adam Johnson" wrote (reply to this):

Thank you for the feedback, that was clearly explained. I will try to remember to CC more relevant folks in the future.

On Mon, 22 Apr 2024, at 20:00, Junio C Hamano wrote:
> "Adam Johnson via GitGitGadget" <gitgitgadget@gmail.com> writes:
> 
> > From: Adam Johnson <me@adamj.eu>
> >
> > "git stash --staged" would crash with binary files, after saving the stash.
> 
> "would crash with" -> "errors out when given", probably.
> 
> We would be better off reserving the word "crash" to refer to an
> uncontrolled exit with non-zero status of the command, possibly
> killed via system signals like SEGV and SIGBUS.  In this case, I
> suspect that you would get an "error:" from underlying apply
> machinery, which leads the caller to exit with a non-zero status
> due to an error.
> 
> > This behaviour dates back to the addition of the feature in 41a28eb6c1
> > (stash: implement '--staged' option for 'push' and 'save', 2021-10-18).
> 
> When you find the commit that introduced the problem you are fixing,
> especially when the author of the commit is still active on the
> mailing list, it would be nice to give a carbon-copy of the message
> to them.
> 
> > Adding the "--binary" option of "diff-tree" fixes this. The "diff-tree" call
> > in stash_patch() also omits "--binary", but that is fine since binary files
> > cannot be selected interactively.
> 
> I love seeing an explanation like this in a proposed commit log
> message.  It is concise but very clear at the same time.  It is so
> clearly written that anybody who is reasonably familiar with the
> code does not even need to look at the patch text itself to see what
> the actual fix would look like.
> 
> Very nicely explained.
> 
> > Helped-By: Jeff King <peff@peff.net>
> > Helped-By: Randall S. Becker <randall.becker@nexbridge.ca>
> > Signed-off-by: Adam Johnson <me@adamj.eu>
> > ---
> 
> Will queue.
> 
> Thanks.
> 
> > diff --git a/builtin/stash.c b/builtin/stash.c
> > index 062be1fbc07..7751bca868e 100644
> > --- a/builtin/stash.c
> > +++ b/builtin/stash.c
> > @@ -1205,8 +1205,8 @@ static int stash_staged(struct stash_info *info, struct strbuf *out_patch,
> >  }
> >  
> >  cp_diff_tree.git_cmd = 1;
> > - strvec_pushl(&cp_diff_tree.args, "diff-tree", "-p", "-U1", "HEAD",
> > -      oid_to_hex(&info->w_tree), "--", NULL);
> > + strvec_pushl(&cp_diff_tree.args, "diff-tree", "-p", "--binary",
> > +      "-U1", "HEAD", oid_to_hex(&info->w_tree), "--", NULL);
> >  if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {
> >  ret = -1;
> >  goto done;
> > diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
> > index 00db82fb245..a7f71f8126f 100755
> > --- a/t/t3903-stash.sh
> > +++ b/t/t3903-stash.sh
> > @@ -393,6 +393,15 @@ test_expect_success 'stash --staged' '
> >  test bar,bar4 = $(cat file),$(cat file2)
> >  '
> >  
> > +test_expect_success 'stash --staged with binary file' '
> > + printf "\0" >file &&
> > + git add file &&
> > + git stash --staged &&
> > + git stash pop &&
> > + printf "\0" >expect &&
> > + test_cmp expect file
> > +'
> 
> 
gitgitgadget[bot] commented 6 months ago

This branch is now known as aj/stash-staged-fix.

gitgitgadget[bot] commented 6 months ago

This patch series was integrated into seen via https://github.com/git/git/commit/840b07181b81ae4c5072696153de2872b786b3c8.

gitgitgadget[bot] commented 6 months ago

There was a status update in the "New Topics" section about the branch aj/stash-staged-fix on the Git mailing list:

"git stash -S" did not handle binary files correctly, which has
been corrected.

Will merge to 'next'.
source: <pull.1722.git.1713781694490.gitgitgadget@gmail.com>
gitgitgadget[bot] commented 6 months ago

On the Git mailing list, Jeff King wrote (reply to this):

On Mon, Apr 22, 2024 at 10:28:14AM +0000, Adam Johnson via GitGitGadget wrote:

> From: Adam Johnson <me@adamj.eu>
> 
> "git stash --staged" would crash with binary files, after saving the stash.
> This behaviour dates back to the addition of the feature in 41a28eb6c1
> (stash: implement '--staged' option for 'push' and 'save', 2021-10-18).
> Adding the "--binary" option of "diff-tree" fixes this. The "diff-tree" call
> in stash_patch() also omits "--binary", but that is fine since binary files
> cannot be selected interactively.
> 
> Helped-By: Jeff King <peff@peff.net>
> Helped-By: Randall S. Becker <randall.becker@nexbridge.ca>
> Signed-off-by: Adam Johnson <me@adamj.eu>

I had to dig in the archive to remember what I might have helped with. ;)

The patch looks good to me, though one thing I noticed:

> +test_expect_success 'stash --staged with binary file' '
> + printf "\0" >file &&
> + git add file &&
> + git stash --staged &&
> + git stash pop &&
> + printf "\0" >expect &&
> + test_cmp expect file
> +'

I wonder if test_cmp would ever have problems with binary files on any
platforms (and yes, I was the one who suggested using it). We use "diff"
on most platforms, but a few (like old SunOS) set GIT_TEST_CMP to "cmp"
because they don't have "diff -u" at all. I'm content to leave it as-is
until somebody turns up a platform that complains, and then the escape
hatch is "OK, so set GIT_TEST_CMP=cmp".

-Peff
gitgitgadget[bot] commented 6 months ago

This patch series was integrated into seen via https://github.com/git/git/commit/c27d80e3aa318351c709d7b7010966fd0fe4bba7.

gitgitgadget[bot] commented 6 months ago

This patch series was integrated into seen via https://github.com/git/git/commit/6b2e32c3884775934955d9abaf1559958b2767f1.

gitgitgadget[bot] commented 6 months ago

This patch series was integrated into next via https://github.com/git/git/commit/d49e9dade0299bb0140c82600d63641b183fc8b3.

gitgitgadget[bot] commented 6 months ago

There was a status update in the "Cooking" section about the branch aj/stash-staged-fix on the Git mailing list:

"git stash -S" did not handle binary files correctly, which has
been corrected.

Will cook in 'next'.
source: <pull.1722.git.1713781694490.gitgitgadget@gmail.com>
gitgitgadget[bot] commented 6 months ago

This patch series was integrated into seen via https://github.com/git/git/commit/89c6d6c74c61ae7013215b31b8612fd708899fbf.

gitgitgadget[bot] commented 6 months ago

This patch series was integrated into seen via https://github.com/git/git/commit/b61974d074de7405dd090d692f24d5f8391ab968.

gitgitgadget[bot] commented 5 months ago

This patch series was integrated into seen via https://github.com/git/git/commit/35231f7570168adb951d91ca7ec0d73856e3cc6d.

gitgitgadget[bot] commented 5 months ago

This patch series was integrated into seen via https://github.com/git/git/commit/8b9c63570b719494532a57cc155a56e4278895d7.

gitgitgadget[bot] commented 5 months ago

There was a status update in the "Cooking" section about the branch aj/stash-staged-fix on the Git mailing list:

"git stash -S" did not handle binary files correctly, which has
been corrected.

Will cook in 'next'.
source: <pull.1722.git.1713781694490.gitgitgadget@gmail.com>
gitgitgadget[bot] commented 5 months ago

This patch series was integrated into next via https://github.com/git/git/commit/90f6b5a597c083d898fcc5d3ae3dbb50779b72a6.

gitgitgadget[bot] commented 5 months ago

This patch series was integrated into seen via https://github.com/git/git/commit/90f6b5a597c083d898fcc5d3ae3dbb50779b72a6.

gitgitgadget[bot] commented 5 months ago

This patch series was integrated into master via https://github.com/git/git/commit/90f6b5a597c083d898fcc5d3ae3dbb50779b72a6.

gitgitgadget[bot] commented 5 months ago

Closed via 90f6b5a597c083d898fcc5d3ae3dbb50779b72a6.