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