merge: fix save_state() to work when there are stat-dirty files

When there are stat-dirty files, but no files are modified, `git stash create` exits with unsuccessful status. This causes merge to fail. Copy some code from sequencer.c's create_autostash to refresh the index first to avoid this problem. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Jul 23, 2022 at 01:53 UTC 1369f1475b998b0022f9bdf3a084ef6e590800aa
2 files changed +19
builtin/merge.c
+8
@@ -313,8 +313,16 @@ static int save_state(struct object_id *stash)
313 int len;
314 struct child_process cp = CHILD_PROCESS_INIT;
315 struct strbuf buffer = STRBUF_INIT;
316 + struct lock_file lock_file = LOCK_INIT;
317 + int fd;
318 int rc = -1;
319
320 + fd = repo_hold_locked_index(the_repository, &lock_file, 0);
321 + refresh_cache(REFRESH_QUIET);
322 + if (0 <= fd)
323 + repo_update_index_if_able(the_repository, &lock_file);
324 + rollback_lock_file(&lock_file);
325 +
326 strvec_pushl(&cp.args, "stash", "create", NULL);
327 cp.out = -1;
328 cp.git_cmd = 1;
t/t6424-merge-unrelated-index-changes.sh
+11
@@ -275,6 +275,17 @@ test_expect_success 'subtree' '
275 test_path_is_missing .git/MERGE_HEAD
276 '
277
278 +test_expect_success 'avoid failure due to stat-dirty files' '
279 + git reset --hard &&
280 + git checkout B^0 &&
281 +
282 + # Make "a" be stat-dirty
283 + test-tool chmtime =+1 a &&
284 +
285 + # stat-dirty file should not prevent stash creation in builtin/merge.c
286 + git merge -s resolve -s recursive D^0
287 +'
288 +
289 test_expect_success 'with multiple strategies, recursive or ort failure do not early abort' '
290 git reset --hard &&
291 git checkout B^0 &&