git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] stash: fix "--staged" with binary files
@ 2024-04-22 10:28 Adam Johnson via GitGitGadget
  2024-04-22 19:00 ` Junio C Hamano
  2024-04-23 21:44 ` Jeff King
  0 siblings, 2 replies; 4+ messages in thread
From: Adam Johnson via GitGitGadget @ 2024-04-22 10:28 UTC (permalink / raw)
  To: git; +Cc: Jeff King, Randall S. Becker, Adam Johnson, Adam Johnson

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>
---
    stash: fix "--staged" with binary files

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1722%2Fadamchainz%2Faj%2Fstash-binary-fix-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1722/adamchainz/aj/stash-binary-fix-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1722

 builtin/stash.c  | 4 ++--
 t/t3903-stash.sh | 9 +++++++++
 2 files changed, 11 insertions(+), 2 deletions(-)

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
+'
+
 test_expect_success 'dont assume push with non-option args' '
 	test_must_fail git stash -q drop 2>err &&
 	test_grep -e "subcommand wasn'\''t specified; '\''push'\'' can'\''t be assumed due to unexpected token '\''drop'\''" err

base-commit: ae3196a5ea84a9e88991d576020cf66512487088
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-04-23 21:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-22 10:28 [PATCH] stash: fix "--staged" with binary files Adam Johnson via GitGitGadget
2024-04-22 19:00 ` Junio C Hamano
2024-04-22 20:48   ` Adam Johnson
2024-04-23 21:44 ` Jeff King

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).