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"
25 #define DEFAULT_TWOHEAD (1<<0)
26 #define DEFAULT_OCTOPUS (1<<1)
27 #define NO_FAST_FORWARD (1<<2)
28 #define NO_TRIVIAL (1<<3)
35 static const char * const builtin_merge_usage
[] = {
36 "git-merge [options] <remote>...",
37 "git-merge [options] <msg> HEAD <remote>",
41 static int show_diffstat
= 1, option_log
, squash
;
42 static int option_commit
= 1, allow_fast_forward
= 1;
43 static int allow_trivial
= 1, have_message
;
44 static struct strbuf merge_msg
;
45 static struct commit_list
*remoteheads
;
46 static unsigned char head
[20], stash
[20];
47 static struct strategy
**use_strategies
;
48 static size_t use_strategies_nr
, use_strategies_alloc
;
49 static const char *branch
;
51 static struct strategy all_strategy
[] = {
52 { "recur", NO_TRIVIAL
},
53 { "recursive", DEFAULT_TWOHEAD
| NO_TRIVIAL
},
54 { "octopus", DEFAULT_OCTOPUS
},
57 { "ours", NO_FAST_FORWARD
| NO_TRIVIAL
},
58 { "subtree", NO_FAST_FORWARD
| NO_TRIVIAL
},
61 static const char *pull_twohead
, *pull_octopus
;
63 static int option_parse_message(const struct option
*opt
,
64 const char *arg
, int unset
)
66 struct strbuf
*buf
= opt
->value
;
69 strbuf_setlen(buf
, 0);
71 strbuf_addf(buf
, "%s\n\n", arg
);
77 static struct strategy
*get_strategy(const char *name
)
84 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
85 if (!strcmp(name
, all_strategy
[i
].name
))
86 return &all_strategy
[i
];
90 static void append_strategy(struct strategy
*s
)
92 ALLOC_GROW(use_strategies
, use_strategies_nr
+ 1, use_strategies_alloc
);
93 use_strategies
[use_strategies_nr
++] = s
;
96 static int option_parse_strategy(const struct option
*opt
,
97 const char *name
, int unset
)
105 s
= get_strategy(name
);
111 strbuf_init(&err
, 0);
112 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
113 strbuf_addf(&err
, " %s", all_strategy
[i
].name
);
114 fprintf(stderr
, "Could not find merge strategy '%s'.\n", name
);
115 fprintf(stderr
, "Available strategies are:%s.\n", err
.buf
);
121 static int option_parse_n(const struct option
*opt
,
122 const char *arg
, int unset
)
124 show_diffstat
= unset
;
128 static struct option builtin_merge_options
[] = {
129 { OPTION_CALLBACK
, 'n', NULL
, NULL
, NULL
,
130 "do not show a diffstat at the end of the merge",
131 PARSE_OPT_NOARG
, option_parse_n
},
132 OPT_BOOLEAN(0, "stat", &show_diffstat
,
133 "show a diffstat at the end of the merge"),
134 OPT_BOOLEAN(0, "summary", &show_diffstat
, "(synonym to --stat)"),
135 OPT_BOOLEAN(0, "log", &option_log
,
136 "add list of one-line log to merge commit message"),
137 OPT_BOOLEAN(0, "squash", &squash
,
138 "create a single commit instead of doing a merge"),
139 OPT_BOOLEAN(0, "commit", &option_commit
,
140 "perform a commit if the merge succeeds (default)"),
141 OPT_BOOLEAN(0, "ff", &allow_fast_forward
,
142 "allow fast forward (default)"),
143 OPT_CALLBACK('s', "strategy", &use_strategies
, "strategy",
144 "merge strategy to use", option_parse_strategy
),
145 OPT_CALLBACK('m', "message", &merge_msg
, "message",
146 "message to be used for the merge commit (if any)",
147 option_parse_message
),
151 /* Cleans up metadata that is uninteresting after a succeeded merge. */
152 static void drop_save(void)
154 unlink(git_path("MERGE_HEAD"));
155 unlink(git_path("MERGE_MSG"));
158 static void save_state(void)
161 struct child_process cp
;
162 struct strbuf buffer
= STRBUF_INIT
;
163 const char *argv
[] = {"stash", "create", NULL
};
165 memset(&cp
, 0, sizeof(cp
));
170 if (start_command(&cp
))
171 die("could not run stash.");
172 len
= strbuf_read(&buffer
, cp
.out
, 1024);
175 if (finish_command(&cp
) || len
< 0)
179 strbuf_setlen(&buffer
, buffer
.len
-1);
180 if (get_sha1(buffer
.buf
, stash
))
181 die("not a valid object: %s", buffer
.buf
);
184 static void reset_hard(unsigned const char *sha1
, int verbose
)
189 args
[i
++] = "read-tree";
192 args
[i
++] = "--reset";
194 args
[i
++] = sha1_to_hex(sha1
);
197 if (run_command_v_opt(args
, RUN_GIT_CMD
))
198 die("read-tree failed");
201 static void restore_state(void)
204 const char *args
[] = { "stash", "apply", NULL
, NULL
};
206 if (is_null_sha1(stash
))
212 args
[2] = sha1_to_hex(stash
);
215 * It is OK to ignore error here, for example when there was
216 * nothing to restore.
218 run_command_v_opt(args
, RUN_GIT_CMD
);
221 refresh_cache(REFRESH_QUIET
);
224 /* This is called when no merge was necessary. */
225 static void finish_up_to_date(const char *msg
)
227 printf("%s%s\n", squash
? " (nothing to squash)" : "", msg
);
231 static void squash_message(void)
234 struct commit
*commit
;
236 struct commit_list
*j
;
239 printf("Squash commit -- not updating HEAD\n");
240 fd
= open(git_path("SQUASH_MSG"), O_WRONLY
| O_CREAT
, 0666);
242 die("Could not write to %s", git_path("SQUASH_MSG"));
244 init_revisions(&rev
, NULL
);
245 rev
.ignore_merges
= 1;
246 rev
.commit_format
= CMIT_FMT_MEDIUM
;
248 commit
= lookup_commit(head
);
249 commit
->object
.flags
|= UNINTERESTING
;
250 add_pending_object(&rev
, &commit
->object
, NULL
);
252 for (j
= remoteheads
; j
; j
= j
->next
)
253 add_pending_object(&rev
, &j
->item
->object
, NULL
);
255 setup_revisions(0, NULL
, &rev
, NULL
);
256 if (prepare_revision_walk(&rev
))
257 die("revision walk setup failed");
259 strbuf_init(&out
, 0);
260 strbuf_addstr(&out
, "Squashed commit of the following:\n");
261 while ((commit
= get_revision(&rev
)) != NULL
) {
262 strbuf_addch(&out
, '\n');
263 strbuf_addf(&out
, "commit %s\n",
264 sha1_to_hex(commit
->object
.sha1
));
265 pretty_print_commit(rev
.commit_format
, commit
, &out
, rev
.abbrev
,
266 NULL
, NULL
, rev
.date_mode
, 0);
268 write(fd
, out
.buf
, out
.len
);
270 strbuf_release(&out
);
273 static int run_hook(const char *name
)
275 struct child_process hook
;
276 const char *argv
[3], *env
[2];
277 char index
[PATH_MAX
];
279 argv
[0] = git_path("hooks/%s", name
);
280 if (access(argv
[0], X_OK
) < 0)
283 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", get_index_file());
293 memset(&hook
, 0, sizeof(hook
));
296 hook
.stdout_to_stderr
= 1;
299 return run_command(&hook
);
302 static void finish(const unsigned char *new_head
, const char *msg
)
304 struct strbuf reflog_message
;
306 strbuf_init(&reflog_message
, 0);
308 strbuf_addstr(&reflog_message
, getenv("GIT_REFLOG_ACTION"));
311 strbuf_addf(&reflog_message
, "%s: %s",
312 getenv("GIT_REFLOG_ACTION"), msg
);
318 printf("No merge message -- not updating HEAD\n");
320 const char *argv_gc_auto
[] = { "gc", "--auto", NULL
};
321 update_ref(reflog_message
.buf
, "HEAD",
325 * We ignore errors in 'gc --auto', since the
326 * user should see them.
328 run_command_v_opt(argv_gc_auto
, RUN_GIT_CMD
);
331 if (new_head
&& show_diffstat
) {
332 struct diff_options opts
;
334 opts
.output_format
|=
335 DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
336 opts
.detect_rename
= DIFF_DETECT_RENAME
;
337 if (diff_use_color_default
> 0)
338 DIFF_OPT_SET(&opts
, COLOR_DIFF
);
339 if (diff_setup_done(&opts
) < 0)
340 die("diff_setup_done failed");
341 diff_tree_sha1(head
, new_head
, "", &opts
);
346 /* Run a post-merge hook */
347 run_hook("post-merge");
349 strbuf_release(&reflog_message
);
352 /* Get the name for the merge commit's message. */
353 static void merge_name(const char *remote
, struct strbuf
*msg
)
355 struct object
*remote_head
;
356 unsigned char branch_head
[20], buf_sha
[20];
361 memset(branch_head
, 0, sizeof(branch_head
));
362 remote_head
= peel_to_type(remote
, 0, NULL
, OBJ_COMMIT
);
364 die("'%s' does not point to a commit", remote
);
366 strbuf_init(&buf
, 0);
367 strbuf_addstr(&buf
, "refs/heads/");
368 strbuf_addstr(&buf
, remote
);
369 resolve_ref(buf
.buf
, branch_head
, 0, 0);
371 if (!hashcmp(remote_head
->sha1
, branch_head
)) {
372 strbuf_addf(msg
, "%s\t\tbranch '%s' of .\n",
373 sha1_to_hex(branch_head
), remote
);
377 /* See if remote matches <name>^^^.. or <name>~<number> */
378 for (len
= 0, ptr
= remote
+ strlen(remote
);
379 remote
< ptr
&& ptr
[-1] == '^';
386 ptr
= strrchr(remote
, '~');
388 int seen_nonzero
= 0;
391 while (*++ptr
&& isdigit(*ptr
)) {
392 seen_nonzero
|= (*ptr
!= '0');
396 len
= 0; /* not ...~<number> */
397 else if (seen_nonzero
)
400 early
= 1; /* "name~" is "name~1"! */
404 struct strbuf truname
= STRBUF_INIT
;
405 strbuf_addstr(&truname
, "refs/heads/");
406 strbuf_addstr(&truname
, remote
);
407 strbuf_setlen(&truname
, len
+11);
408 if (resolve_ref(truname
.buf
, buf_sha
, 0, 0)) {
410 "%s\t\tbranch '%s'%s of .\n",
411 sha1_to_hex(remote_head
->sha1
),
413 (early
? " (early part)" : ""));
418 if (!strcmp(remote
, "FETCH_HEAD") &&
419 !access(git_path("FETCH_HEAD"), R_OK
)) {
424 strbuf_init(&line
, 0);
425 fp
= fopen(git_path("FETCH_HEAD"), "r");
427 die("could not open %s for reading: %s",
428 git_path("FETCH_HEAD"), strerror(errno
));
429 strbuf_getline(&line
, fp
, '\n');
431 ptr
= strstr(line
.buf
, "\tnot-for-merge\t");
433 strbuf_remove(&line
, ptr
-line
.buf
+1, 13);
434 strbuf_addbuf(msg
, &line
);
435 strbuf_release(&line
);
438 strbuf_addf(msg
, "%s\t\tcommit '%s'\n",
439 sha1_to_hex(remote_head
->sha1
), remote
);
442 int git_merge_config(const char *k
, const char *v
, void *cb
)
444 if (branch
&& !prefixcmp(k
, "branch.") &&
445 !prefixcmp(k
+ 7, branch
) &&
446 !strcmp(k
+ 7 + strlen(branch
), ".mergeoptions")) {
452 argc
= split_cmdline(buf
, &argv
);
453 argv
= xrealloc(argv
, sizeof(*argv
) * (argc
+ 2));
454 memmove(argv
+ 1, argv
, sizeof(*argv
) * (argc
+ 1));
456 parse_options(argc
, argv
, builtin_merge_options
,
457 builtin_merge_usage
, 0);
461 if (!strcmp(k
, "merge.diffstat") || !strcmp(k
, "merge.stat"))
462 show_diffstat
= git_config_bool(k
, v
);
463 else if (!strcmp(k
, "pull.twohead"))
464 return git_config_string(&pull_twohead
, k
, v
);
465 else if (!strcmp(k
, "pull.octopus"))
466 return git_config_string(&pull_octopus
, k
, v
);
467 return git_diff_ui_config(k
, v
, cb
);
470 static int read_tree_trivial(unsigned char *common
, unsigned char *head
,
474 struct tree
*trees
[MAX_UNPACK_TREES
];
475 struct tree_desc t
[MAX_UNPACK_TREES
];
476 struct unpack_trees_options opts
;
478 memset(&opts
, 0, sizeof(opts
));
480 opts
.src_index
= &the_index
;
481 opts
.dst_index
= &the_index
;
483 opts
.verbose_update
= 1;
484 opts
.trivial_merges_only
= 1;
486 trees
[nr_trees
] = parse_tree_indirect(common
);
487 if (!trees
[nr_trees
++])
489 trees
[nr_trees
] = parse_tree_indirect(head
);
490 if (!trees
[nr_trees
++])
492 trees
[nr_trees
] = parse_tree_indirect(one
);
493 if (!trees
[nr_trees
++])
495 opts
.fn
= threeway_merge
;
496 cache_tree_free(&active_cache_tree
);
497 for (i
= 0; i
< nr_trees
; i
++) {
498 parse_tree(trees
[i
]);
499 init_tree_desc(t
+i
, trees
[i
]->buffer
, trees
[i
]->size
);
501 if (unpack_trees(nr_trees
, t
, &opts
))
506 static void write_tree_trivial(unsigned char *sha1
)
508 if (write_cache_as_tree(sha1
, 0, NULL
))
509 die("git write-tree failed to write a tree");
512 static int try_merge_strategy(const char *strategy
, struct commit_list
*common
,
513 const char *head_arg
)
517 struct commit_list
*j
;
520 args
= xmalloc((4 + commit_list_count(common
) +
521 commit_list_count(remoteheads
)) * sizeof(char *));
522 strbuf_init(&buf
, 0);
523 strbuf_addf(&buf
, "merge-%s", strategy
);
525 for (j
= common
; j
; j
= j
->next
)
526 args
[i
++] = xstrdup(sha1_to_hex(j
->item
->object
.sha1
));
528 args
[i
++] = head_arg
;
529 for (j
= remoteheads
; j
; j
= j
->next
)
530 args
[i
++] = xstrdup(sha1_to_hex(j
->item
->object
.sha1
));
532 ret
= run_command_v_opt(args
, RUN_GIT_CMD
);
533 strbuf_release(&buf
);
535 for (j
= common
; j
; j
= j
->next
)
536 free((void *)args
[i
++]);
538 for (j
= remoteheads
; j
; j
= j
->next
)
539 free((void *)args
[i
++]);
544 static void count_diff_files(struct diff_queue_struct
*q
,
545 struct diff_options
*opt
, void *data
)
552 static int count_unmerged_entries(void)
554 const struct index_state
*state
= &the_index
;
557 for (i
= 0; i
< state
->cache_nr
; i
++)
558 if (ce_stage(state
->cache
[i
]))
564 static int checkout_fast_forward(unsigned char *head
, unsigned char *remote
)
566 struct tree
*trees
[MAX_UNPACK_TREES
];
567 struct unpack_trees_options opts
;
568 struct tree_desc t
[MAX_UNPACK_TREES
];
569 int i
, fd
, nr_trees
= 0;
570 struct dir_struct dir
;
571 struct lock_file
*lock_file
= xcalloc(1, sizeof(struct lock_file
));
573 if (read_cache_unmerged())
574 die("you need to resolve your current index first");
576 fd
= hold_locked_index(lock_file
, 1);
578 memset(&trees
, 0, sizeof(trees
));
579 memset(&opts
, 0, sizeof(opts
));
580 memset(&t
, 0, sizeof(t
));
581 dir
.show_ignored
= 1;
582 dir
.exclude_per_dir
= ".gitignore";
586 opts
.src_index
= &the_index
;
587 opts
.dst_index
= &the_index
;
589 opts
.verbose_update
= 1;
591 opts
.fn
= twoway_merge
;
593 trees
[nr_trees
] = parse_tree_indirect(head
);
594 if (!trees
[nr_trees
++])
596 trees
[nr_trees
] = parse_tree_indirect(remote
);
597 if (!trees
[nr_trees
++])
599 for (i
= 0; i
< nr_trees
; i
++) {
600 parse_tree(trees
[i
]);
601 init_tree_desc(t
+i
, trees
[i
]->buffer
, trees
[i
]->size
);
603 if (unpack_trees(nr_trees
, t
, &opts
))
605 if (write_cache(fd
, active_cache
, active_nr
) ||
606 commit_locked_index(lock_file
))
607 die("unable to write new index file");
611 static void split_merge_strategies(const char *string
, struct strategy
**list
,
619 buf
= xstrdup(string
);
624 ALLOC_GROW(*list
, *nr
+ 1, *alloc
);
625 (*list
)[(*nr
)++].name
= xstrdup(q
);
630 ALLOC_GROW(*list
, *nr
+ 1, *alloc
);
631 (*list
)[(*nr
)++].name
= xstrdup(q
);
637 static void add_strategies(const char *string
, unsigned attr
)
639 struct strategy
*list
= NULL
;
640 int list_alloc
= 0, list_nr
= 0, i
;
642 memset(&list
, 0, sizeof(list
));
643 split_merge_strategies(string
, &list
, &list_nr
, &list_alloc
);
645 for (i
= 0; i
< list_nr
; i
++) {
648 s
= get_strategy(list
[i
].name
);
654 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
655 if (all_strategy
[i
].attr
& attr
)
656 append_strategy(&all_strategy
[i
]);
660 static int merge_trivial(void)
662 unsigned char result_tree
[20], result_commit
[20];
663 struct commit_list parent
;
665 write_tree_trivial(result_tree
);
666 printf("Wonderful.\n");
667 parent
.item
= remoteheads
->item
;
669 commit_tree(merge_msg
.buf
, result_tree
, &parent
, result_commit
);
670 finish(result_commit
, "In-index merge");
675 static int finish_automerge(struct commit_list
*common
,
676 unsigned char *result_tree
,
677 const char *wt_strategy
)
679 struct commit_list
*parents
= NULL
, *j
;
680 struct strbuf buf
= STRBUF_INIT
;
681 unsigned char result_commit
[20];
683 free_commit_list(common
);
684 if (allow_fast_forward
) {
685 parents
= remoteheads
;
686 commit_list_insert(lookup_commit(head
), &parents
);
687 parents
= reduce_heads(parents
);
689 struct commit_list
**pptr
= &parents
;
691 pptr
= &commit_list_insert(lookup_commit(head
),
693 for (j
= remoteheads
; j
; j
= j
->next
)
694 pptr
= &commit_list_insert(j
->item
, pptr
)->next
;
696 free_commit_list(remoteheads
);
697 strbuf_addch(&merge_msg
, '\n');
698 commit_tree(merge_msg
.buf
, result_tree
, parents
, result_commit
);
699 strbuf_addf(&buf
, "Merge made by %s.", wt_strategy
);
700 finish(result_commit
, buf
.buf
);
701 strbuf_release(&buf
);
706 static int suggest_conflicts(void)
711 fp
= fopen(git_path("MERGE_MSG"), "a");
713 die("Could open %s for writing", git_path("MERGE_MSG"));
714 fprintf(fp
, "\nConflicts:\n");
715 for (pos
= 0; pos
< active_nr
; pos
++) {
716 struct cache_entry
*ce
= active_cache
[pos
];
719 fprintf(fp
, "\t%s\n", ce
->name
);
720 while (pos
+ 1 < active_nr
&&
722 active_cache
[pos
+ 1]->name
))
728 printf("Automatic merge failed; "
729 "fix conflicts and then commit the result.\n");
733 static struct commit
*is_old_style_invocation(int argc
, const char **argv
)
735 struct commit
*second_token
= NULL
;
737 unsigned char second_sha1
[20];
739 if (get_sha1(argv
[1], second_sha1
))
741 second_token
= lookup_commit_reference_gently(second_sha1
, 0);
743 die("'%s' is not a commit", argv
[1]);
744 if (hashcmp(second_token
->object
.sha1
, head
))
750 static int evaluate_result(void)
755 if (read_cache() < 0)
756 die("failed to read the cache");
758 /* Check how many files differ. */
759 init_revisions(&rev
, "");
760 setup_revisions(0, NULL
, &rev
, NULL
);
761 rev
.diffopt
.output_format
|=
762 DIFF_FORMAT_CALLBACK
;
763 rev
.diffopt
.format_callback
= count_diff_files
;
764 rev
.diffopt
.format_callback_data
= &cnt
;
765 run_diff_files(&rev
, 0);
768 * Check how many unmerged entries are
771 cnt
+= count_unmerged_entries();
776 int cmd_merge(int argc
, const char **argv
, const char *prefix
)
778 unsigned char result_tree
[20];
780 const char *head_arg
;
781 int flag
, head_invalid
= 0, i
;
782 int best_cnt
= -1, merge_was_ok
= 0, automerge_was_ok
= 0;
783 struct commit_list
*common
= NULL
;
784 const char *best_strategy
= NULL
, *wt_strategy
= NULL
;
785 struct commit_list
**remotes
= &remoteheads
;
788 if (unmerged_cache())
789 die("You are in the middle of a conflicted merge.");
792 * Check if we are _not_ on a detached HEAD, i.e. if there is a
795 branch
= resolve_ref("HEAD", head
, 0, &flag
);
796 if (branch
&& !prefixcmp(branch
, "refs/heads/"))
798 if (is_null_sha1(head
))
801 git_config(git_merge_config
, NULL
);
804 if (diff_use_color_default
== -1)
805 diff_use_color_default
= git_use_color_default
;
807 argc
= parse_options(argc
, argv
, builtin_merge_options
,
808 builtin_merge_usage
, 0);
811 if (!allow_fast_forward
)
812 die("You cannot combine --squash with --no-ff.");
817 usage_with_options(builtin_merge_usage
,
818 builtin_merge_options
);
821 * This could be traditional "merge <msg> HEAD <commit>..." and
822 * the way we can tell it is to see if the second token is HEAD,
823 * but some people might have misused the interface and used a
824 * committish that is the same as HEAD there instead.
825 * Traditional format never would have "-m" so it is an
826 * additional safety measure to check for it.
828 strbuf_init(&buf
, 0);
830 if (!have_message
&& is_old_style_invocation(argc
, argv
)) {
831 strbuf_addstr(&merge_msg
, argv
[0]);
835 } else if (head_invalid
) {
836 struct object
*remote_head
;
838 * If the merged head is a valid one there is no reason
839 * to forbid "git merge" into a branch yet to be born.
840 * We do the same for "git pull".
843 die("Can merge only exactly one commit into "
845 remote_head
= peel_to_type(argv
[0], 0, NULL
, OBJ_COMMIT
);
847 die("%s - not something we can merge", argv
[0]);
848 update_ref("initial pull", "HEAD", remote_head
->sha1
, NULL
, 0,
850 reset_hard(remote_head
->sha1
, 0);
855 /* We are invoked directly as the first-class UI. */
859 * All the rest are the commits being merged;
860 * prepare the standard merge summary message to
861 * be appended to the given message. If remote
862 * is invalid we will die later in the common
863 * codepath so we discard the error in this
866 strbuf_init(&msg
, 0);
867 for (i
= 0; i
< argc
; i
++)
868 merge_name(argv
[i
], &msg
);
869 fmt_merge_msg(option_log
, &msg
, &merge_msg
);
871 strbuf_setlen(&merge_msg
, merge_msg
.len
-1);
874 if (head_invalid
|| !argc
)
875 usage_with_options(builtin_merge_usage
,
876 builtin_merge_options
);
878 strbuf_addstr(&buf
, "merge");
879 for (i
= 0; i
< argc
; i
++)
880 strbuf_addf(&buf
, " %s", argv
[i
]);
881 setenv("GIT_REFLOG_ACTION", buf
.buf
, 0);
884 for (i
= 0; i
< argc
; i
++) {
887 o
= peel_to_type(argv
[i
], 0, NULL
, OBJ_COMMIT
);
889 die("%s - not something we can merge", argv
[i
]);
890 remotes
= &commit_list_insert(lookup_commit(o
->sha1
),
893 strbuf_addf(&buf
, "GITHEAD_%s", sha1_to_hex(o
->sha1
));
894 setenv(buf
.buf
, argv
[i
], 1);
898 if (!use_strategies
) {
899 if (!remoteheads
->next
)
900 add_strategies(pull_twohead
, DEFAULT_TWOHEAD
);
902 add_strategies(pull_octopus
, DEFAULT_OCTOPUS
);
905 for (i
= 0; i
< use_strategies_nr
; i
++) {
906 if (use_strategies
[i
]->attr
& NO_FAST_FORWARD
)
907 allow_fast_forward
= 0;
908 if (use_strategies
[i
]->attr
& NO_TRIVIAL
)
912 if (!remoteheads
->next
)
913 common
= get_merge_bases(lookup_commit(head
),
914 remoteheads
->item
, 1);
916 struct commit_list
*list
= remoteheads
;
917 commit_list_insert(lookup_commit(head
), &list
);
918 common
= get_octopus_merge_bases(list
);
922 update_ref("updating ORIG_HEAD", "ORIG_HEAD", head
, NULL
, 0,
926 ; /* No common ancestors found. We need a real merge. */
927 else if (!remoteheads
->next
&& !common
->next
&&
928 common
->item
== remoteheads
->item
) {
930 * If head can reach all the merge then we are up to date.
931 * but first the most common case of merging one remote.
933 finish_up_to_date("Already up-to-date.");
935 } else if (allow_fast_forward
&& !remoteheads
->next
&&
937 !hashcmp(common
->item
->object
.sha1
, head
)) {
938 /* Again the most common case of merging one remote. */
943 strcpy(hex
, find_unique_abbrev(head
, DEFAULT_ABBREV
));
945 printf("Updating %s..%s\n",
947 find_unique_abbrev(remoteheads
->item
->object
.sha1
,
949 refresh_cache(REFRESH_QUIET
);
950 strbuf_init(&msg
, 0);
951 strbuf_addstr(&msg
, "Fast forward");
954 " (no commit created; -m option ignored)");
955 o
= peel_to_type(sha1_to_hex(remoteheads
->item
->object
.sha1
),
956 0, NULL
, OBJ_COMMIT
);
960 if (checkout_fast_forward(head
, remoteheads
->item
->object
.sha1
))
963 finish(o
->sha1
, msg
.buf
);
966 } else if (!remoteheads
->next
&& common
->next
)
969 * We are not doing octopus and not fast forward. Need
972 else if (!remoteheads
->next
&& !common
->next
&& option_commit
) {
974 * We are not doing octopus, not fast forward, and have
977 refresh_cache(REFRESH_QUIET
);
979 /* See if it is really trivial. */
980 git_committer_info(IDENT_ERROR_ON_NO_NAME
);
981 printf("Trying really trivial in-index merge...\n");
982 if (!read_tree_trivial(common
->item
->object
.sha1
,
983 head
, remoteheads
->item
->object
.sha1
))
984 return merge_trivial();
989 * An octopus. If we can reach all the remote we are up
993 struct commit_list
*j
;
995 for (j
= remoteheads
; j
; j
= j
->next
) {
996 struct commit_list
*common_one
;
999 * Here we *have* to calculate the individual
1000 * merge_bases again, otherwise "git merge HEAD^
1001 * HEAD^^" would be missed.
1003 common_one
= get_merge_bases(lookup_commit(head
),
1005 if (hashcmp(common_one
->item
->object
.sha1
,
1006 j
->item
->object
.sha1
)) {
1012 finish_up_to_date("Already up-to-date. Yeeah!");
1017 /* We are going to make a new commit. */
1018 git_committer_info(IDENT_ERROR_ON_NO_NAME
);
1021 * At this point, we need a real merge. No matter what strategy
1022 * we use, it would operate on the index, possibly affecting the
1023 * working tree, and when resolved cleanly, have the desired
1024 * tree in the index -- this means that the index must be in
1025 * sync with the head commit. The strategies are responsible
1028 if (use_strategies_nr
!= 1) {
1030 * Stash away the local changes so that we can try more
1035 memcpy(stash
, null_sha1
, 20);
1038 for (i
= 0; i
< use_strategies_nr
; i
++) {
1041 printf("Rewinding the tree to pristine...\n");
1044 if (use_strategies_nr
!= 1)
1045 printf("Trying merge strategy %s...\n",
1046 use_strategies
[i
]->name
);
1048 * Remember which strategy left the state in the working
1051 wt_strategy
= use_strategies
[i
]->name
;
1053 ret
= try_merge_strategy(use_strategies
[i
]->name
,
1055 if (!option_commit
&& !ret
) {
1058 * This is necessary here just to avoid writing
1059 * the tree, but later we will *not* exit with
1060 * status code 1 because merge_was_ok is set.
1067 * The backend exits with 1 when conflicts are
1068 * left to be resolved, with 2 when it does not
1069 * handle the given merge at all.
1072 int cnt
= evaluate_result();
1074 if (best_cnt
<= 0 || cnt
<= best_cnt
) {
1075 best_strategy
= use_strategies
[i
]->name
;
1085 /* Automerge succeeded. */
1086 write_tree_trivial(result_tree
);
1087 automerge_was_ok
= 1;
1092 * If we have a resulting tree, that means the strategy module
1093 * auto resolved the merge cleanly.
1095 if (automerge_was_ok
)
1096 return finish_automerge(common
, result_tree
, wt_strategy
);
1099 * Pick the result from the best strategy and have the user fix
1102 if (!best_strategy
) {
1104 if (use_strategies_nr
> 1)
1106 "No merge strategy handled the merge.\n");
1108 fprintf(stderr
, "Merge with strategy %s failed.\n",
1109 use_strategies
[0]->name
);
1111 } else if (best_strategy
== wt_strategy
)
1112 ; /* We already have its result in the working tree. */
1114 printf("Rewinding the tree to pristine...\n");
1116 printf("Using the %s to prepare resolving by hand.\n",
1118 try_merge_strategy(best_strategy
, common
, head_arg
);
1125 struct commit_list
*j
;
1127 for (j
= remoteheads
; j
; j
= j
->next
)
1128 strbuf_addf(&buf
, "%s\n",
1129 sha1_to_hex(j
->item
->object
.sha1
));
1130 fd
= open(git_path("MERGE_HEAD"), O_WRONLY
| O_CREAT
, 0666);
1132 die("Could open %s for writing",
1133 git_path("MERGE_HEAD"));
1134 if (write_in_full(fd
, buf
.buf
, buf
.len
) != buf
.len
)
1135 die("Could not write to %s", git_path("MERGE_HEAD"));
1137 strbuf_addch(&merge_msg
, '\n');
1138 fd
= open(git_path("MERGE_MSG"), O_WRONLY
| O_CREAT
, 0666);
1140 die("Could open %s for writing", git_path("MERGE_MSG"));
1141 if (write_in_full(fd
, merge_msg
.buf
, merge_msg
.len
) !=
1143 die("Could not write to %s", git_path("MERGE_MSG"));
1148 fprintf(stderr
, "Automatic merge went well; "
1149 "stopped before committing as requested\n");
1152 return suggest_conflicts();