4 * Copyright (c) 2008 Miklos Vajna <vmiklos@frugalware.org>
6 * Based on git-merge.sh by Junio C Hamano.
9 #define USE_THE_INDEX_COMPATIBILITY_MACROS
12 #include "parse-options.h"
15 #include "run-command.h"
22 #include "unpack-trees.h"
23 #include "cache-tree.h"
30 #include "merge-recursive.h"
31 #include "merge-ort-wrappers.h"
32 #include "resolve-undo.h"
34 #include "fmt-merge-msg.h"
35 #include "gpg-interface.h"
36 #include "sequencer.h"
37 #include "string-list.h"
42 #include "commit-reach.h"
43 #include "wt-status.h"
44 #include "commit-graph.h"
46 #define DEFAULT_TWOHEAD (1<<0)
47 #define DEFAULT_OCTOPUS (1<<1)
48 #define NO_FAST_FORWARD (1<<2)
49 #define NO_TRIVIAL (1<<3)
56 static const char * const builtin_merge_usage[] = {
57 N_("git merge [<options>] [<commit>...]"),
58 N_("git merge --abort"),
59 N_("git merge --continue"),
63 static int show_diffstat = 1, shortlog_len = -1, squash;
64 static int option_commit = -1;
65 static int option_edit = -1;
66 static int allow_trivial = 1, have_message, verify_signatures;
67 static int check_trust_level = 1;
68 static int overwrite_ignore = 1;
69 static struct strbuf merge_msg = STRBUF_INIT;
70 static struct strategy **use_strategies;
71 static size_t use_strategies_nr, use_strategies_alloc;
72 static const char **xopts;
73 static size_t xopts_nr, xopts_alloc;
74 static const char *branch;
75 static char *branch_mergeoptions;
77 static int allow_rerere_auto;
78 static int abort_current_merge;
79 static int quit_current_merge;
80 static int continue_current_merge;
81 static int allow_unrelated_histories;
82 static int show_progress = -1;
83 static int default_to_upstream = 1;
85 static const char *sign_commit;
89 static struct strategy all_strategy[] = {
90 { "recursive", DEFAULT_TWOHEAD | NO_TRIVIAL },
91 { "octopus", DEFAULT_OCTOPUS },
92 { "ort", NO_TRIVIAL },
94 { "ours", NO_FAST_FORWARD | NO_TRIVIAL },
95 { "subtree", NO_FAST_FORWARD | NO_TRIVIAL },
98 static const char *pull_twohead, *pull_octopus;
106 static enum ff_type fast_forward = FF_ALLOW;
108 static const char *cleanup_arg;
109 static enum commit_msg_cleanup_mode cleanup_mode;
111 static int option_parse_message(const struct option *opt,
112 const char *arg, int unset)
114 struct strbuf *buf = opt->value;
117 strbuf_setlen(buf, 0);
119 strbuf_addf(buf, "%s%s", buf->len ? "\n\n" : "", arg);
122 return error(_("switch `m' requires a value"));
126 static enum parse_opt_result option_read_message(struct parse_opt_ctx_t *ctx,
127 const struct option *opt,
128 const char *arg_not_used,
131 struct strbuf *buf = opt->value;
134 BUG_ON_OPT_ARG(arg_not_used);
136 BUG("-F cannot be negated");
141 } else if (ctx->argc > 1) {
145 return error(_("option `%s' requires a value"), opt->long_name);
148 strbuf_addch(buf, '\n');
149 if (ctx->prefix && !is_absolute_path(arg))
150 arg = prefix_filename(ctx->prefix, arg);
151 if (strbuf_read_file(buf, arg, 0) < 0)
152 return error(_("could not read file '%s'"), arg);
158 static struct strategy *get_strategy(const char *name)
161 struct strategy *ret;
162 static struct cmdnames main_cmds, other_cmds;
164 char *default_strategy = getenv("GIT_TEST_MERGE_ALGORITHM");
169 if (default_strategy &&
170 !strcmp(default_strategy, "ort") &&
171 !strcmp(name, "recursive")) {
175 for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
176 if (!strcmp(name, all_strategy[i].name))
177 return &all_strategy[i];
180 struct cmdnames not_strategies;
183 memset(¬_strategies, 0, sizeof(struct cmdnames));
184 load_command_list("git-merge-", &main_cmds, &other_cmds);
185 for (i = 0; i < main_cmds.cnt; i++) {
187 struct cmdname *ent = main_cmds.names[i];
188 for (j = 0; j < ARRAY_SIZE(all_strategy); j++)
189 if (!strncmp(ent->name, all_strategy[j].name, ent->len)
190 && !all_strategy[j].name[ent->len])
193 add_cmdname(¬_strategies, ent->name, ent->len);
195 exclude_cmds(&main_cmds, ¬_strategies);
197 if (!is_in_cmdlist(&main_cmds, name) && !is_in_cmdlist(&other_cmds, name)) {
198 fprintf(stderr, _("Could not find merge strategy '%s'.\n"), name);
199 fprintf(stderr, _("Available strategies are:"));
200 for (i = 0; i < main_cmds.cnt; i++)
201 fprintf(stderr, " %s", main_cmds.names[i]->name);
202 fprintf(stderr, ".\n");
203 if (other_cmds.cnt) {
204 fprintf(stderr, _("Available custom strategies are:"));
205 for (i = 0; i < other_cmds.cnt; i++)
206 fprintf(stderr, " %s", other_cmds.names[i]->name);
207 fprintf(stderr, ".\n");
212 ret = xcalloc(1, sizeof(struct strategy));
213 ret->name = xstrdup(name);
214 ret->attr = NO_TRIVIAL;
218 static void append_strategy(struct strategy *s)
220 ALLOC_GROW(use_strategies, use_strategies_nr + 1, use_strategies_alloc);
221 use_strategies[use_strategies_nr++] = s;
224 static int option_parse_strategy(const struct option *opt,
225 const char *name, int unset)
230 append_strategy(get_strategy(name));
234 static int option_parse_x(const struct option *opt,
235 const char *arg, int unset)
240 ALLOC_GROW(xopts, xopts_nr + 1, xopts_alloc);
241 xopts[xopts_nr++] = xstrdup(arg);
245 static int option_parse_n(const struct option *opt,
246 const char *arg, int unset)
249 show_diffstat = unset;
253 static struct option builtin_merge_options[] = {
254 OPT_CALLBACK_F('n', NULL, NULL, NULL,
255 N_("do not show a diffstat at the end of the merge"),
256 PARSE_OPT_NOARG, option_parse_n),
257 OPT_BOOL(0, "stat", &show_diffstat,
258 N_("show a diffstat at the end of the merge")),
259 OPT_BOOL(0, "summary", &show_diffstat, N_("(synonym to --stat)")),
260 { OPTION_INTEGER, 0, "log", &shortlog_len, N_("n"),
261 N_("add (at most <n>) entries from shortlog to merge commit message"),
262 PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
263 OPT_BOOL(0, "squash", &squash,
264 N_("create a single commit instead of doing a merge")),
265 OPT_BOOL(0, "commit", &option_commit,
266 N_("perform a commit if the merge succeeds (default)")),
267 OPT_BOOL('e', "edit", &option_edit,
268 N_("edit message before committing")),
269 OPT_CLEANUP(&cleanup_arg),
270 OPT_SET_INT(0, "ff", &fast_forward, N_("allow fast-forward (default)"), FF_ALLOW),
271 OPT_SET_INT_F(0, "ff-only", &fast_forward,
272 N_("abort if fast-forward is not possible"),
273 FF_ONLY, PARSE_OPT_NONEG),
274 OPT_RERERE_AUTOUPDATE(&allow_rerere_auto),
275 OPT_BOOL(0, "verify-signatures", &verify_signatures,
276 N_("verify that the named commit has a valid GPG signature")),
277 OPT_CALLBACK('s', "strategy", &use_strategies, N_("strategy"),
278 N_("merge strategy to use"), option_parse_strategy),
279 OPT_CALLBACK('X', "strategy-option", &xopts, N_("option=value"),
280 N_("option for selected merge strategy"), option_parse_x),
281 OPT_CALLBACK('m', "message", &merge_msg, N_("message"),
282 N_("merge commit message (for a non-fast-forward merge)"),
283 option_parse_message),
284 { OPTION_LOWLEVEL_CALLBACK, 'F', "file", &merge_msg, N_("path"),
285 N_("read message from file"), PARSE_OPT_NONEG,
286 NULL, 0, option_read_message },
287 OPT__VERBOSITY(&verbosity),
288 OPT_BOOL(0, "abort", &abort_current_merge,
289 N_("abort the current in-progress merge")),
290 OPT_BOOL(0, "quit", &quit_current_merge,
291 N_("--abort but leave index and working tree alone")),
292 OPT_BOOL(0, "continue", &continue_current_merge,
293 N_("continue the current in-progress merge")),
294 OPT_BOOL(0, "allow-unrelated-histories", &allow_unrelated_histories,
295 N_("allow merging unrelated histories")),
296 OPT_SET_INT(0, "progress", &show_progress, N_("force progress reporting"), 1),
297 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
298 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
299 OPT_AUTOSTASH(&autostash),
300 OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore, N_("update ignored files (default)")),
301 OPT_BOOL(0, "signoff", &signoff, N_("add a Signed-off-by trailer")),
302 OPT_BOOL(0, "no-verify", &no_verify, N_("bypass pre-merge-commit and commit-msg hooks")),
306 static int save_state(struct object_id *stash)
309 struct child_process cp = CHILD_PROCESS_INIT;
310 struct strbuf buffer = STRBUF_INIT;
311 const char *argv[] = {"stash", "create", NULL};
318 if (start_command(&cp))
319 die(_("could not run stash."));
320 len = strbuf_read(&buffer, cp.out, 1024);
323 if (finish_command(&cp) || len < 0)
324 die(_("stash failed"));
325 else if (!len) /* no changes */
327 strbuf_setlen(&buffer, buffer.len-1);
328 if (get_oid(buffer.buf, stash))
329 die(_("not a valid object: %s"), buffer.buf);
332 strbuf_release(&buffer);
336 static void read_empty(const struct object_id *oid, int verbose)
341 args[i++] = "read-tree";
346 args[i++] = empty_tree_oid_hex();
347 args[i++] = oid_to_hex(oid);
350 if (run_command_v_opt(args, RUN_GIT_CMD))
351 die(_("read-tree failed"));
354 static void reset_hard(const struct object_id *oid, int verbose)
359 args[i++] = "read-tree";
362 args[i++] = "--reset";
364 args[i++] = oid_to_hex(oid);
367 if (run_command_v_opt(args, RUN_GIT_CMD))
368 die(_("read-tree failed"));
371 static void restore_state(const struct object_id *head,
372 const struct object_id *stash)
374 struct strbuf sb = STRBUF_INIT;
375 const char *args[] = { "stash", "apply", NULL, NULL };
377 if (is_null_oid(stash))
382 args[2] = oid_to_hex(stash);
385 * It is OK to ignore error here, for example when there was
386 * nothing to restore.
388 run_command_v_opt(args, RUN_GIT_CMD);
391 refresh_cache(REFRESH_QUIET);
394 /* This is called when no merge was necessary. */
395 static void finish_up_to_date(const char *msg)
398 printf("%s%s\n", squash ? _(" (nothing to squash)") : "", msg);
399 remove_merge_branch_state(the_repository);
402 static void squash_message(struct commit *commit, struct commit_list *remoteheads)
405 struct strbuf out = STRBUF_INIT;
406 struct commit_list *j;
407 struct pretty_print_context ctx = {0};
409 printf(_("Squash commit -- not updating HEAD\n"));
411 repo_init_revisions(the_repository, &rev, NULL);
412 rev.ignore_merges = 1;
413 rev.commit_format = CMIT_FMT_MEDIUM;
415 commit->object.flags |= UNINTERESTING;
416 add_pending_object(&rev, &commit->object, NULL);
418 for (j = remoteheads; j; j = j->next)
419 add_pending_object(&rev, &j->item->object, NULL);
421 setup_revisions(0, NULL, &rev, NULL);
422 if (prepare_revision_walk(&rev))
423 die(_("revision walk setup failed"));
425 ctx.abbrev = rev.abbrev;
426 ctx.date_mode = rev.date_mode;
427 ctx.fmt = rev.commit_format;
429 strbuf_addstr(&out, "Squashed commit of the following:\n");
430 while ((commit = get_revision(&rev)) != NULL) {
431 strbuf_addch(&out, '\n');
432 strbuf_addf(&out, "commit %s\n",
433 oid_to_hex(&commit->object.oid));
434 pretty_print_commit(&ctx, commit, &out);
436 write_file_buf(git_path_squash_msg(the_repository), out.buf, out.len);
437 strbuf_release(&out);
440 static void finish(struct commit *head_commit,
441 struct commit_list *remoteheads,
442 const struct object_id *new_head, const char *msg)
444 struct strbuf reflog_message = STRBUF_INIT;
445 const struct object_id *head = &head_commit->object.oid;
448 strbuf_addstr(&reflog_message, getenv("GIT_REFLOG_ACTION"));
452 strbuf_addf(&reflog_message, "%s: %s",
453 getenv("GIT_REFLOG_ACTION"), msg);
456 squash_message(head_commit, remoteheads);
458 if (verbosity >= 0 && !merge_msg.len)
459 printf(_("No merge message -- not updating HEAD\n"));
461 update_ref(reflog_message.buf, "HEAD", new_head, head,
462 0, UPDATE_REFS_DIE_ON_ERR);
464 * We ignore errors in 'gc --auto', since the
465 * user should see them.
467 close_object_store(the_repository->objects);
468 run_auto_maintenance(verbosity < 0);
471 if (new_head && show_diffstat) {
472 struct diff_options opts;
473 repo_diff_setup(the_repository, &opts);
474 opts.stat_width = -1; /* use full terminal width */
475 opts.stat_graph_width = -1; /* respect statGraphWidth config */
476 opts.output_format |=
477 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
478 opts.detect_rename = DIFF_DETECT_RENAME;
479 diff_setup_done(&opts);
480 diff_tree_oid(head, new_head, "", &opts);
485 /* Run a post-merge hook */
486 run_hook_le(NULL, "post-merge", squash ? "1" : "0", NULL);
488 apply_autostash(git_path_merge_autostash(the_repository));
489 strbuf_release(&reflog_message);
492 /* Get the name for the merge commit's message. */
493 static void merge_name(const char *remote, struct strbuf *msg)
495 struct commit *remote_head;
496 struct object_id branch_head;
497 struct strbuf buf = STRBUF_INIT;
498 struct strbuf bname = STRBUF_INIT;
499 struct merge_remote_desc *desc;
504 strbuf_branchname(&bname, remote, 0);
507 oidclr(&branch_head);
508 remote_head = get_merge_parent(remote);
510 die(_("'%s' does not point to a commit"), remote);
512 if (dwim_ref(remote, strlen(remote), &branch_head, &found_ref, 0) > 0) {
513 if (starts_with(found_ref, "refs/heads/")) {
514 strbuf_addf(msg, "%s\t\tbranch '%s' of .\n",
515 oid_to_hex(&branch_head), remote);
518 if (starts_with(found_ref, "refs/tags/")) {
519 strbuf_addf(msg, "%s\t\ttag '%s' of .\n",
520 oid_to_hex(&branch_head), remote);
523 if (starts_with(found_ref, "refs/remotes/")) {
524 strbuf_addf(msg, "%s\t\tremote-tracking branch '%s' of .\n",
525 oid_to_hex(&branch_head), remote);
530 /* See if remote matches <name>^^^.. or <name>~<number> */
531 for (len = 0, ptr = remote + strlen(remote);
532 remote < ptr && ptr[-1] == '^';
539 ptr = strrchr(remote, '~');
541 int seen_nonzero = 0;
544 while (*++ptr && isdigit(*ptr)) {
545 seen_nonzero |= (*ptr != '0');
549 len = 0; /* not ...~<number> */
550 else if (seen_nonzero)
553 early = 1; /* "name~" is "name~1"! */
557 struct strbuf truname = STRBUF_INIT;
558 strbuf_addf(&truname, "refs/heads/%s", remote);
559 strbuf_setlen(&truname, truname.len - len);
560 if (ref_exists(truname.buf)) {
562 "%s\t\tbranch '%s'%s of .\n",
563 oid_to_hex(&remote_head->object.oid),
565 (early ? " (early part)" : ""));
566 strbuf_release(&truname);
569 strbuf_release(&truname);
572 desc = merge_remote_util(remote_head);
573 if (desc && desc->obj && desc->obj->type == OBJ_TAG) {
574 strbuf_addf(msg, "%s\t\t%s '%s'\n",
575 oid_to_hex(&desc->obj->oid),
576 type_name(desc->obj->type),
581 strbuf_addf(msg, "%s\t\tcommit '%s'\n",
582 oid_to_hex(&remote_head->object.oid), remote);
584 strbuf_release(&buf);
585 strbuf_release(&bname);
588 static void parse_branch_merge_options(char *bmo)
595 argc = split_cmdline(bmo, &argv);
597 die(_("Bad branch.%s.mergeoptions string: %s"), branch,
598 _(split_cmdline_strerror(argc)));
599 REALLOC_ARRAY(argv, argc + 2);
600 MOVE_ARRAY(argv + 1, argv, argc + 1);
602 argv[0] = "branch.*.mergeoptions";
603 parse_options(argc, argv, NULL, builtin_merge_options,
604 builtin_merge_usage, 0);
608 static int git_merge_config(const char *k, const char *v, void *cb)
614 skip_prefix(k, "branch.", &str) &&
615 skip_prefix(str, branch, &str) &&
616 !strcmp(str, ".mergeoptions")) {
617 free(branch_mergeoptions);
618 branch_mergeoptions = xstrdup(v);
622 if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat"))
623 show_diffstat = git_config_bool(k, v);
624 else if (!strcmp(k, "merge.verifysignatures"))
625 verify_signatures = git_config_bool(k, v);
626 else if (!strcmp(k, "pull.twohead"))
627 return git_config_string(&pull_twohead, k, v);
628 else if (!strcmp(k, "pull.octopus"))
629 return git_config_string(&pull_octopus, k, v);
630 else if (!strcmp(k, "commit.cleanup"))
631 return git_config_string(&cleanup_arg, k, v);
632 else if (!strcmp(k, "merge.ff")) {
633 int boolval = git_parse_maybe_bool(v);
635 fast_forward = boolval ? FF_ALLOW : FF_NO;
636 } else if (v && !strcmp(v, "only")) {
637 fast_forward = FF_ONLY;
638 } /* do not barf on values from future versions of git */
640 } else if (!strcmp(k, "merge.defaulttoupstream")) {
641 default_to_upstream = git_config_bool(k, v);
643 } else if (!strcmp(k, "commit.gpgsign")) {
644 sign_commit = git_config_bool(k, v) ? "" : NULL;
646 } else if (!strcmp(k, "gpg.mintrustlevel")) {
647 check_trust_level = 0;
648 } else if (!strcmp(k, "merge.autostash")) {
649 autostash = git_config_bool(k, v);
653 status = fmt_merge_msg_config(k, v, cb);
656 status = git_gpg_config(k, v, NULL);
659 return git_diff_ui_config(k, v, cb);
662 static int read_tree_trivial(struct object_id *common, struct object_id *head,
663 struct object_id *one)
666 struct tree *trees[MAX_UNPACK_TREES];
667 struct tree_desc t[MAX_UNPACK_TREES];
668 struct unpack_trees_options opts;
670 memset(&opts, 0, sizeof(opts));
672 opts.src_index = &the_index;
673 opts.dst_index = &the_index;
675 opts.verbose_update = 1;
676 opts.trivial_merges_only = 1;
678 trees[nr_trees] = parse_tree_indirect(common);
679 if (!trees[nr_trees++])
681 trees[nr_trees] = parse_tree_indirect(head);
682 if (!trees[nr_trees++])
684 trees[nr_trees] = parse_tree_indirect(one);
685 if (!trees[nr_trees++])
687 opts.fn = threeway_merge;
688 cache_tree_free(&active_cache_tree);
689 for (i = 0; i < nr_trees; i++) {
690 parse_tree(trees[i]);
691 init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
693 if (unpack_trees(nr_trees, t, &opts))
698 static void write_tree_trivial(struct object_id *oid)
700 if (write_cache_as_tree(oid, 0, NULL))
701 die(_("git write-tree failed to write a tree"));
704 static int try_merge_strategy(const char *strategy, struct commit_list *common,
705 struct commit_list *remoteheads,
708 const char *head_arg = "HEAD";
710 if (refresh_and_write_cache(REFRESH_QUIET, SKIP_IF_UNCHANGED, 0) < 0)
711 return error(_("Unable to write index."));
713 if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree") ||
714 !strcmp(strategy, "ort")) {
715 struct lock_file lock = LOCK_INIT;
717 struct commit *result;
718 struct commit_list *reversed = NULL;
719 struct merge_options o;
720 struct commit_list *j;
722 if (remoteheads->next) {
723 error(_("Not handling anything other than two heads merge."));
727 init_merge_options(&o, the_repository);
728 if (!strcmp(strategy, "subtree"))
729 o.subtree_shift = "";
731 o.show_rename_progress =
732 show_progress == -1 ? isatty(2) : show_progress;
734 for (x = 0; x < xopts_nr; x++)
735 if (parse_merge_opt(&o, xopts[x]))
736 die(_("Unknown option for merge-recursive: -X%s"), xopts[x]);
738 o.branch1 = head_arg;
739 o.branch2 = merge_remote_util(remoteheads->item)->name;
741 for (j = common; j; j = j->next)
742 commit_list_insert(j->item, &reversed);
744 hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
745 if (!strcmp(strategy, "ort"))
746 clean = merge_ort_recursive(&o, head, remoteheads->item,
749 clean = merge_recursive(&o, head, remoteheads->item,
753 if (write_locked_index(&the_index, &lock,
754 COMMIT_LOCK | SKIP_IF_UNCHANGED))
755 die(_("unable to write %s"), get_index_file());
756 return clean ? 0 : 1;
758 return try_merge_command(the_repository,
759 strategy, xopts_nr, xopts,
760 common, head_arg, remoteheads);
764 static void count_diff_files(struct diff_queue_struct *q,
765 struct diff_options *opt, void *data)
772 static int count_unmerged_entries(void)
776 for (i = 0; i < active_nr; i++)
777 if (ce_stage(active_cache[i]))
783 static void add_strategies(const char *string, unsigned attr)
788 struct string_list list = STRING_LIST_INIT_DUP;
789 struct string_list_item *item;
790 string_list_split(&list, string, ' ', -1);
791 for_each_string_list_item(item, &list)
792 append_strategy(get_strategy(item->string));
793 string_list_clear(&list, 0);
796 for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
797 if (all_strategy[i].attr & attr)
798 append_strategy(&all_strategy[i]);
802 static void read_merge_msg(struct strbuf *msg)
804 const char *filename = git_path_merge_msg(the_repository);
806 if (strbuf_read_file(msg, filename, 0) < 0)
807 die_errno(_("Could not read from '%s'"), filename);
810 static void write_merge_state(struct commit_list *);
811 static void abort_commit(struct commit_list *remoteheads, const char *err_msg)
814 error("%s", err_msg);
816 _("Not committing merge; use 'git commit' to complete the merge.\n"));
817 write_merge_state(remoteheads);
821 static const char merge_editor_comment[] =
822 N_("Please enter a commit message to explain why this merge is necessary,\n"
823 "especially if it merges an updated upstream into a topic branch.\n"
826 static const char scissors_editor_comment[] =
827 N_("An empty message aborts the commit.\n");
829 static const char no_scissors_editor_comment[] =
830 N_("Lines starting with '%c' will be ignored, and an empty message aborts\n"
833 static void write_merge_heads(struct commit_list *);
834 static void prepare_to_commit(struct commit_list *remoteheads)
836 struct strbuf msg = STRBUF_INIT;
837 const char *index_file = get_index_file();
839 if (!no_verify && run_commit_hook(0 < option_edit, index_file, "pre-merge-commit", NULL))
840 abort_commit(remoteheads, NULL);
842 * Re-read the index as pre-merge-commit hook could have updated it,
843 * and write it out as a tree. We must do this before we invoke
844 * the editor and after we invoke run_status above.
846 if (find_hook("pre-merge-commit"))
848 read_cache_from(index_file);
849 strbuf_addbuf(&msg, &merge_msg);
851 BUG("the control must not reach here under --squash");
852 if (0 < option_edit) {
853 strbuf_addch(&msg, '\n');
854 if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
855 wt_status_append_cut_line(&msg);
856 strbuf_commented_addf(&msg, "\n");
858 strbuf_commented_addf(&msg, _(merge_editor_comment));
859 strbuf_commented_addf(&msg, _(cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS ?
860 scissors_editor_comment :
861 no_scissors_editor_comment), comment_line_char);
864 append_signoff(&msg, ignore_non_trailer(msg.buf, msg.len), 0);
865 write_merge_heads(remoteheads);
866 write_file_buf(git_path_merge_msg(the_repository), msg.buf, msg.len);
867 if (run_commit_hook(0 < option_edit, get_index_file(), "prepare-commit-msg",
868 git_path_merge_msg(the_repository), "merge", NULL))
869 abort_commit(remoteheads, NULL);
870 if (0 < option_edit) {
871 if (launch_editor(git_path_merge_msg(the_repository), NULL, NULL))
872 abort_commit(remoteheads, NULL);
875 if (!no_verify && run_commit_hook(0 < option_edit, get_index_file(),
877 git_path_merge_msg(the_repository), NULL))
878 abort_commit(remoteheads, NULL);
880 read_merge_msg(&msg);
881 cleanup_message(&msg, cleanup_mode, 0);
883 abort_commit(remoteheads, _("Empty commit message."));
884 strbuf_release(&merge_msg);
885 strbuf_addbuf(&merge_msg, &msg);
886 strbuf_release(&msg);
889 static int merge_trivial(struct commit *head, struct commit_list *remoteheads)
891 struct object_id result_tree, result_commit;
892 struct commit_list *parents, **pptr = &parents;
894 if (refresh_and_write_cache(REFRESH_QUIET, SKIP_IF_UNCHANGED, 0) < 0)
895 return error(_("Unable to write index."));
897 write_tree_trivial(&result_tree);
898 printf(_("Wonderful.\n"));
899 pptr = commit_list_append(head, pptr);
900 pptr = commit_list_append(remoteheads->item, pptr);
901 prepare_to_commit(remoteheads);
902 if (commit_tree(merge_msg.buf, merge_msg.len, &result_tree, parents,
903 &result_commit, NULL, sign_commit))
904 die(_("failed to write commit object"));
905 finish(head, remoteheads, &result_commit, "In-index merge");
906 remove_merge_branch_state(the_repository);
910 static int finish_automerge(struct commit *head,
912 struct commit_list *common,
913 struct commit_list *remoteheads,
914 struct object_id *result_tree,
915 const char *wt_strategy)
917 struct commit_list *parents = NULL;
918 struct strbuf buf = STRBUF_INIT;
919 struct object_id result_commit;
921 write_tree_trivial(result_tree);
922 free_commit_list(common);
923 parents = remoteheads;
924 if (!head_subsumed || fast_forward == FF_NO)
925 commit_list_insert(head, &parents);
926 prepare_to_commit(remoteheads);
927 if (commit_tree(merge_msg.buf, merge_msg.len, result_tree, parents,
928 &result_commit, NULL, sign_commit))
929 die(_("failed to write commit object"));
930 strbuf_addf(&buf, "Merge made by the '%s' strategy.", wt_strategy);
931 finish(head, remoteheads, &result_commit, buf.buf);
932 strbuf_release(&buf);
933 remove_merge_branch_state(the_repository);
937 static int suggest_conflicts(void)
939 const char *filename;
941 struct strbuf msgbuf = STRBUF_INIT;
943 filename = git_path_merge_msg(the_repository);
944 fp = xfopen(filename, "a");
947 * We can't use cleanup_mode because if we're not using the editor,
948 * get_cleanup_mode will return COMMIT_MSG_CLEANUP_SPACE instead, even
949 * though the message is meant to be processed later by git-commit.
950 * Thus, we will get the cleanup mode which is returned when we _are_
953 append_conflicts_hint(&the_index, &msgbuf,
954 get_cleanup_mode(cleanup_arg, 1));
955 fputs(msgbuf.buf, fp);
956 strbuf_release(&msgbuf);
958 repo_rerere(the_repository, allow_rerere_auto);
959 printf(_("Automatic merge failed; "
960 "fix conflicts and then commit the result.\n"));
964 static int evaluate_result(void)
969 /* Check how many files differ. */
970 repo_init_revisions(the_repository, &rev, "");
971 setup_revisions(0, NULL, &rev, NULL);
972 rev.diffopt.output_format |=
973 DIFF_FORMAT_CALLBACK;
974 rev.diffopt.format_callback = count_diff_files;
975 rev.diffopt.format_callback_data = &cnt;
976 run_diff_files(&rev, 0);
979 * Check how many unmerged entries are
982 cnt += count_unmerged_entries();
988 * Pretend as if the user told us to merge with the remote-tracking
989 * branch we have for the upstream of the current branch
991 static int setup_with_upstream(const char ***argv)
993 struct branch *branch = branch_get(NULL);
998 die(_("No current branch."));
999 if (!branch->remote_name)
1000 die(_("No remote for the current branch."));
1001 if (!branch->merge_nr)
1002 die(_("No default upstream defined for the current branch."));
1004 args = xcalloc(st_add(branch->merge_nr, 1), sizeof(char *));
1005 for (i = 0; i < branch->merge_nr; i++) {
1006 if (!branch->merge[i]->dst)
1007 die(_("No remote-tracking branch for %s from %s"),
1008 branch->merge[i]->src, branch->remote_name);
1009 args[i] = branch->merge[i]->dst;
1016 static void write_merge_heads(struct commit_list *remoteheads)
1018 struct commit_list *j;
1019 struct strbuf buf = STRBUF_INIT;
1021 for (j = remoteheads; j; j = j->next) {
1022 struct object_id *oid;
1023 struct commit *c = j->item;
1024 struct merge_remote_desc *desc;
1026 desc = merge_remote_util(c);
1027 if (desc && desc->obj) {
1028 oid = &desc->obj->oid;
1030 oid = &c->object.oid;
1032 strbuf_addf(&buf, "%s\n", oid_to_hex(oid));
1034 write_file_buf(git_path_merge_head(the_repository), buf.buf, buf.len);
1037 if (fast_forward == FF_NO)
1038 strbuf_addstr(&buf, "no-ff");
1039 write_file_buf(git_path_merge_mode(the_repository), buf.buf, buf.len);
1040 strbuf_release(&buf);
1043 static void write_merge_state(struct commit_list *remoteheads)
1045 write_merge_heads(remoteheads);
1046 strbuf_addch(&merge_msg, '\n');
1047 write_file_buf(git_path_merge_msg(the_repository), merge_msg.buf,
1051 static int default_edit_option(void)
1053 static const char name[] = "GIT_MERGE_AUTOEDIT";
1054 const char *e = getenv(name);
1055 struct stat st_stdin, st_stdout;
1058 /* an explicit -m msg without --[no-]edit */
1062 int v = git_parse_maybe_bool(e);
1064 die(_("Bad value '%s' in environment '%s'"), e, name);
1068 /* Use editor if stdin and stdout are the same and is a tty */
1069 return (!fstat(0, &st_stdin) &&
1070 !fstat(1, &st_stdout) &&
1071 isatty(0) && isatty(1) &&
1072 st_stdin.st_dev == st_stdout.st_dev &&
1073 st_stdin.st_ino == st_stdout.st_ino &&
1074 st_stdin.st_mode == st_stdout.st_mode);
1077 static struct commit_list *reduce_parents(struct commit *head_commit,
1079 struct commit_list *remoteheads)
1081 struct commit_list *parents, **remotes;
1084 * Is the current HEAD reachable from another commit being
1085 * merged? If so we do not want to record it as a parent of
1086 * the resulting merge, unless --no-ff is given. We will flip
1087 * this variable to 0 when we find HEAD among the independent
1088 * tips being merged.
1092 /* Find what parents to record by checking independent ones. */
1093 parents = reduce_heads(remoteheads);
1094 free_commit_list(remoteheads);
1097 remotes = &remoteheads;
1099 struct commit *commit = pop_commit(&parents);
1100 if (commit == head_commit)
1103 remotes = &commit_list_insert(commit, remotes)->next;
1108 static void prepare_merge_message(struct strbuf *merge_names, struct strbuf *merge_msg)
1110 struct fmt_merge_msg_opts opts;
1112 memset(&opts, 0, sizeof(opts));
1113 opts.add_title = !have_message;
1114 opts.shortlog_len = shortlog_len;
1115 opts.credit_people = (0 < option_edit);
1117 fmt_merge_msg(merge_names, merge_msg, &opts);
1119 strbuf_setlen(merge_msg, merge_msg->len - 1);
1122 static void handle_fetch_head(struct commit_list **remotes, struct strbuf *merge_names)
1124 const char *filename;
1126 struct strbuf fetch_head_file = STRBUF_INIT;
1127 const unsigned hexsz = the_hash_algo->hexsz;
1130 merge_names = &fetch_head_file;
1132 filename = git_path_fetch_head(the_repository);
1133 fd = open(filename, O_RDONLY);
1135 die_errno(_("could not open '%s' for reading"), filename);
1137 if (strbuf_read(merge_names, fd, 0) < 0)
1138 die_errno(_("could not read '%s'"), filename);
1140 die_errno(_("could not close '%s'"), filename);
1142 for (pos = 0; pos < merge_names->len; pos = npos) {
1143 struct object_id oid;
1145 struct commit *commit;
1147 ptr = strchr(merge_names->buf + pos, '\n');
1149 npos = ptr - merge_names->buf + 1;
1151 npos = merge_names->len;
1153 if (npos - pos < hexsz + 2 ||
1154 get_oid_hex(merge_names->buf + pos, &oid))
1155 commit = NULL; /* bad */
1156 else if (memcmp(merge_names->buf + pos + hexsz, "\t\t", 2))
1157 continue; /* not-for-merge */
1159 char saved = merge_names->buf[pos + hexsz];
1160 merge_names->buf[pos + hexsz] = '\0';
1161 commit = get_merge_parent(merge_names->buf + pos);
1162 merge_names->buf[pos + hexsz] = saved;
1167 die(_("not something we can merge in %s: %s"),
1168 filename, merge_names->buf + pos);
1170 remotes = &commit_list_insert(commit, remotes)->next;
1173 if (merge_names == &fetch_head_file)
1174 strbuf_release(&fetch_head_file);
1177 static struct commit_list *collect_parents(struct commit *head_commit,
1179 int argc, const char **argv,
1180 struct strbuf *merge_msg)
1183 struct commit_list *remoteheads = NULL;
1184 struct commit_list **remotes = &remoteheads;
1185 struct strbuf merge_names = STRBUF_INIT, *autogen = NULL;
1187 if (merge_msg && (!have_message || shortlog_len))
1188 autogen = &merge_names;
1191 remotes = &commit_list_insert(head_commit, remotes)->next;
1193 if (argc == 1 && !strcmp(argv[0], "FETCH_HEAD")) {
1194 handle_fetch_head(remotes, autogen);
1195 remoteheads = reduce_parents(head_commit, head_subsumed, remoteheads);
1197 for (i = 0; i < argc; i++) {
1198 struct commit *commit = get_merge_parent(argv[i]);
1200 help_unknown_ref(argv[i], "merge",
1201 _("not something we can merge"));
1202 remotes = &commit_list_insert(commit, remotes)->next;
1204 remoteheads = reduce_parents(head_commit, head_subsumed, remoteheads);
1206 struct commit_list *p;
1207 for (p = remoteheads; p; p = p->next)
1208 merge_name(merge_remote_util(p->item)->name, autogen);
1213 prepare_merge_message(autogen, merge_msg);
1214 strbuf_release(autogen);
1220 static int merging_a_throwaway_tag(struct commit *commit)
1223 struct object_id oid;
1224 int is_throwaway_tag = 0;
1226 /* Are we merging a tag? */
1227 if (!merge_remote_util(commit) ||
1228 !merge_remote_util(commit)->obj ||
1229 merge_remote_util(commit)->obj->type != OBJ_TAG)
1230 return is_throwaway_tag;
1233 * Now we know we are merging a tag object. Are we downstream
1234 * and following the tags from upstream? If so, we must have
1235 * the tag object pointed at by "refs/tags/$T" where $T is the
1236 * tagname recorded in the tag object. We want to allow such
1237 * a "just to catch up" merge to fast-forward.
1239 * Otherwise, we are playing an integrator's role, making a
1240 * merge with a throw-away tag from a contributor with
1241 * something like "git pull $contributor $signed_tag".
1242 * We want to forbid such a merge from fast-forwarding
1243 * by default; otherwise we would not keep the signature
1246 tag_ref = xstrfmt("refs/tags/%s",
1247 ((struct tag *)merge_remote_util(commit)->obj)->tag);
1248 if (!read_ref(tag_ref, &oid) &&
1249 oideq(&oid, &merge_remote_util(commit)->obj->oid))
1250 is_throwaway_tag = 0;
1252 is_throwaway_tag = 1;
1254 return is_throwaway_tag;
1257 int cmd_merge(int argc, const char **argv, const char *prefix)
1259 struct object_id result_tree, stash, head_oid;
1260 struct commit *head_commit;
1261 struct strbuf buf = STRBUF_INIT;
1262 int i, ret = 0, head_subsumed;
1263 int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0;
1264 struct commit_list *common = NULL;
1265 const char *best_strategy = NULL, *wt_strategy = NULL;
1266 struct commit_list *remoteheads, *p;
1267 void *branch_to_free;
1268 int orig_argc = argc;
1270 if (argc == 2 && !strcmp(argv[1], "-h"))
1271 usage_with_options(builtin_merge_usage, builtin_merge_options);
1274 * Check if we are _not_ on a detached HEAD, i.e. if there is a
1277 branch = branch_to_free = resolve_refdup("HEAD", 0, &head_oid, NULL);
1279 skip_prefix(branch, "refs/heads/", &branch);
1281 if (!pull_twohead) {
1282 char *default_strategy = getenv("GIT_TEST_MERGE_ALGORITHM");
1283 if (default_strategy && !strcmp(default_strategy, "ort"))
1284 pull_twohead = "ort";
1287 init_diff_ui_defaults();
1288 git_config(git_merge_config, NULL);
1290 if (!branch || is_null_oid(&head_oid))
1293 head_commit = lookup_commit_or_die(&head_oid, "HEAD");
1295 if (branch_mergeoptions)
1296 parse_branch_merge_options(branch_mergeoptions);
1297 argc = parse_options(argc, argv, prefix, builtin_merge_options,
1298 builtin_merge_usage, 0);
1299 if (shortlog_len < 0)
1300 shortlog_len = (merge_log_config > 0) ? merge_log_config : 0;
1302 if (verbosity < 0 && show_progress == -1)
1305 if (abort_current_merge) {
1307 const char *nargv[] = {"reset", "--merge", NULL};
1308 struct strbuf stash_oid = STRBUF_INIT;
1311 usage_msg_opt(_("--abort expects no arguments"),
1312 builtin_merge_usage, builtin_merge_options);
1314 if (!file_exists(git_path_merge_head(the_repository)))
1315 die(_("There is no merge to abort (MERGE_HEAD missing)."));
1317 if (read_oneliner(&stash_oid, git_path_merge_autostash(the_repository),
1318 READ_ONELINER_SKIP_IF_EMPTY))
1319 unlink(git_path_merge_autostash(the_repository));
1321 /* Invoke 'git reset --merge' */
1322 ret = cmd_reset(nargc, nargv, prefix);
1325 apply_autostash_oid(stash_oid.buf);
1327 strbuf_release(&stash_oid);
1331 if (quit_current_merge) {
1333 usage_msg_opt(_("--quit expects no arguments"),
1334 builtin_merge_usage,
1335 builtin_merge_options);
1337 remove_merge_branch_state(the_repository);
1341 if (continue_current_merge) {
1343 const char *nargv[] = {"commit", NULL};
1346 usage_msg_opt(_("--continue expects no arguments"),
1347 builtin_merge_usage, builtin_merge_options);
1349 if (!file_exists(git_path_merge_head(the_repository)))
1350 die(_("There is no merge in progress (MERGE_HEAD missing)."));
1352 /* Invoke 'git commit' */
1353 ret = cmd_commit(nargc, nargv, prefix);
1357 if (read_cache_unmerged())
1358 die_resolve_conflict("merge");
1360 if (file_exists(git_path_merge_head(the_repository))) {
1362 * There is no unmerged entry, don't advise 'git
1363 * add/rm <file>', just 'git commit'.
1365 if (advice_resolve_conflict)
1366 die(_("You have not concluded your merge (MERGE_HEAD exists).\n"
1367 "Please, commit your changes before you merge."));
1369 die(_("You have not concluded your merge (MERGE_HEAD exists)."));
1371 if (ref_exists("CHERRY_PICK_HEAD")) {
1372 if (advice_resolve_conflict)
1373 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
1374 "Please, commit your changes before you merge."));
1376 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists)."));
1378 resolve_undo_clear();
1380 if (option_edit < 0)
1381 option_edit = default_edit_option();
1383 cleanup_mode = get_cleanup_mode(cleanup_arg, 0 < option_edit);
1389 if (fast_forward == FF_NO)
1390 die(_("You cannot combine --squash with --no-ff."));
1391 if (option_commit > 0)
1392 die(_("You cannot combine --squash with --commit."));
1394 * squash can now silently disable option_commit - this is not
1395 * a problem as it is only overriding the default, not a user
1401 if (option_commit < 0)
1405 if (default_to_upstream)
1406 argc = setup_with_upstream(&argv);
1408 die(_("No commit specified and merge.defaultToUpstream not set."));
1409 } else if (argc == 1 && !strcmp(argv[0], "-")) {
1414 usage_with_options(builtin_merge_usage,
1415 builtin_merge_options);
1419 * If the merged head is a valid one there is no reason
1420 * to forbid "git merge" into a branch yet to be born.
1421 * We do the same for "git pull".
1423 struct object_id *remote_head_oid;
1425 die(_("Squash commit into empty head not supported yet"));
1426 if (fast_forward == FF_NO)
1427 die(_("Non-fast-forward commit does not make sense into "
1429 remoteheads = collect_parents(head_commit, &head_subsumed,
1432 die(_("%s - not something we can merge"), argv[0]);
1433 if (remoteheads->next)
1434 die(_("Can merge only exactly one commit into empty head"));
1436 if (verify_signatures)
1437 verify_merge_signature(remoteheads->item, verbosity,
1440 remote_head_oid = &remoteheads->item->object.oid;
1441 read_empty(remote_head_oid, 0);
1442 update_ref("initial pull", "HEAD", remote_head_oid, NULL, 0,
1443 UPDATE_REFS_DIE_ON_ERR);
1448 * All the rest are the commits being merged; prepare
1449 * the standard merge summary message to be appended
1450 * to the given message.
1452 remoteheads = collect_parents(head_commit, &head_subsumed,
1453 argc, argv, &merge_msg);
1455 if (!head_commit || !argc)
1456 usage_with_options(builtin_merge_usage,
1457 builtin_merge_options);
1459 if (verify_signatures) {
1460 for (p = remoteheads; p; p = p->next) {
1461 verify_merge_signature(p->item, verbosity,
1466 strbuf_addstr(&buf, "merge");
1467 for (p = remoteheads; p; p = p->next)
1468 strbuf_addf(&buf, " %s", merge_remote_util(p->item)->name);
1469 setenv("GIT_REFLOG_ACTION", buf.buf, 0);
1472 for (p = remoteheads; p; p = p->next) {
1473 struct commit *commit = p->item;
1474 strbuf_addf(&buf, "GITHEAD_%s",
1475 oid_to_hex(&commit->object.oid));
1476 setenv(buf.buf, merge_remote_util(commit)->name, 1);
1478 if (fast_forward != FF_ONLY && merging_a_throwaway_tag(commit))
1479 fast_forward = FF_NO;
1482 if (!use_strategies) {
1484 ; /* already up-to-date */
1485 else if (!remoteheads->next)
1486 add_strategies(pull_twohead, DEFAULT_TWOHEAD);
1488 add_strategies(pull_octopus, DEFAULT_OCTOPUS);
1491 for (i = 0; i < use_strategies_nr; i++) {
1492 if (use_strategies[i]->attr & NO_FAST_FORWARD)
1493 fast_forward = FF_NO;
1494 if (use_strategies[i]->attr & NO_TRIVIAL)
1499 ; /* already up-to-date */
1500 else if (!remoteheads->next)
1501 common = get_merge_bases(head_commit, remoteheads->item);
1503 struct commit_list *list = remoteheads;
1504 commit_list_insert(head_commit, &list);
1505 common = get_octopus_merge_bases(list);
1509 update_ref("updating ORIG_HEAD", "ORIG_HEAD",
1510 &head_commit->object.oid, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
1512 if (remoteheads && !common) {
1513 /* No common ancestors found. */
1514 if (!allow_unrelated_histories)
1515 die(_("refusing to merge unrelated histories"));
1516 /* otherwise, we need a real merge. */
1517 } else if (!remoteheads ||
1518 (!remoteheads->next && !common->next &&
1519 common->item == remoteheads->item)) {
1521 * If head can reach all the merge then we are up to date.
1522 * but first the most common case of merging one remote.
1524 finish_up_to_date(_("Already up to date."));
1526 } else if (fast_forward != FF_NO && !remoteheads->next &&
1528 oideq(&common->item->object.oid, &head_commit->object.oid)) {
1529 /* Again the most common case of merging one remote. */
1530 struct strbuf msg = STRBUF_INIT;
1531 struct commit *commit;
1533 if (verbosity >= 0) {
1534 printf(_("Updating %s..%s\n"),
1535 find_unique_abbrev(&head_commit->object.oid,
1537 find_unique_abbrev(&remoteheads->item->object.oid,
1540 strbuf_addstr(&msg, "Fast-forward");
1543 " (no commit created; -m option ignored)");
1544 commit = remoteheads->item;
1551 create_autostash(the_repository,
1552 git_path_merge_autostash(the_repository),
1554 if (checkout_fast_forward(the_repository,
1555 &head_commit->object.oid,
1556 &commit->object.oid,
1557 overwrite_ignore)) {
1562 finish(head_commit, remoteheads, &commit->object.oid, msg.buf);
1563 remove_merge_branch_state(the_repository);
1565 } else if (!remoteheads->next && common->next)
1568 * We are not doing octopus and not fast-forward. Need
1571 else if (!remoteheads->next && !common->next && option_commit) {
1573 * We are not doing octopus, not fast-forward, and have
1576 refresh_cache(REFRESH_QUIET);
1577 if (allow_trivial && fast_forward != FF_ONLY) {
1578 /* See if it is really trivial. */
1579 git_committer_info(IDENT_STRICT);
1580 printf(_("Trying really trivial in-index merge...\n"));
1581 if (!read_tree_trivial(&common->item->object.oid,
1582 &head_commit->object.oid,
1583 &remoteheads->item->object.oid)) {
1584 ret = merge_trivial(head_commit, remoteheads);
1587 printf(_("Nope.\n"));
1591 * An octopus. If we can reach all the remote we are up
1595 struct commit_list *j;
1597 for (j = remoteheads; j; j = j->next) {
1598 struct commit_list *common_one;
1601 * Here we *have* to calculate the individual
1602 * merge_bases again, otherwise "git merge HEAD^
1603 * HEAD^^" would be missed.
1605 common_one = get_merge_bases(head_commit, j->item);
1606 if (!oideq(&common_one->item->object.oid, &j->item->object.oid)) {
1612 finish_up_to_date(_("Already up to date. Yeeah!"));
1617 if (fast_forward == FF_ONLY)
1618 die(_("Not possible to fast-forward, aborting."));
1621 create_autostash(the_repository,
1622 git_path_merge_autostash(the_repository),
1625 /* We are going to make a new commit. */
1626 git_committer_info(IDENT_STRICT);
1629 * At this point, we need a real merge. No matter what strategy
1630 * we use, it would operate on the index, possibly affecting the
1631 * working tree, and when resolved cleanly, have the desired
1632 * tree in the index -- this means that the index must be in
1633 * sync with the head commit. The strategies are responsible
1636 if (use_strategies_nr == 1 ||
1638 * Stash away the local changes so that we can try more than one.
1643 for (i = 0; !merge_was_ok && i < use_strategies_nr; i++) {
1646 printf(_("Rewinding the tree to pristine...\n"));
1647 restore_state(&head_commit->object.oid, &stash);
1649 if (use_strategies_nr != 1)
1650 printf(_("Trying merge strategy %s...\n"),
1651 use_strategies[i]->name);
1653 * Remember which strategy left the state in the working
1656 wt_strategy = use_strategies[i]->name;
1658 ret = try_merge_strategy(use_strategies[i]->name,
1659 common, remoteheads,
1662 * The backend exits with 1 when conflicts are
1663 * left to be resolved, with 2 when it does not
1664 * handle the given merge at all.
1668 if (option_commit) {
1669 /* Automerge succeeded. */
1670 automerge_was_ok = 1;
1675 cnt = (use_strategies_nr > 1) ? evaluate_result() : 0;
1676 if (best_cnt <= 0 || cnt <= best_cnt) {
1677 best_strategy = use_strategies[i]->name;
1684 * If we have a resulting tree, that means the strategy module
1685 * auto resolved the merge cleanly.
1687 if (automerge_was_ok) {
1688 ret = finish_automerge(head_commit, head_subsumed,
1689 common, remoteheads,
1690 &result_tree, wt_strategy);
1695 * Pick the result from the best strategy and have the user fix
1698 if (!best_strategy) {
1699 restore_state(&head_commit->object.oid, &stash);
1700 if (use_strategies_nr > 1)
1702 _("No merge strategy handled the merge.\n"));
1704 fprintf(stderr, _("Merge with strategy %s failed.\n"),
1705 use_strategies[0]->name);
1708 } else if (best_strategy == wt_strategy)
1709 ; /* We already have its result in the working tree. */
1711 printf(_("Rewinding the tree to pristine...\n"));
1712 restore_state(&head_commit->object.oid, &stash);
1713 printf(_("Using the %s to prepare resolving by hand.\n"),
1715 try_merge_strategy(best_strategy, common, remoteheads,
1720 finish(head_commit, remoteheads, NULL, NULL);
1722 git_test_write_commit_graph_or_die();
1724 write_merge_state(remoteheads);
1727 fprintf(stderr, _("Automatic merge went well; "
1728 "stopped before committing as requested\n"));
1730 ret = suggest_conflicts();
1733 free(branch_to_free);