| 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | #include "builtin.h" |
| 3 | #include "advice.h" |
| 4 | #include "gettext.h" |
| 5 | #include "hash.h" |
| 6 | #include "merge-ort-wrappers.h" |
| 7 | #include "object-name.h" |
| 8 | |
| 9 | static const char builtin_merge_recursive_usage[] = |
| 10 | "git %s <base>... -- <head> <remote> ..."; |
| 11 | |
| 12 | static char *better_branch_name(const char *branch) |
| 13 | { |
| 14 | static char githead_env[8 + GIT_MAX_HEXSZ + 1]; |
| 15 | char *name; |
| 16 | |
| 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); |
| 22 | } |
| 23 | |
| 24 | int cmd_merge_recursive(int argc, |
| 25 | const char **argv, |
| 26 | const char *prefix UNUSED, |
| 27 | struct repository *repo UNUSED) |
| 28 | { |
| 29 | 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_basic_merge_options(&o, the_repository); |
| 38 | if (argv[0] && ends_with(argv[0], "-subtree")) |
| 39 | o.subtree_shift = ""; |
| 40 | |
| 41 | if (argc == 2 && (!strcmp(argv[1], "-h") || |
| 42 | !strcmp(argv[1], "--help-all"))) { |
| 43 | struct strbuf msg = STRBUF_INIT; |
| 44 | strbuf_addf(&msg, builtin_merge_recursive_usage, argv[0]); |
| 45 | show_usage_if_asked(argc, argv, msg.buf); |
| 46 | } |
| 47 | |
| 48 | if (argc < 4) |
| 49 | usagef(builtin_merge_recursive_usage, argv[0]); |
| 50 | |
| 51 | for (i = 1; i < argc; ++i) { |
| 52 | const char *arg = argv[i]; |
| 53 | |
| 54 | if (starts_with(arg, "--")) { |
| 55 | if (!arg[2]) |
| 56 | break; |
| 57 | if (parse_merge_opt(&o, arg + 2)) |
| 58 | die(_("unknown option %s"), arg); |
| 59 | continue; |
| 60 | } |
| 61 | if (bases_count < ARRAY_SIZE(bases)-1) { |
| 62 | if (repo_get_oid(the_repository, argv[i], &bases[bases_count++])) |
| 63 | die(_("could not parse object '%s'"), argv[i]); |
| 64 | } |
| 65 | else |
| 66 | warning(Q_("cannot handle more than %d base. " |
| 67 | "Ignoring %s.", |
| 68 | "cannot handle more than %d bases. " |
| 69 | "Ignoring %s.", |
| 70 | ARRAY_SIZE(bases)-1), |
| 71 | (int)ARRAY_SIZE(bases)-1, argv[i]); |
| 72 | } |
| 73 | if (argc - i != 3) /* "--" "<head>" "<remote>" */ |
| 74 | die(_("not handling anything other than two heads merge.")); |
| 75 | |
| 76 | if (repo_read_index_unmerged(the_repository)) |
| 77 | die_resolve_conflict("merge"); |
| 78 | |
| 79 | o.branch1 = argv[++i]; |
| 80 | o.branch2 = argv[++i]; |
| 81 | |
| 82 | if (repo_get_oid(the_repository, o.branch1, &h1)) |
| 83 | die(_("could not resolve ref '%s'"), o.branch1); |
| 84 | if (repo_get_oid(the_repository, o.branch2, &h2)) |
| 85 | die(_("could not resolve ref '%s'"), o.branch2); |
| 86 | |
| 87 | o.branch1 = better1 = better_branch_name(o.branch1); |
| 88 | o.branch2 = better2 = better_branch_name(o.branch2); |
| 89 | |
| 90 | if (o.verbosity >= 3) |
| 91 | printf(_("Merging %s with %s\n"), o.branch1, o.branch2); |
| 92 | |
| 93 | failed = merge_ort_generic(&o, &h1, &h2, bases_count, bases, &result); |
| 94 | |
| 95 | free(better1); |
| 96 | free(better2); |
| 97 | |
| 98 | if (failed < 0) |
| 99 | return 128; /* die() error code */ |
| 100 | return failed; |
| 101 | } |