stash: make internal resets quiet and refresh index

Add the options '-q' and '--refresh' to the 'git reset' executed in 'reset_head()', and '--refresh' to the 'git reset -q' executed in 'do_push_stash(...)'. 'stash' is implemented such that git commands invoked as part of it (e.g., 'clean', 'read-tree', 'reset', etc.) have their informational output silenced. However, the 'reset' in 'reset_head()' is *not* called with '-q', leading to the potential for a misleading printout from 'git stash apply --index' if the stash included a removed file: Unstaged changes after reset: D <deleted file> Not only is this confusing in its own right (since, after the reset, 'git stash' execution would stage the deletion in the index), it would be printed even when the stash was applied with the '-q' option. As a result, the messaging is removed entirely by calling 'git status' with '-q'. Additionally, because the default behavior of 'git reset -q' is to skip refreshing the index, but later operations in 'git stash' subcommands expect a non-stale index, enable '--refresh' as well. Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Victoria Dye <vdye@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Victoria Dye committed Mar 15, 2022 at 01:49 UTC 4b8b0f6fa2778c1f9c373620e3f07787543914c6
2 files changed +36 -2
builtin/stash.c
+3 -2
@@ -310,7 +310,7 @@ static int reset_head(void)
310 * API for resetting.
311 */
312 cp.git_cmd = 1;
313 - strvec_push(&cp.args, "reset");
313 + strvec_pushl(&cp.args, "reset", "--quiet", "--refresh", NULL);
314
315 return run_command(&cp);
316 }
@@ -1633,7 +1633,8 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
1633 struct child_process cp = CHILD_PROCESS_INIT;
1634
1635 cp.git_cmd = 1;
1636 - strvec_pushl(&cp.args, "reset", "-q", "--", NULL);
1636 + strvec_pushl(&cp.args, "reset", "-q", "--refresh", "--",
1637 + NULL);
1638 add_pathspecs(&cp.args, ps);
1639 if (run_command(&cp)) {
1640 ret = -1;
t/t3903-stash.sh
+33
@@ -261,6 +261,18 @@ test_expect_success 'apply -q is quiet' '
261 test_must_be_empty output.out
262 '
263
264 +test_expect_success 'apply --index -q is quiet' '
265 + # Added file, deleted file, modified file all staged for commit
266 + echo foo >new-file &&
267 + echo test >file &&
268 + git add new-file file &&
269 + git rm other-file &&
270 +
271 + git stash &&
272 + git stash apply --index -q >output.out 2>&1 &&
273 + test_must_be_empty output.out
274 +'
275 +
276 test_expect_success 'save -q is quiet' '
277 git stash save --quiet >output.out 2>&1 &&
278 test_must_be_empty output.out
@@ -291,6 +303,27 @@ test_expect_success 'drop -q is quiet' '
303 test_must_be_empty output.out
304 '
305
306 +test_expect_success 'stash push -q --staged refreshes the index' '
307 + git reset --hard &&
308 + echo test >file &&
309 + git add file &&
310 + git stash push -q --staged &&
311 + git diff-files >output.out &&
312 + test_must_be_empty output.out
313 +'
314 +
315 +test_expect_success 'stash apply -q --index refreshes the index' '
316 + echo test >other-file &&
317 + git add other-file &&
318 + echo another-change >other-file &&
319 + git diff-files >expect &&
320 + git stash &&
321 +
322 + git stash apply -q --index &&
323 + git diff-files >actual &&
324 + test_cmp expect actual
325 +'
326 +
327 test_expect_success 'stash -k' '
328 echo bar3 >file &&
329 echo bar4 >file2 &&