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