Skip to content

Commit f8aee85

Browse files
committed
Merge branch 'tg/stash-keep-index-with-removed-paths'
"git stash --keep-index" did not work correctly on paths that have been removed, which has been fixed. * tg/stash-keep-index-with-removed-paths: stash: fix handling removed files with --keep-index
2 parents 984da7f + b932f6a commit f8aee85

File tree

2 files changed

+16
-23
lines changed

2 files changed

+16
-23
lines changed

builtin/stash.c

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,30 +1391,16 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
13911391
}
13921392

13931393
if (keep_index == 1 && !is_null_oid(&info.i_tree)) {
1394-
struct child_process cp_ls = CHILD_PROCESS_INIT;
1395-
struct child_process cp_checkout = CHILD_PROCESS_INIT;
1396-
struct strbuf out = STRBUF_INIT;
1397-
1398-
if (reset_tree(&info.i_tree, 0, 1)) {
1399-
ret = -1;
1400-
goto done;
1401-
}
1402-
1403-
cp_ls.git_cmd = 1;
1404-
argv_array_pushl(&cp_ls.args, "ls-files", "-z",
1405-
"--modified", "--", NULL);
1406-
1407-
add_pathspecs(&cp_ls.args, ps);
1408-
if (pipe_command(&cp_ls, NULL, 0, &out, 0, NULL, 0)) {
1409-
ret = -1;
1410-
goto done;
1411-
}
1394+
struct child_process cp = CHILD_PROCESS_INIT;
14121395

1413-
cp_checkout.git_cmd = 1;
1414-
argv_array_pushl(&cp_checkout.args, "checkout-index",
1415-
"-z", "--force", "--stdin", NULL);
1416-
if (pipe_command(&cp_checkout, out.buf, out.len, NULL,
1417-
0, NULL, 0)) {
1396+
cp.git_cmd = 1;
1397+
argv_array_pushl(&cp.args, "checkout", "--no-overlay",
1398+
oid_to_hex(&info.i_tree), "--", NULL);
1399+
if (!ps->nr)
1400+
argv_array_push(&cp.args, ":/");
1401+
else
1402+
add_pathspecs(&cp.args, ps);
1403+
if (run_command(&cp)) {
14181404
ret = -1;
14191405
goto done;
14201406
}

t/t3903-stash.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,4 +1234,11 @@ test_expect_success 'stash works when user.name and user.email are not set' '
12341234
)
12351235
'
12361236

1237+
test_expect_success 'stash --keep-index with file deleted in index does not resurrect it on disk' '
1238+
test_commit to-remove to-remove &&
1239+
git rm to-remove &&
1240+
git stash --keep-index &&
1241+
test_path_is_missing to-remove
1242+
'
1243+
12371244
test_done

0 commit comments

Comments
 (0)