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