4 * Copyright (c) 2008 Miklos Vajna <vmiklos@frugalware.org>
6 * Based on git-merge.sh by Junio C Hamano.
10 #include "parse-options.h"
12 #include "run-command.h"
18 #include "unpack-trees.h"
19 #include "cache-tree.h"
26 #define DEFAULT_TWOHEAD (1<<0)
27 #define DEFAULT_OCTOPUS (1<<1)
28 #define NO_FAST_FORWARD (1<<2)
29 #define NO_TRIVIAL (1<<3)
36 static const char * const builtin_merge_usage[] = {
37 "git-merge [options] <remote>...",
38 "git-merge [options] <msg> HEAD <remote>",
42 static int show_diffstat = 1, option_log, squash;
43 static int option_commit = 1, allow_fast_forward = 1;
44 static int allow_trivial = 1, have_message;
45 static struct strbuf merge_msg;
46 static struct commit_list *remoteheads;
47 static unsigned char head[20], stash[20];
48 static struct strategy **use_strategies;
49 static size_t use_strategies_nr, use_strategies_alloc;
50 static const char *branch;
52 static struct strategy all_strategy[] = {
53 { "recursive", DEFAULT_TWOHEAD | NO_TRIVIAL },
54 { "octopus", DEFAULT_OCTOPUS },
56 { "ours", NO_FAST_FORWARD | NO_TRIVIAL },
57 { "subtree", NO_FAST_FORWARD | NO_TRIVIAL },
60 static const char *pull_twohead, *pull_octopus;
62 static int option_parse_message(const struct option *opt,
63 const char *arg, int unset)
65 struct strbuf *buf = opt->value;
68 strbuf_setlen(buf, 0);
70 strbuf_addf(buf, "%s\n\n", arg);
73 return error("switch `m' requires a value");
77 static struct strategy *get_strategy(const char *name)
85 for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
86 if (!strcmp(name, all_strategy[i].name))
87 return &all_strategy[i];
90 for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
91 strbuf_addf(&err, " %s", all_strategy[i].name);
92 fprintf(stderr, "Could not find merge strategy '%s'.\n", name);
93 fprintf(stderr, "Available strategies are:%s.\n", err.buf);
97 static void append_strategy(struct strategy *s)
99 ALLOC_GROW(use_strategies, use_strategies_nr + 1, use_strategies_alloc);
100 use_strategies[use_strategies_nr++] = s;
103 static int option_parse_strategy(const struct option *opt,
104 const char *name, int unset)
109 append_strategy(get_strategy(name));
113 static int option_parse_n(const struct option *opt,
114 const char *arg, int unset)
116 show_diffstat = unset;
120 static struct option builtin_merge_options[] = {
121 { OPTION_CALLBACK, 'n', NULL, NULL, NULL,
122 "do not show a diffstat at the end of the merge",
123 PARSE_OPT_NOARG, option_parse_n },
124 OPT_BOOLEAN(0, "stat", &show_diffstat,
125 "show a diffstat at the end of the merge"),
126 OPT_BOOLEAN(0, "summary", &show_diffstat, "(synonym to --stat)"),
127 OPT_BOOLEAN(0, "log", &option_log,
128 "add list of one-line log to merge commit message"),
129 OPT_BOOLEAN(0, "squash", &squash,
130 "create a single commit instead of doing a merge"),
131 OPT_BOOLEAN(0, "commit", &option_commit,
132 "perform a commit if the merge succeeds (default)"),
133 OPT_BOOLEAN(0, "ff", &allow_fast_forward,
134 "allow fast forward (default)"),
135 OPT_CALLBACK('s', "strategy", &use_strategies, "strategy",
136 "merge strategy to use", option_parse_strategy),
137 OPT_CALLBACK('m', "message", &merge_msg, "message",
138 "message to be used for the merge commit (if any)",
139 option_parse_message),
143 /* Cleans up metadata that is uninteresting after a succeeded merge. */
144 static void drop_save(void)
146 unlink(git_path("MERGE_HEAD"));
147 unlink(git_path("MERGE_MSG"));
150 static void save_state(void)
153 struct child_process cp;
154 struct strbuf buffer = STRBUF_INIT;
155 const char *argv[] = {"stash", "create", NULL};
157 memset(&cp, 0, sizeof(cp));
162 if (start_command(&cp))
163 die("could not run stash.");
164 len = strbuf_read(&buffer, cp.out, 1024);
167 if (finish_command(&cp) || len < 0)
171 strbuf_setlen(&buffer, buffer.len-1);
172 if (get_sha1(buffer.buf, stash))
173 die("not a valid object: %s", buffer.buf);
176 static void reset_hard(unsigned const char *sha1, int verbose)
181 args[i++] = "read-tree";
184 args[i++] = "--reset";
186 args[i++] = sha1_to_hex(sha1);
189 if (run_command_v_opt(args, RUN_GIT_CMD))
190 die("read-tree failed");
193 static void restore_state(void)
196 const char *args[] = { "stash", "apply", NULL, NULL };
198 if (is_null_sha1(stash))
204 args[2] = sha1_to_hex(stash);
207 * It is OK to ignore error here, for example when there was
208 * nothing to restore.
210 run_command_v_opt(args, RUN_GIT_CMD);
213 refresh_cache(REFRESH_QUIET);
216 /* This is called when no merge was necessary. */
217 static void finish_up_to_date(const char *msg)
219 printf("%s%s\n", squash ? " (nothing to squash)" : "", msg);
223 static void squash_message(void)
226 struct commit *commit;
228 struct commit_list *j;
231 printf("Squash commit -- not updating HEAD\n");
232 fd = open(git_path("SQUASH_MSG"), O_WRONLY | O_CREAT, 0666);
234 die("Could not write to %s", git_path("SQUASH_MSG"));
236 init_revisions(&rev, NULL);
237 rev.ignore_merges = 1;
238 rev.commit_format = CMIT_FMT_MEDIUM;
240 commit = lookup_commit(head);
241 commit->object.flags |= UNINTERESTING;
242 add_pending_object(&rev, &commit->object, NULL);
244 for (j = remoteheads; j; j = j->next)
245 add_pending_object(&rev, &j->item->object, NULL);
247 setup_revisions(0, NULL, &rev, NULL);
248 if (prepare_revision_walk(&rev))
249 die("revision walk setup failed");
251 strbuf_init(&out, 0);
252 strbuf_addstr(&out, "Squashed commit of the following:\n");
253 while ((commit = get_revision(&rev)) != NULL) {
254 strbuf_addch(&out, '\n');
255 strbuf_addf(&out, "commit %s\n",
256 sha1_to_hex(commit->object.sha1));
257 pretty_print_commit(rev.commit_format, commit, &out, rev.abbrev,
258 NULL, NULL, rev.date_mode, 0);
260 write(fd, out.buf, out.len);
262 strbuf_release(&out);
265 static int run_hook(const char *name)
267 struct child_process hook;
268 const char *argv[3], *env[2];
269 char index[PATH_MAX];
271 argv[0] = git_path("hooks/%s", name);
272 if (access(argv[0], X_OK) < 0)
275 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", get_index_file());
285 memset(&hook, 0, sizeof(hook));
288 hook.stdout_to_stderr = 1;
291 return run_command(&hook);
294 static void finish(const unsigned char *new_head, const char *msg)
296 struct strbuf reflog_message;
298 strbuf_init(&reflog_message, 0);
300 strbuf_addstr(&reflog_message, getenv("GIT_REFLOG_ACTION"));
303 strbuf_addf(&reflog_message, "%s: %s",
304 getenv("GIT_REFLOG_ACTION"), msg);
310 printf("No merge message -- not updating HEAD\n");
312 const char *argv_gc_auto[] = { "gc", "--auto", NULL };
313 update_ref(reflog_message.buf, "HEAD",
317 * We ignore errors in 'gc --auto', since the
318 * user should see them.
320 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
323 if (new_head && show_diffstat) {
324 struct diff_options opts;
326 opts.output_format |=
327 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
328 opts.detect_rename = DIFF_DETECT_RENAME;
329 if (diff_use_color_default > 0)
330 DIFF_OPT_SET(&opts, COLOR_DIFF);
331 if (diff_setup_done(&opts) < 0)
332 die("diff_setup_done failed");
333 diff_tree_sha1(head, new_head, "", &opts);
338 /* Run a post-merge hook */
339 run_hook("post-merge");
341 strbuf_release(&reflog_message);
344 /* Get the name for the merge commit's message. */
345 static void merge_name(const char *remote, struct strbuf *msg)
347 struct object *remote_head;
348 unsigned char branch_head[20], buf_sha[20];
353 memset(branch_head, 0, sizeof(branch_head));
354 remote_head = peel_to_type(remote, 0, NULL, OBJ_COMMIT);
356 die("'%s' does not point to a commit", remote);
358 strbuf_init(&buf, 0);
359 strbuf_addstr(&buf, "refs/heads/");
360 strbuf_addstr(&buf, remote);
361 resolve_ref(buf.buf, branch_head, 0, 0);
363 if (!hashcmp(remote_head->sha1, branch_head)) {
364 strbuf_addf(msg, "%s\t\tbranch '%s' of .\n",
365 sha1_to_hex(branch_head), remote);
369 /* See if remote matches <name>^^^.. or <name>~<number> */
370 for (len = 0, ptr = remote + strlen(remote);
371 remote < ptr && ptr[-1] == '^';
378 ptr = strrchr(remote, '~');
380 int seen_nonzero = 0;
383 while (*++ptr && isdigit(*ptr)) {
384 seen_nonzero |= (*ptr != '0');
388 len = 0; /* not ...~<number> */
389 else if (seen_nonzero)
392 early = 1; /* "name~" is "name~1"! */
396 struct strbuf truname = STRBUF_INIT;
397 strbuf_addstr(&truname, "refs/heads/");
398 strbuf_addstr(&truname, remote);
399 strbuf_setlen(&truname, truname.len - len);
400 if (resolve_ref(truname.buf, buf_sha, 0, 0)) {
402 "%s\t\tbranch '%s'%s of .\n",
403 sha1_to_hex(remote_head->sha1),
405 (early ? " (early part)" : ""));
410 if (!strcmp(remote, "FETCH_HEAD") &&
411 !access(git_path("FETCH_HEAD"), R_OK)) {
416 strbuf_init(&line, 0);
417 fp = fopen(git_path("FETCH_HEAD"), "r");
419 die("could not open %s for reading: %s",
420 git_path("FETCH_HEAD"), strerror(errno));
421 strbuf_getline(&line, fp, '\n');
423 ptr = strstr(line.buf, "\tnot-for-merge\t");
425 strbuf_remove(&line, ptr-line.buf+1, 13);
426 strbuf_addbuf(msg, &line);
427 strbuf_release(&line);
430 strbuf_addf(msg, "%s\t\tcommit '%s'\n",
431 sha1_to_hex(remote_head->sha1), remote);
434 static int git_merge_config(const char *k, const char *v, void *cb)
436 if (branch && !prefixcmp(k, "branch.") &&
437 !prefixcmp(k + 7, branch) &&
438 !strcmp(k + 7 + strlen(branch), ".mergeoptions")) {
444 argc = split_cmdline(buf, &argv);
445 argv = xrealloc(argv, sizeof(*argv) * (argc + 2));
446 memmove(argv + 1, argv, sizeof(*argv) * (argc + 1));
448 parse_options(argc, argv, builtin_merge_options,
449 builtin_merge_usage, 0);
453 if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat"))
454 show_diffstat = git_config_bool(k, v);
455 else if (!strcmp(k, "pull.twohead"))
456 return git_config_string(&pull_twohead, k, v);
457 else if (!strcmp(k, "pull.octopus"))
458 return git_config_string(&pull_octopus, k, v);
459 else if (!strcmp(k, "merge.log") || !strcmp(k, "merge.summary"))
460 option_log = git_config_bool(k, v);
461 return git_diff_ui_config(k, v, cb);
464 static int read_tree_trivial(unsigned char *common, unsigned char *head,
468 struct tree *trees[MAX_UNPACK_TREES];
469 struct tree_desc t[MAX_UNPACK_TREES];
470 struct unpack_trees_options opts;
472 memset(&opts, 0, sizeof(opts));
474 opts.src_index = &the_index;
475 opts.dst_index = &the_index;
477 opts.verbose_update = 1;
478 opts.trivial_merges_only = 1;
480 trees[nr_trees] = parse_tree_indirect(common);
481 if (!trees[nr_trees++])
483 trees[nr_trees] = parse_tree_indirect(head);
484 if (!trees[nr_trees++])
486 trees[nr_trees] = parse_tree_indirect(one);
487 if (!trees[nr_trees++])
489 opts.fn = threeway_merge;
490 cache_tree_free(&active_cache_tree);
491 for (i = 0; i < nr_trees; i++) {
492 parse_tree(trees[i]);
493 init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
495 if (unpack_trees(nr_trees, t, &opts))
500 static void write_tree_trivial(unsigned char *sha1)
502 if (write_cache_as_tree(sha1, 0, NULL))
503 die("git write-tree failed to write a tree");
506 static int try_merge_strategy(const char *strategy, struct commit_list *common,
507 const char *head_arg)
511 struct commit_list *j;
514 args = xmalloc((4 + commit_list_count(common) +
515 commit_list_count(remoteheads)) * sizeof(char *));
516 strbuf_init(&buf, 0);
517 strbuf_addf(&buf, "merge-%s", strategy);
519 for (j = common; j; j = j->next)
520 args[i++] = xstrdup(sha1_to_hex(j->item->object.sha1));
522 args[i++] = head_arg;
523 for (j = remoteheads; j; j = j->next)
524 args[i++] = xstrdup(sha1_to_hex(j->item->object.sha1));
526 ret = run_command_v_opt(args, RUN_GIT_CMD);
527 strbuf_release(&buf);
529 for (j = common; j; j = j->next)
530 free((void *)args[i++]);
532 for (j = remoteheads; j; j = j->next)
533 free((void *)args[i++]);
538 static void count_diff_files(struct diff_queue_struct *q,
539 struct diff_options *opt, void *data)
546 static int count_unmerged_entries(void)
548 const struct index_state *state = &the_index;
551 for (i = 0; i < state->cache_nr; i++)
552 if (ce_stage(state->cache[i]))
558 static int checkout_fast_forward(unsigned char *head, unsigned char *remote)
560 struct tree *trees[MAX_UNPACK_TREES];
561 struct unpack_trees_options opts;
562 struct tree_desc t[MAX_UNPACK_TREES];
563 int i, fd, nr_trees = 0;
564 struct dir_struct dir;
565 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
567 if (read_cache_unmerged())
568 die("you need to resolve your current index first");
570 fd = hold_locked_index(lock_file, 1);
572 memset(&trees, 0, sizeof(trees));
573 memset(&opts, 0, sizeof(opts));
574 memset(&t, 0, sizeof(t));
575 memset(&dir, 0, sizeof(dir));
576 dir.show_ignored = 1;
577 dir.exclude_per_dir = ".gitignore";
581 opts.src_index = &the_index;
582 opts.dst_index = &the_index;
584 opts.verbose_update = 1;
586 opts.fn = twoway_merge;
588 trees[nr_trees] = parse_tree_indirect(head);
589 if (!trees[nr_trees++])
591 trees[nr_trees] = parse_tree_indirect(remote);
592 if (!trees[nr_trees++])
594 for (i = 0; i < nr_trees; i++) {
595 parse_tree(trees[i]);
596 init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
598 if (unpack_trees(nr_trees, t, &opts))
600 if (write_cache(fd, active_cache, active_nr) ||
601 commit_locked_index(lock_file))
602 die("unable to write new index file");
606 static void split_merge_strategies(const char *string, struct strategy **list,
614 buf = xstrdup(string);
619 ALLOC_GROW(*list, *nr + 1, *alloc);
620 (*list)[(*nr)++].name = xstrdup(q);
625 ALLOC_GROW(*list, *nr + 1, *alloc);
626 (*list)[(*nr)++].name = xstrdup(q);
632 static void add_strategies(const char *string, unsigned attr)
634 struct strategy *list = NULL;
635 int list_alloc = 0, list_nr = 0, i;
637 memset(&list, 0, sizeof(list));
638 split_merge_strategies(string, &list, &list_nr, &list_alloc);
640 for (i = 0; i < list_nr; i++)
641 append_strategy(get_strategy(list[i].name));
644 for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
645 if (all_strategy[i].attr & attr)
646 append_strategy(&all_strategy[i]);
650 static int merge_trivial(void)
652 unsigned char result_tree[20], result_commit[20];
653 struct commit_list parent;
655 write_tree_trivial(result_tree);
656 printf("Wonderful.\n");
657 parent.item = remoteheads->item;
659 commit_tree(merge_msg.buf, result_tree, &parent, result_commit);
660 finish(result_commit, "In-index merge");
665 static int finish_automerge(struct commit_list *common,
666 unsigned char *result_tree,
667 const char *wt_strategy)
669 struct commit_list *parents = NULL, *j;
670 struct strbuf buf = STRBUF_INIT;
671 unsigned char result_commit[20];
673 free_commit_list(common);
674 if (allow_fast_forward) {
675 parents = remoteheads;
676 commit_list_insert(lookup_commit(head), &parents);
677 parents = reduce_heads(parents);
679 struct commit_list **pptr = &parents;
681 pptr = &commit_list_insert(lookup_commit(head),
683 for (j = remoteheads; j; j = j->next)
684 pptr = &commit_list_insert(j->item, pptr)->next;
686 free_commit_list(remoteheads);
687 strbuf_addch(&merge_msg, '\n');
688 commit_tree(merge_msg.buf, result_tree, parents, result_commit);
689 strbuf_addf(&buf, "Merge made by %s.", wt_strategy);
690 finish(result_commit, buf.buf);
691 strbuf_release(&buf);
696 static int suggest_conflicts(void)
701 fp = fopen(git_path("MERGE_MSG"), "a");
703 die("Could open %s for writing", git_path("MERGE_MSG"));
704 fprintf(fp, "\nConflicts:\n");
705 for (pos = 0; pos < active_nr; pos++) {
706 struct cache_entry *ce = active_cache[pos];
709 fprintf(fp, "\t%s\n", ce->name);
710 while (pos + 1 < active_nr &&
712 active_cache[pos + 1]->name))
718 printf("Automatic merge failed; "
719 "fix conflicts and then commit the result.\n");
723 static struct commit *is_old_style_invocation(int argc, const char **argv)
725 struct commit *second_token = NULL;
727 unsigned char second_sha1[20];
729 if (get_sha1(argv[1], second_sha1))
731 second_token = lookup_commit_reference_gently(second_sha1, 0);
733 die("'%s' is not a commit", argv[1]);
734 if (hashcmp(second_token->object.sha1, head))
740 static int evaluate_result(void)
745 if (read_cache() < 0)
746 die("failed to read the cache");
748 /* Check how many files differ. */
749 init_revisions(&rev, "");
750 setup_revisions(0, NULL, &rev, NULL);
751 rev.diffopt.output_format |=
752 DIFF_FORMAT_CALLBACK;
753 rev.diffopt.format_callback = count_diff_files;
754 rev.diffopt.format_callback_data = &cnt;
755 run_diff_files(&rev, 0);
758 * Check how many unmerged entries are
761 cnt += count_unmerged_entries();
766 int cmd_merge(int argc, const char **argv, const char *prefix)
768 unsigned char result_tree[20];
770 const char *head_arg;
771 int flag, head_invalid = 0, i;
772 int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0;
773 struct commit_list *common = NULL;
774 const char *best_strategy = NULL, *wt_strategy = NULL;
775 struct commit_list **remotes = &remoteheads;
778 if (unmerged_cache())
779 die("You are in the middle of a conflicted merge.");
782 * Check if we are _not_ on a detached HEAD, i.e. if there is a
785 branch = resolve_ref("HEAD", head, 0, &flag);
786 if (branch && !prefixcmp(branch, "refs/heads/"))
788 if (is_null_sha1(head))
791 git_config(git_merge_config, NULL);
794 if (diff_use_color_default == -1)
795 diff_use_color_default = git_use_color_default;
797 argc = parse_options(argc, argv, builtin_merge_options,
798 builtin_merge_usage, 0);
801 if (!allow_fast_forward)
802 die("You cannot combine --squash with --no-ff.");
807 usage_with_options(builtin_merge_usage,
808 builtin_merge_options);
811 * This could be traditional "merge <msg> HEAD <commit>..." and
812 * the way we can tell it is to see if the second token is HEAD,
813 * but some people might have misused the interface and used a
814 * committish that is the same as HEAD there instead.
815 * Traditional format never would have "-m" so it is an
816 * additional safety measure to check for it.
818 strbuf_init(&buf, 0);
820 if (!have_message && is_old_style_invocation(argc, argv)) {
821 strbuf_addstr(&merge_msg, argv[0]);
825 } else if (head_invalid) {
826 struct object *remote_head;
828 * If the merged head is a valid one there is no reason
829 * to forbid "git merge" into a branch yet to be born.
830 * We do the same for "git pull".
833 die("Can merge only exactly one commit into "
835 remote_head = peel_to_type(argv[0], 0, NULL, OBJ_COMMIT);
837 die("%s - not something we can merge", argv[0]);
838 update_ref("initial pull", "HEAD", remote_head->sha1, NULL, 0,
840 reset_hard(remote_head->sha1, 0);
845 /* We are invoked directly as the first-class UI. */
849 * All the rest are the commits being merged;
850 * prepare the standard merge summary message to
851 * be appended to the given message. If remote
852 * is invalid we will die later in the common
853 * codepath so we discard the error in this
856 strbuf_init(&msg, 0);
857 for (i = 0; i < argc; i++)
858 merge_name(argv[i], &msg);
859 fmt_merge_msg(option_log, &msg, &merge_msg);
861 strbuf_setlen(&merge_msg, merge_msg.len-1);
864 if (head_invalid || !argc)
865 usage_with_options(builtin_merge_usage,
866 builtin_merge_options);
868 strbuf_addstr(&buf, "merge");
869 for (i = 0; i < argc; i++)
870 strbuf_addf(&buf, " %s", argv[i]);
871 setenv("GIT_REFLOG_ACTION", buf.buf, 0);
874 for (i = 0; i < argc; i++) {
877 o = peel_to_type(argv[i], 0, NULL, OBJ_COMMIT);
879 die("%s - not something we can merge", argv[i]);
880 remotes = &commit_list_insert(lookup_commit(o->sha1),
883 strbuf_addf(&buf, "GITHEAD_%s", sha1_to_hex(o->sha1));
884 setenv(buf.buf, argv[i], 1);
888 if (!use_strategies) {
889 if (!remoteheads->next)
890 add_strategies(pull_twohead, DEFAULT_TWOHEAD);
892 add_strategies(pull_octopus, DEFAULT_OCTOPUS);
895 for (i = 0; i < use_strategies_nr; i++) {
896 if (use_strategies[i]->attr & NO_FAST_FORWARD)
897 allow_fast_forward = 0;
898 if (use_strategies[i]->attr & NO_TRIVIAL)
902 if (!remoteheads->next)
903 common = get_merge_bases(lookup_commit(head),
904 remoteheads->item, 1);
906 struct commit_list *list = remoteheads;
907 commit_list_insert(lookup_commit(head), &list);
908 common = get_octopus_merge_bases(list);
912 update_ref("updating ORIG_HEAD", "ORIG_HEAD", head, NULL, 0,
916 ; /* No common ancestors found. We need a real merge. */
917 else if (!remoteheads->next && !common->next &&
918 common->item == remoteheads->item) {
920 * If head can reach all the merge then we are up to date.
921 * but first the most common case of merging one remote.
923 finish_up_to_date("Already up-to-date.");
925 } else if (allow_fast_forward && !remoteheads->next &&
927 !hashcmp(common->item->object.sha1, head)) {
928 /* Again the most common case of merging one remote. */
933 strcpy(hex, find_unique_abbrev(head, DEFAULT_ABBREV));
935 printf("Updating %s..%s\n",
937 find_unique_abbrev(remoteheads->item->object.sha1,
939 refresh_cache(REFRESH_QUIET);
940 strbuf_init(&msg, 0);
941 strbuf_addstr(&msg, "Fast forward");
944 " (no commit created; -m option ignored)");
945 o = peel_to_type(sha1_to_hex(remoteheads->item->object.sha1),
946 0, NULL, OBJ_COMMIT);
950 if (checkout_fast_forward(head, remoteheads->item->object.sha1))
953 finish(o->sha1, msg.buf);
956 } else if (!remoteheads->next && common->next)
959 * We are not doing octopus and not fast forward. Need
962 else if (!remoteheads->next && !common->next && option_commit) {
964 * We are not doing octopus, not fast forward, and have
967 refresh_cache(REFRESH_QUIET);
969 /* See if it is really trivial. */
970 git_committer_info(IDENT_ERROR_ON_NO_NAME);
971 printf("Trying really trivial in-index merge...\n");
972 if (!read_tree_trivial(common->item->object.sha1,
973 head, remoteheads->item->object.sha1))
974 return merge_trivial();
979 * An octopus. If we can reach all the remote we are up
983 struct commit_list *j;
985 for (j = remoteheads; j; j = j->next) {
986 struct commit_list *common_one;
989 * Here we *have* to calculate the individual
990 * merge_bases again, otherwise "git merge HEAD^
991 * HEAD^^" would be missed.
993 common_one = get_merge_bases(lookup_commit(head),
995 if (hashcmp(common_one->item->object.sha1,
996 j->item->object.sha1)) {
1002 finish_up_to_date("Already up-to-date. Yeeah!");
1007 /* We are going to make a new commit. */
1008 git_committer_info(IDENT_ERROR_ON_NO_NAME);
1011 * At this point, we need a real merge. No matter what strategy
1012 * we use, it would operate on the index, possibly affecting the
1013 * working tree, and when resolved cleanly, have the desired
1014 * tree in the index -- this means that the index must be in
1015 * sync with the head commit. The strategies are responsible
1018 if (use_strategies_nr != 1) {
1020 * Stash away the local changes so that we can try more
1025 memcpy(stash, null_sha1, 20);
1028 for (i = 0; i < use_strategies_nr; i++) {
1031 printf("Rewinding the tree to pristine...\n");
1034 if (use_strategies_nr != 1)
1035 printf("Trying merge strategy %s...\n",
1036 use_strategies[i]->name);
1038 * Remember which strategy left the state in the working
1041 wt_strategy = use_strategies[i]->name;
1043 ret = try_merge_strategy(use_strategies[i]->name,
1045 if (!option_commit && !ret) {
1048 * This is necessary here just to avoid writing
1049 * the tree, but later we will *not* exit with
1050 * status code 1 because merge_was_ok is set.
1057 * The backend exits with 1 when conflicts are
1058 * left to be resolved, with 2 when it does not
1059 * handle the given merge at all.
1062 int cnt = evaluate_result();
1064 if (best_cnt <= 0 || cnt <= best_cnt) {
1065 best_strategy = use_strategies[i]->name;
1075 /* Automerge succeeded. */
1076 write_tree_trivial(result_tree);
1077 automerge_was_ok = 1;
1082 * If we have a resulting tree, that means the strategy module
1083 * auto resolved the merge cleanly.
1085 if (automerge_was_ok)
1086 return finish_automerge(common, result_tree, wt_strategy);
1089 * Pick the result from the best strategy and have the user fix
1092 if (!best_strategy) {
1094 if (use_strategies_nr > 1)
1096 "No merge strategy handled the merge.\n");
1098 fprintf(stderr, "Merge with strategy %s failed.\n",
1099 use_strategies[0]->name);
1101 } else if (best_strategy == wt_strategy)
1102 ; /* We already have its result in the working tree. */
1104 printf("Rewinding the tree to pristine...\n");
1106 printf("Using the %s to prepare resolving by hand.\n",
1108 try_merge_strategy(best_strategy, common, head_arg);
1115 struct commit_list *j;
1117 for (j = remoteheads; j; j = j->next)
1118 strbuf_addf(&buf, "%s\n",
1119 sha1_to_hex(j->item->object.sha1));
1120 fd = open(git_path("MERGE_HEAD"), O_WRONLY | O_CREAT, 0666);
1122 die("Could open %s for writing",
1123 git_path("MERGE_HEAD"));
1124 if (write_in_full(fd, buf.buf, buf.len) != buf.len)
1125 die("Could not write to %s", git_path("MERGE_HEAD"));
1127 strbuf_addch(&merge_msg, '\n');
1128 fd = open(git_path("MERGE_MSG"), O_WRONLY | O_CREAT, 0666);
1130 die("Could open %s for writing", git_path("MERGE_MSG"));
1131 if (write_in_full(fd, merge_msg.buf, merge_msg.len) !=
1133 die("Could not write to %s", git_path("MERGE_MSG"));
1138 fprintf(stderr, "Automatic merge went well; "
1139 "stopped before committing as requested\n");
1142 return suggest_conflicts();