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