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