| 1 | /* |
| 2 | * Builtin "git merge" |
| 3 | * |
| 4 | * Copyright (c) 2008 Miklos Vajna <vmiklos@frugalware.org> |
| 5 | * |
| 6 | * Based on git-merge.sh by Junio C Hamano. |
| 7 | */ |
| 8 | |
| 9 | #define USE_THE_REPOSITORY_VARIABLE |
| 10 | #define DISABLE_SIGN_COMPARE_WARNINGS |
| 11 | |
| 12 | #include "builtin.h" |
| 13 | |
| 14 | #include "abspath.h" |
| 15 | #include "advice.h" |
| 16 | #include "config.h" |
| 17 | #include "editor.h" |
| 18 | #include "environment.h" |
| 19 | #include "gettext.h" |
| 20 | #include "hex.h" |
| 21 | #include "object-name.h" |
| 22 | #include "parse-options.h" |
| 23 | #include "lockfile.h" |
| 24 | #include "repository.h" |
| 25 | #include "run-command.h" |
| 26 | #include "hook.h" |
| 27 | #include "diff.h" |
| 28 | #include "diff-merges.h" |
| 29 | #include "refs.h" |
| 30 | #include "refspec.h" |
| 31 | #include "commit.h" |
| 32 | #include "diffcore.h" |
| 33 | #include "path.h" |
| 34 | #include "revision.h" |
| 35 | #include "unpack-trees.h" |
| 36 | #include "cache-tree.h" |
| 37 | #include "dir.h" |
| 38 | #include "color.h" |
| 39 | #include "rerere.h" |
| 40 | #include "help.h" |
| 41 | #include "merge.h" |
| 42 | #include "merge-ort-wrappers.h" |
| 43 | #include "resolve-undo.h" |
| 44 | #include "remote.h" |
| 45 | #include "fmt-merge-msg.h" |
| 46 | #include "sequencer.h" |
| 47 | #include "string-list.h" |
| 48 | #include "tag.h" |
| 49 | #include "alias.h" |
| 50 | #include "branch.h" |
| 51 | #include "commit-reach.h" |
| 52 | #include "wt-status.h" |
| 53 | #include "commit-graph.h" |
| 54 | |
| 55 | #define DEFAULT_TWOHEAD (1<<0) |
| 56 | #define DEFAULT_OCTOPUS (1<<1) |
| 57 | #define NO_FAST_FORWARD (1<<2) |
| 58 | #define NO_TRIVIAL (1<<3) |
| 59 | |
| 60 | struct strategy { |
| 61 | const char *name; |
| 62 | unsigned attr; |
| 63 | }; |
| 64 | |
| 65 | static const char * const builtin_merge_usage[] = { |
| 66 | N_("git merge [<options>] [<commit>...]"), |
| 67 | "git merge --abort", |
| 68 | "git merge --continue", |
| 69 | NULL |
| 70 | }; |
| 71 | |
| 72 | #define MERGE_SHOW_DIFFSTAT 1 |
| 73 | #define MERGE_SHOW_COMPACTSUMMARY 2 |
| 74 | |
| 75 | static int show_diffstat = MERGE_SHOW_DIFFSTAT, shortlog_len = -1, squash; |
| 76 | static int option_commit = -1; |
| 77 | static int option_edit = -1; |
| 78 | static int allow_trivial = 1, have_message, verify_signatures; |
| 79 | static int check_trust_level = 1; |
| 80 | static int overwrite_ignore = 1; |
| 81 | static struct strbuf merge_msg = STRBUF_INIT; |
| 82 | static struct strategy **use_strategies; |
| 83 | static size_t use_strategies_nr, use_strategies_alloc; |
| 84 | static struct strvec xopts = STRVEC_INIT; |
| 85 | static const char *branch; |
| 86 | static char *branch_mergeoptions; |
| 87 | static int verbosity; |
| 88 | static int allow_rerere_auto; |
| 89 | static int abort_current_merge; |
| 90 | static int quit_current_merge; |
| 91 | static int continue_current_merge; |
| 92 | static int allow_unrelated_histories; |
| 93 | static int show_progress = -1; |
| 94 | static int default_to_upstream = 1; |
| 95 | static int signoff; |
| 96 | static const char *sign_commit; |
| 97 | static int autostash; |
| 98 | static int no_verify; |
| 99 | static char *into_name; |
| 100 | |
| 101 | static struct strategy all_strategy[] = { |
| 102 | { "recursive", NO_TRIVIAL }, |
| 103 | { "octopus", DEFAULT_OCTOPUS }, |
| 104 | { "ort", DEFAULT_TWOHEAD | NO_TRIVIAL }, |
| 105 | { "resolve", 0 }, |
| 106 | { "ours", NO_FAST_FORWARD | NO_TRIVIAL }, |
| 107 | { "subtree", NO_FAST_FORWARD | NO_TRIVIAL }, |
| 108 | }; |
| 109 | |
| 110 | static char *pull_twohead, *pull_octopus; |
| 111 | |
| 112 | enum ff_type { |
| 113 | FF_NO, |
| 114 | FF_ALLOW, |
| 115 | FF_ONLY |
| 116 | }; |
| 117 | |
| 118 | static enum ff_type fast_forward = FF_ALLOW; |
| 119 | |
| 120 | static char *cleanup_arg; |
| 121 | static enum commit_msg_cleanup_mode cleanup_mode; |
| 122 | |
| 123 | static int option_parse_message(const struct option *opt, |
| 124 | const char *arg, int unset) |
| 125 | { |
| 126 | struct strbuf *buf = opt->value; |
| 127 | |
| 128 | if (unset) |
| 129 | strbuf_setlen(buf, 0); |
| 130 | else if (arg) { |
| 131 | strbuf_addf(buf, "%s%s", buf->len ? "\n\n" : "", arg); |
| 132 | have_message = 1; |
| 133 | } else |
| 134 | return error(_("switch `m' requires a value")); |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static enum parse_opt_result option_read_message(struct parse_opt_ctx_t *ctx, |
| 139 | const struct option *opt, |
| 140 | const char *arg_not_used, |
| 141 | int unset) |
| 142 | { |
| 143 | struct strbuf *buf = opt->value; |
| 144 | const char *arg; |
| 145 | |
| 146 | BUG_ON_OPT_ARG(arg_not_used); |
| 147 | if (unset) |
| 148 | BUG("-F cannot be negated"); |
| 149 | |
| 150 | if (ctx->opt) { |
| 151 | arg = ctx->opt; |
| 152 | ctx->opt = NULL; |
| 153 | } else if (ctx->argc > 1) { |
| 154 | ctx->argc--; |
| 155 | arg = *++ctx->argv; |
| 156 | } else |
| 157 | return error(_("option `%s' requires a value"), opt->long_name); |
| 158 | |
| 159 | if (buf->len) |
| 160 | strbuf_addch(buf, '\n'); |
| 161 | if (ctx->prefix && !is_absolute_path(arg)) |
| 162 | arg = prefix_filename(ctx->prefix, arg); |
| 163 | if (strbuf_read_file(buf, arg, 0) < 0) |
| 164 | return error(_("could not read file '%s'"), arg); |
| 165 | have_message = 1; |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static struct strategy *get_strategy(const char *name) |
| 171 | { |
| 172 | int i; |
| 173 | struct strategy *ret; |
| 174 | static struct cmdnames main_cmds = {0}, other_cmds = {0}; |
| 175 | static int loaded; |
| 176 | char *default_strategy = NULL; |
| 177 | |
| 178 | if (!name) |
| 179 | return NULL; |
| 180 | |
| 181 | if (default_strategy && |
| 182 | !strcmp(default_strategy, "ort") && |
| 183 | !strcmp(name, "recursive")) { |
| 184 | name = "ort"; |
| 185 | } |
| 186 | |
| 187 | for (i = 0; i < ARRAY_SIZE(all_strategy); i++) |
| 188 | if (!strcmp(name, all_strategy[i].name)) |
| 189 | return &all_strategy[i]; |
| 190 | |
| 191 | if (!loaded) { |
| 192 | struct cmdnames not_strategies = {0}; |
| 193 | loaded = 1; |
| 194 | |
| 195 | load_command_list("git-merge-", &main_cmds, &other_cmds); |
| 196 | for (i = 0; i < main_cmds.cnt; i++) { |
| 197 | int j, found = 0; |
| 198 | struct cmdname *ent = main_cmds.names[i]; |
| 199 | for (j = 0; !found && j < ARRAY_SIZE(all_strategy); j++) |
| 200 | if (!xstrncmpz(all_strategy[j].name, ent->name, ent->len)) |
| 201 | found = 1; |
| 202 | if (!found) |
| 203 | add_cmdname(¬_strategies, ent->name, ent->len); |
| 204 | } |
| 205 | exclude_cmds(&main_cmds, ¬_strategies); |
| 206 | |
| 207 | cmdnames_release(¬_strategies); |
| 208 | } |
| 209 | if (!is_in_cmdlist(&main_cmds, name) && !is_in_cmdlist(&other_cmds, name)) { |
| 210 | fprintf(stderr, _("Could not find merge strategy '%s'.\n"), name); |
| 211 | fprintf(stderr, _("Available strategies are:")); |
| 212 | for (i = 0; i < main_cmds.cnt; i++) |
| 213 | fprintf(stderr, " %s", main_cmds.names[i]->name); |
| 214 | fprintf(stderr, ".\n"); |
| 215 | if (other_cmds.cnt) { |
| 216 | fprintf(stderr, _("Available custom strategies are:")); |
| 217 | for (i = 0; i < other_cmds.cnt; i++) |
| 218 | fprintf(stderr, " %s", other_cmds.names[i]->name); |
| 219 | fprintf(stderr, ".\n"); |
| 220 | } |
| 221 | exit(1); |
| 222 | } |
| 223 | |
| 224 | CALLOC_ARRAY(ret, 1); |
| 225 | ret->name = xstrdup(name); |
| 226 | ret->attr = NO_TRIVIAL; |
| 227 | |
| 228 | cmdnames_release(&main_cmds); |
| 229 | cmdnames_release(&other_cmds); |
| 230 | return ret; |
| 231 | } |
| 232 | |
| 233 | static void append_strategy(struct strategy *s) |
| 234 | { |
| 235 | ALLOC_GROW(use_strategies, use_strategies_nr + 1, use_strategies_alloc); |
| 236 | use_strategies[use_strategies_nr++] = s; |
| 237 | } |
| 238 | |
| 239 | static int option_parse_strategy(const struct option *opt UNUSED, |
| 240 | const char *name, int unset) |
| 241 | { |
| 242 | if (unset) |
| 243 | return 0; |
| 244 | |
| 245 | append_strategy(get_strategy(name)); |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static int option_parse_compact_summary(const struct option *opt, |
| 250 | const char *name UNUSED, int unset) |
| 251 | { |
| 252 | int *setting = opt->value; |
| 253 | |
| 254 | if (unset) |
| 255 | *setting = 0; |
| 256 | else |
| 257 | *setting = MERGE_SHOW_COMPACTSUMMARY; |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | static struct option builtin_merge_options[] = { |
| 262 | OPT_SET_INT('n', NULL, &show_diffstat, |
| 263 | N_("do not show a diffstat at the end of the merge"), 0), |
| 264 | OPT_BOOL(0, "stat", &show_diffstat, |
| 265 | N_("show a diffstat at the end of the merge")), |
| 266 | OPT_BOOL(0, "summary", &show_diffstat, N_("(synonym to --stat)")), |
| 267 | OPT_CALLBACK_F(0, "compact-summary", &show_diffstat, NULL, |
| 268 | N_("show a compact-summary at the end of the merge"), |
| 269 | PARSE_OPT_NOARG, |
| 270 | option_parse_compact_summary), |
| 271 | { |
| 272 | .type = OPTION_INTEGER, |
| 273 | .long_name = "log", |
| 274 | .value = &shortlog_len, |
| 275 | .precision = sizeof(shortlog_len), |
| 276 | .argh = N_("n"), |
| 277 | .help = N_("add (at most <n>) entries from shortlog to merge commit message"), |
| 278 | .flags = PARSE_OPT_OPTARG, |
| 279 | .defval = DEFAULT_MERGE_LOG_LEN, |
| 280 | }, |
| 281 | OPT_BOOL(0, "squash", &squash, |
| 282 | N_("create a single commit instead of doing a merge")), |
| 283 | OPT_BOOL(0, "commit", &option_commit, |
| 284 | N_("perform a commit if the merge succeeds (default)")), |
| 285 | OPT_BOOL('e', "edit", &option_edit, |
| 286 | N_("edit message before committing")), |
| 287 | OPT_CLEANUP(&cleanup_arg), |
| 288 | OPT_SET_INT(0, "ff", &fast_forward, N_("allow fast-forward (default)"), FF_ALLOW), |
| 289 | OPT_SET_INT_F(0, "ff-only", &fast_forward, |
| 290 | N_("abort if fast-forward is not possible"), |
| 291 | FF_ONLY, PARSE_OPT_NONEG), |
| 292 | OPT_RERERE_AUTOUPDATE(&allow_rerere_auto), |
| 293 | OPT_BOOL(0, "verify-signatures", &verify_signatures, |
| 294 | N_("verify that the named commit has a valid GPG signature")), |
| 295 | OPT_CALLBACK('s', "strategy", NULL, N_("strategy"), |
| 296 | N_("merge strategy to use"), option_parse_strategy), |
| 297 | OPT_STRVEC('X', "strategy-option", &xopts, N_("option=value"), |
| 298 | N_("option for selected merge strategy")), |
| 299 | OPT_CALLBACK('m', "message", &merge_msg, N_("message"), |
| 300 | N_("merge commit message (for a non-fast-forward merge)"), |
| 301 | option_parse_message), |
| 302 | { |
| 303 | .type = OPTION_LOWLEVEL_CALLBACK, |
| 304 | .short_name = 'F', |
| 305 | .long_name = "file", |
| 306 | .value = &merge_msg, |
| 307 | .argh = N_("path"), |
| 308 | .help = N_("read message from file"), |
| 309 | .flags = PARSE_OPT_NONEG, |
| 310 | .ll_callback = option_read_message, |
| 311 | }, |
| 312 | OPT_STRING(0, "into-name", &into_name, N_("name"), |
| 313 | N_("use <name> instead of the real target")), |
| 314 | OPT__VERBOSITY(&verbosity), |
| 315 | OPT_BOOL(0, "abort", &abort_current_merge, |
| 316 | N_("abort the current in-progress merge")), |
| 317 | OPT_BOOL(0, "quit", &quit_current_merge, |
| 318 | N_("--abort but leave index and working tree alone")), |
| 319 | OPT_BOOL(0, "continue", &continue_current_merge, |
| 320 | N_("continue the current in-progress merge")), |
| 321 | OPT_BOOL(0, "allow-unrelated-histories", &allow_unrelated_histories, |
| 322 | N_("allow merging unrelated histories")), |
| 323 | OPT_SET_INT(0, "progress", &show_progress, N_("force progress reporting"), 1), |
| 324 | { |
| 325 | .type = OPTION_STRING, |
| 326 | .short_name = 'S', |
| 327 | .long_name = "gpg-sign", |
| 328 | .value = &sign_commit, |
| 329 | .argh = N_("key-id"), |
| 330 | .help = N_("GPG sign commit"), |
| 331 | .flags = PARSE_OPT_OPTARG, |
| 332 | .defval = (intptr_t) "", |
| 333 | }, |
| 334 | OPT_AUTOSTASH(&autostash), |
| 335 | OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore, N_("update ignored files (default)")), |
| 336 | OPT_BOOL(0, "signoff", &signoff, N_("add a Signed-off-by trailer")), |
| 337 | OPT_BOOL(0, "no-verify", &no_verify, N_("bypass pre-merge-commit and commit-msg hooks")), |
| 338 | OPT_END() |
| 339 | }; |
| 340 | |
| 341 | static int save_state(struct object_id *stash) |
| 342 | { |
| 343 | int len; |
| 344 | struct child_process cp = CHILD_PROCESS_INIT; |
| 345 | struct strbuf buffer = STRBUF_INIT; |
| 346 | struct lock_file lock_file = LOCK_INIT; |
| 347 | int fd; |
| 348 | int rc = -1; |
| 349 | |
| 350 | fd = repo_hold_locked_index(the_repository, &lock_file, 0); |
| 351 | refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL); |
| 352 | if (0 <= fd) |
| 353 | repo_update_index_if_able(the_repository, &lock_file); |
| 354 | rollback_lock_file(&lock_file); |
| 355 | |
| 356 | strvec_pushl(&cp.args, "stash", "create", NULL); |
| 357 | cp.out = -1; |
| 358 | cp.git_cmd = 1; |
| 359 | |
| 360 | if (start_command(&cp)) |
| 361 | die(_("could not run stash.")); |
| 362 | len = strbuf_read(&buffer, cp.out, 1024); |
| 363 | close(cp.out); |
| 364 | |
| 365 | if (finish_command(&cp) || len < 0) |
| 366 | die(_("stash failed")); |
| 367 | else if (!len) /* no changes */ |
| 368 | goto out; |
| 369 | strbuf_setlen(&buffer, buffer.len-1); |
| 370 | if (repo_get_oid(the_repository, buffer.buf, stash)) |
| 371 | die(_("not a valid object: %s"), buffer.buf); |
| 372 | rc = 0; |
| 373 | out: |
| 374 | strbuf_release(&buffer); |
| 375 | return rc; |
| 376 | } |
| 377 | |
| 378 | static void read_empty(const struct object_id *oid) |
| 379 | { |
| 380 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 381 | |
| 382 | strvec_pushl(&cmd.args, "read-tree", "-m", "-u", |
| 383 | empty_tree_oid_hex(the_repository->hash_algo), |
| 384 | oid_to_hex(oid), NULL); |
| 385 | cmd.git_cmd = 1; |
| 386 | |
| 387 | if (run_command(&cmd)) |
| 388 | die(_("read-tree failed")); |
| 389 | } |
| 390 | |
| 391 | static void reset_hard(const struct object_id *oid) |
| 392 | { |
| 393 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 394 | |
| 395 | strvec_pushl(&cmd.args, "read-tree", "-v", "--reset", "-u", |
| 396 | oid_to_hex(oid), NULL); |
| 397 | cmd.git_cmd = 1; |
| 398 | |
| 399 | if (run_command(&cmd)) |
| 400 | die(_("read-tree failed")); |
| 401 | } |
| 402 | |
| 403 | static void restore_state(const struct object_id *head, |
| 404 | const struct object_id *stash) |
| 405 | { |
| 406 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 407 | |
| 408 | reset_hard(head); |
| 409 | |
| 410 | if (is_null_oid(stash)) |
| 411 | goto refresh_cache; |
| 412 | |
| 413 | strvec_pushl(&cmd.args, "stash", "apply", "--index", "--quiet", NULL); |
| 414 | strvec_push(&cmd.args, oid_to_hex(stash)); |
| 415 | |
| 416 | /* |
| 417 | * It is OK to ignore error here, for example when there was |
| 418 | * nothing to restore. |
| 419 | */ |
| 420 | cmd.git_cmd = 1; |
| 421 | run_command(&cmd); |
| 422 | |
| 423 | refresh_cache: |
| 424 | discard_index(the_repository->index); |
| 425 | if (repo_read_index(the_repository) < 0) |
| 426 | die(_("could not read index")); |
| 427 | } |
| 428 | |
| 429 | /* This is called when no merge was necessary. */ |
| 430 | static void finish_up_to_date(void) |
| 431 | { |
| 432 | if (verbosity >= 0) { |
| 433 | if (squash) |
| 434 | puts(_("Already up to date. (nothing to squash)")); |
| 435 | else |
| 436 | puts(_("Already up to date.")); |
| 437 | } |
| 438 | remove_merge_branch_state(the_repository); |
| 439 | } |
| 440 | |
| 441 | static void squash_message(struct commit *commit, struct commit_list *remoteheads) |
| 442 | { |
| 443 | struct rev_info rev; |
| 444 | struct strbuf out = STRBUF_INIT; |
| 445 | struct commit_list *j; |
| 446 | struct pretty_print_context ctx = {0}; |
| 447 | |
| 448 | printf(_("Squash commit -- not updating HEAD\n")); |
| 449 | |
| 450 | repo_init_revisions(the_repository, &rev, NULL); |
| 451 | diff_merges_suppress(&rev); |
| 452 | rev.commit_format = CMIT_FMT_MEDIUM; |
| 453 | |
| 454 | commit->object.flags |= UNINTERESTING; |
| 455 | add_pending_object(&rev, &commit->object, NULL); |
| 456 | |
| 457 | for (j = remoteheads; j; j = j->next) |
| 458 | add_pending_object(&rev, &j->item->object, NULL); |
| 459 | |
| 460 | setup_revisions(0, NULL, &rev, NULL); |
| 461 | if (prepare_revision_walk(&rev)) |
| 462 | die(_("revision walk setup failed")); |
| 463 | |
| 464 | ctx.abbrev = rev.abbrev; |
| 465 | ctx.date_mode = rev.date_mode; |
| 466 | ctx.fmt = rev.commit_format; |
| 467 | |
| 468 | strbuf_addstr(&out, "Squashed commit of the following:\n"); |
| 469 | while ((commit = get_revision(&rev)) != NULL) { |
| 470 | strbuf_addch(&out, '\n'); |
| 471 | strbuf_addf(&out, "commit %s\n", |
| 472 | oid_to_hex(&commit->object.oid)); |
| 473 | pretty_print_commit(&ctx, commit, &out); |
| 474 | } |
| 475 | write_file_buf(git_path_squash_msg(the_repository), out.buf, out.len); |
| 476 | strbuf_release(&out); |
| 477 | release_revisions(&rev); |
| 478 | } |
| 479 | |
| 480 | static void finish(struct commit *head_commit, |
| 481 | struct commit_list *remoteheads, |
| 482 | const struct object_id *new_head, const char *msg) |
| 483 | { |
| 484 | struct strbuf reflog_message = STRBUF_INIT; |
| 485 | const struct object_id *head = &head_commit->object.oid; |
| 486 | |
| 487 | if (!msg) |
| 488 | strbuf_addstr(&reflog_message, getenv("GIT_REFLOG_ACTION")); |
| 489 | else { |
| 490 | if (verbosity >= 0) |
| 491 | printf("%s\n", msg); |
| 492 | strbuf_addf(&reflog_message, "%s: %s", |
| 493 | getenv("GIT_REFLOG_ACTION"), msg); |
| 494 | } |
| 495 | if (squash) { |
| 496 | squash_message(head_commit, remoteheads); |
| 497 | } else { |
| 498 | if (verbosity >= 0 && !merge_msg.len) |
| 499 | printf(_("No merge message -- not updating HEAD\n")); |
| 500 | else { |
| 501 | refs_update_ref(get_main_ref_store(the_repository), |
| 502 | reflog_message.buf, "HEAD", new_head, |
| 503 | head, |
| 504 | 0, UPDATE_REFS_DIE_ON_ERR); |
| 505 | /* |
| 506 | * We ignore errors in 'gc --auto', since the |
| 507 | * user should see them. |
| 508 | */ |
| 509 | run_auto_maintenance(the_repository, verbosity < 0); |
| 510 | } |
| 511 | } |
| 512 | if (new_head && show_diffstat) { |
| 513 | struct diff_options opts; |
| 514 | repo_diff_setup(the_repository, &opts); |
| 515 | init_diffstat_widths(&opts); |
| 516 | |
| 517 | switch (show_diffstat) { |
| 518 | case MERGE_SHOW_DIFFSTAT: /* 1 */ |
| 519 | opts.output_format |= |
| 520 | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT; |
| 521 | break; |
| 522 | case MERGE_SHOW_COMPACTSUMMARY: /* 2 */ |
| 523 | opts.output_format |= DIFF_FORMAT_DIFFSTAT; |
| 524 | opts.flags.stat_with_summary = 1; |
| 525 | break; |
| 526 | default: |
| 527 | break; |
| 528 | } |
| 529 | opts.detect_rename = DIFF_DETECT_RENAME; |
| 530 | diff_setup_done(&opts); |
| 531 | diff_tree_oid(head, new_head, "", &opts); |
| 532 | diffcore_std(&opts); |
| 533 | diff_flush(&opts); |
| 534 | } |
| 535 | |
| 536 | /* Run a post-merge hook */ |
| 537 | run_hooks_l(the_repository, "post-merge", squash ? "1" : "0", NULL); |
| 538 | |
| 539 | if (new_head) |
| 540 | apply_autostash_ref(the_repository, "MERGE_AUTOSTASH", |
| 541 | NULL, NULL, NULL, NULL); |
| 542 | strbuf_release(&reflog_message); |
| 543 | } |
| 544 | |
| 545 | /* Get the name for the merge commit's message. */ |
| 546 | static void merge_name(const char *remote, struct strbuf *msg) |
| 547 | { |
| 548 | struct commit *remote_head; |
| 549 | struct object_id branch_head; |
| 550 | struct strbuf bname = STRBUF_INIT; |
| 551 | struct merge_remote_desc *desc; |
| 552 | const char *ptr; |
| 553 | char *found_ref = NULL; |
| 554 | int len, early; |
| 555 | |
| 556 | copy_branchname(&bname, remote, 0); |
| 557 | remote = bname.buf; |
| 558 | |
| 559 | oidclr(&branch_head, the_repository->hash_algo); |
| 560 | remote_head = get_merge_parent(remote); |
| 561 | if (!remote_head) |
| 562 | die(_("'%s' does not point to a commit"), remote); |
| 563 | |
| 564 | if (repo_dwim_ref(the_repository, remote, strlen(remote), &branch_head, |
| 565 | &found_ref, 0) > 0) { |
| 566 | if (starts_with(found_ref, "refs/heads/")) { |
| 567 | strbuf_addf(msg, "%s\t\tbranch '%s' of .\n", |
| 568 | oid_to_hex(&branch_head), remote); |
| 569 | goto cleanup; |
| 570 | } |
| 571 | if (starts_with(found_ref, "refs/tags/")) { |
| 572 | strbuf_addf(msg, "%s\t\ttag '%s' of .\n", |
| 573 | oid_to_hex(&branch_head), remote); |
| 574 | goto cleanup; |
| 575 | } |
| 576 | if (starts_with(found_ref, "refs/remotes/")) { |
| 577 | strbuf_addf(msg, "%s\t\tremote-tracking branch '%s' of .\n", |
| 578 | oid_to_hex(&branch_head), remote); |
| 579 | goto cleanup; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | /* See if remote matches <name>^^^.. or <name>~<number> */ |
| 584 | for (len = 0, ptr = remote + strlen(remote); |
| 585 | remote < ptr && ptr[-1] == '^'; |
| 586 | ptr--) |
| 587 | len++; |
| 588 | if (len) |
| 589 | early = 1; |
| 590 | else { |
| 591 | early = 0; |
| 592 | ptr = strrchr(remote, '~'); |
| 593 | if (ptr) { |
| 594 | int seen_nonzero = 0; |
| 595 | |
| 596 | len++; /* count ~ */ |
| 597 | while (*++ptr && isdigit(*ptr)) { |
| 598 | seen_nonzero |= (*ptr != '0'); |
| 599 | len++; |
| 600 | } |
| 601 | if (*ptr) |
| 602 | len = 0; /* not ...~<number> */ |
| 603 | else if (seen_nonzero) |
| 604 | early = 1; |
| 605 | else if (len == 1) |
| 606 | early = 1; /* "name~" is "name~1"! */ |
| 607 | } |
| 608 | } |
| 609 | if (len) { |
| 610 | struct strbuf truname = STRBUF_INIT; |
| 611 | strbuf_addf(&truname, "refs/heads/%s", remote); |
| 612 | strbuf_setlen(&truname, truname.len - len); |
| 613 | if (refs_ref_exists(get_main_ref_store(the_repository), truname.buf)) { |
| 614 | strbuf_addf(msg, |
| 615 | "%s\t\tbranch '%s'%s of .\n", |
| 616 | oid_to_hex(&remote_head->object.oid), |
| 617 | truname.buf + 11, |
| 618 | (early ? " (early part)" : "")); |
| 619 | strbuf_release(&truname); |
| 620 | goto cleanup; |
| 621 | } |
| 622 | strbuf_release(&truname); |
| 623 | } |
| 624 | |
| 625 | desc = merge_remote_util(remote_head); |
| 626 | if (desc && desc->obj && desc->obj->type == OBJ_TAG) { |
| 627 | strbuf_addf(msg, "%s\t\t%s '%s'\n", |
| 628 | oid_to_hex(&desc->obj->oid), |
| 629 | type_name(desc->obj->type), |
| 630 | remote); |
| 631 | goto cleanup; |
| 632 | } |
| 633 | |
| 634 | strbuf_addf(msg, "%s\t\tcommit '%s'\n", |
| 635 | oid_to_hex(&remote_head->object.oid), remote); |
| 636 | cleanup: |
| 637 | free(found_ref); |
| 638 | strbuf_release(&bname); |
| 639 | } |
| 640 | |
| 641 | static void parse_branch_merge_options(char *bmo) |
| 642 | { |
| 643 | const char **argv; |
| 644 | int argc; |
| 645 | |
| 646 | if (!bmo) |
| 647 | return; |
| 648 | argc = split_cmdline(bmo, &argv); |
| 649 | if (argc < 0) |
| 650 | die(_("Bad branch.%s.mergeoptions string: %s"), branch, |
| 651 | _(split_cmdline_strerror(argc))); |
| 652 | REALLOC_ARRAY(argv, argc + 2); |
| 653 | MOVE_ARRAY(argv + 1, argv, argc + 1); |
| 654 | argc++; |
| 655 | argv[0] = "branch.*.mergeoptions"; |
| 656 | parse_options(argc, argv, NULL, builtin_merge_options, |
| 657 | builtin_merge_usage, 0); |
| 658 | free(argv); |
| 659 | } |
| 660 | |
| 661 | static int git_merge_config(const char *k, const char *v, |
| 662 | const struct config_context *ctx, void *cb) |
| 663 | { |
| 664 | int status; |
| 665 | const char *str; |
| 666 | |
| 667 | if (branch && |
| 668 | skip_prefix(k, "branch.", &str) && |
| 669 | skip_prefix(str, branch, &str) && |
| 670 | !strcmp(str, ".mergeoptions")) { |
| 671 | free(branch_mergeoptions); |
| 672 | branch_mergeoptions = xstrdup(v); |
| 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat")) { |
| 677 | int val = git_parse_maybe_bool_text(v); |
| 678 | switch (val) { |
| 679 | case 0: |
| 680 | show_diffstat = 0; |
| 681 | break; |
| 682 | case 1: |
| 683 | show_diffstat = MERGE_SHOW_DIFFSTAT; |
| 684 | break; |
| 685 | default: |
| 686 | if (!strcmp(v, "compact")) |
| 687 | show_diffstat = MERGE_SHOW_COMPACTSUMMARY; |
| 688 | /* |
| 689 | * We do not need to have an explicit |
| 690 | * |
| 691 | * else if (!strcmp(v, "diffstat")) |
| 692 | * show_diffstat = MERGE_SHOW_DIFFSTAT; |
| 693 | * |
| 694 | * here, because the catch-all uses the |
| 695 | * diffstat style anyway. |
| 696 | */ |
| 697 | else |
| 698 | /* |
| 699 | * A setting from a future? It is not an |
| 700 | * error grave enough to fail the command. |
| 701 | * proceed using the default one. |
| 702 | */ |
| 703 | show_diffstat = MERGE_SHOW_DIFFSTAT; |
| 704 | break; |
| 705 | } |
| 706 | } else if (!strcmp(k, "merge.verifysignatures")) { |
| 707 | verify_signatures = git_config_bool(k, v); |
| 708 | } else if (!strcmp(k, "pull.twohead")) { |
| 709 | FREE_AND_NULL(pull_twohead); |
| 710 | return git_config_string(&pull_twohead, k, v); |
| 711 | } else if (!strcmp(k, "pull.octopus")) { |
| 712 | FREE_AND_NULL(pull_octopus); |
| 713 | return git_config_string(&pull_octopus, k, v); |
| 714 | } else if (!strcmp(k, "commit.cleanup")) { |
| 715 | return git_config_string(&cleanup_arg, k, v); |
| 716 | } else if (!strcmp(k, "merge.ff")) { |
| 717 | int boolval = git_parse_maybe_bool(v); |
| 718 | if (0 <= boolval) { |
| 719 | fast_forward = boolval ? FF_ALLOW : FF_NO; |
| 720 | } else if (v && !strcmp(v, "only")) { |
| 721 | fast_forward = FF_ONLY; |
| 722 | } /* do not barf on values from future versions of git */ |
| 723 | return 0; |
| 724 | } else if (!strcmp(k, "merge.defaulttoupstream")) { |
| 725 | default_to_upstream = git_config_bool(k, v); |
| 726 | return 0; |
| 727 | } else if (!strcmp(k, "commit.gpgsign")) { |
| 728 | sign_commit = git_config_bool(k, v) ? "" : NULL; |
| 729 | return 0; |
| 730 | } else if (!strcmp(k, "gpg.mintrustlevel")) { |
| 731 | check_trust_level = 0; |
| 732 | } else if (!strcmp(k, "merge.autostash")) { |
| 733 | autostash = git_config_bool(k, v); |
| 734 | return 0; |
| 735 | } |
| 736 | |
| 737 | status = fmt_merge_msg_config(k, v, ctx, cb); |
| 738 | if (status) |
| 739 | return status; |
| 740 | return git_diff_ui_config(k, v, ctx, cb); |
| 741 | } |
| 742 | |
| 743 | static int read_tree_trivial(struct object_id *common, struct object_id *head, |
| 744 | struct object_id *one) |
| 745 | { |
| 746 | int i, nr_trees = 0; |
| 747 | struct tree *trees[MAX_UNPACK_TREES]; |
| 748 | struct tree_desc t[MAX_UNPACK_TREES]; |
| 749 | struct unpack_trees_options opts; |
| 750 | |
| 751 | memset(&opts, 0, sizeof(opts)); |
| 752 | opts.head_idx = 2; |
| 753 | opts.src_index = the_repository->index; |
| 754 | opts.dst_index = the_repository->index; |
| 755 | opts.update = 1; |
| 756 | opts.verbose_update = 1; |
| 757 | opts.trivial_merges_only = 1; |
| 758 | opts.merge = 1; |
| 759 | opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */ |
| 760 | trees[nr_trees] = repo_parse_tree_indirect(the_repository, common); |
| 761 | if (!trees[nr_trees++]) |
| 762 | return -1; |
| 763 | trees[nr_trees] = repo_parse_tree_indirect(the_repository, head); |
| 764 | if (!trees[nr_trees++]) |
| 765 | return -1; |
| 766 | trees[nr_trees] = repo_parse_tree_indirect(the_repository, one); |
| 767 | if (!trees[nr_trees++]) |
| 768 | return -1; |
| 769 | opts.fn = threeway_merge; |
| 770 | cache_tree_free(&the_repository->index->cache_tree); |
| 771 | for (i = 0; i < nr_trees; i++) { |
| 772 | repo_parse_tree(the_repository, trees[i]); |
| 773 | init_tree_desc(t+i, &trees[i]->object.oid, |
| 774 | trees[i]->buffer, trees[i]->size); |
| 775 | } |
| 776 | if (unpack_trees(nr_trees, t, &opts)) |
| 777 | return -1; |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | static void write_tree_trivial(struct object_id *oid) |
| 782 | { |
| 783 | if (write_index_as_tree(oid, the_repository->index, |
| 784 | repo_get_index_file(the_repository), |
| 785 | 0, NULL)) |
| 786 | die(_("git write-tree failed to write a tree")); |
| 787 | } |
| 788 | |
| 789 | static int try_merge_strategy(const char *strategy, struct commit_list *common, |
| 790 | struct commit_list *remoteheads, |
| 791 | struct commit *head) |
| 792 | { |
| 793 | const char *head_arg = "HEAD"; |
| 794 | |
| 795 | if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, |
| 796 | SKIP_IF_UNCHANGED, 0, NULL, NULL, |
| 797 | NULL) < 0) |
| 798 | die(_("Unable to write index.")); |
| 799 | |
| 800 | if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree") || |
| 801 | !strcmp(strategy, "ort")) { |
| 802 | struct lock_file lock = LOCK_INIT; |
| 803 | int clean, x; |
| 804 | struct commit *result; |
| 805 | struct commit_list *reversed = NULL; |
| 806 | struct merge_options o; |
| 807 | struct commit_list *j; |
| 808 | |
| 809 | if (remoteheads->next) { |
| 810 | error(_("Not handling anything other than two heads merge.")); |
| 811 | return 2; |
| 812 | } |
| 813 | |
| 814 | init_ui_merge_options(&o, the_repository); |
| 815 | if (!strcmp(strategy, "subtree")) |
| 816 | o.subtree_shift = ""; |
| 817 | |
| 818 | o.show_rename_progress = |
| 819 | show_progress == -1 ? isatty(2) : show_progress; |
| 820 | |
| 821 | for (x = 0; x < xopts.nr; x++) |
| 822 | if (parse_merge_opt(&o, xopts.v[x])) |
| 823 | die(_("unknown strategy option: -X%s"), xopts.v[x]); |
| 824 | |
| 825 | o.branch1 = head_arg; |
| 826 | o.branch2 = merge_remote_util(remoteheads->item)->name; |
| 827 | |
| 828 | for (j = common; j; j = j->next) |
| 829 | commit_list_insert(j->item, &reversed); |
| 830 | |
| 831 | repo_hold_locked_index(the_repository, &lock, |
| 832 | LOCK_DIE_ON_ERROR); |
| 833 | clean = merge_ort_recursive(&o, head, remoteheads->item, |
| 834 | reversed, &result); |
| 835 | commit_list_free(reversed); |
| 836 | strbuf_release(&o.obuf); |
| 837 | |
| 838 | if (clean < 0) { |
| 839 | rollback_lock_file(&lock); |
| 840 | return 2; |
| 841 | } |
| 842 | if (write_locked_index(the_repository->index, &lock, |
| 843 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) |
| 844 | die(_("unable to write %s"), repo_get_index_file(the_repository)); |
| 845 | return clean ? 0 : 1; |
| 846 | } else { |
| 847 | return try_merge_command(the_repository, |
| 848 | strategy, xopts.nr, xopts.v, |
| 849 | common, head_arg, remoteheads); |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | static void count_diff_files(struct diff_queue_struct *q, |
| 854 | struct diff_options *opt UNUSED, void *data) |
| 855 | { |
| 856 | int *count = data; |
| 857 | |
| 858 | (*count) += q->nr; |
| 859 | } |
| 860 | |
| 861 | static int count_unmerged_entries(void) |
| 862 | { |
| 863 | int i, ret = 0; |
| 864 | |
| 865 | for (i = 0; i < the_repository->index->cache_nr; i++) |
| 866 | if (ce_stage(the_repository->index->cache[i])) |
| 867 | ret++; |
| 868 | |
| 869 | return ret; |
| 870 | } |
| 871 | |
| 872 | static void add_strategies(const char *string, unsigned attr) |
| 873 | { |
| 874 | int i; |
| 875 | |
| 876 | if (string) { |
| 877 | struct string_list list = STRING_LIST_INIT_DUP; |
| 878 | struct string_list_item *item; |
| 879 | string_list_split(&list, string, " ", -1); |
| 880 | for_each_string_list_item(item, &list) |
| 881 | append_strategy(get_strategy(item->string)); |
| 882 | string_list_clear(&list, 0); |
| 883 | return; |
| 884 | } |
| 885 | for (i = 0; i < ARRAY_SIZE(all_strategy); i++) |
| 886 | if (all_strategy[i].attr & attr) |
| 887 | append_strategy(&all_strategy[i]); |
| 888 | |
| 889 | } |
| 890 | |
| 891 | static void read_merge_msg(struct strbuf *msg) |
| 892 | { |
| 893 | const char *filename = git_path_merge_msg(the_repository); |
| 894 | strbuf_reset(msg); |
| 895 | if (strbuf_read_file(msg, filename, 0) < 0) |
| 896 | die_errno(_("Could not read from '%s'"), filename); |
| 897 | } |
| 898 | |
| 899 | static void write_merge_state(struct commit_list *); |
| 900 | static void abort_commit(struct commit_list *remoteheads, const char *err_msg) |
| 901 | { |
| 902 | if (err_msg) |
| 903 | error("%s", err_msg); |
| 904 | fprintf(stderr, |
| 905 | _("Not committing merge; use 'git commit' to complete the merge.\n")); |
| 906 | write_merge_state(remoteheads); |
| 907 | exit(1); |
| 908 | } |
| 909 | |
| 910 | static const char merge_editor_comment[] = |
| 911 | N_("Please enter a commit message to explain why this merge is necessary,\n" |
| 912 | "especially if it merges an updated upstream into a topic branch.\n" |
| 913 | "\n"); |
| 914 | |
| 915 | static const char scissors_editor_comment[] = |
| 916 | N_("An empty message aborts the commit.\n"); |
| 917 | |
| 918 | static const char no_scissors_editor_comment[] = |
| 919 | N_("Lines starting with '%s' will be ignored, and an empty message aborts\n" |
| 920 | "the commit.\n"); |
| 921 | |
| 922 | static void write_merge_heads(struct commit_list *); |
| 923 | static void prepare_to_commit(struct commit_list *remoteheads) |
| 924 | { |
| 925 | struct strbuf msg = STRBUF_INIT; |
| 926 | const char *index_file = repo_get_index_file(the_repository); |
| 927 | |
| 928 | if (!no_verify) { |
| 929 | int invoked_hook; |
| 930 | |
| 931 | if (run_commit_hook(0 < option_edit, index_file, &invoked_hook, |
| 932 | "pre-merge-commit", NULL)) |
| 933 | abort_commit(remoteheads, NULL); |
| 934 | /* |
| 935 | * Re-read the index as pre-merge-commit hook could have updated it, |
| 936 | * and write it out as a tree. We must do this before we invoke |
| 937 | * the editor and after we invoke run_status above. |
| 938 | */ |
| 939 | if (invoked_hook) |
| 940 | discard_index(the_repository->index); |
| 941 | } |
| 942 | read_index_from(the_repository->index, index_file, |
| 943 | repo_get_git_dir(the_repository)); |
| 944 | strbuf_addbuf(&msg, &merge_msg); |
| 945 | if (squash) |
| 946 | BUG("the control must not reach here under --squash"); |
| 947 | if (0 < option_edit) { |
| 948 | strbuf_addch(&msg, '\n'); |
| 949 | if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) { |
| 950 | wt_status_append_cut_line(&msg); |
| 951 | strbuf_commented_addf(&msg, comment_line_str, "\n"); |
| 952 | } |
| 953 | strbuf_commented_addf(&msg, comment_line_str, |
| 954 | _(merge_editor_comment)); |
| 955 | if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) |
| 956 | strbuf_commented_addf(&msg, comment_line_str, |
| 957 | _(scissors_editor_comment)); |
| 958 | else |
| 959 | strbuf_commented_addf(&msg, comment_line_str, |
| 960 | _(no_scissors_editor_comment), comment_line_str); |
| 961 | } |
| 962 | if (signoff) |
| 963 | append_signoff(&msg, ignored_log_message_bytes(msg.buf, msg.len), 0); |
| 964 | write_merge_heads(remoteheads); |
| 965 | write_file_buf(git_path_merge_msg(the_repository), msg.buf, msg.len); |
| 966 | if (run_commit_hook(0 < option_edit, repo_get_index_file(the_repository), |
| 967 | NULL, "prepare-commit-msg", |
| 968 | git_path_merge_msg(the_repository), "merge", NULL)) |
| 969 | abort_commit(remoteheads, NULL); |
| 970 | if (0 < option_edit) { |
| 971 | if (launch_editor(git_path_merge_msg(the_repository), NULL, NULL)) |
| 972 | abort_commit(remoteheads, NULL); |
| 973 | } |
| 974 | |
| 975 | if (!no_verify && run_commit_hook(0 < option_edit, repo_get_index_file(the_repository), |
| 976 | NULL, "commit-msg", |
| 977 | git_path_merge_msg(the_repository), NULL)) |
| 978 | abort_commit(remoteheads, NULL); |
| 979 | |
| 980 | read_merge_msg(&msg); |
| 981 | cleanup_message(&msg, cleanup_mode, 0); |
| 982 | if (!msg.len) |
| 983 | abort_commit(remoteheads, _("Empty commit message.")); |
| 984 | strbuf_release(&merge_msg); |
| 985 | strbuf_addbuf(&merge_msg, &msg); |
| 986 | strbuf_release(&msg); |
| 987 | } |
| 988 | |
| 989 | static int merge_trivial(struct commit *head, struct commit_list *remoteheads) |
| 990 | { |
| 991 | struct object_id result_tree, result_commit; |
| 992 | struct commit_list *parents = NULL, **pptr = &parents; |
| 993 | |
| 994 | if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, |
| 995 | SKIP_IF_UNCHANGED, 0, NULL, NULL, |
| 996 | NULL) < 0) |
| 997 | return error(_("Unable to write index.")); |
| 998 | |
| 999 | write_tree_trivial(&result_tree); |
| 1000 | printf(_("Wonderful.\n")); |
| 1001 | pptr = commit_list_append(head, pptr); |
| 1002 | pptr = commit_list_append(remoteheads->item, pptr); |
| 1003 | prepare_to_commit(remoteheads); |
| 1004 | if (commit_tree(merge_msg.buf, merge_msg.len, &result_tree, parents, |
| 1005 | &result_commit, NULL, sign_commit)) |
| 1006 | die(_("failed to write commit object")); |
| 1007 | finish(head, remoteheads, &result_commit, "In-index merge"); |
| 1008 | |
| 1009 | remove_merge_branch_state(the_repository); |
| 1010 | commit_list_free(parents); |
| 1011 | return 0; |
| 1012 | } |
| 1013 | |
| 1014 | static int finish_automerge(struct commit *head, |
| 1015 | int head_subsumed, |
| 1016 | struct commit_list *common, |
| 1017 | struct commit_list *remoteheads, |
| 1018 | struct object_id *result_tree, |
| 1019 | const char *wt_strategy) |
| 1020 | { |
| 1021 | struct commit_list *parents = NULL; |
| 1022 | struct strbuf buf = STRBUF_INIT; |
| 1023 | struct object_id result_commit; |
| 1024 | |
| 1025 | write_tree_trivial(result_tree); |
| 1026 | commit_list_free(common); |
| 1027 | parents = remoteheads; |
| 1028 | if (!head_subsumed || fast_forward == FF_NO) |
| 1029 | commit_list_insert(head, &parents); |
| 1030 | prepare_to_commit(remoteheads); |
| 1031 | if (commit_tree(merge_msg.buf, merge_msg.len, result_tree, parents, |
| 1032 | &result_commit, NULL, sign_commit)) |
| 1033 | die(_("failed to write commit object")); |
| 1034 | strbuf_addf(&buf, "Merge made by the '%s' strategy.", wt_strategy); |
| 1035 | finish(head, remoteheads, &result_commit, buf.buf); |
| 1036 | |
| 1037 | strbuf_release(&buf); |
| 1038 | remove_merge_branch_state(the_repository); |
| 1039 | commit_list_free(parents); |
| 1040 | return 0; |
| 1041 | } |
| 1042 | |
| 1043 | static int suggest_conflicts(void) |
| 1044 | { |
| 1045 | const char *filename; |
| 1046 | FILE *fp; |
| 1047 | struct strbuf msgbuf = STRBUF_INIT; |
| 1048 | |
| 1049 | filename = git_path_merge_msg(the_repository); |
| 1050 | fp = xfopen(filename, "a"); |
| 1051 | |
| 1052 | /* |
| 1053 | * We can't use cleanup_mode because if we're not using the editor, |
| 1054 | * get_cleanup_mode will return COMMIT_MSG_CLEANUP_SPACE instead, even |
| 1055 | * though the message is meant to be processed later by git-commit. |
| 1056 | * Thus, we will get the cleanup mode which is returned when we _are_ |
| 1057 | * using an editor. |
| 1058 | */ |
| 1059 | append_conflicts_hint(the_repository->index, &msgbuf, |
| 1060 | get_cleanup_mode(cleanup_arg, 1)); |
| 1061 | fputs(msgbuf.buf, fp); |
| 1062 | strbuf_release(&msgbuf); |
| 1063 | fclose(fp); |
| 1064 | repo_rerere(the_repository, allow_rerere_auto); |
| 1065 | printf(_("Automatic merge failed; " |
| 1066 | "fix conflicts and then commit the result.\n")); |
| 1067 | return 1; |
| 1068 | } |
| 1069 | |
| 1070 | static int evaluate_result(void) |
| 1071 | { |
| 1072 | int cnt = 0; |
| 1073 | struct rev_info rev; |
| 1074 | |
| 1075 | /* Check how many files differ. */ |
| 1076 | repo_init_revisions(the_repository, &rev, ""); |
| 1077 | setup_revisions(0, NULL, &rev, NULL); |
| 1078 | rev.diffopt.output_format |= |
| 1079 | DIFF_FORMAT_CALLBACK; |
| 1080 | rev.diffopt.format_callback = count_diff_files; |
| 1081 | rev.diffopt.format_callback_data = &cnt; |
| 1082 | run_diff_files(&rev, 0); |
| 1083 | |
| 1084 | /* |
| 1085 | * Check how many unmerged entries are |
| 1086 | * there. |
| 1087 | */ |
| 1088 | cnt += count_unmerged_entries(); |
| 1089 | |
| 1090 | release_revisions(&rev); |
| 1091 | return cnt; |
| 1092 | } |
| 1093 | |
| 1094 | /* |
| 1095 | * Pretend as if the user told us to merge with the remote-tracking |
| 1096 | * branch we have for the upstream of the current branch |
| 1097 | */ |
| 1098 | static int setup_with_upstream(const char ***argv) |
| 1099 | { |
| 1100 | struct branch *branch = branch_get(NULL); |
| 1101 | int i; |
| 1102 | const char **args; |
| 1103 | |
| 1104 | if (!branch) |
| 1105 | die(_("No current branch.")); |
| 1106 | if (!branch->remote_name) |
| 1107 | die(_("No remote for the current branch.")); |
| 1108 | if (!branch->merge_nr) |
| 1109 | die(_("No default upstream defined for the current branch.")); |
| 1110 | |
| 1111 | args = xcalloc(st_add(branch->merge_nr, 1), sizeof(char *)); |
| 1112 | for (i = 0; i < branch->merge_nr; i++) { |
| 1113 | if (!branch->merge[i]->dst) |
| 1114 | die(_("No remote-tracking branch for %s from %s"), |
| 1115 | branch->merge[i]->src, branch->remote_name); |
| 1116 | args[i] = branch->merge[i]->dst; |
| 1117 | } |
| 1118 | args[i] = NULL; |
| 1119 | *argv = args; |
| 1120 | return i; |
| 1121 | } |
| 1122 | |
| 1123 | static void write_merge_heads(struct commit_list *remoteheads) |
| 1124 | { |
| 1125 | struct commit_list *j; |
| 1126 | struct strbuf buf = STRBUF_INIT; |
| 1127 | |
| 1128 | for (j = remoteheads; j; j = j->next) { |
| 1129 | struct object_id *oid; |
| 1130 | struct commit *c = j->item; |
| 1131 | struct merge_remote_desc *desc; |
| 1132 | |
| 1133 | desc = merge_remote_util(c); |
| 1134 | if (desc && desc->obj) { |
| 1135 | oid = &desc->obj->oid; |
| 1136 | } else { |
| 1137 | oid = &c->object.oid; |
| 1138 | } |
| 1139 | strbuf_addf(&buf, "%s\n", oid_to_hex(oid)); |
| 1140 | } |
| 1141 | write_file_buf(git_path_merge_head(the_repository), buf.buf, buf.len); |
| 1142 | |
| 1143 | strbuf_reset(&buf); |
| 1144 | if (fast_forward == FF_NO) |
| 1145 | strbuf_addstr(&buf, "no-ff"); |
| 1146 | write_file_buf(git_path_merge_mode(the_repository), buf.buf, buf.len); |
| 1147 | strbuf_release(&buf); |
| 1148 | } |
| 1149 | |
| 1150 | static void write_merge_state(struct commit_list *remoteheads) |
| 1151 | { |
| 1152 | write_merge_heads(remoteheads); |
| 1153 | strbuf_addch(&merge_msg, '\n'); |
| 1154 | write_file_buf(git_path_merge_msg(the_repository), merge_msg.buf, |
| 1155 | merge_msg.len); |
| 1156 | } |
| 1157 | |
| 1158 | static int default_edit_option(void) |
| 1159 | { |
| 1160 | static const char name[] = "GIT_MERGE_AUTOEDIT"; |
| 1161 | const char *e = getenv(name); |
| 1162 | struct stat st_stdin, st_stdout; |
| 1163 | |
| 1164 | if (have_message) |
| 1165 | /* an explicit -m msg without --[no-]edit */ |
| 1166 | return 0; |
| 1167 | |
| 1168 | if (e) { |
| 1169 | int v = git_parse_maybe_bool(e); |
| 1170 | if (v < 0) |
| 1171 | die(_("Bad value '%s' in environment '%s'"), e, name); |
| 1172 | return v; |
| 1173 | } |
| 1174 | |
| 1175 | /* Use editor if stdin and stdout are the same and is a tty */ |
| 1176 | return (!fstat(0, &st_stdin) && |
| 1177 | !fstat(1, &st_stdout) && |
| 1178 | isatty(0) && isatty(1) && |
| 1179 | st_stdin.st_dev == st_stdout.st_dev && |
| 1180 | st_stdin.st_ino == st_stdout.st_ino && |
| 1181 | st_stdin.st_mode == st_stdout.st_mode); |
| 1182 | } |
| 1183 | |
| 1184 | static struct commit_list *reduce_parents(struct commit *head_commit, |
| 1185 | int *head_subsumed, |
| 1186 | struct commit_list *remoteheads) |
| 1187 | { |
| 1188 | struct commit_list *parents, **remotes; |
| 1189 | |
| 1190 | /* |
| 1191 | * Is the current HEAD reachable from another commit being |
| 1192 | * merged? If so we do not want to record it as a parent of |
| 1193 | * the resulting merge, unless --no-ff is given. We will flip |
| 1194 | * this variable to 0 when we find HEAD among the independent |
| 1195 | * tips being merged. |
| 1196 | */ |
| 1197 | *head_subsumed = 1; |
| 1198 | |
| 1199 | /* Find what parents to record by checking independent ones. */ |
| 1200 | parents = reduce_heads(remoteheads); |
| 1201 | commit_list_free(remoteheads); |
| 1202 | |
| 1203 | remoteheads = NULL; |
| 1204 | remotes = &remoteheads; |
| 1205 | while (parents) { |
| 1206 | struct commit *commit = pop_commit(&parents); |
| 1207 | if (commit == head_commit) |
| 1208 | *head_subsumed = 0; |
| 1209 | else |
| 1210 | remotes = &commit_list_insert(commit, remotes)->next; |
| 1211 | } |
| 1212 | return remoteheads; |
| 1213 | } |
| 1214 | |
| 1215 | static void prepare_merge_message(struct strbuf *merge_names, struct strbuf *merge_msg) |
| 1216 | { |
| 1217 | struct fmt_merge_msg_opts opts; |
| 1218 | |
| 1219 | memset(&opts, 0, sizeof(opts)); |
| 1220 | opts.add_title = !have_message; |
| 1221 | opts.shortlog_len = shortlog_len; |
| 1222 | opts.credit_people = (0 < option_edit); |
| 1223 | opts.into_name = into_name; |
| 1224 | |
| 1225 | fmt_merge_msg(merge_names, merge_msg, &opts); |
| 1226 | if (merge_msg->len) |
| 1227 | strbuf_setlen(merge_msg, merge_msg->len - 1); |
| 1228 | } |
| 1229 | |
| 1230 | static void handle_fetch_head(struct commit_list **remotes, struct strbuf *merge_names) |
| 1231 | { |
| 1232 | const char *filename; |
| 1233 | int fd, pos, npos; |
| 1234 | struct strbuf fetch_head_file = STRBUF_INIT; |
| 1235 | const unsigned hexsz = the_hash_algo->hexsz; |
| 1236 | |
| 1237 | if (!merge_names) |
| 1238 | merge_names = &fetch_head_file; |
| 1239 | |
| 1240 | filename = git_path_fetch_head(the_repository); |
| 1241 | fd = xopen(filename, O_RDONLY); |
| 1242 | |
| 1243 | if (strbuf_read(merge_names, fd, 0) < 0) |
| 1244 | die_errno(_("could not read '%s'"), filename); |
| 1245 | if (close(fd) < 0) |
| 1246 | die_errno(_("could not close '%s'"), filename); |
| 1247 | |
| 1248 | for (pos = 0; pos < merge_names->len; pos = npos) { |
| 1249 | struct object_id oid; |
| 1250 | char *ptr; |
| 1251 | struct commit *commit; |
| 1252 | |
| 1253 | ptr = strchr(merge_names->buf + pos, '\n'); |
| 1254 | if (ptr) |
| 1255 | npos = ptr - merge_names->buf + 1; |
| 1256 | else |
| 1257 | npos = merge_names->len; |
| 1258 | |
| 1259 | if (npos - pos < hexsz + 2 || |
| 1260 | get_oid_hex(merge_names->buf + pos, &oid)) |
| 1261 | commit = NULL; /* bad */ |
| 1262 | else if (memcmp(merge_names->buf + pos + hexsz, "\t\t", 2)) |
| 1263 | continue; /* not-for-merge */ |
| 1264 | else { |
| 1265 | char saved = merge_names->buf[pos + hexsz]; |
| 1266 | merge_names->buf[pos + hexsz] = '\0'; |
| 1267 | commit = get_merge_parent(merge_names->buf + pos); |
| 1268 | merge_names->buf[pos + hexsz] = saved; |
| 1269 | } |
| 1270 | if (!commit) { |
| 1271 | if (ptr) |
| 1272 | *ptr = '\0'; |
| 1273 | die(_("not something we can merge in %s: %s"), |
| 1274 | filename, merge_names->buf + pos); |
| 1275 | } |
| 1276 | remotes = &commit_list_insert(commit, remotes)->next; |
| 1277 | } |
| 1278 | |
| 1279 | if (merge_names == &fetch_head_file) |
| 1280 | strbuf_release(&fetch_head_file); |
| 1281 | } |
| 1282 | |
| 1283 | static struct commit_list *collect_parents(struct commit *head_commit, |
| 1284 | int *head_subsumed, |
| 1285 | int argc, const char **argv, |
| 1286 | struct strbuf *merge_msg) |
| 1287 | { |
| 1288 | int i; |
| 1289 | struct commit_list *remoteheads = NULL; |
| 1290 | struct commit_list **remotes = &remoteheads; |
| 1291 | struct strbuf merge_names = STRBUF_INIT, *autogen = NULL; |
| 1292 | |
| 1293 | if (merge_msg && (!have_message || shortlog_len)) |
| 1294 | autogen = &merge_names; |
| 1295 | |
| 1296 | if (head_commit) |
| 1297 | remotes = &commit_list_insert(head_commit, remotes)->next; |
| 1298 | |
| 1299 | if (argc == 1 && !strcmp(argv[0], "FETCH_HEAD")) { |
| 1300 | handle_fetch_head(remotes, autogen); |
| 1301 | remoteheads = reduce_parents(head_commit, head_subsumed, remoteheads); |
| 1302 | } else { |
| 1303 | for (i = 0; i < argc; i++) { |
| 1304 | struct commit *commit = get_merge_parent(argv[i]); |
| 1305 | if (!commit) |
| 1306 | help_unknown_ref(argv[i], "merge", |
| 1307 | _("not something we can merge")); |
| 1308 | remotes = &commit_list_insert(commit, remotes)->next; |
| 1309 | } |
| 1310 | remoteheads = reduce_parents(head_commit, head_subsumed, remoteheads); |
| 1311 | if (autogen) { |
| 1312 | struct commit_list *p; |
| 1313 | for (p = remoteheads; p; p = p->next) |
| 1314 | merge_name(merge_remote_util(p->item)->name, autogen); |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | if (autogen) { |
| 1319 | prepare_merge_message(autogen, merge_msg); |
| 1320 | strbuf_release(autogen); |
| 1321 | } |
| 1322 | |
| 1323 | return remoteheads; |
| 1324 | } |
| 1325 | |
| 1326 | static int merging_a_throwaway_tag(struct commit *commit) |
| 1327 | { |
| 1328 | char *tag_ref; |
| 1329 | struct object_id oid; |
| 1330 | int is_throwaway_tag = 0; |
| 1331 | |
| 1332 | /* Are we merging a tag? */ |
| 1333 | if (!merge_remote_util(commit) || |
| 1334 | !merge_remote_util(commit)->obj || |
| 1335 | merge_remote_util(commit)->obj->type != OBJ_TAG) |
| 1336 | return is_throwaway_tag; |
| 1337 | |
| 1338 | /* |
| 1339 | * Now we know we are merging a tag object. Are we downstream |
| 1340 | * and following the tags from upstream? If so, we must have |
| 1341 | * the tag object pointed at by "refs/tags/$T" where $T is the |
| 1342 | * tagname recorded in the tag object. We want to allow such |
| 1343 | * a "just to catch up" merge to fast-forward. |
| 1344 | * |
| 1345 | * Otherwise, we are playing an integrator's role, making a |
| 1346 | * merge with a throw-away tag from a contributor with |
| 1347 | * something like "git pull $contributor $signed_tag". |
| 1348 | * We want to forbid such a merge from fast-forwarding |
| 1349 | * by default; otherwise we would not keep the signature |
| 1350 | * anywhere. |
| 1351 | */ |
| 1352 | tag_ref = xstrfmt("refs/tags/%s", |
| 1353 | ((struct tag *)merge_remote_util(commit)->obj)->tag); |
| 1354 | if (!refs_read_ref(get_main_ref_store(the_repository), tag_ref, &oid) && |
| 1355 | oideq(&oid, &merge_remote_util(commit)->obj->oid)) |
| 1356 | is_throwaway_tag = 0; |
| 1357 | else |
| 1358 | is_throwaway_tag = 1; |
| 1359 | free(tag_ref); |
| 1360 | return is_throwaway_tag; |
| 1361 | } |
| 1362 | |
| 1363 | int cmd_merge(int argc, |
| 1364 | const char **argv, |
| 1365 | const char *prefix, |
| 1366 | struct repository *repo UNUSED) |
| 1367 | { |
| 1368 | struct object_id result_tree, stash, head_oid; |
| 1369 | struct commit *head_commit; |
| 1370 | struct strbuf buf = STRBUF_INIT; |
| 1371 | int i, ret = 0, head_subsumed; |
| 1372 | int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0; |
| 1373 | struct commit_list *common = NULL; |
| 1374 | const char *best_strategy = NULL, *wt_strategy = NULL; |
| 1375 | struct commit_list *remoteheads = NULL, *p; |
| 1376 | void *branch_to_free; |
| 1377 | int orig_argc = argc; |
| 1378 | int merge_log_config = -1; |
| 1379 | |
| 1380 | show_usage_with_options_if_asked(argc, argv, |
| 1381 | builtin_merge_usage, builtin_merge_options); |
| 1382 | |
| 1383 | #ifndef WITH_BREAKING_CHANGES |
| 1384 | warn_on_auto_comment_char = true; |
| 1385 | #endif /* !WITH_BREAKING_CHANGES */ |
| 1386 | prepare_repo_settings(the_repository); |
| 1387 | the_repository->settings.command_requires_full_index = 0; |
| 1388 | |
| 1389 | /* |
| 1390 | * Check if we are _not_ on a detached HEAD, i.e. if there is a |
| 1391 | * current branch. |
| 1392 | */ |
| 1393 | branch = branch_to_free = refs_resolve_refdup(get_main_ref_store(the_repository), |
| 1394 | "HEAD", 0, &head_oid, |
| 1395 | NULL); |
| 1396 | if (branch) |
| 1397 | skip_prefix(branch, "refs/heads/", &branch); |
| 1398 | |
| 1399 | init_diff_ui_defaults(); |
| 1400 | repo_config(the_repository, git_merge_config, &merge_log_config); |
| 1401 | |
| 1402 | if (!branch || is_null_oid(&head_oid)) |
| 1403 | head_commit = NULL; |
| 1404 | else |
| 1405 | head_commit = lookup_commit_or_die(&head_oid, "HEAD"); |
| 1406 | |
| 1407 | if (branch_mergeoptions) |
| 1408 | parse_branch_merge_options(branch_mergeoptions); |
| 1409 | argc = parse_options(argc, argv, prefix, builtin_merge_options, |
| 1410 | builtin_merge_usage, 0); |
| 1411 | if (shortlog_len < 0) |
| 1412 | shortlog_len = (merge_log_config > 0) ? merge_log_config : 0; |
| 1413 | |
| 1414 | if (verbosity < 0 && show_progress == -1) |
| 1415 | show_progress = 0; |
| 1416 | |
| 1417 | if (abort_current_merge) { |
| 1418 | int nargc = 2; |
| 1419 | const char *nargv[] = {"reset", "--merge", NULL}; |
| 1420 | char stash_oid_hex[GIT_MAX_HEXSZ + 1]; |
| 1421 | struct object_id stash_oid = {0}; |
| 1422 | |
| 1423 | if (orig_argc != 2) |
| 1424 | usage_msg_opt(_("--abort expects no arguments"), |
| 1425 | builtin_merge_usage, builtin_merge_options); |
| 1426 | |
| 1427 | if (!file_exists(git_path_merge_head(the_repository))) |
| 1428 | die(_("There is no merge to abort (MERGE_HEAD missing).")); |
| 1429 | |
| 1430 | if (!refs_read_ref(get_main_ref_store(the_repository), "MERGE_AUTOSTASH", &stash_oid)) |
| 1431 | refs_delete_ref(get_main_ref_store(the_repository), |
| 1432 | "", "MERGE_AUTOSTASH", &stash_oid, |
| 1433 | REF_NO_DEREF); |
| 1434 | |
| 1435 | /* Invoke 'git reset --merge' */ |
| 1436 | ret = cmd_reset(nargc, nargv, prefix, the_repository); |
| 1437 | |
| 1438 | if (!is_null_oid(&stash_oid)) { |
| 1439 | oid_to_hex_r(stash_oid_hex, &stash_oid); |
| 1440 | apply_autostash_oid(stash_oid_hex); |
| 1441 | } |
| 1442 | |
| 1443 | goto done; |
| 1444 | } |
| 1445 | |
| 1446 | if (quit_current_merge) { |
| 1447 | if (orig_argc != 2) |
| 1448 | usage_msg_opt(_("--quit expects no arguments"), |
| 1449 | builtin_merge_usage, |
| 1450 | builtin_merge_options); |
| 1451 | |
| 1452 | remove_merge_branch_state(the_repository); |
| 1453 | goto done; |
| 1454 | } |
| 1455 | |
| 1456 | if (continue_current_merge) { |
| 1457 | int nargc = 1; |
| 1458 | const char *nargv[] = {"commit", NULL}; |
| 1459 | |
| 1460 | if (orig_argc != 2) |
| 1461 | usage_msg_opt(_("--continue expects no arguments"), |
| 1462 | builtin_merge_usage, builtin_merge_options); |
| 1463 | |
| 1464 | if (!file_exists(git_path_merge_head(the_repository))) |
| 1465 | die(_("There is no merge in progress (MERGE_HEAD missing).")); |
| 1466 | |
| 1467 | /* Invoke 'git commit' */ |
| 1468 | ret = cmd_commit(nargc, nargv, prefix, the_repository); |
| 1469 | goto done; |
| 1470 | } |
| 1471 | |
| 1472 | if (repo_read_index_unmerged(the_repository)) |
| 1473 | die_resolve_conflict("merge"); |
| 1474 | |
| 1475 | if (file_exists(git_path_merge_head(the_repository))) { |
| 1476 | /* |
| 1477 | * There is no unmerged entry, don't advise 'git |
| 1478 | * add/rm <file>', just 'git commit'. |
| 1479 | */ |
| 1480 | if (advice_enabled(ADVICE_RESOLVE_CONFLICT)) |
| 1481 | die(_("You have not concluded your merge (MERGE_HEAD exists).\n" |
| 1482 | "Please, commit your changes before you merge.")); |
| 1483 | else |
| 1484 | die(_("You have not concluded your merge (MERGE_HEAD exists).")); |
| 1485 | } |
| 1486 | if (refs_ref_exists(get_main_ref_store(the_repository), "CHERRY_PICK_HEAD")) { |
| 1487 | if (advice_enabled(ADVICE_RESOLVE_CONFLICT)) |
| 1488 | die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n" |
| 1489 | "Please, commit your changes before you merge.")); |
| 1490 | else |
| 1491 | die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).")); |
| 1492 | } |
| 1493 | resolve_undo_clear_index(the_repository->index); |
| 1494 | |
| 1495 | if (option_edit < 0) |
| 1496 | option_edit = default_edit_option(); |
| 1497 | |
| 1498 | cleanup_mode = get_cleanup_mode(cleanup_arg, 0 < option_edit); |
| 1499 | |
| 1500 | if (verbosity < 0) |
| 1501 | show_diffstat = 0; |
| 1502 | |
| 1503 | if (squash) { |
| 1504 | if (fast_forward == FF_NO) |
| 1505 | die(_("options '%s' and '%s' cannot be used together"), "--squash", "--no-ff."); |
| 1506 | if (option_commit > 0) |
| 1507 | die(_("options '%s' and '%s' cannot be used together"), "--squash", "--commit."); |
| 1508 | /* |
| 1509 | * squash can now silently disable option_commit - this is not |
| 1510 | * a problem as it is only overriding the default, not a user |
| 1511 | * supplied option. |
| 1512 | */ |
| 1513 | option_commit = 0; |
| 1514 | } |
| 1515 | |
| 1516 | if (option_commit < 0) |
| 1517 | option_commit = 1; |
| 1518 | |
| 1519 | if (!argc) { |
| 1520 | if (default_to_upstream) |
| 1521 | argc = setup_with_upstream(&argv); |
| 1522 | else |
| 1523 | die(_("No commit specified and merge.defaultToUpstream not set.")); |
| 1524 | } else if (argc == 1 && !strcmp(argv[0], "-")) { |
| 1525 | argv[0] = "@{-1}"; |
| 1526 | } |
| 1527 | |
| 1528 | if (!argc) |
| 1529 | usage_with_options(builtin_merge_usage, |
| 1530 | builtin_merge_options); |
| 1531 | |
| 1532 | if (!head_commit) { |
| 1533 | /* |
| 1534 | * If the merged head is a valid one there is no reason |
| 1535 | * to forbid "git merge" into a branch yet to be born. |
| 1536 | * We do the same for "git pull". |
| 1537 | */ |
| 1538 | struct object_id *remote_head_oid; |
| 1539 | if (squash) |
| 1540 | die(_("Squash commit into empty head not supported yet")); |
| 1541 | if (fast_forward == FF_NO) |
| 1542 | die(_("Non-fast-forward commit does not make sense into " |
| 1543 | "an empty head")); |
| 1544 | remoteheads = collect_parents(head_commit, &head_subsumed, |
| 1545 | argc, argv, NULL); |
| 1546 | if (!remoteheads) |
| 1547 | die(_("%s - not something we can merge"), argv[0]); |
| 1548 | if (remoteheads->next) |
| 1549 | die(_("Can merge only exactly one commit into empty head")); |
| 1550 | |
| 1551 | if (verify_signatures) |
| 1552 | verify_merge_signature(remoteheads->item, verbosity, |
| 1553 | check_trust_level); |
| 1554 | |
| 1555 | remote_head_oid = &remoteheads->item->object.oid; |
| 1556 | read_empty(remote_head_oid); |
| 1557 | refs_update_ref(get_main_ref_store(the_repository), |
| 1558 | "initial pull", "HEAD", remote_head_oid, NULL, |
| 1559 | 0, |
| 1560 | UPDATE_REFS_DIE_ON_ERR); |
| 1561 | goto done; |
| 1562 | } |
| 1563 | |
| 1564 | /* |
| 1565 | * All the rest are the commits being merged; prepare |
| 1566 | * the standard merge summary message to be appended |
| 1567 | * to the given message. |
| 1568 | */ |
| 1569 | remoteheads = collect_parents(head_commit, &head_subsumed, |
| 1570 | argc, argv, &merge_msg); |
| 1571 | |
| 1572 | if (!head_commit || !argc) |
| 1573 | usage_with_options(builtin_merge_usage, |
| 1574 | builtin_merge_options); |
| 1575 | |
| 1576 | if (verify_signatures) { |
| 1577 | for (p = remoteheads; p; p = p->next) { |
| 1578 | verify_merge_signature(p->item, verbosity, |
| 1579 | check_trust_level); |
| 1580 | } |
| 1581 | } |
| 1582 | |
| 1583 | strbuf_addstr(&buf, "merge"); |
| 1584 | for (p = remoteheads; p; p = p->next) |
| 1585 | strbuf_addf(&buf, " %s", merge_remote_util(p->item)->name); |
| 1586 | setenv("GIT_REFLOG_ACTION", buf.buf, 0); |
| 1587 | strbuf_reset(&buf); |
| 1588 | |
| 1589 | for (p = remoteheads; p; p = p->next) { |
| 1590 | struct commit *commit = p->item; |
| 1591 | strbuf_addf(&buf, "GITHEAD_%s", |
| 1592 | oid_to_hex(&commit->object.oid)); |
| 1593 | setenv(buf.buf, merge_remote_util(commit)->name, 1); |
| 1594 | strbuf_reset(&buf); |
| 1595 | if (fast_forward != FF_ONLY && merging_a_throwaway_tag(commit)) |
| 1596 | fast_forward = FF_NO; |
| 1597 | } |
| 1598 | |
| 1599 | if (!use_strategies) { |
| 1600 | if (!remoteheads) |
| 1601 | ; /* already up-to-date */ |
| 1602 | else if (!remoteheads->next) |
| 1603 | add_strategies(pull_twohead, DEFAULT_TWOHEAD); |
| 1604 | else |
| 1605 | add_strategies(pull_octopus, DEFAULT_OCTOPUS); |
| 1606 | } |
| 1607 | |
| 1608 | for (i = 0; i < use_strategies_nr; i++) { |
| 1609 | if (use_strategies[i]->attr & NO_FAST_FORWARD) |
| 1610 | fast_forward = FF_NO; |
| 1611 | if (use_strategies[i]->attr & NO_TRIVIAL) |
| 1612 | allow_trivial = 0; |
| 1613 | } |
| 1614 | |
| 1615 | if (!remoteheads) |
| 1616 | ; /* already up-to-date */ |
| 1617 | else if (!remoteheads->next) { |
| 1618 | if (repo_get_merge_bases(the_repository, head_commit, |
| 1619 | remoteheads->item, &common) < 0) { |
| 1620 | ret = 2; |
| 1621 | goto done; |
| 1622 | } |
| 1623 | } else { |
| 1624 | struct commit_list *list = remoteheads; |
| 1625 | commit_list_insert(head_commit, &list); |
| 1626 | if (get_octopus_merge_bases(list, &common) < 0) { |
| 1627 | free(list); |
| 1628 | ret = 2; |
| 1629 | goto done; |
| 1630 | } |
| 1631 | free(list); |
| 1632 | } |
| 1633 | |
| 1634 | refs_update_ref(get_main_ref_store(the_repository), |
| 1635 | "updating ORIG_HEAD", "ORIG_HEAD", |
| 1636 | &head_commit->object.oid, NULL, 0, |
| 1637 | UPDATE_REFS_DIE_ON_ERR); |
| 1638 | |
| 1639 | if (remoteheads && !common) { |
| 1640 | /* No common ancestors found. */ |
| 1641 | if (!allow_unrelated_histories) |
| 1642 | die(_("refusing to merge unrelated histories")); |
| 1643 | /* otherwise, we need a real merge. */ |
| 1644 | } else if (!remoteheads || |
| 1645 | (!remoteheads->next && !common->next && |
| 1646 | common->item == remoteheads->item)) { |
| 1647 | /* |
| 1648 | * If head can reach all the merge then we are up to date. |
| 1649 | * but first the most common case of merging one remote. |
| 1650 | */ |
| 1651 | finish_up_to_date(); |
| 1652 | goto done; |
| 1653 | } else if (fast_forward != FF_NO && !remoteheads->next && |
| 1654 | !common->next && |
| 1655 | oideq(&common->item->object.oid, &head_commit->object.oid)) { |
| 1656 | /* Again the most common case of merging one remote. */ |
| 1657 | const char *msg = have_message ? |
| 1658 | "Fast-forward (no commit created; -m option ignored)" : |
| 1659 | "Fast-forward"; |
| 1660 | struct commit *commit; |
| 1661 | |
| 1662 | if (verbosity >= 0) { |
| 1663 | printf(_("Updating %s..%s\n"), |
| 1664 | repo_find_unique_abbrev(the_repository, &head_commit->object.oid, |
| 1665 | DEFAULT_ABBREV), |
| 1666 | repo_find_unique_abbrev(the_repository, &remoteheads->item->object.oid, |
| 1667 | DEFAULT_ABBREV)); |
| 1668 | } |
| 1669 | commit = remoteheads->item; |
| 1670 | if (!commit) { |
| 1671 | ret = 1; |
| 1672 | goto done; |
| 1673 | } |
| 1674 | |
| 1675 | if (autostash) |
| 1676 | create_autostash_ref(the_repository, "MERGE_AUTOSTASH", |
| 1677 | NULL, false); |
| 1678 | if (checkout_fast_forward(the_repository, |
| 1679 | &head_commit->object.oid, |
| 1680 | &commit->object.oid, |
| 1681 | overwrite_ignore)) { |
| 1682 | apply_autostash_ref(the_repository, "MERGE_AUTOSTASH", |
| 1683 | NULL, NULL, NULL, NULL); |
| 1684 | ret = 1; |
| 1685 | goto done; |
| 1686 | } |
| 1687 | |
| 1688 | finish(head_commit, remoteheads, &commit->object.oid, msg); |
| 1689 | remove_merge_branch_state(the_repository); |
| 1690 | goto done; |
| 1691 | } else if (!remoteheads->next && common->next) |
| 1692 | ; |
| 1693 | /* |
| 1694 | * We are not doing octopus and not fast-forward. Need |
| 1695 | * a real merge. |
| 1696 | */ |
| 1697 | else if (!remoteheads->next && !common->next && option_commit) { |
| 1698 | /* |
| 1699 | * We are not doing octopus, not fast-forward, and have |
| 1700 | * only one common. |
| 1701 | */ |
| 1702 | refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL); |
| 1703 | if (allow_trivial && fast_forward != FF_ONLY) { |
| 1704 | /* |
| 1705 | * Must first ensure that index matches HEAD before |
| 1706 | * attempting a trivial merge. |
| 1707 | */ |
| 1708 | struct tree *head_tree = repo_get_commit_tree(the_repository, |
| 1709 | head_commit); |
| 1710 | struct strbuf sb = STRBUF_INIT; |
| 1711 | |
| 1712 | if (repo_index_has_changes(the_repository, head_tree, |
| 1713 | &sb)) { |
| 1714 | error(_("Your local changes to the following files would be overwritten by merge:\n %s"), |
| 1715 | sb.buf); |
| 1716 | strbuf_release(&sb); |
| 1717 | ret = 2; |
| 1718 | goto done; |
| 1719 | } |
| 1720 | |
| 1721 | /* See if it is really trivial. */ |
| 1722 | git_committer_info(IDENT_STRICT); |
| 1723 | printf(_("Trying really trivial in-index merge...\n")); |
| 1724 | if (!read_tree_trivial(&common->item->object.oid, |
| 1725 | &head_commit->object.oid, |
| 1726 | &remoteheads->item->object.oid)) { |
| 1727 | ret = merge_trivial(head_commit, remoteheads); |
| 1728 | goto done; |
| 1729 | } |
| 1730 | printf(_("Nope.\n")); |
| 1731 | } |
| 1732 | } else { |
| 1733 | /* |
| 1734 | * An octopus. If we can reach all the remote we are up |
| 1735 | * to date. |
| 1736 | */ |
| 1737 | int up_to_date = 1; |
| 1738 | struct commit_list *j; |
| 1739 | |
| 1740 | for (j = remoteheads; j; j = j->next) { |
| 1741 | int ret = repo_in_merge_bases(the_repository, |
| 1742 | j->item, head_commit); |
| 1743 | if (ret < 0) |
| 1744 | exit(128); |
| 1745 | if (!ret) { |
| 1746 | up_to_date = 0; |
| 1747 | break; |
| 1748 | } |
| 1749 | } |
| 1750 | if (up_to_date) { |
| 1751 | finish_up_to_date(); |
| 1752 | goto done; |
| 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | if (fast_forward == FF_ONLY) |
| 1757 | die_ff_impossible(); |
| 1758 | |
| 1759 | if (autostash) |
| 1760 | create_autostash_ref(the_repository, "MERGE_AUTOSTASH", |
| 1761 | NULL, false); |
| 1762 | |
| 1763 | /* We are going to make a new commit. */ |
| 1764 | git_committer_info(IDENT_STRICT); |
| 1765 | |
| 1766 | /* |
| 1767 | * At this point, we need a real merge. No matter what strategy |
| 1768 | * we use, it would operate on the index, possibly affecting the |
| 1769 | * working tree, and when resolved cleanly, have the desired |
| 1770 | * tree in the index -- this means that the index must be in |
| 1771 | * sync with the head commit. The strategies are responsible |
| 1772 | * to ensure this. |
| 1773 | * |
| 1774 | * Stash away the local changes so that we can try more than one |
| 1775 | * and/or recover from merge strategies bailing while leaving the |
| 1776 | * index and working tree polluted. |
| 1777 | */ |
| 1778 | if (save_state(&stash)) |
| 1779 | oidclr(&stash, the_repository->hash_algo); |
| 1780 | |
| 1781 | for (i = 0; i < use_strategies_nr; i++) { |
| 1782 | int ret, cnt; |
| 1783 | if (i) { |
| 1784 | printf(_("Rewinding the tree to pristine...\n")); |
| 1785 | restore_state(&head_commit->object.oid, &stash); |
| 1786 | } |
| 1787 | if (use_strategies_nr != 1) |
| 1788 | printf(_("Trying merge strategy %s...\n"), |
| 1789 | use_strategies[i]->name); |
| 1790 | /* |
| 1791 | * Remember which strategy left the state in the working |
| 1792 | * tree. |
| 1793 | */ |
| 1794 | wt_strategy = use_strategies[i]->name; |
| 1795 | |
| 1796 | ret = try_merge_strategy(wt_strategy, |
| 1797 | common, remoteheads, |
| 1798 | head_commit); |
| 1799 | /* |
| 1800 | * The backend exits with 1 when conflicts are |
| 1801 | * left to be resolved, with 2 when it does not |
| 1802 | * handle the given merge at all. |
| 1803 | */ |
| 1804 | if (ret < 2) { |
| 1805 | if (!ret) { |
| 1806 | /* |
| 1807 | * This strategy worked; no point in trying |
| 1808 | * another. |
| 1809 | */ |
| 1810 | merge_was_ok = 1; |
| 1811 | best_strategy = wt_strategy; |
| 1812 | break; |
| 1813 | } |
| 1814 | cnt = (use_strategies_nr > 1) ? evaluate_result() : 0; |
| 1815 | if (best_cnt <= 0 || cnt <= best_cnt) { |
| 1816 | best_strategy = wt_strategy; |
| 1817 | best_cnt = cnt; |
| 1818 | } |
| 1819 | } |
| 1820 | } |
| 1821 | |
| 1822 | /* |
| 1823 | * If we have a resulting tree, that means the strategy module |
| 1824 | * auto resolved the merge cleanly. |
| 1825 | */ |
| 1826 | if (merge_was_ok && option_commit) { |
| 1827 | automerge_was_ok = 1; |
| 1828 | ret = finish_automerge(head_commit, head_subsumed, |
| 1829 | common, remoteheads, |
| 1830 | &result_tree, wt_strategy); |
| 1831 | goto done; |
| 1832 | } |
| 1833 | |
| 1834 | /* |
| 1835 | * Pick the result from the best strategy and have the user fix |
| 1836 | * it up. |
| 1837 | */ |
| 1838 | if (!best_strategy) { |
| 1839 | restore_state(&head_commit->object.oid, &stash); |
| 1840 | if (use_strategies_nr > 1) |
| 1841 | fprintf(stderr, |
| 1842 | _("No merge strategy handled the merge.\n")); |
| 1843 | else |
| 1844 | fprintf(stderr, _("Merge with strategy %s failed.\n"), |
| 1845 | use_strategies[0]->name); |
| 1846 | apply_autostash_ref(the_repository, "MERGE_AUTOSTASH", |
| 1847 | NULL, NULL, NULL, NULL); |
| 1848 | ret = 2; |
| 1849 | goto done; |
| 1850 | } else if (best_strategy == wt_strategy) |
| 1851 | ; /* We already have its result in the working tree. */ |
| 1852 | else { |
| 1853 | printf(_("Rewinding the tree to pristine...\n")); |
| 1854 | restore_state(&head_commit->object.oid, &stash); |
| 1855 | printf(_("Using the %s strategy to prepare resolving by hand.\n"), |
| 1856 | best_strategy); |
| 1857 | try_merge_strategy(best_strategy, common, remoteheads, |
| 1858 | head_commit); |
| 1859 | } |
| 1860 | |
| 1861 | if (squash) { |
| 1862 | finish(head_commit, remoteheads, NULL, NULL); |
| 1863 | |
| 1864 | git_test_write_commit_graph_or_die(the_repository->objects->sources); |
| 1865 | } else |
| 1866 | write_merge_state(remoteheads); |
| 1867 | |
| 1868 | if (merge_was_ok) |
| 1869 | fprintf(stderr, _("Automatic merge went well; " |
| 1870 | "stopped before committing as requested\n")); |
| 1871 | else |
| 1872 | ret = suggest_conflicts(); |
| 1873 | if (autostash) |
| 1874 | printf(_("When finished, apply stashed changes with `git stash pop`\n")); |
| 1875 | |
| 1876 | done: |
| 1877 | if (!automerge_was_ok) { |
| 1878 | commit_list_free(common); |
| 1879 | commit_list_free(remoteheads); |
| 1880 | } |
| 1881 | strbuf_release(&buf); |
| 1882 | free(branch_to_free); |
| 1883 | free(pull_twohead); |
| 1884 | free(pull_octopus); |
| 1885 | discard_index(the_repository->index); |
| 1886 | return ret; |
| 1887 | } |