Merge branch 'jc/fsmonitor-sanity-fix'
[git] / builtin / merge-recursive.c
1 #include "cache.h"
2 #include "builtin.h"
3 #include "commit.h"
4 #include "tag.h"
5 #include "merge-recursive.h"
6 #include "xdiff-interface.h"
7
8 static const char builtin_merge_recursive_usage[] =
9         "git %s <base>... -- <head> <remote> ...";
10
11 static char *better_branch_name(const char *branch)
12 {
13         static char githead_env[8 + GIT_MAX_HEXSZ + 1];
14         char *name;
15
16         if (strlen(branch) != the_hash_algo->hexsz)
17                 return xstrdup(branch);
18         xsnprintf(githead_env, sizeof(githead_env), "GITHEAD_%s", branch);
19         name = getenv(githead_env);
20         return xstrdup(name ? name : branch);
21 }
22
23 int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
24 {
25         const struct object_id *bases[21];
26         unsigned bases_count = 0;
27         int i, failed;
28         struct object_id h1, h2;
29         struct merge_options o;
30         char *better1, *better2;
31         struct commit *result;
32
33         init_merge_options(&o, the_repository);
34         if (argv[0] && ends_with(argv[0], "-subtree"))
35                 o.subtree_shift = "";
36
37         if (argc < 4)
38                 usagef(builtin_merge_recursive_usage, argv[0]);
39
40         for (i = 1; i < argc; ++i) {
41                 const char *arg = argv[i];
42
43                 if (starts_with(arg, "--")) {
44                         if (!arg[2])
45                                 break;
46                         if (parse_merge_opt(&o, arg + 2))
47                                 die(_("unknown option %s"), arg);
48                         continue;
49                 }
50                 if (bases_count < ARRAY_SIZE(bases)-1) {
51                         struct object_id *oid = xmalloc(sizeof(struct object_id));
52                         if (get_oid(argv[i], oid))
53                                 die(_("could not parse object '%s'"), argv[i]);
54                         bases[bases_count++] = oid;
55                 }
56                 else
57                         warning(Q_("cannot handle more than %d base. "
58                                    "Ignoring %s.",
59                                    "cannot handle more than %d bases. "
60                                    "Ignoring %s.",
61                                     (int)ARRAY_SIZE(bases)-1),
62                                 (int)ARRAY_SIZE(bases)-1, argv[i]);
63         }
64         if (argc - i != 3) /* "--" "<head>" "<remote>" */
65                 die(_("not handling anything other than two heads merge."));
66
67         if (repo_read_index_unmerged(the_repository))
68                 die_resolve_conflict("merge");
69
70         o.branch1 = argv[++i];
71         o.branch2 = argv[++i];
72
73         if (get_oid(o.branch1, &h1))
74                 die(_("could not resolve ref '%s'"), o.branch1);
75         if (get_oid(o.branch2, &h2))
76                 die(_("could not resolve ref '%s'"), o.branch2);
77
78         o.branch1 = better1 = better_branch_name(o.branch1);
79         o.branch2 = better2 = 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
86         free(better1);
87         free(better2);
88
89         if (failed < 0)
90                 return 128; /* die() error code */
91         return failed;
92 }