Add sample pre-push hook script
[git/mingw.git] / builtin / merge.c
blob9307e9c726587461d002530938dbf2c0d74d55a1
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"
30 #include "gpg-interface.h"
32 #define DEFAULT_TWOHEAD (1<<0)
33 #define DEFAULT_OCTOPUS (1<<1)
34 #define NO_FAST_FORWARD (1<<2)
35 #define NO_TRIVIAL (1<<3)
37 struct strategy {
38 const char *name;
39 unsigned attr;
42 static const char * const builtin_merge_usage[] = {
43 N_("git merge [options] [<commit>...]"),
44 N_("git merge [options] <msg> HEAD <commit>"),
45 N_("git merge --abort"),
46 NULL
49 static int show_diffstat = 1, shortlog_len = -1, squash;
50 static int option_commit = 1, allow_fast_forward = 1;
51 static int fast_forward_only, option_edit = -1;
52 static int allow_trivial = 1, have_message;
53 static int overwrite_ignore = 1;
54 static struct strbuf merge_msg = STRBUF_INIT;
55 static struct strategy **use_strategies;
56 static size_t use_strategies_nr, use_strategies_alloc;
57 static const char **xopts;
58 static size_t xopts_nr, xopts_alloc;
59 static const char *branch;
60 static char *branch_mergeoptions;
61 static int option_renormalize;
62 static int verbosity;
63 static int allow_rerere_auto;
64 static int abort_current_merge;
65 static int show_progress = -1;
66 static int default_to_upstream;
67 static const char *sign_commit;
69 static struct strategy all_strategy[] = {
70 { "recursive", DEFAULT_TWOHEAD | NO_TRIVIAL },
71 { "octopus", DEFAULT_OCTOPUS },
72 { "resolve", 0 },
73 { "ours", NO_FAST_FORWARD | NO_TRIVIAL },
74 { "subtree", NO_FAST_FORWARD | NO_TRIVIAL },
77 static const char *pull_twohead, *pull_octopus;
79 static int option_parse_message(const struct option *opt,
80 const char *arg, int unset)
82 struct strbuf *buf = opt->value;
84 if (unset)
85 strbuf_setlen(buf, 0);
86 else if (arg) {
87 strbuf_addf(buf, "%s%s", buf->len ? "\n\n" : "", arg);
88 have_message = 1;
89 } else
90 return error(_("switch `m' requires a value"));
91 return 0;
94 static struct strategy *get_strategy(const char *name)
96 int i;
97 struct strategy *ret;
98 static struct cmdnames main_cmds, other_cmds;
99 static int loaded;
101 if (!name)
102 return NULL;
104 for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
105 if (!strcmp(name, all_strategy[i].name))
106 return &all_strategy[i];
108 if (!loaded) {
109 struct cmdnames not_strategies;
110 loaded = 1;
112 memset(&not_strategies, 0, sizeof(struct cmdnames));
113 load_command_list("git-merge-", &main_cmds, &other_cmds);
114 for (i = 0; i < main_cmds.cnt; i++) {
115 int j, found = 0;
116 struct cmdname *ent = main_cmds.names[i];
117 for (j = 0; j < ARRAY_SIZE(all_strategy); j++)
118 if (!strncmp(ent->name, all_strategy[j].name, ent->len)
119 && !all_strategy[j].name[ent->len])
120 found = 1;
121 if (!found)
122 add_cmdname(&not_strategies, ent->name, ent->len);
124 exclude_cmds(&main_cmds, &not_strategies);
126 if (!is_in_cmdlist(&main_cmds, name) && !is_in_cmdlist(&other_cmds, name)) {
127 fprintf(stderr, _("Could not find merge strategy '%s'.\n"), name);
128 fprintf(stderr, _("Available strategies are:"));
129 for (i = 0; i < main_cmds.cnt; i++)
130 fprintf(stderr, " %s", main_cmds.names[i]->name);
131 fprintf(stderr, ".\n");
132 if (other_cmds.cnt) {
133 fprintf(stderr, _("Available custom strategies are:"));
134 for (i = 0; i < other_cmds.cnt; i++)
135 fprintf(stderr, " %s", other_cmds.names[i]->name);
136 fprintf(stderr, ".\n");
138 exit(1);
141 ret = xcalloc(1, sizeof(struct strategy));
142 ret->name = xstrdup(name);
143 ret->attr = NO_TRIVIAL;
144 return ret;
147 static void append_strategy(struct strategy *s)
149 ALLOC_GROW(use_strategies, use_strategies_nr + 1, use_strategies_alloc);
150 use_strategies[use_strategies_nr++] = s;
153 static int option_parse_strategy(const struct option *opt,
154 const char *name, int unset)
156 if (unset)
157 return 0;
159 append_strategy(get_strategy(name));
160 return 0;
163 static int option_parse_x(const struct option *opt,
164 const char *arg, int unset)
166 if (unset)
167 return 0;
169 ALLOC_GROW(xopts, xopts_nr + 1, xopts_alloc);
170 xopts[xopts_nr++] = xstrdup(arg);
171 return 0;
174 static int option_parse_n(const struct option *opt,
175 const char *arg, int unset)
177 show_diffstat = unset;
178 return 0;
181 static struct option builtin_merge_options[] = {
182 { OPTION_CALLBACK, 'n', NULL, NULL, NULL,
183 N_("do not show a diffstat at the end of the merge"),
184 PARSE_OPT_NOARG, option_parse_n },
185 OPT_BOOLEAN(0, "stat", &show_diffstat,
186 N_("show a diffstat at the end of the merge")),
187 OPT_BOOLEAN(0, "summary", &show_diffstat, N_("(synonym to --stat)")),
188 { OPTION_INTEGER, 0, "log", &shortlog_len, N_("n"),
189 N_("add (at most <n>) entries from shortlog to merge commit message"),
190 PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
191 OPT_BOOLEAN(0, "squash", &squash,
192 N_("create a single commit instead of doing a merge")),
193 OPT_BOOLEAN(0, "commit", &option_commit,
194 N_("perform a commit if the merge succeeds (default)")),
195 OPT_BOOL('e', "edit", &option_edit,
196 N_("edit message before committing")),
197 OPT_BOOLEAN(0, "ff", &allow_fast_forward,
198 N_("allow fast-forward (default)")),
199 OPT_BOOLEAN(0, "ff-only", &fast_forward_only,
200 N_("abort if fast-forward is not possible")),
201 OPT_RERERE_AUTOUPDATE(&allow_rerere_auto),
202 OPT_CALLBACK('s', "strategy", &use_strategies, N_("strategy"),
203 N_("merge strategy to use"), option_parse_strategy),
204 OPT_CALLBACK('X', "strategy-option", &xopts, N_("option=value"),
205 N_("option for selected merge strategy"), option_parse_x),
206 OPT_CALLBACK('m', "message", &merge_msg, N_("message"),
207 N_("merge commit message (for a non-fast-forward merge)"),
208 option_parse_message),
209 OPT__VERBOSITY(&verbosity),
210 OPT_BOOLEAN(0, "abort", &abort_current_merge,
211 N_("abort the current in-progress merge")),
212 OPT_SET_INT(0, "progress", &show_progress, N_("force progress reporting"), 1),
213 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key id"),
214 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
215 OPT_BOOLEAN(0, "overwrite-ignore", &overwrite_ignore, N_("update ignored files (default)")),
216 OPT_END()
219 /* Cleans up metadata that is uninteresting after a succeeded merge. */
220 static void drop_save(void)
222 unlink(git_path("MERGE_HEAD"));
223 unlink(git_path("MERGE_MSG"));
224 unlink(git_path("MERGE_MODE"));
227 static int save_state(unsigned char *stash)
229 int len;
230 struct child_process cp;
231 struct strbuf buffer = STRBUF_INIT;
232 const char *argv[] = {"stash", "create", NULL};
234 memset(&cp, 0, sizeof(cp));
235 cp.argv = argv;
236 cp.out = -1;
237 cp.git_cmd = 1;
239 if (start_command(&cp))
240 die(_("could not run stash."));
241 len = strbuf_read(&buffer, cp.out, 1024);
242 close(cp.out);
244 if (finish_command(&cp) || len < 0)
245 die(_("stash failed"));
246 else if (!len) /* no changes */
247 return -1;
248 strbuf_setlen(&buffer, buffer.len-1);
249 if (get_sha1(buffer.buf, stash))
250 die(_("not a valid object: %s"), buffer.buf);
251 return 0;
254 static void read_empty(unsigned const char *sha1, int verbose)
256 int i = 0;
257 const char *args[7];
259 args[i++] = "read-tree";
260 if (verbose)
261 args[i++] = "-v";
262 args[i++] = "-m";
263 args[i++] = "-u";
264 args[i++] = EMPTY_TREE_SHA1_HEX;
265 args[i++] = sha1_to_hex(sha1);
266 args[i] = NULL;
268 if (run_command_v_opt(args, RUN_GIT_CMD))
269 die(_("read-tree failed"));
272 static void reset_hard(unsigned const char *sha1, int verbose)
274 int i = 0;
275 const char *args[6];
277 args[i++] = "read-tree";
278 if (verbose)
279 args[i++] = "-v";
280 args[i++] = "--reset";
281 args[i++] = "-u";
282 args[i++] = sha1_to_hex(sha1);
283 args[i] = NULL;
285 if (run_command_v_opt(args, RUN_GIT_CMD))
286 die(_("read-tree failed"));
289 static void restore_state(const unsigned char *head,
290 const unsigned char *stash)
292 struct strbuf sb = STRBUF_INIT;
293 const char *args[] = { "stash", "apply", NULL, NULL };
295 if (is_null_sha1(stash))
296 return;
298 reset_hard(head, 1);
300 args[2] = sha1_to_hex(stash);
303 * It is OK to ignore error here, for example when there was
304 * nothing to restore.
306 run_command_v_opt(args, RUN_GIT_CMD);
308 strbuf_release(&sb);
309 refresh_cache(REFRESH_QUIET);
312 /* This is called when no merge was necessary. */
313 static void finish_up_to_date(const char *msg)
315 if (verbosity >= 0)
316 printf("%s%s\n", squash ? _(" (nothing to squash)") : "", msg);
317 drop_save();
320 static void squash_message(struct commit *commit, struct commit_list *remoteheads)
322 struct rev_info rev;
323 struct strbuf out = STRBUF_INIT;
324 struct commit_list *j;
325 const char *filename;
326 int fd;
327 struct pretty_print_context ctx = {0};
329 printf(_("Squash commit -- not updating HEAD\n"));
330 filename = git_path("SQUASH_MSG");
331 fd = open(filename, O_WRONLY | O_CREAT, 0666);
332 if (fd < 0)
333 die_errno(_("Could not write to '%s'"), filename);
335 init_revisions(&rev, NULL);
336 rev.ignore_merges = 1;
337 rev.commit_format = CMIT_FMT_MEDIUM;
339 commit->object.flags |= UNINTERESTING;
340 add_pending_object(&rev, &commit->object, NULL);
342 for (j = remoteheads; j; j = j->next)
343 add_pending_object(&rev, &j->item->object, NULL);
345 setup_revisions(0, NULL, &rev, NULL);
346 if (prepare_revision_walk(&rev))
347 die(_("revision walk setup failed"));
349 ctx.abbrev = rev.abbrev;
350 ctx.date_mode = rev.date_mode;
351 ctx.fmt = rev.commit_format;
353 strbuf_addstr(&out, "Squashed commit of the following:\n");
354 while ((commit = get_revision(&rev)) != NULL) {
355 strbuf_addch(&out, '\n');
356 strbuf_addf(&out, "commit %s\n",
357 sha1_to_hex(commit->object.sha1));
358 pretty_print_commit(&ctx, commit, &out);
360 if (write(fd, out.buf, out.len) < 0)
361 die_errno(_("Writing SQUASH_MSG"));
362 if (close(fd))
363 die_errno(_("Finishing SQUASH_MSG"));
364 strbuf_release(&out);
367 static void finish(struct commit *head_commit,
368 struct commit_list *remoteheads,
369 const unsigned char *new_head, const char *msg)
371 struct strbuf reflog_message = STRBUF_INIT;
372 const unsigned char *head = head_commit->object.sha1;
374 if (!msg)
375 strbuf_addstr(&reflog_message, getenv("GIT_REFLOG_ACTION"));
376 else {
377 if (verbosity >= 0)
378 printf("%s\n", msg);
379 strbuf_addf(&reflog_message, "%s: %s",
380 getenv("GIT_REFLOG_ACTION"), msg);
382 if (squash) {
383 squash_message(head_commit, remoteheads);
384 } else {
385 if (verbosity >= 0 && !merge_msg.len)
386 printf(_("No merge message -- not updating HEAD\n"));
387 else {
388 const char *argv_gc_auto[] = { "gc", "--auto", NULL };
389 update_ref(reflog_message.buf, "HEAD",
390 new_head, head, 0,
391 DIE_ON_ERR);
393 * We ignore errors in 'gc --auto', since the
394 * user should see them.
396 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
399 if (new_head && show_diffstat) {
400 struct diff_options opts;
401 diff_setup(&opts);
402 opts.stat_width = -1; /* use full terminal width */
403 opts.stat_graph_width = -1; /* respect statGraphWidth config */
404 opts.output_format |=
405 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
406 opts.detect_rename = DIFF_DETECT_RENAME;
407 diff_setup_done(&opts);
408 diff_tree_sha1(head, new_head, "", &opts);
409 diffcore_std(&opts);
410 diff_flush(&opts);
413 /* Run a post-merge hook */
414 run_hook(NULL, "post-merge", squash ? "1" : "0", NULL);
416 strbuf_release(&reflog_message);
419 /* Get the name for the merge commit's message. */
420 static void merge_name(const char *remote, struct strbuf *msg)
422 struct commit *remote_head;
423 unsigned char branch_head[20];
424 struct strbuf buf = STRBUF_INIT;
425 struct strbuf bname = STRBUF_INIT;
426 const char *ptr;
427 char *found_ref;
428 int len, early;
430 strbuf_branchname(&bname, remote);
431 remote = bname.buf;
433 memset(branch_head, 0, sizeof(branch_head));
434 remote_head = get_merge_parent(remote);
435 if (!remote_head)
436 die(_("'%s' does not point to a commit"), remote);
438 if (dwim_ref(remote, strlen(remote), branch_head, &found_ref) > 0) {
439 if (!prefixcmp(found_ref, "refs/heads/")) {
440 strbuf_addf(msg, "%s\t\tbranch '%s' of .\n",
441 sha1_to_hex(branch_head), remote);
442 goto cleanup;
444 if (!prefixcmp(found_ref, "refs/tags/")) {
445 strbuf_addf(msg, "%s\t\ttag '%s' of .\n",
446 sha1_to_hex(branch_head), remote);
447 goto cleanup;
449 if (!prefixcmp(found_ref, "refs/remotes/")) {
450 strbuf_addf(msg, "%s\t\tremote-tracking branch '%s' of .\n",
451 sha1_to_hex(branch_head), remote);
452 goto cleanup;
456 /* See if remote matches <name>^^^.. or <name>~<number> */
457 for (len = 0, ptr = remote + strlen(remote);
458 remote < ptr && ptr[-1] == '^';
459 ptr--)
460 len++;
461 if (len)
462 early = 1;
463 else {
464 early = 0;
465 ptr = strrchr(remote, '~');
466 if (ptr) {
467 int seen_nonzero = 0;
469 len++; /* count ~ */
470 while (*++ptr && isdigit(*ptr)) {
471 seen_nonzero |= (*ptr != '0');
472 len++;
474 if (*ptr)
475 len = 0; /* not ...~<number> */
476 else if (seen_nonzero)
477 early = 1;
478 else if (len == 1)
479 early = 1; /* "name~" is "name~1"! */
482 if (len) {
483 struct strbuf truname = STRBUF_INIT;
484 strbuf_addstr(&truname, "refs/heads/");
485 strbuf_addstr(&truname, remote);
486 strbuf_setlen(&truname, truname.len - len);
487 if (ref_exists(truname.buf)) {
488 strbuf_addf(msg,
489 "%s\t\tbranch '%s'%s of .\n",
490 sha1_to_hex(remote_head->object.sha1),
491 truname.buf + 11,
492 (early ? " (early part)" : ""));
493 strbuf_release(&truname);
494 goto cleanup;
498 if (!strcmp(remote, "FETCH_HEAD") &&
499 !access(git_path("FETCH_HEAD"), R_OK)) {
500 const char *filename;
501 FILE *fp;
502 struct strbuf line = STRBUF_INIT;
503 char *ptr;
505 filename = git_path("FETCH_HEAD");
506 fp = fopen(filename, "r");
507 if (!fp)
508 die_errno(_("could not open '%s' for reading"),
509 filename);
510 strbuf_getline(&line, fp, '\n');
511 fclose(fp);
512 ptr = strstr(line.buf, "\tnot-for-merge\t");
513 if (ptr)
514 strbuf_remove(&line, ptr-line.buf+1, 13);
515 strbuf_addbuf(msg, &line);
516 strbuf_release(&line);
517 goto cleanup;
519 strbuf_addf(msg, "%s\t\tcommit '%s'\n",
520 sha1_to_hex(remote_head->object.sha1), remote);
521 cleanup:
522 strbuf_release(&buf);
523 strbuf_release(&bname);
526 static void parse_branch_merge_options(char *bmo)
528 const char **argv;
529 int argc;
531 if (!bmo)
532 return;
533 argc = split_cmdline(bmo, &argv);
534 if (argc < 0)
535 die(_("Bad branch.%s.mergeoptions string: %s"), branch,
536 split_cmdline_strerror(argc));
537 argv = xrealloc(argv, sizeof(*argv) * (argc + 2));
538 memmove(argv + 1, argv, sizeof(*argv) * (argc + 1));
539 argc++;
540 argv[0] = "branch.*.mergeoptions";
541 parse_options(argc, argv, NULL, builtin_merge_options,
542 builtin_merge_usage, 0);
543 free(argv);
546 static int git_merge_config(const char *k, const char *v, void *cb)
548 int status;
550 if (branch && !prefixcmp(k, "branch.") &&
551 !prefixcmp(k + 7, branch) &&
552 !strcmp(k + 7 + strlen(branch), ".mergeoptions")) {
553 free(branch_mergeoptions);
554 branch_mergeoptions = xstrdup(v);
555 return 0;
558 if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat"))
559 show_diffstat = git_config_bool(k, v);
560 else if (!strcmp(k, "pull.twohead"))
561 return git_config_string(&pull_twohead, k, v);
562 else if (!strcmp(k, "pull.octopus"))
563 return git_config_string(&pull_octopus, k, v);
564 else if (!strcmp(k, "merge.renormalize"))
565 option_renormalize = git_config_bool(k, v);
566 else if (!strcmp(k, "merge.ff")) {
567 int boolval = git_config_maybe_bool(k, v);
568 if (0 <= boolval) {
569 allow_fast_forward = boolval;
570 } else if (v && !strcmp(v, "only")) {
571 allow_fast_forward = 1;
572 fast_forward_only = 1;
573 } /* do not barf on values from future versions of git */
574 return 0;
575 } else if (!strcmp(k, "merge.defaulttoupstream")) {
576 default_to_upstream = git_config_bool(k, v);
577 return 0;
580 status = fmt_merge_msg_config(k, v, cb);
581 if (status)
582 return status;
583 status = git_gpg_config(k, v, NULL);
584 if (status)
585 return status;
586 return git_diff_ui_config(k, v, cb);
589 static int read_tree_trivial(unsigned char *common, unsigned char *head,
590 unsigned char *one)
592 int i, nr_trees = 0;
593 struct tree *trees[MAX_UNPACK_TREES];
594 struct tree_desc t[MAX_UNPACK_TREES];
595 struct unpack_trees_options opts;
597 memset(&opts, 0, sizeof(opts));
598 opts.head_idx = 2;
599 opts.src_index = &the_index;
600 opts.dst_index = &the_index;
601 opts.update = 1;
602 opts.verbose_update = 1;
603 opts.trivial_merges_only = 1;
604 opts.merge = 1;
605 trees[nr_trees] = parse_tree_indirect(common);
606 if (!trees[nr_trees++])
607 return -1;
608 trees[nr_trees] = parse_tree_indirect(head);
609 if (!trees[nr_trees++])
610 return -1;
611 trees[nr_trees] = parse_tree_indirect(one);
612 if (!trees[nr_trees++])
613 return -1;
614 opts.fn = threeway_merge;
615 cache_tree_free(&active_cache_tree);
616 for (i = 0; i < nr_trees; i++) {
617 parse_tree(trees[i]);
618 init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
620 if (unpack_trees(nr_trees, t, &opts))
621 return -1;
622 return 0;
625 static void write_tree_trivial(unsigned char *sha1)
627 if (write_cache_as_tree(sha1, 0, NULL))
628 die(_("git write-tree failed to write a tree"));
631 static int try_merge_strategy(const char *strategy, struct commit_list *common,
632 struct commit_list *remoteheads,
633 struct commit *head, const char *head_arg)
635 int index_fd;
636 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
638 index_fd = hold_locked_index(lock, 1);
639 refresh_cache(REFRESH_QUIET);
640 if (active_cache_changed &&
641 (write_cache(index_fd, active_cache, active_nr) ||
642 commit_locked_index(lock)))
643 return error(_("Unable to write index."));
644 rollback_lock_file(lock);
646 if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) {
647 int clean, x;
648 struct commit *result;
649 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
650 int index_fd;
651 struct commit_list *reversed = NULL;
652 struct merge_options o;
653 struct commit_list *j;
655 if (remoteheads->next) {
656 error(_("Not handling anything other than two heads merge."));
657 return 2;
660 init_merge_options(&o);
661 if (!strcmp(strategy, "subtree"))
662 o.subtree_shift = "";
664 o.renormalize = option_renormalize;
665 o.show_rename_progress =
666 show_progress == -1 ? isatty(2) : show_progress;
668 for (x = 0; x < xopts_nr; x++)
669 if (parse_merge_opt(&o, xopts[x]))
670 die(_("Unknown option for merge-recursive: -X%s"), xopts[x]);
672 o.branch1 = head_arg;
673 o.branch2 = merge_remote_util(remoteheads->item)->name;
675 for (j = common; j; j = j->next)
676 commit_list_insert(j->item, &reversed);
678 index_fd = hold_locked_index(lock, 1);
679 clean = merge_recursive(&o, head,
680 remoteheads->item, reversed, &result);
681 if (active_cache_changed &&
682 (write_cache(index_fd, active_cache, active_nr) ||
683 commit_locked_index(lock)))
684 die (_("unable to write %s"), get_index_file());
685 rollback_lock_file(lock);
686 return clean ? 0 : 1;
687 } else {
688 return try_merge_command(strategy, xopts_nr, xopts,
689 common, head_arg, remoteheads);
693 static void count_diff_files(struct diff_queue_struct *q,
694 struct diff_options *opt, void *data)
696 int *count = data;
698 (*count) += q->nr;
701 static int count_unmerged_entries(void)
703 int i, ret = 0;
705 for (i = 0; i < active_nr; i++)
706 if (ce_stage(active_cache[i]))
707 ret++;
709 return ret;
712 static void split_merge_strategies(const char *string, struct strategy **list,
713 int *nr, int *alloc)
715 char *p, *q, *buf;
717 if (!string)
718 return;
720 buf = xstrdup(string);
721 q = buf;
722 for (;;) {
723 p = strchr(q, ' ');
724 if (!p) {
725 ALLOC_GROW(*list, *nr + 1, *alloc);
726 (*list)[(*nr)++].name = xstrdup(q);
727 free(buf);
728 return;
729 } else {
730 *p = '\0';
731 ALLOC_GROW(*list, *nr + 1, *alloc);
732 (*list)[(*nr)++].name = xstrdup(q);
733 q = ++p;
738 static void add_strategies(const char *string, unsigned attr)
740 struct strategy *list = NULL;
741 int list_alloc = 0, list_nr = 0, i;
743 memset(&list, 0, sizeof(list));
744 split_merge_strategies(string, &list, &list_nr, &list_alloc);
745 if (list) {
746 for (i = 0; i < list_nr; i++)
747 append_strategy(get_strategy(list[i].name));
748 return;
750 for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
751 if (all_strategy[i].attr & attr)
752 append_strategy(&all_strategy[i]);
756 static void write_merge_msg(struct strbuf *msg)
758 const char *filename = git_path("MERGE_MSG");
759 int fd = open(filename, O_WRONLY | O_CREAT, 0666);
760 if (fd < 0)
761 die_errno(_("Could not open '%s' for writing"),
762 filename);
763 if (write_in_full(fd, msg->buf, msg->len) != msg->len)
764 die_errno(_("Could not write to '%s'"), filename);
765 close(fd);
768 static void read_merge_msg(struct strbuf *msg)
770 const char *filename = git_path("MERGE_MSG");
771 strbuf_reset(msg);
772 if (strbuf_read_file(msg, filename, 0) < 0)
773 die_errno(_("Could not read from '%s'"), filename);
776 static void write_merge_state(struct commit_list *);
777 static void abort_commit(struct commit_list *remoteheads, const char *err_msg)
779 if (err_msg)
780 error("%s", err_msg);
781 fprintf(stderr,
782 _("Not committing merge; use 'git commit' to complete the merge.\n"));
783 write_merge_state(remoteheads);
784 exit(1);
787 static const char merge_editor_comment[] =
788 N_("Please enter a commit message to explain why this merge is necessary,\n"
789 "especially if it merges an updated upstream into a topic branch.\n"
790 "\n"
791 "Lines starting with '#' will be ignored, and an empty message aborts\n"
792 "the commit.\n");
794 static void prepare_to_commit(struct commit_list *remoteheads)
796 struct strbuf msg = STRBUF_INIT;
797 const char *comment = _(merge_editor_comment);
798 strbuf_addbuf(&msg, &merge_msg);
799 strbuf_addch(&msg, '\n');
800 if (0 < option_edit)
801 strbuf_add_lines(&msg, "# ", comment, strlen(comment));
802 write_merge_msg(&msg);
803 if (run_hook(get_index_file(), "prepare-commit-msg",
804 git_path("MERGE_MSG"), "merge", NULL, NULL))
805 abort_commit(remoteheads, NULL);
806 if (0 < option_edit) {
807 if (launch_editor(git_path("MERGE_MSG"), NULL, NULL))
808 abort_commit(remoteheads, NULL);
810 read_merge_msg(&msg);
811 stripspace(&msg, 0 < option_edit);
812 if (!msg.len)
813 abort_commit(remoteheads, _("Empty commit message."));
814 strbuf_release(&merge_msg);
815 strbuf_addbuf(&merge_msg, &msg);
816 strbuf_release(&msg);
819 static int merge_trivial(struct commit *head, struct commit_list *remoteheads)
821 unsigned char result_tree[20], result_commit[20];
822 struct commit_list *parent = xmalloc(sizeof(*parent));
824 write_tree_trivial(result_tree);
825 printf(_("Wonderful.\n"));
826 parent->item = head;
827 parent->next = xmalloc(sizeof(*parent->next));
828 parent->next->item = remoteheads->item;
829 parent->next->next = NULL;
830 prepare_to_commit(remoteheads);
831 if (commit_tree(&merge_msg, result_tree, parent, result_commit, NULL,
832 sign_commit))
833 die(_("failed to write commit object"));
834 finish(head, remoteheads, result_commit, "In-index merge");
835 drop_save();
836 return 0;
839 static int finish_automerge(struct commit *head,
840 int head_subsumed,
841 struct commit_list *common,
842 struct commit_list *remoteheads,
843 unsigned char *result_tree,
844 const char *wt_strategy)
846 struct commit_list *parents = NULL;
847 struct strbuf buf = STRBUF_INIT;
848 unsigned char result_commit[20];
850 free_commit_list(common);
851 parents = remoteheads;
852 if (!head_subsumed || !allow_fast_forward)
853 commit_list_insert(head, &parents);
854 strbuf_addch(&merge_msg, '\n');
855 prepare_to_commit(remoteheads);
856 if (commit_tree(&merge_msg, result_tree, parents, result_commit,
857 NULL, sign_commit))
858 die(_("failed to write commit object"));
859 strbuf_addf(&buf, "Merge made by the '%s' strategy.", wt_strategy);
860 finish(head, remoteheads, result_commit, buf.buf);
861 strbuf_release(&buf);
862 drop_save();
863 return 0;
866 static int suggest_conflicts(int renormalizing)
868 const char *filename;
869 FILE *fp;
870 int pos;
872 filename = git_path("MERGE_MSG");
873 fp = fopen(filename, "a");
874 if (!fp)
875 die_errno(_("Could not open '%s' for writing"), filename);
876 fprintf(fp, "\nConflicts:\n");
877 for (pos = 0; pos < active_nr; pos++) {
878 struct cache_entry *ce = active_cache[pos];
880 if (ce_stage(ce)) {
881 fprintf(fp, "\t%s\n", ce->name);
882 while (pos + 1 < active_nr &&
883 !strcmp(ce->name,
884 active_cache[pos + 1]->name))
885 pos++;
888 fclose(fp);
889 rerere(allow_rerere_auto);
890 printf(_("Automatic merge failed; "
891 "fix conflicts and then commit the result.\n"));
892 return 1;
895 static struct commit *is_old_style_invocation(int argc, const char **argv,
896 const unsigned char *head)
898 struct commit *second_token = NULL;
899 if (argc > 2) {
900 unsigned char second_sha1[20];
902 if (get_sha1(argv[1], second_sha1))
903 return NULL;
904 second_token = lookup_commit_reference_gently(second_sha1, 0);
905 if (!second_token)
906 die(_("'%s' is not a commit"), argv[1]);
907 if (hashcmp(second_token->object.sha1, head))
908 return NULL;
910 return second_token;
913 static int evaluate_result(void)
915 int cnt = 0;
916 struct rev_info rev;
918 /* Check how many files differ. */
919 init_revisions(&rev, "");
920 setup_revisions(0, NULL, &rev, NULL);
921 rev.diffopt.output_format |=
922 DIFF_FORMAT_CALLBACK;
923 rev.diffopt.format_callback = count_diff_files;
924 rev.diffopt.format_callback_data = &cnt;
925 run_diff_files(&rev, 0);
928 * Check how many unmerged entries are
929 * there.
931 cnt += count_unmerged_entries();
933 return cnt;
937 * Pretend as if the user told us to merge with the tracking
938 * branch we have for the upstream of the current branch
940 static int setup_with_upstream(const char ***argv)
942 struct branch *branch = branch_get(NULL);
943 int i;
944 const char **args;
946 if (!branch)
947 die(_("No current branch."));
948 if (!branch->remote)
949 die(_("No remote for the current branch."));
950 if (!branch->merge_nr)
951 die(_("No default upstream defined for the current branch."));
953 args = xcalloc(branch->merge_nr + 1, sizeof(char *));
954 for (i = 0; i < branch->merge_nr; i++) {
955 if (!branch->merge[i]->dst)
956 die(_("No remote tracking branch for %s from %s"),
957 branch->merge[i]->src, branch->remote_name);
958 args[i] = branch->merge[i]->dst;
960 args[i] = NULL;
961 *argv = args;
962 return i;
965 static void write_merge_state(struct commit_list *remoteheads)
967 const char *filename;
968 int fd;
969 struct commit_list *j;
970 struct strbuf buf = STRBUF_INIT;
972 for (j = remoteheads; j; j = j->next) {
973 unsigned const char *sha1;
974 struct commit *c = j->item;
975 if (c->util && merge_remote_util(c)->obj) {
976 sha1 = merge_remote_util(c)->obj->sha1;
977 } else {
978 sha1 = c->object.sha1;
980 strbuf_addf(&buf, "%s\n", sha1_to_hex(sha1));
982 filename = git_path("MERGE_HEAD");
983 fd = open(filename, O_WRONLY | O_CREAT, 0666);
984 if (fd < 0)
985 die_errno(_("Could not open '%s' for writing"), filename);
986 if (write_in_full(fd, buf.buf, buf.len) != buf.len)
987 die_errno(_("Could not write to '%s'"), filename);
988 close(fd);
989 strbuf_addch(&merge_msg, '\n');
990 write_merge_msg(&merge_msg);
992 filename = git_path("MERGE_MODE");
993 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
994 if (fd < 0)
995 die_errno(_("Could not open '%s' for writing"), filename);
996 strbuf_reset(&buf);
997 if (!allow_fast_forward)
998 strbuf_addf(&buf, "no-ff");
999 if (write_in_full(fd, buf.buf, buf.len) != buf.len)
1000 die_errno(_("Could not write to '%s'"), filename);
1001 close(fd);
1004 static int default_edit_option(void)
1006 static const char name[] = "GIT_MERGE_AUTOEDIT";
1007 const char *e = getenv(name);
1008 struct stat st_stdin, st_stdout;
1010 if (have_message)
1011 /* an explicit -m msg without --[no-]edit */
1012 return 0;
1014 if (e) {
1015 int v = git_config_maybe_bool(name, e);
1016 if (v < 0)
1017 die("Bad value '%s' in environment '%s'", e, name);
1018 return v;
1021 /* Use editor if stdin and stdout are the same and is a tty */
1022 return (!fstat(0, &st_stdin) &&
1023 !fstat(1, &st_stdout) &&
1024 isatty(0) && isatty(1) &&
1025 st_stdin.st_dev == st_stdout.st_dev &&
1026 st_stdin.st_ino == st_stdout.st_ino &&
1027 st_stdin.st_mode == st_stdout.st_mode);
1030 static struct commit_list *collect_parents(struct commit *head_commit,
1031 int *head_subsumed,
1032 int argc, const char **argv)
1034 int i;
1035 struct commit_list *remoteheads = NULL, *parents, *next;
1036 struct commit_list **remotes = &remoteheads;
1038 if (head_commit)
1039 remotes = &commit_list_insert(head_commit, remotes)->next;
1040 for (i = 0; i < argc; i++) {
1041 struct commit *commit = get_merge_parent(argv[i]);
1042 if (!commit)
1043 die(_("%s - not something we can merge"), argv[i]);
1044 remotes = &commit_list_insert(commit, remotes)->next;
1046 *remotes = NULL;
1048 parents = reduce_heads(remoteheads);
1050 *head_subsumed = 1; /* we will flip this to 0 when we find it */
1051 for (remoteheads = NULL, remotes = &remoteheads;
1052 parents;
1053 parents = next) {
1054 struct commit *commit = parents->item;
1055 next = parents->next;
1056 if (commit == head_commit)
1057 *head_subsumed = 0;
1058 else
1059 remotes = &commit_list_insert(commit, remotes)->next;
1061 return remoteheads;
1064 int cmd_merge(int argc, const char **argv, const char *prefix)
1066 unsigned char result_tree[20];
1067 unsigned char stash[20];
1068 unsigned char head_sha1[20];
1069 struct commit *head_commit;
1070 struct strbuf buf = STRBUF_INIT;
1071 const char *head_arg;
1072 int flag, i, ret = 0, head_subsumed;
1073 int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0;
1074 struct commit_list *common = NULL;
1075 const char *best_strategy = NULL, *wt_strategy = NULL;
1076 struct commit_list *remoteheads, *p;
1077 void *branch_to_free;
1079 if (argc == 2 && !strcmp(argv[1], "-h"))
1080 usage_with_options(builtin_merge_usage, builtin_merge_options);
1083 * Check if we are _not_ on a detached HEAD, i.e. if there is a
1084 * current branch.
1086 branch = branch_to_free = resolve_refdup("HEAD", head_sha1, 0, &flag);
1087 if (branch && !prefixcmp(branch, "refs/heads/"))
1088 branch += 11;
1089 if (!branch || is_null_sha1(head_sha1))
1090 head_commit = NULL;
1091 else
1092 head_commit = lookup_commit_or_die(head_sha1, "HEAD");
1094 git_config(git_merge_config, NULL);
1096 if (branch_mergeoptions)
1097 parse_branch_merge_options(branch_mergeoptions);
1098 argc = parse_options(argc, argv, prefix, builtin_merge_options,
1099 builtin_merge_usage, 0);
1100 if (shortlog_len < 0)
1101 shortlog_len = (merge_log_config > 0) ? merge_log_config : 0;
1103 if (verbosity < 0 && show_progress == -1)
1104 show_progress = 0;
1106 if (abort_current_merge) {
1107 int nargc = 2;
1108 const char *nargv[] = {"reset", "--merge", NULL};
1110 if (!file_exists(git_path("MERGE_HEAD")))
1111 die(_("There is no merge to abort (MERGE_HEAD missing)."));
1113 /* Invoke 'git reset --merge' */
1114 ret = cmd_reset(nargc, nargv, prefix);
1115 goto done;
1118 if (read_cache_unmerged())
1119 die_resolve_conflict("merge");
1121 if (file_exists(git_path("MERGE_HEAD"))) {
1123 * There is no unmerged entry, don't advise 'git
1124 * add/rm <file>', just 'git commit'.
1126 if (advice_resolve_conflict)
1127 die(_("You have not concluded your merge (MERGE_HEAD exists).\n"
1128 "Please, commit your changes before you can merge."));
1129 else
1130 die(_("You have not concluded your merge (MERGE_HEAD exists)."));
1132 if (file_exists(git_path("CHERRY_PICK_HEAD"))) {
1133 if (advice_resolve_conflict)
1134 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
1135 "Please, commit your changes before you can merge."));
1136 else
1137 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists)."));
1139 resolve_undo_clear();
1141 if (verbosity < 0)
1142 show_diffstat = 0;
1144 if (squash) {
1145 if (!allow_fast_forward)
1146 die(_("You cannot combine --squash with --no-ff."));
1147 option_commit = 0;
1150 if (!allow_fast_forward && fast_forward_only)
1151 die(_("You cannot combine --no-ff with --ff-only."));
1153 if (!abort_current_merge) {
1154 if (!argc) {
1155 if (default_to_upstream)
1156 argc = setup_with_upstream(&argv);
1157 else
1158 die(_("No commit specified and merge.defaultToUpstream not set."));
1159 } else if (argc == 1 && !strcmp(argv[0], "-"))
1160 argv[0] = "@{-1}";
1162 if (!argc)
1163 usage_with_options(builtin_merge_usage,
1164 builtin_merge_options);
1167 * This could be traditional "merge <msg> HEAD <commit>..." and
1168 * the way we can tell it is to see if the second token is HEAD,
1169 * but some people might have misused the interface and used a
1170 * committish that is the same as HEAD there instead.
1171 * Traditional format never would have "-m" so it is an
1172 * additional safety measure to check for it.
1175 if (!have_message && head_commit &&
1176 is_old_style_invocation(argc, argv, head_commit->object.sha1)) {
1177 strbuf_addstr(&merge_msg, argv[0]);
1178 head_arg = argv[1];
1179 argv += 2;
1180 argc -= 2;
1181 remoteheads = collect_parents(head_commit, &head_subsumed, argc, argv);
1182 } else if (!head_commit) {
1183 struct commit *remote_head;
1185 * If the merged head is a valid one there is no reason
1186 * to forbid "git merge" into a branch yet to be born.
1187 * We do the same for "git pull".
1189 if (argc != 1)
1190 die(_("Can merge only exactly one commit into "
1191 "empty head"));
1192 if (squash)
1193 die(_("Squash commit into empty head not supported yet"));
1194 if (!allow_fast_forward)
1195 die(_("Non-fast-forward commit does not make sense into "
1196 "an empty head"));
1197 remoteheads = collect_parents(head_commit, &head_subsumed, argc, argv);
1198 remote_head = remoteheads->item;
1199 if (!remote_head)
1200 die(_("%s - not something we can merge"), argv[0]);
1201 read_empty(remote_head->object.sha1, 0);
1202 update_ref("initial pull", "HEAD", remote_head->object.sha1,
1203 NULL, 0, DIE_ON_ERR);
1204 goto done;
1205 } else {
1206 struct strbuf merge_names = STRBUF_INIT;
1208 /* We are invoked directly as the first-class UI. */
1209 head_arg = "HEAD";
1212 * All the rest are the commits being merged; prepare
1213 * the standard merge summary message to be appended
1214 * to the given message.
1216 remoteheads = collect_parents(head_commit, &head_subsumed, argc, argv);
1217 for (p = remoteheads; p; p = p->next)
1218 merge_name(merge_remote_util(p->item)->name, &merge_names);
1220 if (!have_message || shortlog_len) {
1221 struct fmt_merge_msg_opts opts;
1222 memset(&opts, 0, sizeof(opts));
1223 opts.add_title = !have_message;
1224 opts.shortlog_len = shortlog_len;
1225 opts.credit_people = (0 < option_edit);
1227 fmt_merge_msg(&merge_names, &merge_msg, &opts);
1228 if (merge_msg.len)
1229 strbuf_setlen(&merge_msg, merge_msg.len - 1);
1233 if (!head_commit || !argc)
1234 usage_with_options(builtin_merge_usage,
1235 builtin_merge_options);
1237 strbuf_addstr(&buf, "merge");
1238 for (p = remoteheads; p; p = p->next)
1239 strbuf_addf(&buf, " %s", merge_remote_util(p->item)->name);
1240 setenv("GIT_REFLOG_ACTION", buf.buf, 0);
1241 strbuf_reset(&buf);
1243 for (p = remoteheads; p; p = p->next) {
1244 struct commit *commit = p->item;
1245 strbuf_addf(&buf, "GITHEAD_%s",
1246 sha1_to_hex(commit->object.sha1));
1247 setenv(buf.buf, merge_remote_util(commit)->name, 1);
1248 strbuf_reset(&buf);
1249 if (!fast_forward_only &&
1250 merge_remote_util(commit) &&
1251 merge_remote_util(commit)->obj &&
1252 merge_remote_util(commit)->obj->type == OBJ_TAG)
1253 allow_fast_forward = 0;
1256 if (option_edit < 0)
1257 option_edit = default_edit_option();
1259 if (!use_strategies) {
1260 if (!remoteheads)
1261 ; /* already up-to-date */
1262 else if (!remoteheads->next)
1263 add_strategies(pull_twohead, DEFAULT_TWOHEAD);
1264 else
1265 add_strategies(pull_octopus, DEFAULT_OCTOPUS);
1268 for (i = 0; i < use_strategies_nr; i++) {
1269 if (use_strategies[i]->attr & NO_FAST_FORWARD)
1270 allow_fast_forward = 0;
1271 if (use_strategies[i]->attr & NO_TRIVIAL)
1272 allow_trivial = 0;
1275 if (!remoteheads)
1276 ; /* already up-to-date */
1277 else if (!remoteheads->next)
1278 common = get_merge_bases(head_commit, remoteheads->item, 1);
1279 else {
1280 struct commit_list *list = remoteheads;
1281 commit_list_insert(head_commit, &list);
1282 common = get_octopus_merge_bases(list);
1283 free(list);
1286 update_ref("updating ORIG_HEAD", "ORIG_HEAD", head_commit->object.sha1,
1287 NULL, 0, DIE_ON_ERR);
1289 if (remoteheads && !common)
1290 ; /* No common ancestors found. We need a real merge. */
1291 else if (!remoteheads ||
1292 (!remoteheads->next && !common->next &&
1293 common->item == remoteheads->item)) {
1295 * If head can reach all the merge then we are up to date.
1296 * but first the most common case of merging one remote.
1298 finish_up_to_date("Already up-to-date.");
1299 goto done;
1300 } else if (allow_fast_forward && !remoteheads->next &&
1301 !common->next &&
1302 !hashcmp(common->item->object.sha1, head_commit->object.sha1)) {
1303 /* Again the most common case of merging one remote. */
1304 struct strbuf msg = STRBUF_INIT;
1305 struct commit *commit;
1306 char hex[41];
1308 strcpy(hex, find_unique_abbrev(head_commit->object.sha1, DEFAULT_ABBREV));
1310 if (verbosity >= 0)
1311 printf(_("Updating %s..%s\n"),
1312 hex,
1313 find_unique_abbrev(remoteheads->item->object.sha1,
1314 DEFAULT_ABBREV));
1315 strbuf_addstr(&msg, "Fast-forward");
1316 if (have_message)
1317 strbuf_addstr(&msg,
1318 " (no commit created; -m option ignored)");
1319 commit = remoteheads->item;
1320 if (!commit) {
1321 ret = 1;
1322 goto done;
1325 if (checkout_fast_forward(head_commit->object.sha1,
1326 commit->object.sha1,
1327 overwrite_ignore)) {
1328 ret = 1;
1329 goto done;
1332 finish(head_commit, remoteheads, commit->object.sha1, msg.buf);
1333 drop_save();
1334 goto done;
1335 } else if (!remoteheads->next && common->next)
1338 * We are not doing octopus and not fast-forward. Need
1339 * a real merge.
1341 else if (!remoteheads->next && !common->next && option_commit) {
1343 * We are not doing octopus, not fast-forward, and have
1344 * only one common.
1346 refresh_cache(REFRESH_QUIET);
1347 if (allow_trivial && !fast_forward_only) {
1348 /* See if it is really trivial. */
1349 git_committer_info(IDENT_STRICT);
1350 printf(_("Trying really trivial in-index merge...\n"));
1351 if (!read_tree_trivial(common->item->object.sha1,
1352 head_commit->object.sha1,
1353 remoteheads->item->object.sha1)) {
1354 ret = merge_trivial(head_commit, remoteheads);
1355 goto done;
1357 printf(_("Nope.\n"));
1359 } else {
1361 * An octopus. If we can reach all the remote we are up
1362 * to date.
1364 int up_to_date = 1;
1365 struct commit_list *j;
1367 for (j = remoteheads; j; j = j->next) {
1368 struct commit_list *common_one;
1371 * Here we *have* to calculate the individual
1372 * merge_bases again, otherwise "git merge HEAD^
1373 * HEAD^^" would be missed.
1375 common_one = get_merge_bases(head_commit, j->item, 1);
1376 if (hashcmp(common_one->item->object.sha1,
1377 j->item->object.sha1)) {
1378 up_to_date = 0;
1379 break;
1382 if (up_to_date) {
1383 finish_up_to_date("Already up-to-date. Yeeah!");
1384 goto done;
1388 if (fast_forward_only)
1389 die(_("Not possible to fast-forward, aborting."));
1391 /* We are going to make a new commit. */
1392 git_committer_info(IDENT_STRICT);
1395 * At this point, we need a real merge. No matter what strategy
1396 * we use, it would operate on the index, possibly affecting the
1397 * working tree, and when resolved cleanly, have the desired
1398 * tree in the index -- this means that the index must be in
1399 * sync with the head commit. The strategies are responsible
1400 * to ensure this.
1402 if (use_strategies_nr == 1 ||
1404 * Stash away the local changes so that we can try more than one.
1406 save_state(stash))
1407 hashcpy(stash, null_sha1);
1409 for (i = 0; i < use_strategies_nr; i++) {
1410 int ret;
1411 if (i) {
1412 printf(_("Rewinding the tree to pristine...\n"));
1413 restore_state(head_commit->object.sha1, stash);
1415 if (use_strategies_nr != 1)
1416 printf(_("Trying merge strategy %s...\n"),
1417 use_strategies[i]->name);
1419 * Remember which strategy left the state in the working
1420 * tree.
1422 wt_strategy = use_strategies[i]->name;
1424 ret = try_merge_strategy(use_strategies[i]->name,
1425 common, remoteheads,
1426 head_commit, head_arg);
1427 if (!option_commit && !ret) {
1428 merge_was_ok = 1;
1430 * This is necessary here just to avoid writing
1431 * the tree, but later we will *not* exit with
1432 * status code 1 because merge_was_ok is set.
1434 ret = 1;
1437 if (ret) {
1439 * The backend exits with 1 when conflicts are
1440 * left to be resolved, with 2 when it does not
1441 * handle the given merge at all.
1443 if (ret == 1) {
1444 int cnt = evaluate_result();
1446 if (best_cnt <= 0 || cnt <= best_cnt) {
1447 best_strategy = use_strategies[i]->name;
1448 best_cnt = cnt;
1451 if (merge_was_ok)
1452 break;
1453 else
1454 continue;
1457 /* Automerge succeeded. */
1458 write_tree_trivial(result_tree);
1459 automerge_was_ok = 1;
1460 break;
1464 * If we have a resulting tree, that means the strategy module
1465 * auto resolved the merge cleanly.
1467 if (automerge_was_ok) {
1468 ret = finish_automerge(head_commit, head_subsumed,
1469 common, remoteheads,
1470 result_tree, wt_strategy);
1471 goto done;
1475 * Pick the result from the best strategy and have the user fix
1476 * it up.
1478 if (!best_strategy) {
1479 restore_state(head_commit->object.sha1, stash);
1480 if (use_strategies_nr > 1)
1481 fprintf(stderr,
1482 _("No merge strategy handled the merge.\n"));
1483 else
1484 fprintf(stderr, _("Merge with strategy %s failed.\n"),
1485 use_strategies[0]->name);
1486 ret = 2;
1487 goto done;
1488 } else if (best_strategy == wt_strategy)
1489 ; /* We already have its result in the working tree. */
1490 else {
1491 printf(_("Rewinding the tree to pristine...\n"));
1492 restore_state(head_commit->object.sha1, stash);
1493 printf(_("Using the %s to prepare resolving by hand.\n"),
1494 best_strategy);
1495 try_merge_strategy(best_strategy, common, remoteheads,
1496 head_commit, head_arg);
1499 if (squash)
1500 finish(head_commit, remoteheads, NULL, NULL);
1501 else
1502 write_merge_state(remoteheads);
1504 if (merge_was_ok)
1505 fprintf(stderr, _("Automatic merge went well; "
1506 "stopped before committing as requested\n"));
1507 else
1508 ret = suggest_conflicts(option_renormalize);
1510 done:
1511 free(branch_to_free);
1512 return ret;