Skip to content

Commit e787e66

Browse files
newrenchriscool
authored andcommitted
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 <[email protected]> Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Christian Couder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a9df61a commit e787e66

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

builtin/replay.c

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,35 @@ static struct commit *create_commit(struct tree *tree,
8989
return (struct commit *)obj;
9090
}
9191

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+
92121
int cmd_replay(int argc, const char **argv, const char *prefix)
93122
{
94123
struct commit *onto;
@@ -100,7 +129,7 @@ int cmd_replay(int argc, const char **argv, const char *prefix)
100129
struct rev_info revs;
101130
struct commit *commit;
102131
struct merge_options merge_opt;
103-
struct tree *next_tree, *base_tree, *head_tree;
132+
struct tree *head_tree;
104133
struct merge_result result;
105134
struct strbuf reflog_msg = STRBUF_INIT;
106135
struct strbuf branch_name = STRBUF_INIT;
@@ -175,7 +204,7 @@ int cmd_replay(int argc, const char **argv, const char *prefix)
175204
result.tree = head_tree;
176205
last_commit = onto;
177206
while ((commit = get_revision(&revs))) {
178-
struct commit *base;
207+
struct commit *pick;
179208

180209
fprintf(stderr, "Rebasing %s...\r",
181210
oid_to_hex(&commit->object.oid));
@@ -185,26 +214,11 @@ int cmd_replay(int argc, const char **argv, const char *prefix)
185214
if (commit->parents->next)
186215
die(_("replaying merge commits is not supported yet!"));
187216

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)
205219
break;
220+
last_commit = pick;
206221
last_picked_commit = commit;
207-
last_commit = create_commit(result.tree, commit, last_commit);
208222
}
209223

210224
merge_finalize(&merge_opt, &result);

0 commit comments

Comments
 (0)