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 <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Christian Couder committed
Mar 31, 2010 at 21:22 UTC
3f9083cde3b434155c274168f166ffce2bb243e7
1 file changed
+45
-36
builtin/merge.c
+45
-36
@@ -548,13 +548,53 @@ static void write_tree_trivial(unsigned char *sha1)
548
die("git write-tree failed to write a tree");
549
}
550
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)
553
{
554
const char **args;
555
int i = 0, x = 0, ret;
556
struct commit_list *j;
557
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
+{
598
int index_fd;
599
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
600
@@ -567,12 +607,13 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
607
rollback_lock_file(lock);
608
609
if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) {
570
- int clean;
610
+ int clean, x;
611
struct commit *result;
612
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
613
int index_fd;
614
struct commit_list *reversed = NULL;
615
struct merge_options o;
616
+ struct commit_list *j;
617
618
if (remoteheads->next) {
619
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,
653
rollback_lock_file(lock);
654
return clean ? 0 : 1;
655
} 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);
657
}
658
}
659