diff mbox series

[09/14] replay: very coarse worktree updating

Message ID 20230407072415.1360068-10-christian.couder@gmail.com (mailing list archive)
State New, archived
Headers show
Series Introduce new `git replay` command | expand

Commit Message

Christian Couder April 7, 2023, 7:24 a.m. UTC
From: Elijah Newren <newren@gmail.com>

In case of conflict, let's just update the worktree and index. And then
let's just die() as this command doesn't have ways to handle conflicts
yet.

Note that we might want to improve this behavior in the case of a bare
repository in a future commit.

We also have to lock the index only after all the commits have been
picked, so that in case of conflict the index is not already locked.
Note that this locking of the index will be removed in a following
commit as we will not want to modify it anymore.

Co-authored-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin/replay.c | 35 +++++++++++++++++++++++++++++------
 1 file changed, 29 insertions(+), 6 deletions(-)
diff mbox series

Patch

diff --git a/builtin/replay.c b/builtin/replay.c
index a331887d12..9c795c05a7 100644
--- a/builtin/replay.c
+++ b/builtin/replay.c
@@ -152,10 +152,6 @@  int cmd_replay(int argc, const char **argv, const char *prefix)
 	onto = peel_committish(onto_name);
 	strbuf_addf(&branch_name, "refs/heads/%s", argv[2]);
 
-	repo_hold_locked_index(the_repository, &lock, LOCK_DIE_ON_ERROR);
-	if (repo_read_index(the_repository) < 0)
-		BUG("Could not read index");
-
 	repo_init_revisions(the_repository, &revs, prefix);
 
 	strvec_pushl(&rev_walk_args, "", argv[2], "--not", argv[1], NULL);
@@ -194,12 +190,39 @@  int cmd_replay(int argc, const char **argv, const char *prefix)
 			die(_("replaying merge commits is not supported yet!"));
 
 		pick = pick_regular_commit(commit, last_commit, &merge_opt, &result);
-		if (!pick)
-			break;
+		if (!pick) {
+			/* TODO: handle conflicts in sparse worktree instead */
+			struct object_id head;
+			struct tree *head_tree;
+			struct lock_file lock = LOCK_INIT;
+
+			repo_hold_locked_index(the_repository, &lock, LOCK_DIE_ON_ERROR);
+			if (repo_read_index(the_repository) < 0)
+				BUG("Could not read index");
+
+			get_oid("HEAD", &head);
+			head_tree = parse_tree_indirect(&head);
+			printf("Switching from %s to %s.\n",
+			       oid_to_hex(&head_tree->object.oid),
+			       oid_to_hex(&result.tree->object.oid));
+			merge_switch_to_result(&merge_opt, head_tree, &result,
+					       1, 1);
+			if (write_locked_index(&the_index, &lock,
+					       COMMIT_LOCK | SKIP_IF_UNCHANGED))
+				die(_("unable to write %s"), get_index_file());
+
+			die(_("failure to pick %s; cannot handle conflicts yet"),
+			    oid_to_hex(&commit->object.oid));
+		}
+
 		last_commit = pick;
 		last_picked_commit = commit;
 	}
 
+	repo_hold_locked_index(the_repository, &lock, LOCK_DIE_ON_ERROR);
+	if (repo_read_index(the_repository) < 0)
+		BUG("Could not read index");
+
 	merge_switch_to_result(&merge_opt, head_tree, &result, 1, !result.clean);
 
 	if (result.clean < 0)