merge-ort: write $GIT_DIR/AUTO_MERGE whenever we hit a conflict

There are a variety of questions users might ask while resolving conflicts: * What changes have been made since the previous (first) parent? * What changes are staged? * What is still unstaged? (or what is still conflicted?) * What changes did I make to resolve conflicts so far? The first three of these have simple answers: * git diff HEAD * git diff --cached * git diff There was no way to answer the final question previously. Adding one is trivial in merge-ort, since it works by creating a tree representing what should be written to the working copy complete with conflict markers. Simply write that tree to .git/AUTO_MERGE, allowing users to answer the fourth question with * git diff AUTO_MERGE I avoided using a name like "MERGE_AUTO", because that would be merge-specific (much like MERGE_HEAD, REBASE_HEAD, REVERT_HEAD, CHERRY_PICK_HEAD) and I wanted a name that didn't change depending on which type of operation the merge was part of. Ensure that paths which clean out other temporary operation-specific files (e.g. CHERRY_PICK_HEAD, MERGE_MSG, rebase-merge/ state directory) also clean out this AUTO_MERGE file. Signed-off-by: Elijah Newren <newren@gmail.com> Reviewed-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Mar 20, 2021 at 00:03 UTC 5291828df8386ebeb01039d1403d9f845d2f6e20
6 files changed +20
branch.c
+1
@@ -344,6 +344,7 @@ void remove_merge_branch_state(struct repository *r)
344 unlink(git_path_merge_rr(r));
345 unlink(git_path_merge_msg(r));
346 unlink(git_path_merge_mode(r));
347 + unlink(git_path_auto_merge(r));
348 save_autostash(git_path_merge_autostash(r));
349 }
350
builtin/rebase.c
+1
@@ -737,6 +737,7 @@ static int finish_rebase(struct rebase_options *opts)
737 int ret = 0;
738
739 delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
740 + unlink(git_path_auto_merge(the_repository));
741 apply_autostash(state_dir_path("autostash", opts));
742 close_object_store(the_repository->objects);
743 /*
merge-ort.c
+10
@@ -3496,6 +3496,9 @@ void merge_switch_to_result(struct merge_options *opt,
3496 {
3497 assert(opt->priv == NULL);
3498 if (result->clean >= 0 && update_worktree_and_index) {
3499 + const char *filename;
3500 + FILE *fp;
3501 +
3502 trace2_region_enter("merge", "checkout", opt->repo);
3503 if (checkout(opt, head, result->tree)) {
3504 /* failure to function */
@@ -3514,6 +3517,13 @@ void merge_switch_to_result(struct merge_options *opt,
3517 }
3518 opt->priv = NULL;
3519 trace2_region_leave("merge", "record_conflicted", opt->repo);
3520 +
3521 + trace2_region_enter("merge", "write_auto_merge", opt->repo);
3522 + filename = git_path_auto_merge(opt->repo);
3523 + fp = xfopen(filename, "w");
3524 + fprintf(fp, "%s\n", oid_to_hex(&result->tree->object.oid));
3525 + fclose(fp);
3526 + trace2_region_leave("merge", "write_auto_merge", opt->repo);
3527 }
3528
3529 if (display_update_msgs) {
path.c
+1
@@ -1534,5 +1534,6 @@ REPO_GIT_PATH_FUNC(merge_rr, "MERGE_RR")
1534 REPO_GIT_PATH_FUNC(merge_mode, "MERGE_MODE")
1535 REPO_GIT_PATH_FUNC(merge_head, "MERGE_HEAD")
1536 REPO_GIT_PATH_FUNC(merge_autostash, "MERGE_AUTOSTASH")
1537 +REPO_GIT_PATH_FUNC(auto_merge, "AUTO_MERGE")
1538 REPO_GIT_PATH_FUNC(fetch_head, "FETCH_HEAD")
1539 REPO_GIT_PATH_FUNC(shallow, "shallow")
path.h
+2
@@ -176,6 +176,7 @@ struct path_cache {
176 const char *merge_mode;
177 const char *merge_head;
178 const char *merge_autostash;
179 + const char *auto_merge;
180 const char *fetch_head;
181 const char *shallow;
182 };
@@ -191,6 +192,7 @@ const char *git_path_merge_rr(struct repository *r);
192 const char *git_path_merge_mode(struct repository *r);
193 const char *git_path_merge_head(struct repository *r);
194 const char *git_path_merge_autostash(struct repository *r);
195 +const char *git_path_auto_merge(struct repository *r);
196 const char *git_path_fetch_head(struct repository *r);
197 const char *git_path_shallow(struct repository *r);
198
sequencer.c
+5
@@ -2096,6 +2096,7 @@ static int do_pick_commit(struct repository *r,
2096 refs_delete_ref(get_main_ref_store(r), "", "CHERRY_PICK_HEAD",
2097 NULL, 0);
2098 unlink(git_path_merge_msg(r));
2099 + unlink(git_path_auto_merge(r));
2100 fprintf(stderr,
2101 _("dropping %s %s -- patch contents already upstream\n"),
2102 oid_to_hex(&commit->object.oid), msg.subject);
@@ -2451,6 +2452,8 @@ void sequencer_post_commit_cleanup(struct repository *r, int verbose)
2452 need_cleanup = 1;
2453 }
2454
2455 + unlink(git_path_auto_merge(r));
2456 +
2457 if (!need_cleanup)
2458 return;
2459
@@ -4111,6 +4114,7 @@ static int pick_commits(struct repository *r,
4114 unlink(rebase_path_stopped_sha());
4115 unlink(rebase_path_amend());
4116 unlink(git_path_merge_head(r));
4117 + unlink(git_path_auto_merge(r));
4118 delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
4119
4120 if (item->command == TODO_BREAK) {
@@ -4505,6 +4509,7 @@ static int commit_staged_changes(struct repository *r,
4509 return error(_("could not commit staged changes."));
4510 unlink(rebase_path_amend());
4511 unlink(git_path_merge_head(r));
4512 + unlink(git_path_auto_merge(r));
4513 if (final_fixup) {
4514 unlink(rebase_path_fixup_msg());
4515 unlink(rebase_path_squash_msg());