]> git.ipfire.org Git - thirdparty/git.git/commitdiff
stash: fix "--staged" with binary files
authorAdam Johnson <me@adamj.eu>
Mon, 22 Apr 2024 10:28:14 +0000 (10:28 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 22 Apr 2024 20:57:18 +0000 (13:57 -0700)
"git stash --staged" errors out when given 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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/stash.c
t/t3903-stash.sh

index 86cd0b456e7752d7ffdb578ce892ca17c4e9c6a9..f4ec21e5157efaec7fff0f2c49bd415d69bad691 100644 (file)
@@ -1147,8 +1147,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;
index 686747e55a301a661d75c63223a7d682c30dbd4b..a62b3a3d40bf6e00581526da1002c44ff18a7641 100755 (executable)
@@ -318,6 +318,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_i18ngrep -e "subcommand wasn'\''t specified; '\''push'\'' can'\''t be assumed due to unexpected token '\''drop'\''" err