4 * Copyright (c) 2008 Miklos Vajna <vmiklos@frugalware.org>
6 * Based on git-merge.sh by Junio C Hamano.
9 #define USE_THE_INDEX_COMPATIBILITY_MACROS
12 #include "parse-options.h"
15 #include "run-command.h"
22 #include "unpack-trees.h"
23 #include "cache-tree.h"
30 #include "merge-recursive.h"
31 #include "resolve-undo.h"
33 #include "fmt-merge-msg.h"
34 #include "gpg-interface.h"
35 #include "sequencer.h"
36 #include "string-list.h"
41 #include "commit-reach.h"
42 #include "wt-status.h"
44 #define DEFAULT_TWOHEAD (1<<0)
45 #define DEFAULT_OCTOPUS (1<<1)
46 #define NO_FAST_FORWARD (1<<2)
47 #define NO_TRIVIAL (1<<3)
54 static const char * const builtin_merge_usage
[] = {
55 N_("git merge [<options>] [<commit>...]"),
56 N_("git merge --abort"),
57 N_("git merge --continue"),
61 static int show_diffstat
= 1, shortlog_len
= -1, squash
;
62 static int option_commit
= -1;
63 static int option_edit
= -1;
64 static int allow_trivial
= 1, have_message
, verify_signatures
;
65 static int check_trust_level
= 1;
66 static int overwrite_ignore
= 1;
67 static struct strbuf merge_msg
= STRBUF_INIT
;
68 static struct strategy
**use_strategies
;
69 static size_t use_strategies_nr
, use_strategies_alloc
;
70 static const char **xopts
;
71 static size_t xopts_nr
, xopts_alloc
;
72 static const char *branch
;
73 static char *branch_mergeoptions
;
74 static int option_renormalize
;
76 static int allow_rerere_auto
;
77 static int abort_current_merge
;
78 static int quit_current_merge
;
79 static int continue_current_merge
;
80 static int allow_unrelated_histories
;
81 static int show_progress
= -1;
82 static int default_to_upstream
= 1;
84 static const char *sign_commit
;
87 static struct strategy all_strategy
[] = {
88 { "recursive", DEFAULT_TWOHEAD
| NO_TRIVIAL
},
89 { "octopus", DEFAULT_OCTOPUS
},
91 { "ours", NO_FAST_FORWARD
| NO_TRIVIAL
},
92 { "subtree", NO_FAST_FORWARD
| NO_TRIVIAL
},
95 static const char *pull_twohead
, *pull_octopus
;
103 static enum ff_type fast_forward
= FF_ALLOW
;
105 static const char *cleanup_arg
;
106 static enum commit_msg_cleanup_mode cleanup_mode
;
108 static int option_parse_message(const struct option
*opt
,
109 const char *arg
, int unset
)
111 struct strbuf
*buf
= opt
->value
;
114 strbuf_setlen(buf
, 0);
116 strbuf_addf(buf
, "%s%s", buf
->len
? "\n\n" : "", arg
);
119 return error(_("switch `m' requires a value"));
123 static enum parse_opt_result
option_read_message(struct parse_opt_ctx_t
*ctx
,
124 const struct option
*opt
,
125 const char *arg_not_used
,
128 struct strbuf
*buf
= opt
->value
;
131 BUG_ON_OPT_ARG(arg_not_used
);
133 BUG("-F cannot be negated");
138 } else if (ctx
->argc
> 1) {
142 return error(_("option `%s' requires a value"), opt
->long_name
);
145 strbuf_addch(buf
, '\n');
146 if (ctx
->prefix
&& !is_absolute_path(arg
))
147 arg
= prefix_filename(ctx
->prefix
, arg
);
148 if (strbuf_read_file(buf
, arg
, 0) < 0)
149 return error(_("could not read file '%s'"), arg
);
155 static struct strategy
*get_strategy(const char *name
)
158 struct strategy
*ret
;
159 static struct cmdnames main_cmds
, other_cmds
;
165 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
166 if (!strcmp(name
, all_strategy
[i
].name
))
167 return &all_strategy
[i
];
170 struct cmdnames not_strategies
;
173 memset(¬_strategies
, 0, sizeof(struct cmdnames
));
174 load_command_list("git-merge-", &main_cmds
, &other_cmds
);
175 for (i
= 0; i
< main_cmds
.cnt
; i
++) {
177 struct cmdname
*ent
= main_cmds
.names
[i
];
178 for (j
= 0; j
< ARRAY_SIZE(all_strategy
); j
++)
179 if (!strncmp(ent
->name
, all_strategy
[j
].name
, ent
->len
)
180 && !all_strategy
[j
].name
[ent
->len
])
183 add_cmdname(¬_strategies
, ent
->name
, ent
->len
);
185 exclude_cmds(&main_cmds
, ¬_strategies
);
187 if (!is_in_cmdlist(&main_cmds
, name
) && !is_in_cmdlist(&other_cmds
, name
)) {
188 fprintf(stderr
, _("Could not find merge strategy '%s'.\n"), name
);
189 fprintf(stderr
, _("Available strategies are:"));
190 for (i
= 0; i
< main_cmds
.cnt
; i
++)
191 fprintf(stderr
, " %s", main_cmds
.names
[i
]->name
);
192 fprintf(stderr
, ".\n");
193 if (other_cmds
.cnt
) {
194 fprintf(stderr
, _("Available custom strategies are:"));
195 for (i
= 0; i
< other_cmds
.cnt
; i
++)
196 fprintf(stderr
, " %s", other_cmds
.names
[i
]->name
);
197 fprintf(stderr
, ".\n");
202 ret
= xcalloc(1, sizeof(struct strategy
));
203 ret
->name
= xstrdup(name
);
204 ret
->attr
= NO_TRIVIAL
;
208 static void append_strategy(struct strategy
*s
)
210 ALLOC_GROW(use_strategies
, use_strategies_nr
+ 1, use_strategies_alloc
);
211 use_strategies
[use_strategies_nr
++] = s
;
214 static int option_parse_strategy(const struct option
*opt
,
215 const char *name
, int unset
)
220 append_strategy(get_strategy(name
));
224 static int option_parse_x(const struct option
*opt
,
225 const char *arg
, int unset
)
230 ALLOC_GROW(xopts
, xopts_nr
+ 1, xopts_alloc
);
231 xopts
[xopts_nr
++] = xstrdup(arg
);
235 static int option_parse_n(const struct option
*opt
,
236 const char *arg
, int unset
)
239 show_diffstat
= unset
;
243 static struct option builtin_merge_options
[] = {
244 { OPTION_CALLBACK
, 'n', NULL
, NULL
, NULL
,
245 N_("do not show a diffstat at the end of the merge"),
246 PARSE_OPT_NOARG
, option_parse_n
},
247 OPT_BOOL(0, "stat", &show_diffstat
,
248 N_("show a diffstat at the end of the merge")),
249 OPT_BOOL(0, "summary", &show_diffstat
, N_("(synonym to --stat)")),
250 { OPTION_INTEGER
, 0, "log", &shortlog_len
, N_("n"),
251 N_("add (at most <n>) entries from shortlog to merge commit message"),
252 PARSE_OPT_OPTARG
, NULL
, DEFAULT_MERGE_LOG_LEN
},
253 OPT_BOOL(0, "squash", &squash
,
254 N_("create a single commit instead of doing a merge")),
255 OPT_BOOL(0, "commit", &option_commit
,
256 N_("perform a commit if the merge succeeds (default)")),
257 OPT_BOOL('e', "edit", &option_edit
,
258 N_("edit message before committing")),
259 OPT_CLEANUP(&cleanup_arg
),
260 OPT_SET_INT(0, "ff", &fast_forward
, N_("allow fast-forward (default)"), FF_ALLOW
),
261 OPT_SET_INT_F(0, "ff-only", &fast_forward
,
262 N_("abort if fast-forward is not possible"),
263 FF_ONLY
, PARSE_OPT_NONEG
),
264 OPT_RERERE_AUTOUPDATE(&allow_rerere_auto
),
265 OPT_BOOL(0, "verify-signatures", &verify_signatures
,
266 N_("verify that the named commit has a valid GPG signature")),
267 OPT_CALLBACK('s', "strategy", &use_strategies
, N_("strategy"),
268 N_("merge strategy to use"), option_parse_strategy
),
269 OPT_CALLBACK('X', "strategy-option", &xopts
, N_("option=value"),
270 N_("option for selected merge strategy"), option_parse_x
),
271 OPT_CALLBACK('m', "message", &merge_msg
, N_("message"),
272 N_("merge commit message (for a non-fast-forward merge)"),
273 option_parse_message
),
274 { OPTION_LOWLEVEL_CALLBACK
, 'F', "file", &merge_msg
, N_("path"),
275 N_("read message from file"), PARSE_OPT_NONEG
,
276 NULL
, 0, option_read_message
},
277 OPT__VERBOSITY(&verbosity
),
278 OPT_BOOL(0, "abort", &abort_current_merge
,
279 N_("abort the current in-progress merge")),
280 OPT_BOOL(0, "quit", &quit_current_merge
,
281 N_("--abort but leave index and working tree alone")),
282 OPT_BOOL(0, "continue", &continue_current_merge
,
283 N_("continue the current in-progress merge")),
284 OPT_BOOL(0, "allow-unrelated-histories", &allow_unrelated_histories
,
285 N_("allow merging unrelated histories")),
286 OPT_SET_INT(0, "progress", &show_progress
, N_("force progress reporting"), 1),
287 { OPTION_STRING
, 'S', "gpg-sign", &sign_commit
, N_("key-id"),
288 N_("GPG sign commit"), PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
289 OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore
, N_("update ignored files (default)")),
290 OPT_BOOL(0, "signoff", &signoff
, N_("add Signed-off-by:")),
291 OPT_BOOL(0, "no-verify", &no_verify
, N_("bypass pre-merge-commit and commit-msg hooks")),
295 static int save_state(struct object_id
*stash
)
298 struct child_process cp
= CHILD_PROCESS_INIT
;
299 struct strbuf buffer
= STRBUF_INIT
;
300 const char *argv
[] = {"stash", "create", NULL
};
307 if (start_command(&cp
))
308 die(_("could not run stash."));
309 len
= strbuf_read(&buffer
, cp
.out
, 1024);
312 if (finish_command(&cp
) || len
< 0)
313 die(_("stash failed"));
314 else if (!len
) /* no changes */
316 strbuf_setlen(&buffer
, buffer
.len
-1);
317 if (get_oid(buffer
.buf
, stash
))
318 die(_("not a valid object: %s"), buffer
.buf
);
321 strbuf_release(&buffer
);
325 static void read_empty(const struct object_id
*oid
, int verbose
)
330 args
[i
++] = "read-tree";
335 args
[i
++] = empty_tree_oid_hex();
336 args
[i
++] = oid_to_hex(oid
);
339 if (run_command_v_opt(args
, RUN_GIT_CMD
))
340 die(_("read-tree failed"));
343 static void reset_hard(const struct object_id
*oid
, int verbose
)
348 args
[i
++] = "read-tree";
351 args
[i
++] = "--reset";
353 args
[i
++] = oid_to_hex(oid
);
356 if (run_command_v_opt(args
, RUN_GIT_CMD
))
357 die(_("read-tree failed"));
360 static void restore_state(const struct object_id
*head
,
361 const struct object_id
*stash
)
363 struct strbuf sb
= STRBUF_INIT
;
364 const char *args
[] = { "stash", "apply", NULL
, NULL
};
366 if (is_null_oid(stash
))
371 args
[2] = oid_to_hex(stash
);
374 * It is OK to ignore error here, for example when there was
375 * nothing to restore.
377 run_command_v_opt(args
, RUN_GIT_CMD
);
380 refresh_cache(REFRESH_QUIET
);
383 /* This is called when no merge was necessary. */
384 static void finish_up_to_date(const char *msg
)
387 printf("%s%s\n", squash
? _(" (nothing to squash)") : "", msg
);
388 remove_merge_branch_state(the_repository
);
391 static void squash_message(struct commit
*commit
, struct commit_list
*remoteheads
)
394 struct strbuf out
= STRBUF_INIT
;
395 struct commit_list
*j
;
396 struct pretty_print_context ctx
= {0};
398 printf(_("Squash commit -- not updating HEAD\n"));
400 repo_init_revisions(the_repository
, &rev
, NULL
);
401 rev
.ignore_merges
= 1;
402 rev
.commit_format
= CMIT_FMT_MEDIUM
;
404 commit
->object
.flags
|= UNINTERESTING
;
405 add_pending_object(&rev
, &commit
->object
, NULL
);
407 for (j
= remoteheads
; j
; j
= j
->next
)
408 add_pending_object(&rev
, &j
->item
->object
, NULL
);
410 setup_revisions(0, NULL
, &rev
, NULL
);
411 if (prepare_revision_walk(&rev
))
412 die(_("revision walk setup failed"));
414 ctx
.abbrev
= rev
.abbrev
;
415 ctx
.date_mode
= rev
.date_mode
;
416 ctx
.fmt
= rev
.commit_format
;
418 strbuf_addstr(&out
, "Squashed commit of the following:\n");
419 while ((commit
= get_revision(&rev
)) != NULL
) {
420 strbuf_addch(&out
, '\n');
421 strbuf_addf(&out
, "commit %s\n",
422 oid_to_hex(&commit
->object
.oid
));
423 pretty_print_commit(&ctx
, commit
, &out
);
425 write_file_buf(git_path_squash_msg(the_repository
), out
.buf
, out
.len
);
426 strbuf_release(&out
);
429 static void finish(struct commit
*head_commit
,
430 struct commit_list
*remoteheads
,
431 const struct object_id
*new_head
, const char *msg
)
433 struct strbuf reflog_message
= STRBUF_INIT
;
434 const struct object_id
*head
= &head_commit
->object
.oid
;
437 strbuf_addstr(&reflog_message
, getenv("GIT_REFLOG_ACTION"));
441 strbuf_addf(&reflog_message
, "%s: %s",
442 getenv("GIT_REFLOG_ACTION"), msg
);
445 squash_message(head_commit
, remoteheads
);
447 if (verbosity
>= 0 && !merge_msg
.len
)
448 printf(_("No merge message -- not updating HEAD\n"));
450 const char *argv_gc_auto
[] = { "gc", "--auto", NULL
};
451 update_ref(reflog_message
.buf
, "HEAD", new_head
, head
,
452 0, UPDATE_REFS_DIE_ON_ERR
);
454 * We ignore errors in 'gc --auto', since the
455 * user should see them.
457 close_object_store(the_repository
->objects
);
458 run_command_v_opt(argv_gc_auto
, RUN_GIT_CMD
);
461 if (new_head
&& show_diffstat
) {
462 struct diff_options opts
;
463 repo_diff_setup(the_repository
, &opts
);
464 opts
.stat_width
= -1; /* use full terminal width */
465 opts
.stat_graph_width
= -1; /* respect statGraphWidth config */
466 opts
.output_format
|=
467 DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
468 opts
.detect_rename
= DIFF_DETECT_RENAME
;
469 diff_setup_done(&opts
);
470 diff_tree_oid(head
, new_head
, "", &opts
);
475 /* Run a post-merge hook */
476 run_hook_le(NULL
, "post-merge", squash
? "1" : "0", NULL
);
478 strbuf_release(&reflog_message
);
481 /* Get the name for the merge commit's message. */
482 static void merge_name(const char *remote
, struct strbuf
*msg
)
484 struct commit
*remote_head
;
485 struct object_id branch_head
;
486 struct strbuf buf
= STRBUF_INIT
;
487 struct strbuf bname
= STRBUF_INIT
;
488 struct merge_remote_desc
*desc
;
493 strbuf_branchname(&bname
, remote
, 0);
496 oidclr(&branch_head
);
497 remote_head
= get_merge_parent(remote
);
499 die(_("'%s' does not point to a commit"), remote
);
501 if (dwim_ref(remote
, strlen(remote
), &branch_head
, &found_ref
) > 0) {
502 if (starts_with(found_ref
, "refs/heads/")) {
503 strbuf_addf(msg
, "%s\t\tbranch '%s' of .\n",
504 oid_to_hex(&branch_head
), remote
);
507 if (starts_with(found_ref
, "refs/tags/")) {
508 strbuf_addf(msg
, "%s\t\ttag '%s' of .\n",
509 oid_to_hex(&branch_head
), remote
);
512 if (starts_with(found_ref
, "refs/remotes/")) {
513 strbuf_addf(msg
, "%s\t\tremote-tracking branch '%s' of .\n",
514 oid_to_hex(&branch_head
), remote
);
519 /* See if remote matches <name>^^^.. or <name>~<number> */
520 for (len
= 0, ptr
= remote
+ strlen(remote
);
521 remote
< ptr
&& ptr
[-1] == '^';
528 ptr
= strrchr(remote
, '~');
530 int seen_nonzero
= 0;
533 while (*++ptr
&& isdigit(*ptr
)) {
534 seen_nonzero
|= (*ptr
!= '0');
538 len
= 0; /* not ...~<number> */
539 else if (seen_nonzero
)
542 early
= 1; /* "name~" is "name~1"! */
546 struct strbuf truname
= STRBUF_INIT
;
547 strbuf_addf(&truname
, "refs/heads/%s", remote
);
548 strbuf_setlen(&truname
, truname
.len
- len
);
549 if (ref_exists(truname
.buf
)) {
551 "%s\t\tbranch '%s'%s of .\n",
552 oid_to_hex(&remote_head
->object
.oid
),
554 (early
? " (early part)" : ""));
555 strbuf_release(&truname
);
558 strbuf_release(&truname
);
561 desc
= merge_remote_util(remote_head
);
562 if (desc
&& desc
->obj
&& desc
->obj
->type
== OBJ_TAG
) {
563 strbuf_addf(msg
, "%s\t\t%s '%s'\n",
564 oid_to_hex(&desc
->obj
->oid
),
565 type_name(desc
->obj
->type
),
570 strbuf_addf(msg
, "%s\t\tcommit '%s'\n",
571 oid_to_hex(&remote_head
->object
.oid
), remote
);
573 strbuf_release(&buf
);
574 strbuf_release(&bname
);
577 static void parse_branch_merge_options(char *bmo
)
584 argc
= split_cmdline(bmo
, &argv
);
586 die(_("Bad branch.%s.mergeoptions string: %s"), branch
,
587 _(split_cmdline_strerror(argc
)));
588 REALLOC_ARRAY(argv
, argc
+ 2);
589 MOVE_ARRAY(argv
+ 1, argv
, argc
+ 1);
591 argv
[0] = "branch.*.mergeoptions";
592 parse_options(argc
, argv
, NULL
, builtin_merge_options
,
593 builtin_merge_usage
, 0);
597 static int git_merge_config(const char *k
, const char *v
, void *cb
)
601 if (branch
&& starts_with(k
, "branch.") &&
602 starts_with(k
+ 7, branch
) &&
603 !strcmp(k
+ 7 + strlen(branch
), ".mergeoptions")) {
604 free(branch_mergeoptions
);
605 branch_mergeoptions
= xstrdup(v
);
609 if (!strcmp(k
, "merge.diffstat") || !strcmp(k
, "merge.stat"))
610 show_diffstat
= git_config_bool(k
, v
);
611 else if (!strcmp(k
, "merge.verifysignatures"))
612 verify_signatures
= git_config_bool(k
, v
);
613 else if (!strcmp(k
, "pull.twohead"))
614 return git_config_string(&pull_twohead
, k
, v
);
615 else if (!strcmp(k
, "pull.octopus"))
616 return git_config_string(&pull_octopus
, k
, v
);
617 else if (!strcmp(k
, "commit.cleanup"))
618 return git_config_string(&cleanup_arg
, k
, v
);
619 else if (!strcmp(k
, "merge.renormalize"))
620 option_renormalize
= git_config_bool(k
, v
);
621 else if (!strcmp(k
, "merge.ff")) {
622 int boolval
= git_parse_maybe_bool(v
);
624 fast_forward
= boolval
? FF_ALLOW
: FF_NO
;
625 } else if (v
&& !strcmp(v
, "only")) {
626 fast_forward
= FF_ONLY
;
627 } /* do not barf on values from future versions of git */
629 } else if (!strcmp(k
, "merge.defaulttoupstream")) {
630 default_to_upstream
= git_config_bool(k
, v
);
632 } else if (!strcmp(k
, "commit.gpgsign")) {
633 sign_commit
= git_config_bool(k
, v
) ? "" : NULL
;
635 } else if (!strcmp(k
, "gpg.mintrustlevel")) {
636 check_trust_level
= 0;
639 status
= fmt_merge_msg_config(k
, v
, cb
);
642 status
= git_gpg_config(k
, v
, NULL
);
645 return git_diff_ui_config(k
, v
, cb
);
648 static int read_tree_trivial(struct object_id
*common
, struct object_id
*head
,
649 struct object_id
*one
)
652 struct tree
*trees
[MAX_UNPACK_TREES
];
653 struct tree_desc t
[MAX_UNPACK_TREES
];
654 struct unpack_trees_options opts
;
656 memset(&opts
, 0, sizeof(opts
));
658 opts
.src_index
= &the_index
;
659 opts
.dst_index
= &the_index
;
661 opts
.verbose_update
= 1;
662 opts
.trivial_merges_only
= 1;
664 trees
[nr_trees
] = parse_tree_indirect(common
);
665 if (!trees
[nr_trees
++])
667 trees
[nr_trees
] = parse_tree_indirect(head
);
668 if (!trees
[nr_trees
++])
670 trees
[nr_trees
] = parse_tree_indirect(one
);
671 if (!trees
[nr_trees
++])
673 opts
.fn
= threeway_merge
;
674 cache_tree_free(&active_cache_tree
);
675 for (i
= 0; i
< nr_trees
; i
++) {
676 parse_tree(trees
[i
]);
677 init_tree_desc(t
+i
, trees
[i
]->buffer
, trees
[i
]->size
);
679 if (unpack_trees(nr_trees
, t
, &opts
))
684 static void write_tree_trivial(struct object_id
*oid
)
686 if (write_cache_as_tree(oid
, 0, NULL
))
687 die(_("git write-tree failed to write a tree"));
690 static int try_merge_strategy(const char *strategy
, struct commit_list
*common
,
691 struct commit_list
*remoteheads
,
694 const char *head_arg
= "HEAD";
696 if (refresh_and_write_cache(REFRESH_QUIET
, SKIP_IF_UNCHANGED
, 0) < 0)
697 return error(_("Unable to write index."));
699 if (!strcmp(strategy
, "recursive") || !strcmp(strategy
, "subtree")) {
700 struct lock_file lock
= LOCK_INIT
;
702 struct commit
*result
;
703 struct commit_list
*reversed
= NULL
;
704 struct merge_options o
;
705 struct commit_list
*j
;
707 if (remoteheads
->next
) {
708 error(_("Not handling anything other than two heads merge."));
712 init_merge_options(&o
, the_repository
);
713 if (!strcmp(strategy
, "subtree"))
714 o
.subtree_shift
= "";
716 o
.renormalize
= option_renormalize
;
717 o
.show_rename_progress
=
718 show_progress
== -1 ? isatty(2) : show_progress
;
720 for (x
= 0; x
< xopts_nr
; x
++)
721 if (parse_merge_opt(&o
, xopts
[x
]))
722 die(_("Unknown option for merge-recursive: -X%s"), xopts
[x
]);
724 o
.branch1
= head_arg
;
725 o
.branch2
= merge_remote_util(remoteheads
->item
)->name
;
727 for (j
= common
; j
; j
= j
->next
)
728 commit_list_insert(j
->item
, &reversed
);
730 hold_locked_index(&lock
, LOCK_DIE_ON_ERROR
);
731 clean
= merge_recursive(&o
, head
,
732 remoteheads
->item
, reversed
, &result
);
735 if (write_locked_index(&the_index
, &lock
,
736 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
737 die(_("unable to write %s"), get_index_file());
738 return clean
? 0 : 1;
740 return try_merge_command(the_repository
,
741 strategy
, xopts_nr
, xopts
,
742 common
, head_arg
, remoteheads
);
746 static void count_diff_files(struct diff_queue_struct
*q
,
747 struct diff_options
*opt
, void *data
)
754 static int count_unmerged_entries(void)
758 for (i
= 0; i
< active_nr
; i
++)
759 if (ce_stage(active_cache
[i
]))
765 static void add_strategies(const char *string
, unsigned attr
)
770 struct string_list list
= STRING_LIST_INIT_DUP
;
771 struct string_list_item
*item
;
772 string_list_split(&list
, string
, ' ', -1);
773 for_each_string_list_item(item
, &list
)
774 append_strategy(get_strategy(item
->string
));
775 string_list_clear(&list
, 0);
778 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
779 if (all_strategy
[i
].attr
& attr
)
780 append_strategy(&all_strategy
[i
]);
784 static void read_merge_msg(struct strbuf
*msg
)
786 const char *filename
= git_path_merge_msg(the_repository
);
788 if (strbuf_read_file(msg
, filename
, 0) < 0)
789 die_errno(_("Could not read from '%s'"), filename
);
792 static void write_merge_state(struct commit_list
*);
793 static void abort_commit(struct commit_list
*remoteheads
, const char *err_msg
)
796 error("%s", err_msg
);
798 _("Not committing merge; use 'git commit' to complete the merge.\n"));
799 write_merge_state(remoteheads
);
803 static const char merge_editor_comment
[] =
804 N_("Please enter a commit message to explain why this merge is necessary,\n"
805 "especially if it merges an updated upstream into a topic branch.\n"
808 static const char scissors_editor_comment
[] =
809 N_("An empty message aborts the commit.\n");
811 static const char no_scissors_editor_comment
[] =
812 N_("Lines starting with '%c' will be ignored, and an empty message aborts\n"
815 static void write_merge_heads(struct commit_list
*);
816 static void prepare_to_commit(struct commit_list
*remoteheads
)
818 struct strbuf msg
= STRBUF_INIT
;
819 const char *index_file
= get_index_file();
821 if (!no_verify
&& run_commit_hook(0 < option_edit
, index_file
, "pre-merge-commit", NULL
))
822 abort_commit(remoteheads
, NULL
);
824 * Re-read the index as pre-merge-commit hook could have updated it,
825 * and write it out as a tree. We must do this before we invoke
826 * the editor and after we invoke run_status above.
828 if (find_hook("pre-merge-commit"))
830 read_cache_from(index_file
);
831 strbuf_addbuf(&msg
, &merge_msg
);
833 BUG("the control must not reach here under --squash");
834 if (0 < option_edit
) {
835 strbuf_addch(&msg
, '\n');
836 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
) {
837 wt_status_append_cut_line(&msg
);
838 strbuf_commented_addf(&msg
, "\n");
840 strbuf_commented_addf(&msg
, _(merge_editor_comment
));
841 strbuf_commented_addf(&msg
, _(cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
?
842 scissors_editor_comment
:
843 no_scissors_editor_comment
), comment_line_char
);
846 append_signoff(&msg
, ignore_non_trailer(msg
.buf
, msg
.len
), 0);
847 write_merge_heads(remoteheads
);
848 write_file_buf(git_path_merge_msg(the_repository
), msg
.buf
, msg
.len
);
849 if (run_commit_hook(0 < option_edit
, get_index_file(), "prepare-commit-msg",
850 git_path_merge_msg(the_repository
), "merge", NULL
))
851 abort_commit(remoteheads
, NULL
);
852 if (0 < option_edit
) {
853 if (launch_editor(git_path_merge_msg(the_repository
), NULL
, NULL
))
854 abort_commit(remoteheads
, NULL
);
857 if (!no_verify
&& run_commit_hook(0 < option_edit
, get_index_file(),
859 git_path_merge_msg(the_repository
), NULL
))
860 abort_commit(remoteheads
, NULL
);
862 read_merge_msg(&msg
);
863 cleanup_message(&msg
, cleanup_mode
, 0);
865 abort_commit(remoteheads
, _("Empty commit message."));
866 strbuf_release(&merge_msg
);
867 strbuf_addbuf(&merge_msg
, &msg
);
868 strbuf_release(&msg
);
871 static int merge_trivial(struct commit
*head
, struct commit_list
*remoteheads
)
873 struct object_id result_tree
, result_commit
;
874 struct commit_list
*parents
, **pptr
= &parents
;
876 if (refresh_and_write_cache(REFRESH_QUIET
, SKIP_IF_UNCHANGED
, 0) < 0)
877 return error(_("Unable to write index."));
879 write_tree_trivial(&result_tree
);
880 printf(_("Wonderful.\n"));
881 pptr
= commit_list_append(head
, pptr
);
882 pptr
= commit_list_append(remoteheads
->item
, pptr
);
883 prepare_to_commit(remoteheads
);
884 if (commit_tree(merge_msg
.buf
, merge_msg
.len
, &result_tree
, parents
,
885 &result_commit
, NULL
, sign_commit
))
886 die(_("failed to write commit object"));
887 finish(head
, remoteheads
, &result_commit
, "In-index merge");
888 remove_merge_branch_state(the_repository
);
892 static int finish_automerge(struct commit
*head
,
894 struct commit_list
*common
,
895 struct commit_list
*remoteheads
,
896 struct object_id
*result_tree
,
897 const char *wt_strategy
)
899 struct commit_list
*parents
= NULL
;
900 struct strbuf buf
= STRBUF_INIT
;
901 struct object_id result_commit
;
903 write_tree_trivial(result_tree
);
904 free_commit_list(common
);
905 parents
= remoteheads
;
906 if (!head_subsumed
|| fast_forward
== FF_NO
)
907 commit_list_insert(head
, &parents
);
908 prepare_to_commit(remoteheads
);
909 if (commit_tree(merge_msg
.buf
, merge_msg
.len
, result_tree
, parents
,
910 &result_commit
, NULL
, sign_commit
))
911 die(_("failed to write commit object"));
912 strbuf_addf(&buf
, "Merge made by the '%s' strategy.", wt_strategy
);
913 finish(head
, remoteheads
, &result_commit
, buf
.buf
);
914 strbuf_release(&buf
);
915 remove_merge_branch_state(the_repository
);
919 static int suggest_conflicts(void)
921 const char *filename
;
923 struct strbuf msgbuf
= STRBUF_INIT
;
925 filename
= git_path_merge_msg(the_repository
);
926 fp
= xfopen(filename
, "a");
929 * We can't use cleanup_mode because if we're not using the editor,
930 * get_cleanup_mode will return COMMIT_MSG_CLEANUP_SPACE instead, even
931 * though the message is meant to be processed later by git-commit.
932 * Thus, we will get the cleanup mode which is returned when we _are_
935 append_conflicts_hint(&the_index
, &msgbuf
,
936 get_cleanup_mode(cleanup_arg
, 1));
937 fputs(msgbuf
.buf
, fp
);
938 strbuf_release(&msgbuf
);
940 repo_rerere(the_repository
, allow_rerere_auto
);
941 printf(_("Automatic merge failed; "
942 "fix conflicts and then commit the result.\n"));
946 static int evaluate_result(void)
951 /* Check how many files differ. */
952 repo_init_revisions(the_repository
, &rev
, "");
953 setup_revisions(0, NULL
, &rev
, NULL
);
954 rev
.diffopt
.output_format
|=
955 DIFF_FORMAT_CALLBACK
;
956 rev
.diffopt
.format_callback
= count_diff_files
;
957 rev
.diffopt
.format_callback_data
= &cnt
;
958 run_diff_files(&rev
, 0);
961 * Check how many unmerged entries are
964 cnt
+= count_unmerged_entries();
970 * Pretend as if the user told us to merge with the remote-tracking
971 * branch we have for the upstream of the current branch
973 static int setup_with_upstream(const char ***argv
)
975 struct branch
*branch
= branch_get(NULL
);
980 die(_("No current branch."));
981 if (!branch
->remote_name
)
982 die(_("No remote for the current branch."));
983 if (!branch
->merge_nr
)
984 die(_("No default upstream defined for the current branch."));
986 args
= xcalloc(st_add(branch
->merge_nr
, 1), sizeof(char *));
987 for (i
= 0; i
< branch
->merge_nr
; i
++) {
988 if (!branch
->merge
[i
]->dst
)
989 die(_("No remote-tracking branch for %s from %s"),
990 branch
->merge
[i
]->src
, branch
->remote_name
);
991 args
[i
] = branch
->merge
[i
]->dst
;
998 static void write_merge_heads(struct commit_list
*remoteheads
)
1000 struct commit_list
*j
;
1001 struct strbuf buf
= STRBUF_INIT
;
1003 for (j
= remoteheads
; j
; j
= j
->next
) {
1004 struct object_id
*oid
;
1005 struct commit
*c
= j
->item
;
1006 struct merge_remote_desc
*desc
;
1008 desc
= merge_remote_util(c
);
1009 if (desc
&& desc
->obj
) {
1010 oid
= &desc
->obj
->oid
;
1012 oid
= &c
->object
.oid
;
1014 strbuf_addf(&buf
, "%s\n", oid_to_hex(oid
));
1016 write_file_buf(git_path_merge_head(the_repository
), buf
.buf
, buf
.len
);
1019 if (fast_forward
== FF_NO
)
1020 strbuf_addstr(&buf
, "no-ff");
1021 write_file_buf(git_path_merge_mode(the_repository
), buf
.buf
, buf
.len
);
1022 strbuf_release(&buf
);
1025 static void write_merge_state(struct commit_list
*remoteheads
)
1027 write_merge_heads(remoteheads
);
1028 strbuf_addch(&merge_msg
, '\n');
1029 write_file_buf(git_path_merge_msg(the_repository
), merge_msg
.buf
,
1033 static int default_edit_option(void)
1035 static const char name
[] = "GIT_MERGE_AUTOEDIT";
1036 const char *e
= getenv(name
);
1037 struct stat st_stdin
, st_stdout
;
1040 /* an explicit -m msg without --[no-]edit */
1044 int v
= git_parse_maybe_bool(e
);
1046 die(_("Bad value '%s' in environment '%s'"), e
, name
);
1050 /* Use editor if stdin and stdout are the same and is a tty */
1051 return (!fstat(0, &st_stdin
) &&
1052 !fstat(1, &st_stdout
) &&
1053 isatty(0) && isatty(1) &&
1054 st_stdin
.st_dev
== st_stdout
.st_dev
&&
1055 st_stdin
.st_ino
== st_stdout
.st_ino
&&
1056 st_stdin
.st_mode
== st_stdout
.st_mode
);
1059 static struct commit_list
*reduce_parents(struct commit
*head_commit
,
1061 struct commit_list
*remoteheads
)
1063 struct commit_list
*parents
, **remotes
;
1066 * Is the current HEAD reachable from another commit being
1067 * merged? If so we do not want to record it as a parent of
1068 * the resulting merge, unless --no-ff is given. We will flip
1069 * this variable to 0 when we find HEAD among the independent
1070 * tips being merged.
1074 /* Find what parents to record by checking independent ones. */
1075 parents
= reduce_heads(remoteheads
);
1076 free_commit_list(remoteheads
);
1079 remotes
= &remoteheads
;
1081 struct commit
*commit
= pop_commit(&parents
);
1082 if (commit
== head_commit
)
1085 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1090 static void prepare_merge_message(struct strbuf
*merge_names
, struct strbuf
*merge_msg
)
1092 struct fmt_merge_msg_opts opts
;
1094 memset(&opts
, 0, sizeof(opts
));
1095 opts
.add_title
= !have_message
;
1096 opts
.shortlog_len
= shortlog_len
;
1097 opts
.credit_people
= (0 < option_edit
);
1099 fmt_merge_msg(merge_names
, merge_msg
, &opts
);
1101 strbuf_setlen(merge_msg
, merge_msg
->len
- 1);
1104 static void handle_fetch_head(struct commit_list
**remotes
, struct strbuf
*merge_names
)
1106 const char *filename
;
1108 struct strbuf fetch_head_file
= STRBUF_INIT
;
1109 const unsigned hexsz
= the_hash_algo
->hexsz
;
1112 merge_names
= &fetch_head_file
;
1114 filename
= git_path_fetch_head(the_repository
);
1115 fd
= open(filename
, O_RDONLY
);
1117 die_errno(_("could not open '%s' for reading"), filename
);
1119 if (strbuf_read(merge_names
, fd
, 0) < 0)
1120 die_errno(_("could not read '%s'"), filename
);
1122 die_errno(_("could not close '%s'"), filename
);
1124 for (pos
= 0; pos
< merge_names
->len
; pos
= npos
) {
1125 struct object_id oid
;
1127 struct commit
*commit
;
1129 ptr
= strchr(merge_names
->buf
+ pos
, '\n');
1131 npos
= ptr
- merge_names
->buf
+ 1;
1133 npos
= merge_names
->len
;
1135 if (npos
- pos
< hexsz
+ 2 ||
1136 get_oid_hex(merge_names
->buf
+ pos
, &oid
))
1137 commit
= NULL
; /* bad */
1138 else if (memcmp(merge_names
->buf
+ pos
+ hexsz
, "\t\t", 2))
1139 continue; /* not-for-merge */
1141 char saved
= merge_names
->buf
[pos
+ hexsz
];
1142 merge_names
->buf
[pos
+ hexsz
] = '\0';
1143 commit
= get_merge_parent(merge_names
->buf
+ pos
);
1144 merge_names
->buf
[pos
+ hexsz
] = saved
;
1149 die(_("not something we can merge in %s: %s"),
1150 filename
, merge_names
->buf
+ pos
);
1152 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1155 if (merge_names
== &fetch_head_file
)
1156 strbuf_release(&fetch_head_file
);
1159 static struct commit_list
*collect_parents(struct commit
*head_commit
,
1161 int argc
, const char **argv
,
1162 struct strbuf
*merge_msg
)
1165 struct commit_list
*remoteheads
= NULL
;
1166 struct commit_list
**remotes
= &remoteheads
;
1167 struct strbuf merge_names
= STRBUF_INIT
, *autogen
= NULL
;
1169 if (merge_msg
&& (!have_message
|| shortlog_len
))
1170 autogen
= &merge_names
;
1173 remotes
= &commit_list_insert(head_commit
, remotes
)->next
;
1175 if (argc
== 1 && !strcmp(argv
[0], "FETCH_HEAD")) {
1176 handle_fetch_head(remotes
, autogen
);
1177 remoteheads
= reduce_parents(head_commit
, head_subsumed
, remoteheads
);
1179 for (i
= 0; i
< argc
; i
++) {
1180 struct commit
*commit
= get_merge_parent(argv
[i
]);
1182 help_unknown_ref(argv
[i
], "merge",
1183 _("not something we can merge"));
1184 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1186 remoteheads
= reduce_parents(head_commit
, head_subsumed
, remoteheads
);
1188 struct commit_list
*p
;
1189 for (p
= remoteheads
; p
; p
= p
->next
)
1190 merge_name(merge_remote_util(p
->item
)->name
, autogen
);
1195 prepare_merge_message(autogen
, merge_msg
);
1196 strbuf_release(autogen
);
1202 static int merging_a_throwaway_tag(struct commit
*commit
)
1205 struct object_id oid
;
1206 int is_throwaway_tag
= 0;
1208 /* Are we merging a tag? */
1209 if (!merge_remote_util(commit
) ||
1210 !merge_remote_util(commit
)->obj
||
1211 merge_remote_util(commit
)->obj
->type
!= OBJ_TAG
)
1212 return is_throwaway_tag
;
1215 * Now we know we are merging a tag object. Are we downstream
1216 * and following the tags from upstream? If so, we must have
1217 * the tag object pointed at by "refs/tags/$T" where $T is the
1218 * tagname recorded in the tag object. We want to allow such
1219 * a "just to catch up" merge to fast-forward.
1221 * Otherwise, we are playing an integrator's role, making a
1222 * merge with a throw-away tag from a contributor with
1223 * something like "git pull $contributor $signed_tag".
1224 * We want to forbid such a merge from fast-forwarding
1225 * by default; otherwise we would not keep the signature
1228 tag_ref
= xstrfmt("refs/tags/%s",
1229 ((struct tag
*)merge_remote_util(commit
)->obj
)->tag
);
1230 if (!read_ref(tag_ref
, &oid
) &&
1231 oideq(&oid
, &merge_remote_util(commit
)->obj
->oid
))
1232 is_throwaway_tag
= 0;
1234 is_throwaway_tag
= 1;
1236 return is_throwaway_tag
;
1239 int cmd_merge(int argc
, const char **argv
, const char *prefix
)
1241 struct object_id result_tree
, stash
, head_oid
;
1242 struct commit
*head_commit
;
1243 struct strbuf buf
= STRBUF_INIT
;
1244 int i
, ret
= 0, head_subsumed
;
1245 int best_cnt
= -1, merge_was_ok
= 0, automerge_was_ok
= 0;
1246 struct commit_list
*common
= NULL
;
1247 const char *best_strategy
= NULL
, *wt_strategy
= NULL
;
1248 struct commit_list
*remoteheads
, *p
;
1249 void *branch_to_free
;
1250 int orig_argc
= argc
;
1252 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1253 usage_with_options(builtin_merge_usage
, builtin_merge_options
);
1256 * Check if we are _not_ on a detached HEAD, i.e. if there is a
1259 branch
= branch_to_free
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
1261 skip_prefix(branch
, "refs/heads/", &branch
);
1263 init_diff_ui_defaults();
1264 git_config(git_merge_config
, NULL
);
1266 if (!branch
|| is_null_oid(&head_oid
))
1269 head_commit
= lookup_commit_or_die(&head_oid
, "HEAD");
1271 if (branch_mergeoptions
)
1272 parse_branch_merge_options(branch_mergeoptions
);
1273 argc
= parse_options(argc
, argv
, prefix
, builtin_merge_options
,
1274 builtin_merge_usage
, 0);
1275 if (shortlog_len
< 0)
1276 shortlog_len
= (merge_log_config
> 0) ? merge_log_config
: 0;
1278 if (verbosity
< 0 && show_progress
== -1)
1281 if (abort_current_merge
) {
1283 const char *nargv
[] = {"reset", "--merge", NULL
};
1286 usage_msg_opt(_("--abort expects no arguments"),
1287 builtin_merge_usage
, builtin_merge_options
);
1289 if (!file_exists(git_path_merge_head(the_repository
)))
1290 die(_("There is no merge to abort (MERGE_HEAD missing)."));
1292 /* Invoke 'git reset --merge' */
1293 ret
= cmd_reset(nargc
, nargv
, prefix
);
1297 if (quit_current_merge
) {
1299 usage_msg_opt(_("--quit expects no arguments"),
1300 builtin_merge_usage
,
1301 builtin_merge_options
);
1303 remove_merge_branch_state(the_repository
);
1307 if (continue_current_merge
) {
1309 const char *nargv
[] = {"commit", NULL
};
1312 usage_msg_opt(_("--continue expects no arguments"),
1313 builtin_merge_usage
, builtin_merge_options
);
1315 if (!file_exists(git_path_merge_head(the_repository
)))
1316 die(_("There is no merge in progress (MERGE_HEAD missing)."));
1318 /* Invoke 'git commit' */
1319 ret
= cmd_commit(nargc
, nargv
, prefix
);
1323 if (read_cache_unmerged())
1324 die_resolve_conflict("merge");
1326 if (file_exists(git_path_merge_head(the_repository
))) {
1328 * There is no unmerged entry, don't advise 'git
1329 * add/rm <file>', just 'git commit'.
1331 if (advice_resolve_conflict
)
1332 die(_("You have not concluded your merge (MERGE_HEAD exists).\n"
1333 "Please, commit your changes before you merge."));
1335 die(_("You have not concluded your merge (MERGE_HEAD exists)."));
1337 if (file_exists(git_path_cherry_pick_head(the_repository
))) {
1338 if (advice_resolve_conflict
)
1339 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
1340 "Please, commit your changes before you merge."));
1342 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists)."));
1344 resolve_undo_clear();
1346 if (option_edit
< 0)
1347 option_edit
= default_edit_option();
1349 cleanup_mode
= get_cleanup_mode(cleanup_arg
, 0 < option_edit
);
1355 if (fast_forward
== FF_NO
)
1356 die(_("You cannot combine --squash with --no-ff."));
1357 if (option_commit
> 0)
1358 die(_("You cannot combine --squash with --commit."));
1360 * squash can now silently disable option_commit - this is not
1361 * a problem as it is only overriding the default, not a user
1367 if (option_commit
< 0)
1371 if (default_to_upstream
)
1372 argc
= setup_with_upstream(&argv
);
1374 die(_("No commit specified and merge.defaultToUpstream not set."));
1375 } else if (argc
== 1 && !strcmp(argv
[0], "-")) {
1380 usage_with_options(builtin_merge_usage
,
1381 builtin_merge_options
);
1385 * If the merged head is a valid one there is no reason
1386 * to forbid "git merge" into a branch yet to be born.
1387 * We do the same for "git pull".
1389 struct object_id
*remote_head_oid
;
1391 die(_("Squash commit into empty head not supported yet"));
1392 if (fast_forward
== FF_NO
)
1393 die(_("Non-fast-forward commit does not make sense into "
1395 remoteheads
= collect_parents(head_commit
, &head_subsumed
,
1398 die(_("%s - not something we can merge"), argv
[0]);
1399 if (remoteheads
->next
)
1400 die(_("Can merge only exactly one commit into empty head"));
1402 if (verify_signatures
)
1403 verify_merge_signature(remoteheads
->item
, verbosity
,
1406 remote_head_oid
= &remoteheads
->item
->object
.oid
;
1407 read_empty(remote_head_oid
, 0);
1408 update_ref("initial pull", "HEAD", remote_head_oid
, NULL
, 0,
1409 UPDATE_REFS_DIE_ON_ERR
);
1414 * All the rest are the commits being merged; prepare
1415 * the standard merge summary message to be appended
1416 * to the given message.
1418 remoteheads
= collect_parents(head_commit
, &head_subsumed
,
1419 argc
, argv
, &merge_msg
);
1421 if (!head_commit
|| !argc
)
1422 usage_with_options(builtin_merge_usage
,
1423 builtin_merge_options
);
1425 if (verify_signatures
) {
1426 for (p
= remoteheads
; p
; p
= p
->next
) {
1427 verify_merge_signature(p
->item
, verbosity
,
1432 strbuf_addstr(&buf
, "merge");
1433 for (p
= remoteheads
; p
; p
= p
->next
)
1434 strbuf_addf(&buf
, " %s", merge_remote_util(p
->item
)->name
);
1435 setenv("GIT_REFLOG_ACTION", buf
.buf
, 0);
1438 for (p
= remoteheads
; p
; p
= p
->next
) {
1439 struct commit
*commit
= p
->item
;
1440 strbuf_addf(&buf
, "GITHEAD_%s",
1441 oid_to_hex(&commit
->object
.oid
));
1442 setenv(buf
.buf
, merge_remote_util(commit
)->name
, 1);
1444 if (fast_forward
!= FF_ONLY
&& merging_a_throwaway_tag(commit
))
1445 fast_forward
= FF_NO
;
1448 if (!use_strategies
) {
1450 ; /* already up-to-date */
1451 else if (!remoteheads
->next
)
1452 add_strategies(pull_twohead
, DEFAULT_TWOHEAD
);
1454 add_strategies(pull_octopus
, DEFAULT_OCTOPUS
);
1457 for (i
= 0; i
< use_strategies_nr
; i
++) {
1458 if (use_strategies
[i
]->attr
& NO_FAST_FORWARD
)
1459 fast_forward
= FF_NO
;
1460 if (use_strategies
[i
]->attr
& NO_TRIVIAL
)
1465 ; /* already up-to-date */
1466 else if (!remoteheads
->next
)
1467 common
= get_merge_bases(head_commit
, remoteheads
->item
);
1469 struct commit_list
*list
= remoteheads
;
1470 commit_list_insert(head_commit
, &list
);
1471 common
= get_octopus_merge_bases(list
);
1475 update_ref("updating ORIG_HEAD", "ORIG_HEAD",
1476 &head_commit
->object
.oid
, NULL
, 0, UPDATE_REFS_DIE_ON_ERR
);
1478 if (remoteheads
&& !common
) {
1479 /* No common ancestors found. */
1480 if (!allow_unrelated_histories
)
1481 die(_("refusing to merge unrelated histories"));
1482 /* otherwise, we need a real merge. */
1483 } else if (!remoteheads
||
1484 (!remoteheads
->next
&& !common
->next
&&
1485 common
->item
== remoteheads
->item
)) {
1487 * If head can reach all the merge then we are up to date.
1488 * but first the most common case of merging one remote.
1490 finish_up_to_date(_("Already up to date."));
1492 } else if (fast_forward
!= FF_NO
&& !remoteheads
->next
&&
1494 oideq(&common
->item
->object
.oid
, &head_commit
->object
.oid
)) {
1495 /* Again the most common case of merging one remote. */
1496 struct strbuf msg
= STRBUF_INIT
;
1497 struct commit
*commit
;
1499 if (verbosity
>= 0) {
1500 printf(_("Updating %s..%s\n"),
1501 find_unique_abbrev(&head_commit
->object
.oid
,
1503 find_unique_abbrev(&remoteheads
->item
->object
.oid
,
1506 strbuf_addstr(&msg
, "Fast-forward");
1509 " (no commit created; -m option ignored)");
1510 commit
= remoteheads
->item
;
1516 if (checkout_fast_forward(the_repository
,
1517 &head_commit
->object
.oid
,
1518 &commit
->object
.oid
,
1519 overwrite_ignore
)) {
1524 finish(head_commit
, remoteheads
, &commit
->object
.oid
, msg
.buf
);
1525 remove_merge_branch_state(the_repository
);
1527 } else if (!remoteheads
->next
&& common
->next
)
1530 * We are not doing octopus and not fast-forward. Need
1533 else if (!remoteheads
->next
&& !common
->next
&& option_commit
) {
1535 * We are not doing octopus, not fast-forward, and have
1538 refresh_cache(REFRESH_QUIET
);
1539 if (allow_trivial
&& fast_forward
!= FF_ONLY
) {
1540 /* See if it is really trivial. */
1541 git_committer_info(IDENT_STRICT
);
1542 printf(_("Trying really trivial in-index merge...\n"));
1543 if (!read_tree_trivial(&common
->item
->object
.oid
,
1544 &head_commit
->object
.oid
,
1545 &remoteheads
->item
->object
.oid
)) {
1546 ret
= merge_trivial(head_commit
, remoteheads
);
1549 printf(_("Nope.\n"));
1553 * An octopus. If we can reach all the remote we are up
1557 struct commit_list
*j
;
1559 for (j
= remoteheads
; j
; j
= j
->next
) {
1560 struct commit_list
*common_one
;
1563 * Here we *have* to calculate the individual
1564 * merge_bases again, otherwise "git merge HEAD^
1565 * HEAD^^" would be missed.
1567 common_one
= get_merge_bases(head_commit
, j
->item
);
1568 if (!oideq(&common_one
->item
->object
.oid
, &j
->item
->object
.oid
)) {
1574 finish_up_to_date(_("Already up to date. Yeeah!"));
1579 if (fast_forward
== FF_ONLY
)
1580 die(_("Not possible to fast-forward, aborting."));
1582 /* We are going to make a new commit. */
1583 git_committer_info(IDENT_STRICT
);
1586 * At this point, we need a real merge. No matter what strategy
1587 * we use, it would operate on the index, possibly affecting the
1588 * working tree, and when resolved cleanly, have the desired
1589 * tree in the index -- this means that the index must be in
1590 * sync with the head commit. The strategies are responsible
1593 if (use_strategies_nr
== 1 ||
1595 * Stash away the local changes so that we can try more than one.
1600 for (i
= 0; !merge_was_ok
&& i
< use_strategies_nr
; i
++) {
1603 printf(_("Rewinding the tree to pristine...\n"));
1604 restore_state(&head_commit
->object
.oid
, &stash
);
1606 if (use_strategies_nr
!= 1)
1607 printf(_("Trying merge strategy %s...\n"),
1608 use_strategies
[i
]->name
);
1610 * Remember which strategy left the state in the working
1613 wt_strategy
= use_strategies
[i
]->name
;
1615 ret
= try_merge_strategy(use_strategies
[i
]->name
,
1616 common
, remoteheads
,
1619 * The backend exits with 1 when conflicts are
1620 * left to be resolved, with 2 when it does not
1621 * handle the given merge at all.
1625 if (option_commit
) {
1626 /* Automerge succeeded. */
1627 automerge_was_ok
= 1;
1632 cnt
= evaluate_result();
1633 if (best_cnt
<= 0 || cnt
<= best_cnt
) {
1634 best_strategy
= use_strategies
[i
]->name
;
1641 * If we have a resulting tree, that means the strategy module
1642 * auto resolved the merge cleanly.
1644 if (automerge_was_ok
) {
1645 ret
= finish_automerge(head_commit
, head_subsumed
,
1646 common
, remoteheads
,
1647 &result_tree
, wt_strategy
);
1652 * Pick the result from the best strategy and have the user fix
1655 if (!best_strategy
) {
1656 restore_state(&head_commit
->object
.oid
, &stash
);
1657 if (use_strategies_nr
> 1)
1659 _("No merge strategy handled the merge.\n"));
1661 fprintf(stderr
, _("Merge with strategy %s failed.\n"),
1662 use_strategies
[0]->name
);
1665 } else if (best_strategy
== wt_strategy
)
1666 ; /* We already have its result in the working tree. */
1668 printf(_("Rewinding the tree to pristine...\n"));
1669 restore_state(&head_commit
->object
.oid
, &stash
);
1670 printf(_("Using the %s to prepare resolving by hand.\n"),
1672 try_merge_strategy(best_strategy
, common
, remoteheads
,
1677 finish(head_commit
, remoteheads
, NULL
, NULL
);
1679 write_merge_state(remoteheads
);
1682 fprintf(stderr
, _("Automatic merge went well; "
1683 "stopped before committing as requested\n"));
1685 ret
= suggest_conflicts();
1688 free(branch_to_free
);