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