submodule: Use cat instead of echo to avoid DOS line-endings
[git/dscho.git] / builtin / merge.c
blob6a18f9eeec2cb925ba5ea18b175a2a70b1b5c20f
1 /*
2 * Builtin "git merge"
4 * Copyright (c) 2008 Miklos Vajna <vmiklos@frugalware.org>
6 * Based on git-merge.sh by Junio C Hamano.
7 */
9 #include "cache.h"
10 #include "parse-options.h"
11 #include "builtin.h"
12 #include "run-command.h"
13 #include "diff.h"
14 #include "refs.h"
15 #include "commit.h"
16 #include "diffcore.h"
17 #include "revision.h"
18 #include "unpack-trees.h"
19 #include "cache-tree.h"
20 #include "dir.h"
21 #include "utf8.h"
22 #include "log-tree.h"
23 #include "color.h"
24 #include "rerere.h"
25 #include "help.h"
26 #include "merge-recursive.h"
27 #include "resolve-undo.h"
28 #include "remote.h"
29 #include "fmt-merge-msg.h"
31 #define DEFAULT_TWOHEAD (1<<0)
32 #define DEFAULT_OCTOPUS (1<<1)
33 #define NO_FAST_FORWARD (1<<2)
34 #define NO_TRIVIAL (1<<3)
36 struct strategy {
37 const char *name;
38 unsigned attr;
41 static const char * const builtin_merge_usage[] = {
42 "git merge [options] [<commit>...]",
43 "git merge [options] <msg> HEAD <commit>",
44 "git merge --abort",
45 NULL
48 static int show_diffstat = 1, shortlog_len = -1, squash;
49 static int option_commit = 1, allow_fast_forward = 1;
50 static int fast_forward_only, option_edit;
51 static int allow_trivial = 1, have_message;
52 static struct strbuf merge_msg;
53 static struct commit_list *remoteheads;
54 static struct strategy **use_strategies;
55 static size_t use_strategies_nr, use_strategies_alloc;
56 static const char **xopts;
57 static size_t xopts_nr, xopts_alloc;
58 static const char *branch;
59 static char *branch_mergeoptions;
60 static int option_renormalize;
61 static int verbosity;
62 static int allow_rerere_auto;
63 static int abort_current_merge;
64 static int show_progress = -1;
65 static int default_to_upstream;
67 static struct strategy all_strategy[] = {
68 { "recursive", DEFAULT_TWOHEAD | NO_TRIVIAL },
69 { "octopus", DEFAULT_OCTOPUS },
70 { "resolve", 0 },
71 { "ours", NO_FAST_FORWARD | NO_TRIVIAL },
72 { "subtree", NO_FAST_FORWARD | NO_TRIVIAL },
75 static const char *pull_twohead, *pull_octopus;
77 static int option_parse_message(const struct option *opt,
78 const char *arg, int unset)
80 struct strbuf *buf = opt->value;
82 if (unset)
83 strbuf_setlen(buf, 0);
84 else if (arg) {
85 strbuf_addf(buf, "%s%s", buf->len ? "\n\n" : "", arg);
86 have_message = 1;
87 } else
88 return error(_("switch `m' requires a value"));
89 return 0;
92 static struct strategy *get_strategy(const char *name)
94 int i;
95 struct strategy *ret;
96 static struct cmdnames main_cmds, other_cmds;
97 static int loaded;
99 if (!name)
100 return NULL;
102 for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
103 if (!strcmp(name, all_strategy[i].name))
104 return &all_strategy[i];
106 if (!loaded) {
107 struct cmdnames not_strategies;
108 loaded = 1;
110 memset(&not_strategies, 0, sizeof(struct cmdnames));
111 load_command_list("git-merge-", &main_cmds, &other_cmds);
112 for (i = 0; i < main_cmds.cnt; i++) {
113 int j, found = 0;
114 struct cmdname *ent = main_cmds.names[i];
115 for (j = 0; j < ARRAY_SIZE(all_strategy); j++)
116 if (!strncmp(ent->name, all_strategy[j].name, ent->len)
117 && !all_strategy[j].name[ent->len])
118 found = 1;
119 if (!found)
120 add_cmdname(&not_strategies, ent->name, ent->len);
122 exclude_cmds(&main_cmds, &not_strategies);
124 if (!is_in_cmdlist(&main_cmds, name) && !is_in_cmdlist(&other_cmds, name)) {
125 fprintf(stderr, _("Could not find merge strategy '%s'.\n"), name);
126 fprintf(stderr, _("Available strategies are:"));
127 for (i = 0; i < main_cmds.cnt; i++)
128 fprintf(stderr, " %s", main_cmds.names[i]->name);
129 fprintf(stderr, ".\n");
130 if (other_cmds.cnt) {
131 fprintf(stderr, _("Available custom strategies are:"));
132 for (i = 0; i < other_cmds.cnt; i++)
133 fprintf(stderr, " %s", other_cmds.names[i]->name);
134 fprintf(stderr, ".\n");
136 exit(1);
139 ret = xcalloc(1, sizeof(struct strategy));
140 ret->name = xstrdup(name);
141 ret->attr = NO_TRIVIAL;
142 return ret;
145 static void append_strategy(struct strategy *s)
147 ALLOC_GROW(use_strategies, use_strategies_nr + 1, use_strategies_alloc);
148 use_strategies[use_strategies_nr++] = s;
151 static int option_parse_strategy(const struct option *opt,
152 const char *name, int unset)
154 if (unset)
155 return 0;
157 append_strategy(get_strategy(name));
158 return 0;
161 static int option_parse_x(const struct option *opt,
162 const char *arg, int unset)
164 if (unset)
165 return 0;
167 ALLOC_GROW(xopts, xopts_nr + 1, xopts_alloc);
168 xopts[xopts_nr++] = xstrdup(arg);
169 return 0;
172 static int option_parse_n(const struct option *opt,
173 const char *arg, int unset)
175 show_diffstat = unset;
176 return 0;
179 static struct option builtin_merge_options[] = {
180 { OPTION_CALLBACK, 'n', NULL, NULL, NULL,
181 "do not show a diffstat at the end of the merge",
182 PARSE_OPT_NOARG, option_parse_n },
183 OPT_BOOLEAN(0, "stat", &show_diffstat,
184 "show a diffstat at the end of the merge"),
185 OPT_BOOLEAN(0, "summary", &show_diffstat, "(synonym to --stat)"),
186 { OPTION_INTEGER, 0, "log", &shortlog_len, "n",
187 "add (at most <n>) entries from shortlog to merge commit message",
188 PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
189 OPT_BOOLEAN(0, "squash", &squash,
190 "create a single commit instead of doing a merge"),
191 OPT_BOOLEAN(0, "commit", &option_commit,
192 "perform a commit if the merge succeeds (default)"),
193 OPT_BOOLEAN('e', "edit", &option_edit,
194 "edit message before committing"),
195 OPT_BOOLEAN(0, "ff", &allow_fast_forward,
196 "allow fast-forward (default)"),
197 OPT_BOOLEAN(0, "ff-only", &fast_forward_only,
198 "abort if fast-forward is not possible"),
199 OPT_RERERE_AUTOUPDATE(&allow_rerere_auto),
200 OPT_CALLBACK('s', "strategy", &use_strategies, "strategy",
201 "merge strategy to use", option_parse_strategy),
202 OPT_CALLBACK('X', "strategy-option", &xopts, "option=value",
203 "option for selected merge strategy", option_parse_x),
204 OPT_CALLBACK('m', "message", &merge_msg, "message",
205 "merge commit message (for a non-fast-forward merge)",
206 option_parse_message),
207 OPT__VERBOSITY(&verbosity),
208 OPT_BOOLEAN(0, "abort", &abort_current_merge,
209 "abort the current in-progress merge"),
210 OPT_SET_INT(0, "progress", &show_progress, "force progress reporting", 1),
211 OPT_END()
214 /* Cleans up metadata that is uninteresting after a succeeded merge. */
215 static void drop_save(void)
217 unlink(git_path("MERGE_HEAD"));
218 unlink(git_path("MERGE_MSG"));
219 unlink(git_path("MERGE_MODE"));
222 static int save_state(unsigned char *stash)
224 int len;
225 struct child_process cp;
226 struct strbuf buffer = STRBUF_INIT;
227 const char *argv[] = {"stash", "create", NULL};
229 memset(&cp, 0, sizeof(cp));
230 cp.argv = argv;
231 cp.out = -1;
232 cp.git_cmd = 1;
234 if (start_command(&cp))
235 die(_("could not run stash."));
236 len = strbuf_read(&buffer, cp.out, 1024);
237 close(cp.out);
239 if (finish_command(&cp) || len < 0)
240 die(_("stash failed"));
241 else if (!len) /* no changes */
242 return -1;
243 strbuf_setlen(&buffer, buffer.len-1);
244 if (get_sha1(buffer.buf, stash))
245 die(_("not a valid object: %s"), buffer.buf);
246 return 0;
249 static void read_empty(unsigned const char *sha1, int verbose)
251 int i = 0;
252 const char *args[7];
254 args[i++] = "read-tree";
255 if (verbose)
256 args[i++] = "-v";
257 args[i++] = "-m";
258 args[i++] = "-u";
259 args[i++] = EMPTY_TREE_SHA1_HEX;
260 args[i++] = sha1_to_hex(sha1);
261 args[i] = NULL;
263 if (run_command_v_opt(args, RUN_GIT_CMD))
264 die(_("read-tree failed"));
267 static void reset_hard(unsigned const char *sha1, int verbose)
269 int i = 0;
270 const char *args[6];
272 args[i++] = "read-tree";
273 if (verbose)
274 args[i++] = "-v";
275 args[i++] = "--reset";
276 args[i++] = "-u";
277 args[i++] = sha1_to_hex(sha1);
278 args[i] = NULL;
280 if (run_command_v_opt(args, RUN_GIT_CMD))
281 die(_("read-tree failed"));
284 static void restore_state(const unsigned char *head,
285 const unsigned char *stash)
287 struct strbuf sb = STRBUF_INIT;
288 const char *args[] = { "stash", "apply", NULL, NULL };
290 if (is_null_sha1(stash))
291 return;
293 reset_hard(head, 1);
295 args[2] = sha1_to_hex(stash);
298 * It is OK to ignore error here, for example when there was
299 * nothing to restore.
301 run_command_v_opt(args, RUN_GIT_CMD);
303 strbuf_release(&sb);
304 refresh_cache(REFRESH_QUIET);
307 /* This is called when no merge was necessary. */
308 static void finish_up_to_date(const char *msg)
310 if (verbosity >= 0)
311 printf("%s%s\n", squash ? _(" (nothing to squash)") : "", msg);
312 drop_save();
315 static void squash_message(struct commit *commit)
317 struct rev_info rev;
318 struct strbuf out = STRBUF_INIT;
319 struct commit_list *j;
320 const char *filename;
321 int fd;
322 struct pretty_print_context ctx = {0};
324 printf(_("Squash commit -- not updating HEAD\n"));
325 filename = git_path("SQUASH_MSG");
326 fd = open(filename, O_WRONLY | O_CREAT, 0666);
327 if (fd < 0)
328 die_errno(_("Could not write to '%s'"), filename);
330 init_revisions(&rev, NULL);
331 rev.ignore_merges = 1;
332 rev.commit_format = CMIT_FMT_MEDIUM;
334 commit->object.flags |= UNINTERESTING;
335 add_pending_object(&rev, &commit->object, NULL);
337 for (j = remoteheads; j; j = j->next)
338 add_pending_object(&rev, &j->item->object, NULL);
340 setup_revisions(0, NULL, &rev, NULL);
341 if (prepare_revision_walk(&rev))
342 die(_("revision walk setup failed"));
344 ctx.abbrev = rev.abbrev;
345 ctx.date_mode = rev.date_mode;
346 ctx.fmt = rev.commit_format;
348 strbuf_addstr(&out, "Squashed commit of the following:\n");
349 while ((commit = get_revision(&rev)) != NULL) {
350 strbuf_addch(&out, '\n');
351 strbuf_addf(&out, "commit %s\n",
352 sha1_to_hex(commit->object.sha1));
353 pretty_print_commit(&ctx, commit, &out);
355 if (write(fd, out.buf, out.len) < 0)
356 die_errno(_("Writing SQUASH_MSG"));
357 if (close(fd))
358 die_errno(_("Finishing SQUASH_MSG"));
359 strbuf_release(&out);
362 static void finish(struct commit *head_commit,
363 const unsigned char *new_head, const char *msg)
365 struct strbuf reflog_message = STRBUF_INIT;
366 const unsigned char *head = head_commit->object.sha1;
368 if (!msg)
369 strbuf_addstr(&reflog_message, getenv("GIT_REFLOG_ACTION"));
370 else {
371 if (verbosity >= 0)
372 printf("%s\n", msg);
373 strbuf_addf(&reflog_message, "%s: %s",
374 getenv("GIT_REFLOG_ACTION"), msg);
376 if (squash) {
377 squash_message(head_commit);
378 } else {
379 if (verbosity >= 0 && !merge_msg.len)
380 printf(_("No merge message -- not updating HEAD\n"));
381 else {
382 const char *argv_gc_auto[] = { "gc", "--auto", NULL };
383 update_ref(reflog_message.buf, "HEAD",
384 new_head, head, 0,
385 DIE_ON_ERR);
387 * We ignore errors in 'gc --auto', since the
388 * user should see them.
390 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
393 if (new_head && show_diffstat) {
394 struct diff_options opts;
395 diff_setup(&opts);
396 opts.output_format |=
397 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
398 opts.detect_rename = DIFF_DETECT_RENAME;
399 if (diff_setup_done(&opts) < 0)
400 die(_("diff_setup_done failed"));
401 diff_tree_sha1(head, new_head, "", &opts);
402 diffcore_std(&opts);
403 diff_flush(&opts);
406 /* Run a post-merge hook */
407 run_hook(NULL, "post-merge", squash ? "1" : "0", NULL);
409 strbuf_release(&reflog_message);
412 /* Get the name for the merge commit's message. */
413 static void merge_name(const char *remote, struct strbuf *msg)
415 struct commit *remote_head;
416 unsigned char branch_head[20];
417 struct strbuf buf = STRBUF_INIT;
418 struct strbuf bname = STRBUF_INIT;
419 const char *ptr;
420 char *found_ref;
421 int len, early;
423 strbuf_branchname(&bname, remote);
424 remote = bname.buf;
426 memset(branch_head, 0, sizeof(branch_head));
427 remote_head = get_merge_parent(remote);
428 if (!remote_head)
429 die(_("'%s' does not point to a commit"), remote);
431 if (dwim_ref(remote, strlen(remote), branch_head, &found_ref) > 0) {
432 if (!prefixcmp(found_ref, "refs/heads/")) {
433 strbuf_addf(msg, "%s\t\tbranch '%s' of .\n",
434 sha1_to_hex(branch_head), remote);
435 goto cleanup;
437 if (!prefixcmp(found_ref, "refs/tags/")) {
438 strbuf_addf(msg, "%s\t\ttag '%s' of .\n",
439 sha1_to_hex(branch_head), remote);
440 goto cleanup;
442 if (!prefixcmp(found_ref, "refs/remotes/")) {
443 strbuf_addf(msg, "%s\t\tremote-tracking branch '%s' of .\n",
444 sha1_to_hex(branch_head), remote);
445 goto cleanup;
449 /* See if remote matches <name>^^^.. or <name>~<number> */
450 for (len = 0, ptr = remote + strlen(remote);
451 remote < ptr && ptr[-1] == '^';
452 ptr--)
453 len++;
454 if (len)
455 early = 1;
456 else {
457 early = 0;
458 ptr = strrchr(remote, '~');
459 if (ptr) {
460 int seen_nonzero = 0;
462 len++; /* count ~ */
463 while (*++ptr && isdigit(*ptr)) {
464 seen_nonzero |= (*ptr != '0');
465 len++;
467 if (*ptr)
468 len = 0; /* not ...~<number> */
469 else if (seen_nonzero)
470 early = 1;
471 else if (len == 1)
472 early = 1; /* "name~" is "name~1"! */
475 if (len) {
476 struct strbuf truname = STRBUF_INIT;
477 strbuf_addstr(&truname, "refs/heads/");
478 strbuf_addstr(&truname, remote);
479 strbuf_setlen(&truname, truname.len - len);
480 if (ref_exists(truname.buf)) {
481 strbuf_addf(msg,
482 "%s\t\tbranch '%s'%s of .\n",
483 sha1_to_hex(remote_head->object.sha1),
484 truname.buf + 11,
485 (early ? " (early part)" : ""));
486 strbuf_release(&truname);
487 goto cleanup;
491 if (!strcmp(remote, "FETCH_HEAD") &&
492 !access(git_path("FETCH_HEAD"), R_OK)) {
493 const char *filename;
494 FILE *fp;
495 struct strbuf line = STRBUF_INIT;
496 char *ptr;
498 filename = git_path("FETCH_HEAD");
499 fp = fopen(filename, "r");
500 if (!fp)
501 die_errno(_("could not open '%s' for reading"),
502 filename);
503 strbuf_getline(&line, fp, '\n');
504 fclose(fp);
505 ptr = strstr(line.buf, "\tnot-for-merge\t");
506 if (ptr)
507 strbuf_remove(&line, ptr-line.buf+1, 13);
508 strbuf_addbuf(msg, &line);
509 strbuf_release(&line);
510 goto cleanup;
512 strbuf_addf(msg, "%s\t\tcommit '%s'\n",
513 sha1_to_hex(remote_head->object.sha1), remote);
514 cleanup:
515 strbuf_release(&buf);
516 strbuf_release(&bname);
519 static void parse_branch_merge_options(char *bmo)
521 const char **argv;
522 int argc;
524 if (!bmo)
525 return;
526 argc = split_cmdline(bmo, &argv);
527 if (argc < 0)
528 die(_("Bad branch.%s.mergeoptions string: %s"), branch,
529 split_cmdline_strerror(argc));
530 argv = xrealloc(argv, sizeof(*argv) * (argc + 2));
531 memmove(argv + 1, argv, sizeof(*argv) * (argc + 1));
532 argc++;
533 argv[0] = "branch.*.mergeoptions";
534 parse_options(argc, argv, NULL, builtin_merge_options,
535 builtin_merge_usage, 0);
536 free(argv);
539 static int git_merge_config(const char *k, const char *v, void *cb)
541 int status;
543 if (branch && !prefixcmp(k, "branch.") &&
544 !prefixcmp(k + 7, branch) &&
545 !strcmp(k + 7 + strlen(branch), ".mergeoptions")) {
546 free(branch_mergeoptions);
547 branch_mergeoptions = xstrdup(v);
548 return 0;
551 if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat"))
552 show_diffstat = git_config_bool(k, v);
553 else if (!strcmp(k, "pull.twohead"))
554 return git_config_string(&pull_twohead, k, v);
555 else if (!strcmp(k, "pull.octopus"))
556 return git_config_string(&pull_octopus, k, v);
557 else if (!strcmp(k, "merge.renormalize"))
558 option_renormalize = git_config_bool(k, v);
559 else if (!strcmp(k, "merge.ff")) {
560 int boolval = git_config_maybe_bool(k, v);
561 if (0 <= boolval) {
562 allow_fast_forward = boolval;
563 } else if (v && !strcmp(v, "only")) {
564 allow_fast_forward = 1;
565 fast_forward_only = 1;
566 } /* do not barf on values from future versions of git */
567 return 0;
568 } else if (!strcmp(k, "merge.defaulttoupstream")) {
569 default_to_upstream = git_config_bool(k, v);
570 return 0;
572 status = fmt_merge_msg_config(k, v, cb);
573 if (status)
574 return status;
575 return git_diff_ui_config(k, v, cb);
578 static int read_tree_trivial(unsigned char *common, unsigned char *head,
579 unsigned char *one)
581 int i, nr_trees = 0;
582 struct tree *trees[MAX_UNPACK_TREES];
583 struct tree_desc t[MAX_UNPACK_TREES];
584 struct unpack_trees_options opts;
586 memset(&opts, 0, sizeof(opts));
587 opts.head_idx = 2;
588 opts.src_index = &the_index;
589 opts.dst_index = &the_index;
590 opts.update = 1;
591 opts.verbose_update = 1;
592 opts.trivial_merges_only = 1;
593 opts.merge = 1;
594 trees[nr_trees] = parse_tree_indirect(common);
595 if (!trees[nr_trees++])
596 return -1;
597 trees[nr_trees] = parse_tree_indirect(head);
598 if (!trees[nr_trees++])
599 return -1;
600 trees[nr_trees] = parse_tree_indirect(one);
601 if (!trees[nr_trees++])
602 return -1;
603 opts.fn = threeway_merge;
604 cache_tree_free(&active_cache_tree);
605 for (i = 0; i < nr_trees; i++) {
606 parse_tree(trees[i]);
607 init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
609 if (unpack_trees(nr_trees, t, &opts))
610 return -1;
611 return 0;
614 static void write_tree_trivial(unsigned char *sha1)
616 if (write_cache_as_tree(sha1, 0, NULL))
617 die(_("git write-tree failed to write a tree"));
620 static const char *merge_argument(struct commit *commit)
622 if (commit)
623 return sha1_to_hex(commit->object.sha1);
624 else
625 return EMPTY_TREE_SHA1_HEX;
628 int try_merge_command(const char *strategy, size_t xopts_nr,
629 const char **xopts, struct commit_list *common,
630 const char *head_arg, struct commit_list *remotes)
632 const char **args;
633 int i = 0, x = 0, ret;
634 struct commit_list *j;
635 struct strbuf buf = STRBUF_INIT;
637 args = xmalloc((4 + xopts_nr + commit_list_count(common) +
638 commit_list_count(remotes)) * sizeof(char *));
639 strbuf_addf(&buf, "merge-%s", strategy);
640 args[i++] = buf.buf;
641 for (x = 0; x < xopts_nr; x++) {
642 char *s = xmalloc(strlen(xopts[x])+2+1);
643 strcpy(s, "--");
644 strcpy(s+2, xopts[x]);
645 args[i++] = s;
647 for (j = common; j; j = j->next)
648 args[i++] = xstrdup(merge_argument(j->item));
649 args[i++] = "--";
650 args[i++] = head_arg;
651 for (j = remotes; j; j = j->next)
652 args[i++] = xstrdup(merge_argument(j->item));
653 args[i] = NULL;
654 ret = run_command_v_opt(args, RUN_GIT_CMD);
655 strbuf_release(&buf);
656 i = 1;
657 for (x = 0; x < xopts_nr; x++)
658 free((void *)args[i++]);
659 for (j = common; j; j = j->next)
660 free((void *)args[i++]);
661 i += 2;
662 for (j = remotes; j; j = j->next)
663 free((void *)args[i++]);
664 free(args);
665 discard_cache();
666 if (read_cache() < 0)
667 die(_("failed to read the cache"));
668 resolve_undo_clear();
670 return ret;
673 static int try_merge_strategy(const char *strategy, struct commit_list *common,
674 struct commit *head, const char *head_arg)
676 int index_fd;
677 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
679 index_fd = hold_locked_index(lock, 1);
680 refresh_cache(REFRESH_QUIET);
681 if (active_cache_changed &&
682 (write_cache(index_fd, active_cache, active_nr) ||
683 commit_locked_index(lock)))
684 return error(_("Unable to write index."));
685 rollback_lock_file(lock);
687 if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) {
688 int clean, x;
689 struct commit *result;
690 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
691 int index_fd;
692 struct commit_list *reversed = NULL;
693 struct merge_options o;
694 struct commit_list *j;
696 if (remoteheads->next) {
697 error(_("Not handling anything other than two heads merge."));
698 return 2;
701 init_merge_options(&o);
702 if (!strcmp(strategy, "subtree"))
703 o.subtree_shift = "";
705 o.renormalize = option_renormalize;
706 o.show_rename_progress =
707 show_progress == -1 ? isatty(2) : show_progress;
709 for (x = 0; x < xopts_nr; x++)
710 if (parse_merge_opt(&o, xopts[x]))
711 die(_("Unknown option for merge-recursive: -X%s"), xopts[x]);
713 o.branch1 = head_arg;
714 o.branch2 = merge_remote_util(remoteheads->item)->name;
716 for (j = common; j; j = j->next)
717 commit_list_insert(j->item, &reversed);
719 index_fd = hold_locked_index(lock, 1);
720 clean = merge_recursive(&o, head,
721 remoteheads->item, reversed, &result);
722 if (active_cache_changed &&
723 (write_cache(index_fd, active_cache, active_nr) ||
724 commit_locked_index(lock)))
725 die (_("unable to write %s"), get_index_file());
726 rollback_lock_file(lock);
727 return clean ? 0 : 1;
728 } else {
729 return try_merge_command(strategy, xopts_nr, xopts,
730 common, head_arg, remoteheads);
734 static void count_diff_files(struct diff_queue_struct *q,
735 struct diff_options *opt, void *data)
737 int *count = data;
739 (*count) += q->nr;
742 static int count_unmerged_entries(void)
744 int i, ret = 0;
746 for (i = 0; i < active_nr; i++)
747 if (ce_stage(active_cache[i]))
748 ret++;
750 return ret;
753 int checkout_fast_forward(const unsigned char *head, const unsigned char *remote)
755 struct tree *trees[MAX_UNPACK_TREES];
756 struct unpack_trees_options opts;
757 struct tree_desc t[MAX_UNPACK_TREES];
758 int i, fd, nr_trees = 0;
759 struct dir_struct dir;
760 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
762 refresh_cache(REFRESH_QUIET);
764 fd = hold_locked_index(lock_file, 1);
766 memset(&trees, 0, sizeof(trees));
767 memset(&opts, 0, sizeof(opts));
768 memset(&t, 0, sizeof(t));
769 memset(&dir, 0, sizeof(dir));
770 dir.flags |= DIR_SHOW_IGNORED;
771 dir.exclude_per_dir = ".gitignore";
772 opts.dir = &dir;
774 opts.head_idx = 1;
775 opts.src_index = &the_index;
776 opts.dst_index = &the_index;
777 opts.update = 1;
778 opts.verbose_update = 1;
779 opts.merge = 1;
780 opts.fn = twoway_merge;
781 setup_unpack_trees_porcelain(&opts, "merge");
783 trees[nr_trees] = parse_tree_indirect(head);
784 if (!trees[nr_trees++])
785 return -1;
786 trees[nr_trees] = parse_tree_indirect(remote);
787 if (!trees[nr_trees++])
788 return -1;
789 for (i = 0; i < nr_trees; i++) {
790 parse_tree(trees[i]);
791 init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
793 if (unpack_trees(nr_trees, t, &opts))
794 return -1;
795 if (write_cache(fd, active_cache, active_nr) ||
796 commit_locked_index(lock_file))
797 die(_("unable to write new index file"));
798 return 0;
801 static void split_merge_strategies(const char *string, struct strategy **list,
802 int *nr, int *alloc)
804 char *p, *q, *buf;
806 if (!string)
807 return;
809 buf = xstrdup(string);
810 q = buf;
811 for (;;) {
812 p = strchr(q, ' ');
813 if (!p) {
814 ALLOC_GROW(*list, *nr + 1, *alloc);
815 (*list)[(*nr)++].name = xstrdup(q);
816 free(buf);
817 return;
818 } else {
819 *p = '\0';
820 ALLOC_GROW(*list, *nr + 1, *alloc);
821 (*list)[(*nr)++].name = xstrdup(q);
822 q = ++p;
827 static void add_strategies(const char *string, unsigned attr)
829 struct strategy *list = NULL;
830 int list_alloc = 0, list_nr = 0, i;
832 memset(&list, 0, sizeof(list));
833 split_merge_strategies(string, &list, &list_nr, &list_alloc);
834 if (list) {
835 for (i = 0; i < list_nr; i++)
836 append_strategy(get_strategy(list[i].name));
837 return;
839 for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
840 if (all_strategy[i].attr & attr)
841 append_strategy(&all_strategy[i]);
845 static void write_merge_msg(struct strbuf *msg)
847 const char *filename = git_path("MERGE_MSG");
848 int fd = open(filename, O_WRONLY | O_CREAT, 0666);
849 if (fd < 0)
850 die_errno(_("Could not open '%s' for writing"),
851 filename);
852 if (write_in_full(fd, msg->buf, msg->len) != msg->len)
853 die_errno(_("Could not write to '%s'"), filename);
854 close(fd);
857 static void read_merge_msg(struct strbuf *msg)
859 const char *filename = git_path("MERGE_MSG");
860 strbuf_reset(msg);
861 if (strbuf_read_file(msg, filename, 0) < 0)
862 die_errno(_("Could not read from '%s'"), filename);
865 static void write_merge_state(void);
866 static void abort_commit(const char *err_msg)
868 if (err_msg)
869 error("%s", err_msg);
870 fprintf(stderr,
871 _("Not committing merge; use 'git commit' to complete the merge.\n"));
872 write_merge_state();
873 exit(1);
876 static void prepare_to_commit(void)
878 struct strbuf msg = STRBUF_INIT;
879 strbuf_addbuf(&msg, &merge_msg);
880 strbuf_addch(&msg, '\n');
881 write_merge_msg(&msg);
882 run_hook(get_index_file(), "prepare-commit-msg",
883 git_path("MERGE_MSG"), "merge", NULL, NULL);
884 if (option_edit) {
885 if (launch_editor(git_path("MERGE_MSG"), NULL, NULL))
886 abort_commit(NULL);
888 read_merge_msg(&msg);
889 stripspace(&msg, option_edit);
890 if (!msg.len)
891 abort_commit(_("Empty commit message."));
892 strbuf_release(&merge_msg);
893 strbuf_addbuf(&merge_msg, &msg);
894 strbuf_release(&msg);
897 static int merge_trivial(struct commit *head)
899 unsigned char result_tree[20], result_commit[20];
900 struct commit_list *parent = xmalloc(sizeof(*parent));
902 write_tree_trivial(result_tree);
903 printf(_("Wonderful.\n"));
904 parent->item = head;
905 parent->next = xmalloc(sizeof(*parent->next));
906 parent->next->item = remoteheads->item;
907 parent->next->next = NULL;
908 prepare_to_commit();
909 commit_tree(merge_msg.buf, result_tree, parent, result_commit, NULL);
910 finish(head, result_commit, "In-index merge");
911 drop_save();
912 return 0;
915 static int finish_automerge(struct commit *head,
916 struct commit_list *common,
917 unsigned char *result_tree,
918 const char *wt_strategy)
920 struct commit_list *parents = NULL, *j;
921 struct strbuf buf = STRBUF_INIT;
922 unsigned char result_commit[20];
924 free_commit_list(common);
925 if (allow_fast_forward) {
926 parents = remoteheads;
927 commit_list_insert(head, &parents);
928 parents = reduce_heads(parents);
929 } else {
930 struct commit_list **pptr = &parents;
932 pptr = &commit_list_insert(head,
933 pptr)->next;
934 for (j = remoteheads; j; j = j->next)
935 pptr = &commit_list_insert(j->item, pptr)->next;
937 strbuf_addch(&merge_msg, '\n');
938 prepare_to_commit();
939 free_commit_list(remoteheads);
940 commit_tree(merge_msg.buf, result_tree, parents, result_commit, NULL);
941 strbuf_addf(&buf, "Merge made by the '%s' strategy.", wt_strategy);
942 finish(head, result_commit, buf.buf);
943 strbuf_release(&buf);
944 drop_save();
945 return 0;
948 static int suggest_conflicts(int renormalizing)
950 const char *filename;
951 FILE *fp;
952 int pos;
954 filename = git_path("MERGE_MSG");
955 fp = fopen(filename, "a");
956 if (!fp)
957 die_errno(_("Could not open '%s' for writing"), filename);
958 fprintf(fp, "\nConflicts:\n");
959 for (pos = 0; pos < active_nr; pos++) {
960 struct cache_entry *ce = active_cache[pos];
962 if (ce_stage(ce)) {
963 fprintf(fp, "\t%s\n", ce->name);
964 while (pos + 1 < active_nr &&
965 !strcmp(ce->name,
966 active_cache[pos + 1]->name))
967 pos++;
970 fclose(fp);
971 rerere(allow_rerere_auto);
972 printf(_("Automatic merge failed; "
973 "fix conflicts and then commit the result.\n"));
974 return 1;
977 static struct commit *is_old_style_invocation(int argc, const char **argv,
978 const unsigned char *head)
980 struct commit *second_token = NULL;
981 if (argc > 2) {
982 unsigned char second_sha1[20];
984 if (get_sha1(argv[1], second_sha1))
985 return NULL;
986 second_token = lookup_commit_reference_gently(second_sha1, 0);
987 if (!second_token)
988 die(_("'%s' is not a commit"), argv[1]);
989 if (hashcmp(second_token->object.sha1, head))
990 return NULL;
992 return second_token;
995 static int evaluate_result(void)
997 int cnt = 0;
998 struct rev_info rev;
1000 /* Check how many files differ. */
1001 init_revisions(&rev, "");
1002 setup_revisions(0, NULL, &rev, NULL);
1003 rev.diffopt.output_format |=
1004 DIFF_FORMAT_CALLBACK;
1005 rev.diffopt.format_callback = count_diff_files;
1006 rev.diffopt.format_callback_data = &cnt;
1007 run_diff_files(&rev, 0);
1010 * Check how many unmerged entries are
1011 * there.
1013 cnt += count_unmerged_entries();
1015 return cnt;
1019 * Pretend as if the user told us to merge with the tracking
1020 * branch we have for the upstream of the current branch
1022 static int setup_with_upstream(const char ***argv)
1024 struct branch *branch = branch_get(NULL);
1025 int i;
1026 const char **args;
1028 if (!branch)
1029 die(_("No current branch."));
1030 if (!branch->remote)
1031 die(_("No remote for the current branch."));
1032 if (!branch->merge_nr)
1033 die(_("No default upstream defined for the current branch."));
1035 args = xcalloc(branch->merge_nr + 1, sizeof(char *));
1036 for (i = 0; i < branch->merge_nr; i++) {
1037 if (!branch->merge[i]->dst)
1038 die(_("No remote tracking branch for %s from %s"),
1039 branch->merge[i]->src, branch->remote_name);
1040 args[i] = branch->merge[i]->dst;
1042 args[i] = NULL;
1043 *argv = args;
1044 return i;
1047 static void write_merge_state(void)
1049 const char *filename;
1050 int fd;
1051 struct commit_list *j;
1052 struct strbuf buf = STRBUF_INIT;
1054 for (j = remoteheads; j; j = j->next) {
1055 unsigned const char *sha1;
1056 struct commit *c = j->item;
1057 if (c->util && merge_remote_util(c)->obj) {
1058 sha1 = merge_remote_util(c)->obj->sha1;
1059 } else {
1060 sha1 = c->object.sha1;
1062 strbuf_addf(&buf, "%s\n", sha1_to_hex(sha1));
1064 filename = git_path("MERGE_HEAD");
1065 fd = open(filename, O_WRONLY | O_CREAT, 0666);
1066 if (fd < 0)
1067 die_errno(_("Could not open '%s' for writing"), filename);
1068 if (write_in_full(fd, buf.buf, buf.len) != buf.len)
1069 die_errno(_("Could not write to '%s'"), filename);
1070 close(fd);
1071 strbuf_addch(&merge_msg, '\n');
1072 write_merge_msg(&merge_msg);
1074 filename = git_path("MERGE_MODE");
1075 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
1076 if (fd < 0)
1077 die_errno(_("Could not open '%s' for writing"), filename);
1078 strbuf_reset(&buf);
1079 if (!allow_fast_forward)
1080 strbuf_addf(&buf, "no-ff");
1081 if (write_in_full(fd, buf.buf, buf.len) != buf.len)
1082 die_errno(_("Could not write to '%s'"), filename);
1083 close(fd);
1086 int cmd_merge(int argc, const char **argv, const char *prefix)
1088 unsigned char result_tree[20];
1089 unsigned char stash[20];
1090 unsigned char head_sha1[20];
1091 struct commit *head_commit;
1092 struct strbuf buf = STRBUF_INIT;
1093 const char *head_arg;
1094 int flag, i, ret = 0;
1095 int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0;
1096 struct commit_list *common = NULL;
1097 const char *best_strategy = NULL, *wt_strategy = NULL;
1098 struct commit_list **remotes = &remoteheads;
1100 if (argc == 2 && !strcmp(argv[1], "-h"))
1101 usage_with_options(builtin_merge_usage, builtin_merge_options);
1104 * Check if we are _not_ on a detached HEAD, i.e. if there is a
1105 * current branch.
1107 branch = resolve_ref("HEAD", head_sha1, 0, &flag);
1108 if (branch) {
1109 if (!prefixcmp(branch, "refs/heads/"))
1110 branch += 11;
1111 branch = xstrdup(branch);
1113 if (!branch || is_null_sha1(head_sha1))
1114 head_commit = NULL;
1115 else
1116 head_commit = lookup_commit_or_die(head_sha1, "HEAD");
1118 git_config(git_merge_config, NULL);
1120 if (branch_mergeoptions)
1121 parse_branch_merge_options(branch_mergeoptions);
1122 argc = parse_options(argc, argv, prefix, builtin_merge_options,
1123 builtin_merge_usage, 0);
1124 if (shortlog_len < 0)
1125 shortlog_len = (merge_log_config > 0) ? merge_log_config : 0;
1127 if (verbosity < 0 && show_progress == -1)
1128 show_progress = 0;
1130 if (abort_current_merge) {
1131 int nargc = 2;
1132 const char *nargv[] = {"reset", "--merge", NULL};
1134 if (!file_exists(git_path("MERGE_HEAD")))
1135 die(_("There is no merge to abort (MERGE_HEAD missing)."));
1137 /* Invoke 'git reset --merge' */
1138 ret = cmd_reset(nargc, nargv, prefix);
1139 goto done;
1142 if (read_cache_unmerged())
1143 die_resolve_conflict("merge");
1145 if (file_exists(git_path("MERGE_HEAD"))) {
1147 * There is no unmerged entry, don't advise 'git
1148 * add/rm <file>', just 'git commit'.
1150 if (advice_resolve_conflict)
1151 die(_("You have not concluded your merge (MERGE_HEAD exists).\n"
1152 "Please, commit your changes before you can merge."));
1153 else
1154 die(_("You have not concluded your merge (MERGE_HEAD exists)."));
1156 if (file_exists(git_path("CHERRY_PICK_HEAD"))) {
1157 if (advice_resolve_conflict)
1158 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
1159 "Please, commit your changes before you can merge."));
1160 else
1161 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists)."));
1163 resolve_undo_clear();
1165 if (verbosity < 0)
1166 show_diffstat = 0;
1168 if (squash) {
1169 if (!allow_fast_forward)
1170 die(_("You cannot combine --squash with --no-ff."));
1171 option_commit = 0;
1174 if (!allow_fast_forward && fast_forward_only)
1175 die(_("You cannot combine --no-ff with --ff-only."));
1177 if (!abort_current_merge) {
1178 if (!argc && default_to_upstream)
1179 argc = setup_with_upstream(&argv);
1180 else if (argc == 1 && !strcmp(argv[0], "-"))
1181 argv[0] = "@{-1}";
1183 if (!argc)
1184 usage_with_options(builtin_merge_usage,
1185 builtin_merge_options);
1188 * This could be traditional "merge <msg> HEAD <commit>..." and
1189 * the way we can tell it is to see if the second token is HEAD,
1190 * but some people might have misused the interface and used a
1191 * committish that is the same as HEAD there instead.
1192 * Traditional format never would have "-m" so it is an
1193 * additional safety measure to check for it.
1196 if (!have_message && head_commit &&
1197 is_old_style_invocation(argc, argv, head_commit->object.sha1)) {
1198 strbuf_addstr(&merge_msg, argv[0]);
1199 head_arg = argv[1];
1200 argv += 2;
1201 argc -= 2;
1202 } else if (!head_commit) {
1203 struct commit *remote_head;
1205 * If the merged head is a valid one there is no reason
1206 * to forbid "git merge" into a branch yet to be born.
1207 * We do the same for "git pull".
1209 if (argc != 1)
1210 die(_("Can merge only exactly one commit into "
1211 "empty head"));
1212 if (squash)
1213 die(_("Squash commit into empty head not supported yet"));
1214 if (!allow_fast_forward)
1215 die(_("Non-fast-forward commit does not make sense into "
1216 "an empty head"));
1217 remote_head = get_merge_parent(argv[0]);
1218 if (!remote_head)
1219 die(_("%s - not something we can merge"), argv[0]);
1220 read_empty(remote_head->object.sha1, 0);
1221 update_ref("initial pull", "HEAD", remote_head->object.sha1,
1222 NULL, 0, DIE_ON_ERR);
1223 goto done;
1224 } else {
1225 struct strbuf merge_names = STRBUF_INIT;
1227 /* We are invoked directly as the first-class UI. */
1228 head_arg = "HEAD";
1231 * All the rest are the commits being merged; prepare
1232 * the standard merge summary message to be appended
1233 * to the given message.
1235 for (i = 0; i < argc; i++)
1236 merge_name(argv[i], &merge_names);
1238 if (!have_message || shortlog_len) {
1239 struct fmt_merge_msg_opts opts;
1240 memset(&opts, 0, sizeof(opts));
1241 opts.add_title = !have_message;
1242 opts.shortlog_len = shortlog_len;
1244 fmt_merge_msg(&merge_names, &merge_msg, &opts);
1245 if (merge_msg.len)
1246 strbuf_setlen(&merge_msg, merge_msg.len - 1);
1250 if (!head_commit || !argc)
1251 usage_with_options(builtin_merge_usage,
1252 builtin_merge_options);
1254 strbuf_addstr(&buf, "merge");
1255 for (i = 0; i < argc; i++)
1256 strbuf_addf(&buf, " %s", argv[i]);
1257 setenv("GIT_REFLOG_ACTION", buf.buf, 0);
1258 strbuf_reset(&buf);
1260 for (i = 0; i < argc; i++) {
1261 struct commit *commit = get_merge_parent(argv[i]);
1262 if (!commit)
1263 die(_("%s - not something we can merge"), argv[i]);
1264 remotes = &commit_list_insert(commit, remotes)->next;
1265 strbuf_addf(&buf, "GITHEAD_%s",
1266 sha1_to_hex(commit->object.sha1));
1267 setenv(buf.buf, argv[i], 1);
1268 strbuf_reset(&buf);
1269 if (merge_remote_util(commit) &&
1270 merge_remote_util(commit)->obj &&
1271 merge_remote_util(commit)->obj->type == OBJ_TAG) {
1272 option_edit = 1;
1273 allow_fast_forward = 0;
1277 if (!use_strategies) {
1278 if (!remoteheads->next)
1279 add_strategies(pull_twohead, DEFAULT_TWOHEAD);
1280 else
1281 add_strategies(pull_octopus, DEFAULT_OCTOPUS);
1284 for (i = 0; i < use_strategies_nr; i++) {
1285 if (use_strategies[i]->attr & NO_FAST_FORWARD)
1286 allow_fast_forward = 0;
1287 if (use_strategies[i]->attr & NO_TRIVIAL)
1288 allow_trivial = 0;
1291 if (!remoteheads->next)
1292 common = get_merge_bases(head_commit, remoteheads->item, 1);
1293 else {
1294 struct commit_list *list = remoteheads;
1295 commit_list_insert(head_commit, &list);
1296 common = get_octopus_merge_bases(list);
1297 free(list);
1300 update_ref("updating ORIG_HEAD", "ORIG_HEAD", head_commit->object.sha1,
1301 NULL, 0, DIE_ON_ERR);
1303 if (!common)
1304 ; /* No common ancestors found. We need a real merge. */
1305 else if (!remoteheads->next && !common->next &&
1306 common->item == remoteheads->item) {
1308 * If head can reach all the merge then we are up to date.
1309 * but first the most common case of merging one remote.
1311 finish_up_to_date("Already up-to-date.");
1312 goto done;
1313 } else if (allow_fast_forward && !remoteheads->next &&
1314 !common->next &&
1315 !hashcmp(common->item->object.sha1, head_commit->object.sha1)) {
1316 /* Again the most common case of merging one remote. */
1317 struct strbuf msg = STRBUF_INIT;
1318 struct commit *commit;
1319 char hex[41];
1321 strcpy(hex, find_unique_abbrev(head_commit->object.sha1, DEFAULT_ABBREV));
1323 if (verbosity >= 0)
1324 printf(_("Updating %s..%s\n"),
1325 hex,
1326 find_unique_abbrev(remoteheads->item->object.sha1,
1327 DEFAULT_ABBREV));
1328 strbuf_addstr(&msg, "Fast-forward");
1329 if (have_message)
1330 strbuf_addstr(&msg,
1331 " (no commit created; -m option ignored)");
1332 commit = remoteheads->item;
1333 if (!commit) {
1334 ret = 1;
1335 goto done;
1338 if (checkout_fast_forward(head_commit->object.sha1,
1339 commit->object.sha1)) {
1340 ret = 1;
1341 goto done;
1344 finish(head_commit, commit->object.sha1, msg.buf);
1345 drop_save();
1346 goto done;
1347 } else if (!remoteheads->next && common->next)
1350 * We are not doing octopus and not fast-forward. Need
1351 * a real merge.
1353 else if (!remoteheads->next && !common->next && option_commit) {
1355 * We are not doing octopus, not fast-forward, and have
1356 * only one common.
1358 refresh_cache(REFRESH_QUIET);
1359 if (allow_trivial && !fast_forward_only) {
1360 /* See if it is really trivial. */
1361 git_committer_info(IDENT_ERROR_ON_NO_NAME);
1362 printf(_("Trying really trivial in-index merge...\n"));
1363 if (!read_tree_trivial(common->item->object.sha1,
1364 head_commit->object.sha1,
1365 remoteheads->item->object.sha1)) {
1366 ret = merge_trivial(head_commit);
1367 goto done;
1369 printf(_("Nope.\n"));
1371 } else {
1373 * An octopus. If we can reach all the remote we are up
1374 * to date.
1376 int up_to_date = 1;
1377 struct commit_list *j;
1379 for (j = remoteheads; j; j = j->next) {
1380 struct commit_list *common_one;
1383 * Here we *have* to calculate the individual
1384 * merge_bases again, otherwise "git merge HEAD^
1385 * HEAD^^" would be missed.
1387 common_one = get_merge_bases(head_commit, j->item, 1);
1388 if (hashcmp(common_one->item->object.sha1,
1389 j->item->object.sha1)) {
1390 up_to_date = 0;
1391 break;
1394 if (up_to_date) {
1395 finish_up_to_date("Already up-to-date. Yeeah!");
1396 goto done;
1400 if (fast_forward_only)
1401 die(_("Not possible to fast-forward, aborting."));
1403 /* We are going to make a new commit. */
1404 git_committer_info(IDENT_ERROR_ON_NO_NAME);
1407 * At this point, we need a real merge. No matter what strategy
1408 * we use, it would operate on the index, possibly affecting the
1409 * working tree, and when resolved cleanly, have the desired
1410 * tree in the index -- this means that the index must be in
1411 * sync with the head commit. The strategies are responsible
1412 * to ensure this.
1414 if (use_strategies_nr == 1 ||
1416 * Stash away the local changes so that we can try more than one.
1418 save_state(stash))
1419 hashcpy(stash, null_sha1);
1421 for (i = 0; i < use_strategies_nr; i++) {
1422 int ret;
1423 if (i) {
1424 printf(_("Rewinding the tree to pristine...\n"));
1425 restore_state(head_commit->object.sha1, stash);
1427 if (use_strategies_nr != 1)
1428 printf(_("Trying merge strategy %s...\n"),
1429 use_strategies[i]->name);
1431 * Remember which strategy left the state in the working
1432 * tree.
1434 wt_strategy = use_strategies[i]->name;
1436 ret = try_merge_strategy(use_strategies[i]->name,
1437 common, head_commit, head_arg);
1438 if (!option_commit && !ret) {
1439 merge_was_ok = 1;
1441 * This is necessary here just to avoid writing
1442 * the tree, but later we will *not* exit with
1443 * status code 1 because merge_was_ok is set.
1445 ret = 1;
1448 if (ret) {
1450 * The backend exits with 1 when conflicts are
1451 * left to be resolved, with 2 when it does not
1452 * handle the given merge at all.
1454 if (ret == 1) {
1455 int cnt = evaluate_result();
1457 if (best_cnt <= 0 || cnt <= best_cnt) {
1458 best_strategy = use_strategies[i]->name;
1459 best_cnt = cnt;
1462 if (merge_was_ok)
1463 break;
1464 else
1465 continue;
1468 /* Automerge succeeded. */
1469 write_tree_trivial(result_tree);
1470 automerge_was_ok = 1;
1471 break;
1475 * If we have a resulting tree, that means the strategy module
1476 * auto resolved the merge cleanly.
1478 if (automerge_was_ok) {
1479 ret = finish_automerge(head_commit, common, result_tree,
1480 wt_strategy);
1481 goto done;
1485 * Pick the result from the best strategy and have the user fix
1486 * it up.
1488 if (!best_strategy) {
1489 restore_state(head_commit->object.sha1, stash);
1490 if (use_strategies_nr > 1)
1491 fprintf(stderr,
1492 _("No merge strategy handled the merge.\n"));
1493 else
1494 fprintf(stderr, _("Merge with strategy %s failed.\n"),
1495 use_strategies[0]->name);
1496 ret = 2;
1497 goto done;
1498 } else if (best_strategy == wt_strategy)
1499 ; /* We already have its result in the working tree. */
1500 else {
1501 printf(_("Rewinding the tree to pristine...\n"));
1502 restore_state(head_commit->object.sha1, stash);
1503 printf(_("Using the %s to prepare resolving by hand.\n"),
1504 best_strategy);
1505 try_merge_strategy(best_strategy, common, head_commit, head_arg);
1508 if (squash)
1509 finish(head_commit, NULL, NULL);
1510 else
1511 write_merge_state();
1513 if (merge_was_ok)
1514 fprintf(stderr, _("Automatic merge went well; "
1515 "stopped before committing as requested\n"));
1516 else
1517 ret = suggest_conflicts(option_renormalize);
1519 done:
1520 free((char*)branch);
1521 return ret;