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