merge-recursive: honor diff.algorithm
[git/gitster.git] / builtin / merge-recursive.c
blobe951b09805dd872ea3a8eeadf056c4d069483bd5
1 #include "builtin.h"
2 #include "advice.h"
3 #include "gettext.h"
4 #include "hash.h"
5 #include "merge-recursive.h"
6 #include "object-name.h"
7 #include "repository.h"
9 static const char builtin_merge_recursive_usage[] =
10 "git %s <base>... -- <head> <remote> ...";
12 static char *better_branch_name(const char *branch)
14 static char githead_env[8 + GIT_MAX_HEXSZ + 1];
15 char *name;
17 if (strlen(branch) != the_hash_algo->hexsz)
18 return xstrdup(branch);
19 xsnprintf(githead_env, sizeof(githead_env), "GITHEAD_%s", branch);
20 name = getenv(githead_env);
21 return xstrdup(name ? name : branch);
24 int cmd_merge_recursive(int argc, const char **argv, const char *prefix UNUSED)
26 struct object_id bases[21];
27 unsigned bases_count = 0;
28 int i, failed;
29 struct object_id h1, h2;
30 struct merge_options o;
31 char *better1, *better2;
32 struct commit *result;
34 init_basic_merge_options(&o, the_repository);
35 if (argv[0] && ends_with(argv[0], "-subtree"))
36 o.subtree_shift = "";
38 if (argc < 4)
39 usagef(builtin_merge_recursive_usage, argv[0]);
41 for (i = 1; i < argc; ++i) {
42 const char *arg = argv[i];
44 if (starts_with(arg, "--")) {
45 if (!arg[2])
46 break;
47 if (parse_merge_opt(&o, arg + 2))
48 die(_("unknown option %s"), arg);
49 continue;
51 if (bases_count < ARRAY_SIZE(bases)-1) {
52 if (repo_get_oid(the_repository, argv[i], &bases[bases_count++]))
53 die(_("could not parse object '%s'"), argv[i]);
55 else
56 warning(Q_("cannot handle more than %d base. "
57 "Ignoring %s.",
58 "cannot handle more than %d bases. "
59 "Ignoring %s.",
60 ARRAY_SIZE(bases)-1),
61 (int)ARRAY_SIZE(bases)-1, argv[i]);
63 if (argc - i != 3) /* "--" "<head>" "<remote>" */
64 die(_("not handling anything other than two heads merge."));
66 if (repo_read_index_unmerged(the_repository))
67 die_resolve_conflict("merge");
69 o.branch1 = argv[++i];
70 o.branch2 = argv[++i];
72 if (repo_get_oid(the_repository, o.branch1, &h1))
73 die(_("could not resolve ref '%s'"), o.branch1);
74 if (repo_get_oid(the_repository, o.branch2, &h2))
75 die(_("could not resolve ref '%s'"), o.branch2);
77 o.branch1 = better1 = better_branch_name(o.branch1);
78 o.branch2 = better2 = better_branch_name(o.branch2);
80 if (o.verbosity >= 3)
81 printf(_("Merging %s with %s\n"), o.branch1, o.branch2);
83 failed = merge_recursive_generic(&o, &h1, &h2, bases_count, bases, &result);
85 free(better1);
86 free(better2);
88 if (failed < 0)
89 return 128; /* die() error code */
90 return failed;