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