Skip to content

Commit ae8c79f

Browse files
chriscoolgitster
authored andcommitted
revert: refactor merge recursive code into its own function
The code that is used to do a recursive merge is extracted from the revert_or_cherry_pick() function and put into a new do_recursive_merge() function. Signed-off-by: Christian Couder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bc84a7f commit ae8c79f

File tree

1 file changed

+58
-48
lines changed

1 file changed

+58
-48
lines changed

builtin/revert.c

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -279,18 +279,69 @@ static NORETURN void die_dirty_index(const char *me)
279279
}
280280
}
281281

282+
static void do_recursive_merge(struct commit *base, struct commit *next,
283+
const char *base_label, const char *next_label,
284+
unsigned char *head, struct strbuf *msgbuf,
285+
char *defmsg)
286+
{
287+
struct merge_options o;
288+
struct tree *result, *next_tree, *base_tree, *head_tree;
289+
int clean, index_fd;
290+
static struct lock_file index_lock;
291+
292+
index_fd = hold_locked_index(&index_lock, 1);
293+
294+
read_cache();
295+
init_merge_options(&o);
296+
o.ancestor = base ? base_label : "(empty tree)";
297+
o.branch1 = "HEAD";
298+
o.branch2 = next ? next_label : "(empty tree)";
299+
300+
head_tree = parse_tree_indirect(head);
301+
next_tree = next ? next->tree : empty_tree();
302+
base_tree = base ? base->tree : empty_tree();
303+
304+
clean = merge_trees(&o,
305+
head_tree,
306+
next_tree, base_tree, &result);
307+
308+
if (active_cache_changed &&
309+
(write_cache(index_fd, active_cache, active_nr) ||
310+
commit_locked_index(&index_lock)))
311+
die("%s: Unable to write new index file", me);
312+
rollback_lock_file(&index_lock);
313+
314+
if (!clean) {
315+
int i;
316+
strbuf_addstr(msgbuf, "\nConflicts:\n\n");
317+
for (i = 0; i < active_nr;) {
318+
struct cache_entry *ce = active_cache[i++];
319+
if (ce_stage(ce)) {
320+
strbuf_addch(msgbuf, '\t');
321+
strbuf_addstr(msgbuf, ce->name);
322+
strbuf_addch(msgbuf, '\n');
323+
while (i < active_nr && !strcmp(ce->name,
324+
active_cache[i]->name))
325+
i++;
326+
}
327+
}
328+
write_message(msgbuf, defmsg);
329+
fprintf(stderr, "Automatic %s failed.%s\n",
330+
me, help_msg(commit_name));
331+
rerere(allow_rerere_auto);
332+
exit(1);
333+
}
334+
write_message(msgbuf, defmsg);
335+
fprintf(stderr, "Finished one %s.\n", me);
336+
}
337+
282338
static int revert_or_cherry_pick(int argc, const char **argv)
283339
{
284340
unsigned char head[20];
285341
struct commit *base, *next, *parent;
286342
const char *base_label, *next_label;
287-
int i, index_fd, clean;
288343
struct commit_message msg = { NULL, NULL, NULL, NULL, NULL };
289-
290344
char *defmsg = git_pathdup("MERGE_MSG");
291-
struct merge_options o;
292-
struct tree *result, *next_tree, *base_tree, *head_tree;
293-
static struct lock_file index_lock;
294345
struct strbuf msgbuf = STRBUF_INIT;
295346

296347
git_config(git_default_config, NULL);
@@ -321,8 +372,6 @@ static int revert_or_cherry_pick(int argc, const char **argv)
321372
}
322373
discard_cache();
323374

324-
index_fd = hold_locked_index(&index_lock, 1);
325-
326375
if (!commit->parents) {
327376
if (action == REVERT)
328377
die ("Cannot revert a root commit");
@@ -395,47 +444,8 @@ static int revert_or_cherry_pick(int argc, const char **argv)
395444
}
396445
}
397446

398-
read_cache();
399-
init_merge_options(&o);
400-
o.ancestor = base ? base_label : "(empty tree)";
401-
o.branch1 = "HEAD";
402-
o.branch2 = next ? next_label : "(empty tree)";
403-
404-
head_tree = parse_tree_indirect(head);
405-
next_tree = next ? next->tree : empty_tree();
406-
base_tree = base ? base->tree : empty_tree();
407-
408-
clean = merge_trees(&o,
409-
head_tree,
410-
next_tree, base_tree, &result);
411-
412-
if (active_cache_changed &&
413-
(write_cache(index_fd, active_cache, active_nr) ||
414-
commit_locked_index(&index_lock)))
415-
die("%s: Unable to write new index file", me);
416-
rollback_lock_file(&index_lock);
417-
418-
if (!clean) {
419-
strbuf_addstr(&msgbuf, "\nConflicts:\n\n");
420-
for (i = 0; i < active_nr;) {
421-
struct cache_entry *ce = active_cache[i++];
422-
if (ce_stage(ce)) {
423-
strbuf_addch(&msgbuf, '\t');
424-
strbuf_addstr(&msgbuf, ce->name);
425-
strbuf_addch(&msgbuf, '\n');
426-
while (i < active_nr && !strcmp(ce->name,
427-
active_cache[i]->name))
428-
i++;
429-
}
430-
}
431-
write_message(&msgbuf, defmsg);
432-
fprintf(stderr, "Automatic %s failed.%s\n",
433-
me, help_msg(commit_name));
434-
rerere(allow_rerere_auto);
435-
exit(1);
436-
}
437-
write_message(&msgbuf, defmsg);
438-
fprintf(stderr, "Finished one %s.\n", me);
447+
do_recursive_merge(base, next, base_label, next_label,
448+
head, &msgbuf, defmsg);
439449

440450
/*
441451
*

0 commit comments

Comments
 (0)