Skip to content

Commit 5fb7686

Browse files
adamchainzgitster
authored andcommitted
stash: fix "--staged" with binary files
"git stash --staged" errors out when given binary files, after saving the stash. This behaviour dates back to the addition of the feature in 41a28eb (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 <[email protected]> Helped-By: Randall S. Becker <[email protected]> Signed-off-by: Adam Johnson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7380a72 commit 5fb7686

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

builtin/stash.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,8 +1147,8 @@ static int stash_staged(struct stash_info *info, struct strbuf *out_patch,
11471147
}
11481148

11491149
cp_diff_tree.git_cmd = 1;
1150-
strvec_pushl(&cp_diff_tree.args, "diff-tree", "-p", "-U1", "HEAD",
1151-
oid_to_hex(&info->w_tree), "--", NULL);
1150+
strvec_pushl(&cp_diff_tree.args, "diff-tree", "-p", "--binary",
1151+
"-U1", "HEAD", oid_to_hex(&info->w_tree), "--", NULL);
11521152
if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {
11531153
ret = -1;
11541154
goto done;

t/t3903-stash.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,15 @@ test_expect_success 'stash --staged' '
318318
test bar,bar4 = $(cat file),$(cat file2)
319319
'
320320

321+
test_expect_success 'stash --staged with binary file' '
322+
printf "\0" >file &&
323+
git add file &&
324+
git stash --staged &&
325+
git stash pop &&
326+
printf "\0" >expect &&
327+
test_cmp expect file
328+
'
329+
321330
test_expect_success 'dont assume push with non-option args' '
322331
test_must_fail git stash -q drop 2>err &&
323332
test_i18ngrep -e "subcommand wasn'\''t specified; '\''push'\'' can'\''t be assumed due to unexpected token '\''drop'\''" err

0 commit comments

Comments
 (0)