4 #include "merge-recursive.h"
6 static const char builtin_merge_recursive_usage[] =
7 "git %s <base>... -- <head> <remote> ...";
9 static const char *better_branch_name(const char *branch)
11 static char githead_env[8 + 40 + 1];
14 if (strlen(branch) != 40)
16 sprintf(githead_env, "GITHEAD_%s", branch);
17 name = getenv(githead_env);
18 return name ? name : branch;
21 int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
23 const unsigned char *bases[21];
24 unsigned bases_count = 0;
26 unsigned char h1[20], h2[20];
27 struct merge_options o;
28 struct commit *result;
30 init_merge_options(&o);
31 if (argv[0] && !suffixcmp(argv[0], "-subtree"))
35 usagef(builtin_merge_recursive_usage, argv[0]);
37 for (i = 1; i < argc; ++i) {
38 const char *arg = argv[i];
40 if (!prefixcmp(arg, "--")) {
43 if (!strcmp(arg+2, "ours"))
44 o.recursive_variant = MERGE_RECURSIVE_OURS;
45 else if (!strcmp(arg+2, "theirs"))
46 o.recursive_variant = MERGE_RECURSIVE_THEIRS;
47 else if (!strcmp(arg+2, "subtree"))
49 else if (!prefixcmp(arg+2, "subtree="))
50 o.subtree_shift = arg + 10;
51 else if (!strcmp(arg+2, "renormalize"))
53 else if (!strcmp(arg+2, "no-renormalize"))
56 die("Unknown option %s", arg);
59 if (bases_count < ARRAY_SIZE(bases)-1) {
60 unsigned char *sha = xmalloc(20);
61 if (get_sha1(argv[i], sha))
62 die("Could not parse object '%s'", argv[i]);
63 bases[bases_count++] = sha;
66 warning("Cannot handle more than %d bases. "
68 (int)ARRAY_SIZE(bases)-1, argv[i]);
70 if (argc - i != 3) /* "--" "<head>" "<remote>" */
71 die("Not handling anything other than two heads merge.");
73 o.branch1 = argv[++i];
74 o.branch2 = argv[++i];
76 if (get_sha1(o.branch1, h1))
77 die("Could not resolve ref '%s'", o.branch1);
78 if (get_sha1(o.branch2, h2))
79 die("Could not resolve ref '%s'", o.branch2);
81 o.branch1 = better_branch_name(o.branch1);
82 o.branch2 = better_branch_name(o.branch2);
85 printf("Merging %s with %s\n", o.branch1, o.branch2);
87 failed = merge_recursive_generic(&o, h1, h2, bases_count, bases, &result);
89 return 128; /* die() error code */