Git 1.7.3.4
[git] / builtin / merge-recursive.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "tag.h"
4 #include "merge-recursive.h"
5
6 static const char builtin_merge_recursive_usage[] =
7         "git %s <base>... -- <head> <remote> ...";
8
9 static 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
21 int 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 if (!strcmp(arg+2, "renormalize"))
52                                 o.renormalize = 1;
53                         else if (!strcmp(arg+2, "no-renormalize"))
54                                 o.renormalize = 0;
55                         else
56                                 die("Unknown option %s", arg);
57                         continue;
58                 }
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;
64                 }
65                 else
66                         warning("Cannot handle more than %d bases. "
67                                 "Ignoring %s.",
68                                 (int)ARRAY_SIZE(bases)-1, argv[i]);
69         }
70         if (argc - i != 3) /* "--" "<head>" "<remote>" */
71                 die("Not handling anything other than two heads merge.");
72
73         o.branch1 = argv[++i];
74         o.branch2 = argv[++i];
75
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);
80
81         o.branch1 = better_branch_name(o.branch1);
82         o.branch2 = better_branch_name(o.branch2);
83
84         if (o.verbosity >= 3)
85                 printf("Merging %s with %s\n", o.branch1, o.branch2);
86
87         failed = merge_recursive_generic(&o, h1, h2, bases_count, bases, &result);
88         if (failed < 0)
89                 return 128; /* die() error code */
90         return failed;
91 }