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