Skip to content

Commit 3f9083c

Browse files
chriscoolgitster
authored andcommitted
merge: refactor code that calls "git merge-STRATEGY"
In the try_merge_strategy() function, when the strategy is "recursive" or "subtree", the merge_recursive() function is called. Otherwise we launch a "git merge-STRATEGY" process. To make it possible to reuse code that launches a "git merge-STRATEGY" process, this patch refactors this code into a new try_merge_command() function. Signed-off-by: Christian Couder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ae8c79f commit 3f9083c

File tree

1 file changed

+45
-36
lines changed

1 file changed

+45
-36
lines changed

builtin/merge.c

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -548,13 +548,53 @@ static void write_tree_trivial(unsigned char *sha1)
548548
die("git write-tree failed to write a tree");
549549
}
550550

551-
static int try_merge_strategy(const char *strategy, struct commit_list *common,
552-
const char *head_arg)
551+
static int try_merge_command(const char *strategy, struct commit_list *common,
552+
const char *head_arg, struct commit_list *remotes)
553553
{
554554
const char **args;
555555
int i = 0, x = 0, ret;
556556
struct commit_list *j;
557557
struct strbuf buf = STRBUF_INIT;
558+
559+
args = xmalloc((4 + xopts_nr + commit_list_count(common) +
560+
commit_list_count(remotes)) * sizeof(char *));
561+
strbuf_addf(&buf, "merge-%s", strategy);
562+
args[i++] = buf.buf;
563+
for (x = 0; x < xopts_nr; x++) {
564+
char *s = xmalloc(strlen(xopts[x])+2+1);
565+
strcpy(s, "--");
566+
strcpy(s+2, xopts[x]);
567+
args[i++] = s;
568+
}
569+
for (j = common; j; j = j->next)
570+
args[i++] = xstrdup(sha1_to_hex(j->item->object.sha1));
571+
args[i++] = "--";
572+
args[i++] = head_arg;
573+
for (j = remotes; j; j = j->next)
574+
args[i++] = xstrdup(sha1_to_hex(j->item->object.sha1));
575+
args[i] = NULL;
576+
ret = run_command_v_opt(args, RUN_GIT_CMD);
577+
strbuf_release(&buf);
578+
i = 1;
579+
for (x = 0; x < xopts_nr; x++)
580+
free((void *)args[i++]);
581+
for (j = common; j; j = j->next)
582+
free((void *)args[i++]);
583+
i += 2;
584+
for (j = remotes; j; j = j->next)
585+
free((void *)args[i++]);
586+
free(args);
587+
discard_cache();
588+
if (read_cache() < 0)
589+
die("failed to read the cache");
590+
resolve_undo_clear();
591+
592+
return ret;
593+
}
594+
595+
static int try_merge_strategy(const char *strategy, struct commit_list *common,
596+
const char *head_arg)
597+
{
558598
int index_fd;
559599
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
560600

@@ -567,12 +607,13 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
567607
rollback_lock_file(lock);
568608

569609
if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) {
570-
int clean;
610+
int clean, x;
571611
struct commit *result;
572612
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
573613
int index_fd;
574614
struct commit_list *reversed = NULL;
575615
struct merge_options o;
616+
struct commit_list *j;
576617

577618
if (remoteheads->next) {
578619
error("Not handling anything other than two heads merge.");
@@ -612,39 +653,7 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
612653
rollback_lock_file(lock);
613654
return clean ? 0 : 1;
614655
} else {
615-
args = xmalloc((4 + xopts_nr + commit_list_count(common) +
616-
commit_list_count(remoteheads)) * sizeof(char *));
617-
strbuf_addf(&buf, "merge-%s", strategy);
618-
args[i++] = buf.buf;
619-
for (x = 0; x < xopts_nr; x++) {
620-
char *s = xmalloc(strlen(xopts[x])+2+1);
621-
strcpy(s, "--");
622-
strcpy(s+2, xopts[x]);
623-
args[i++] = s;
624-
}
625-
for (j = common; j; j = j->next)
626-
args[i++] = xstrdup(sha1_to_hex(j->item->object.sha1));
627-
args[i++] = "--";
628-
args[i++] = head_arg;
629-
for (j = remoteheads; j; j = j->next)
630-
args[i++] = xstrdup(sha1_to_hex(j->item->object.sha1));
631-
args[i] = NULL;
632-
ret = run_command_v_opt(args, RUN_GIT_CMD);
633-
strbuf_release(&buf);
634-
i = 1;
635-
for (x = 0; x < xopts_nr; x++)
636-
free((void *)args[i++]);
637-
for (j = common; j; j = j->next)
638-
free((void *)args[i++]);
639-
i += 2;
640-
for (j = remoteheads; j; j = j->next)
641-
free((void *)args[i++]);
642-
free(args);
643-
discard_cache();
644-
if (read_cache() < 0)
645-
die("failed to read the cache");
646-
resolve_undo_clear();
647-
return ret;
656+
return try_merge_command(strategy, common, head_arg, remoteheads);
648657
}
649658
}
650659

0 commit comments

Comments
 (0)