Git 2.23-rc1
[git.git] / builtin / merge-recursive.c
blob5b910e351e4dede4527d23a04a8294e17ebd371c
1 #include "builtin.h"
2 #include "commit.h"
3 #include "tag.h"
4 #include "merge-recursive.h"
5 #include "xdiff-interface.h"
7 static const char builtin_merge_recursive_usage[] =
8 "git %s <base>... -- <head> <remote> ...";
10 static char *better_branch_name(const char *branch)
12 static char githead_env[8 + GIT_MAX_HEXSZ + 1];
13 char *name;
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);
22 int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
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;
32 init_merge_options(&o, the_repository);
33 if (argv[0] && ends_with(argv[0], "-subtree"))
34 o.subtree_shift = "";
36 if (argc < 4)
37 usagef(builtin_merge_recursive_usage, argv[0]);
39 for (i = 1; i < argc; ++i) {
40 const char *arg = argv[i];
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;
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;
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]);
63 if (argc - i != 3) /* "--" "<head>" "<remote>" */
64 die(_("not handling anything other than two heads merge."));
66 o.branch1 = argv[++i];
67 o.branch2 = argv[++i];
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);
74 o.branch1 = better1 = better_branch_name(o.branch1);
75 o.branch2 = better2 = better_branch_name(o.branch2);
77 if (o.verbosity >= 3)
78 printf(_("Merging %s with %s\n"), o.branch1, o.branch2);
80 failed = merge_recursive_generic(&o, &h1, &h2, bases_count, bases, &result);
82 free(better1);
83 free(better2);
85 if (failed < 0)
86 return 128; /* die() error code */
87 return failed;