1 #include "cache.h"
2 #include "commit.h"
3 #include "tag.h"
4 #include "merge-recursive.h"
5 #include "xdiff-interface.h"
7 static const char *better_branch_name(const char *branch)
8 {
9 static char githead_env[8 + 40 + 1];
10 char *name;
12 if (strlen(branch) != 40)
13 return branch;
14 sprintf(githead_env, "GITHEAD_%s", branch);
15 name = getenv(githead_env);
16 return name ? name : branch;
17 }
19 int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
20 {
21 const unsigned char *bases[21];
22 unsigned bases_count = 0;
23 int i, failed;
24 unsigned char h1[20], h2[20];
25 struct merge_options o;
26 struct commit *result;
28 init_merge_options(&o);
29 if (argv[0] && !suffixcmp(argv[0], "-subtree"))
30 o.subtree_shift = "";
32 if (argc < 4)
33 usagef("%s <base>... -- <head> <remote> ...", argv[0]);
35 for (i = 1; i < argc; ++i) {
36 const char *arg = argv[i];
38 if (!prefixcmp(arg, "--")) {
39 if (!arg[2])
40 break;
41 if (parse_merge_opt(&o, arg + 2))
42 die("Unknown option %s", arg);
43 continue;
44 }
45 if (bases_count < ARRAY_SIZE(bases)-1) {
46 unsigned char *sha = xmalloc(20);
47 if (get_sha1(argv[i], sha))
48 die("Could not parse object '%s'", argv[i]);
49 bases[bases_count++] = sha;
50 }
51 else
52 warning("Cannot handle more than %d bases. "
53 "Ignoring %s.",
54 (int)ARRAY_SIZE(bases)-1, argv[i]);
55 }
56 if (argc - i != 3) /* "--" "<head>" "<remote>" */
57 die("Not handling anything other than two heads merge.");
59 o.branch1 = argv[++i];
60 o.branch2 = argv[++i];
62 if (get_sha1(o.branch1, h1))
63 die("Could not resolve ref '%s'", o.branch1);
64 if (get_sha1(o.branch2, h2))
65 die("Could not resolve ref '%s'", o.branch2);
67 o.branch1 = better_branch_name(o.branch1);
68 o.branch2 = better_branch_name(o.branch2);
70 if (o.verbosity >= 3)
71 printf("Merging %s with %s\n", o.branch1, o.branch2);
73 failed = merge_recursive_generic(&o, h1, h2, bases_count, bases, &result);
74 if (failed < 0)
75 return 128; /* die() error code */
76 return failed;
77 }