]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/merge-recursive.c
Start the 2.46 cycle
[thirdparty/git.git] / builtin / merge-recursive.c
CommitLineData
c2e86add 1#include "builtin.h"
6c6ddf92 2#include "advice.h"
f394e093 3#include "gettext.h"
d1cbe1e6 4#include "hash.h"
e1b3a2ca 5#include "merge-recursive.h"
dabab1d6 6#include "object-name.h"
d1cbe1e6 7#include "repository.h"
6d297f81 8
e78d01bf
TF
9static const char builtin_merge_recursive_usage[] =
10 "git %s <base>... -- <head> <remote> ...";
11
d64bb065 12static char *better_branch_name(const char *branch)
7ba3c078 13{
b7f20f72 14 static char githead_env[8 + GIT_MAX_HEXSZ + 1];
7ba3c078
SP
15 char *name;
16
b7f20f72 17 if (strlen(branch) != the_hash_algo->hexsz)
d64bb065 18 return xstrdup(branch);
5096d490 19 xsnprintf(githead_env, sizeof(githead_env), "GITHEAD_%s", branch);
7ba3c078 20 name = getenv(githead_env);
d64bb065 21 return xstrdup(name ? name : branch);
7ba3c078
SP
22}
23
5247b762 24int cmd_merge_recursive(int argc, const char **argv, const char *prefix UNUSED)
6d297f81 25{
4e8161a8 26 const struct object_id *bases[21];
73118f89
SB
27 unsigned bases_count = 0;
28 int i, failed;
4e8161a8 29 struct object_id h1, h2;
8a2fce18 30 struct merge_options o;
d64bb065 31 char *better1, *better2;
8a2fce18 32 struct commit *result;
6d297f81 33
0d6caa2d 34 init_merge_options(&o, the_repository);
59556548 35 if (argv[0] && ends_with(argv[0], "-subtree"))
85e51b78 36 o.subtree_shift = "";
68faf689 37
6d297f81 38 if (argc < 4)
e78d01bf 39 usagef(builtin_merge_recursive_usage, argv[0]);
6d297f81 40
6d297f81 41 for (i = 1; i < argc; ++i) {
8cc5b290
AP
42 const char *arg = argv[i];
43
59556548 44 if (starts_with(arg, "--")) {
8cc5b290
AP
45 if (!arg[2])
46 break;
635a7bb1 47 if (parse_merge_opt(&o, arg + 2))
ccf78131 48 die(_("unknown option %s"), arg);
8cc5b290
AP
49 continue;
50 }
8a2fce18 51 if (bases_count < ARRAY_SIZE(bases)-1) {
4e8161a8 52 struct object_id *oid = xmalloc(sizeof(struct object_id));
d850b7a5 53 if (repo_get_oid(the_repository, argv[i], oid))
ccf78131 54 die(_("could not parse object '%s'"), argv[i]);
4e8161a8 55 bases[bases_count++] = oid;
73118f89 56 }
73118f89 57 else
ccf78131
VA
58 warning(Q_("cannot handle more than %d base. "
59 "Ignoring %s.",
60 "cannot handle more than %d bases. "
61 "Ignoring %s.",
6f693252 62 ARRAY_SIZE(bases)-1),
b74d779b 63 (int)ARRAY_SIZE(bases)-1, argv[i]);
6d297f81
JS
64 }
65 if (argc - i != 3) /* "--" "<head>" "<remote>" */
ccf78131 66 die(_("not handling anything other than two heads merge."));
6d297f81 67
9822175d
EN
68 if (repo_read_index_unmerged(the_repository))
69 die_resolve_conflict("merge");
70
8a2fce18
MV
71 o.branch1 = argv[++i];
72 o.branch2 = argv[++i];
6d297f81 73
d850b7a5 74 if (repo_get_oid(the_repository, o.branch1, &h1))
ccf78131 75 die(_("could not resolve ref '%s'"), o.branch1);
d850b7a5 76 if (repo_get_oid(the_repository, o.branch2, &h2))
ccf78131 77 die(_("could not resolve ref '%s'"), o.branch2);
6d297f81 78
d64bb065
JK
79 o.branch1 = better1 = better_branch_name(o.branch1);
80 o.branch2 = better2 = better_branch_name(o.branch2);
3f6ee2d1 81
8a2fce18 82 if (o.verbosity >= 3)
765773c8 83 printf(_("Merging %s with %s\n"), o.branch1, o.branch2);
e0ec1819 84
4e8161a8 85 failed = merge_recursive_generic(&o, &h1, &h2, bases_count, bases, &result);
d64bb065
JK
86
87 free(better1);
88 free(better2);
89
73118f89
SB
90 if (failed < 0)
91 return 128; /* die() error code */
92 return failed;
6d297f81 93}