@matheusbsbs / matheus-bagliano / commits / df53c80822
Fork 0

stash: make sure we have a valid index before writing it

In 'do_apply_stash()' we refresh the index in the end. Since 34933d0eff ("stash: make sure to write refreshed cache", 2019-09-11), we also write that refreshed index when --quiet is given to 'git stash apply'. However if '--index' is not given to 'git stash apply', we also discard the index in the else clause just before. We need to do so because we use an external 'git update-index --add --stdin', which leads to an out of date in-core index. Later we call 'refresh_and_write_cache', which now leads to writing the discarded index, which means we essentially write an empty index file. This is obviously not correct, or the behaviour the user wanted. We should not modify the users index without being asked to do so. Make sure to re-read the index after discarding the current in-core index, to avoid dealing with outdated information. Instead we could also drop the 'discard_cache()' + 'read_cache()', however that would make it easy to fall into the same trap as 34933d0eff did, so it's better to avoid that. We can also drop the 'refresh_and_write_cache' completely in the quiet case. Previously in legacy stash we relied on 'git status' to refresh the index after calling 'git read-tree' when '--index' was passed to 'git apply'. However the 'reset_tree()' call that replaced 'git read-tree' always passes options that are equivalent to '-m', making the refresh of the index unnecessary. Reported-by: Grzegorz Rajchman <rayman17@gmail.com> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Gummerer committed Nov 13, 2019 at 15:01 UTC df53c80822735ddd30638ba77be3face438d5ad8
2 files changed +9 -5
builtin/stash.c
+3 -4
@@ -481,13 +481,12 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
481 if (ret)
482 return -1;
483
484 + /* read back the result of update_index() back from the disk */
485 discard_cache();
486 + read_cache();
487 }
488
487 - if (quiet) {
488 - if (refresh_and_write_cache(REFRESH_QUIET, 0, 0))
489 - warning("could not refresh index");
490 - } else {
489 + if (!quiet) {
490 struct child_process cp = CHILD_PROCESS_INIT;
491
492 /*
t/t3903-stash.sh
+6 -1
@@ -232,8 +232,11 @@ test_expect_success 'save -q is quiet' '
232 test_must_be_empty output.out
233 '
234
235 -test_expect_success 'pop -q is quiet' '
235 +test_expect_success 'pop -q works and is quiet' '
236 git stash pop -q >output.out 2>&1 &&
237 + echo bar >expect &&
238 + git show :file >actual &&
239 + test_cmp expect actual &&
240 test_must_be_empty output.out
241 '
242
@@ -242,6 +245,8 @@ test_expect_success 'pop -q --index works and is quiet' '
245 git add file &&
246 git stash save --quiet &&
247 git stash pop -q --index >output.out 2>&1 &&
248 + git diff-files file2 >file2.diff &&
249 + test_must_be_empty file2.diff &&
250 test foo = "$(git show :file)" &&
251 test_must_be_empty output.out
252 '