replay: introduce pick_regular_commit()

Let's refactor the code to handle a regular commit (a commit that is neither a root commit nor a merge commit) into a single function instead of keeping it inside cmd_replay(). This is good for separation of concerns, and this will help further work in the future to replay merge commits. Co-authored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Nov 24, 2023 at 12:10 UTC e787e664da640d1d4b15c6dc67a704dfa56592f9
1 file changed +34 -20
builtin/replay.c
+34 -20
@@ -89,6 +89,35 @@ static struct commit *create_commit(struct tree *tree,
89 return (struct commit *)obj;
90 }
91
92 +static struct commit *pick_regular_commit(struct commit *pickme,
93 + struct commit *last_commit,
94 + struct merge_options *merge_opt,
95 + struct merge_result *result)
96 +{
97 + struct commit *base;
98 + struct tree *pickme_tree, *base_tree;
99 +
100 + base = pickme->parents->item;
101 +
102 + pickme_tree = repo_get_commit_tree(the_repository, pickme);
103 + base_tree = repo_get_commit_tree(the_repository, base);
104 +
105 + merge_opt->branch2 = short_commit_name(pickme);
106 + merge_opt->ancestor = xstrfmt("parent of %s", merge_opt->branch2);
107 +
108 + merge_incore_nonrecursive(merge_opt,
109 + base_tree,
110 + result->tree,
111 + pickme_tree,
112 + result);
113 +
114 + free((char*)merge_opt->ancestor);
115 + merge_opt->ancestor = NULL;
116 + if (!result->clean)
117 + return NULL;
118 + return create_commit(result->tree, pickme, last_commit);
119 +}
120 +
121 int cmd_replay(int argc, const char **argv, const char *prefix)
122 {
123 struct commit *onto;
@@ -100,7 +129,7 @@ int cmd_replay(int argc, const char **argv, const char *prefix)
129 struct rev_info revs;
130 struct commit *commit;
131 struct merge_options merge_opt;
103 - struct tree *next_tree, *base_tree, *head_tree;
132 + struct tree *head_tree;
133 struct merge_result result;
134 struct strbuf reflog_msg = STRBUF_INIT;
135 struct strbuf branch_name = STRBUF_INIT;
@@ -175,7 +204,7 @@ int cmd_replay(int argc, const char **argv, const char *prefix)
204 result.tree = head_tree;
205 last_commit = onto;
206 while ((commit = get_revision(&revs))) {
178 - struct commit *base;
207 + struct commit *pick;
208
209 fprintf(stderr, "Rebasing %s...\r",
210 oid_to_hex(&commit->object.oid));
@@ -185,26 +214,11 @@ int cmd_replay(int argc, const char **argv, const char *prefix)
214 if (commit->parents->next)
215 die(_("replaying merge commits is not supported yet!"));
216
188 - base = commit->parents->item;
189 -
190 - next_tree = repo_get_commit_tree(the_repository, commit);
191 - base_tree = repo_get_commit_tree(the_repository, base);
192 -
193 - merge_opt.branch2 = short_commit_name(commit);
194 - merge_opt.ancestor = xstrfmt("parent of %s", merge_opt.branch2);
195 -
196 - merge_incore_nonrecursive(&merge_opt,
197 - base_tree,
198 - result.tree,
199 - next_tree,
200 - &result);
201 -
202 - free((char*)merge_opt.ancestor);
203 - merge_opt.ancestor = NULL;
204 - if (!result.clean)
217 + pick = pick_regular_commit(commit, last_commit, &merge_opt, &result);
218 + if (!pick)
219 break;
220 + last_commit = pick;
221 last_picked_commit = commit;
207 - last_commit = create_commit(result.tree, commit, last_commit);
222 }
223
224 merge_finalize(&merge_opt, &result);