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"
43 #include "commit-graph.h"
45 #define DEFAULT_TWOHEAD (1<<0)
46 #define DEFAULT_OCTOPUS (1<<1)
47 #define NO_FAST_FORWARD (1<<2)
48 #define NO_TRIVIAL (1<<3)
55 static const char * const builtin_merge_usage
[] = {
56 N_("git merge [<options>] [<commit>...]"),
57 N_("git merge --abort"),
58 N_("git merge --continue"),
62 static int show_diffstat
= 1, shortlog_len
= -1, squash
;
63 static int option_commit
= -1;
64 static int option_edit
= -1;
65 static int allow_trivial
= 1, have_message
, verify_signatures
;
66 static int check_trust_level
= 1;
67 static int overwrite_ignore
= 1;
68 static struct strbuf merge_msg
= STRBUF_INIT
;
69 static struct strategy
**use_strategies
;
70 static size_t use_strategies_nr
, use_strategies_alloc
;
71 static const char **xopts
;
72 static size_t xopts_nr
, xopts_alloc
;
73 static const char *branch
;
74 static char *branch_mergeoptions
;
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
;
88 static struct strategy all_strategy
[] = {
89 { "recursive", DEFAULT_TWOHEAD
| NO_TRIVIAL
},
90 { "octopus", DEFAULT_OCTOPUS
},
92 { "ours", NO_FAST_FORWARD
| NO_TRIVIAL
},
93 { "subtree", NO_FAST_FORWARD
| NO_TRIVIAL
},
96 static const char *pull_twohead
, *pull_octopus
;
104 static enum ff_type fast_forward
= FF_ALLOW
;
106 static const char *cleanup_arg
;
107 static enum commit_msg_cleanup_mode cleanup_mode
;
109 static int option_parse_message(const struct option
*opt
,
110 const char *arg
, int unset
)
112 struct strbuf
*buf
= opt
->value
;
115 strbuf_setlen(buf
, 0);
117 strbuf_addf(buf
, "%s%s", buf
->len
? "\n\n" : "", arg
);
120 return error(_("switch `m' requires a value"));
124 static enum parse_opt_result
option_read_message(struct parse_opt_ctx_t
*ctx
,
125 const struct option
*opt
,
126 const char *arg_not_used
,
129 struct strbuf
*buf
= opt
->value
;
132 BUG_ON_OPT_ARG(arg_not_used
);
134 BUG("-F cannot be negated");
139 } else if (ctx
->argc
> 1) {
143 return error(_("option `%s' requires a value"), opt
->long_name
);
146 strbuf_addch(buf
, '\n');
147 if (ctx
->prefix
&& !is_absolute_path(arg
))
148 arg
= prefix_filename(ctx
->prefix
, arg
);
149 if (strbuf_read_file(buf
, arg
, 0) < 0)
150 return error(_("could not read file '%s'"), arg
);
156 static struct strategy
*get_strategy(const char *name
)
159 struct strategy
*ret
;
160 static struct cmdnames main_cmds
, other_cmds
;
166 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
167 if (!strcmp(name
, all_strategy
[i
].name
))
168 return &all_strategy
[i
];
171 struct cmdnames not_strategies
;
174 memset(¬_strategies
, 0, sizeof(struct cmdnames
));
175 load_command_list("git-merge-", &main_cmds
, &other_cmds
);
176 for (i
= 0; i
< main_cmds
.cnt
; i
++) {
178 struct cmdname
*ent
= main_cmds
.names
[i
];
179 for (j
= 0; j
< ARRAY_SIZE(all_strategy
); j
++)
180 if (!strncmp(ent
->name
, all_strategy
[j
].name
, ent
->len
)
181 && !all_strategy
[j
].name
[ent
->len
])
184 add_cmdname(¬_strategies
, ent
->name
, ent
->len
);
186 exclude_cmds(&main_cmds
, ¬_strategies
);
188 if (!is_in_cmdlist(&main_cmds
, name
) && !is_in_cmdlist(&other_cmds
, name
)) {
189 fprintf(stderr
, _("Could not find merge strategy '%s'.\n"), name
);
190 fprintf(stderr
, _("Available strategies are:"));
191 for (i
= 0; i
< main_cmds
.cnt
; i
++)
192 fprintf(stderr
, " %s", main_cmds
.names
[i
]->name
);
193 fprintf(stderr
, ".\n");
194 if (other_cmds
.cnt
) {
195 fprintf(stderr
, _("Available custom strategies are:"));
196 for (i
= 0; i
< other_cmds
.cnt
; i
++)
197 fprintf(stderr
, " %s", other_cmds
.names
[i
]->name
);
198 fprintf(stderr
, ".\n");
203 ret
= xcalloc(1, sizeof(struct strategy
));
204 ret
->name
= xstrdup(name
);
205 ret
->attr
= NO_TRIVIAL
;
209 static void append_strategy(struct strategy
*s
)
211 ALLOC_GROW(use_strategies
, use_strategies_nr
+ 1, use_strategies_alloc
);
212 use_strategies
[use_strategies_nr
++] = s
;
215 static int option_parse_strategy(const struct option
*opt
,
216 const char *name
, int unset
)
221 append_strategy(get_strategy(name
));
225 static int option_parse_x(const struct option
*opt
,
226 const char *arg
, int unset
)
231 ALLOC_GROW(xopts
, xopts_nr
+ 1, xopts_alloc
);
232 xopts
[xopts_nr
++] = xstrdup(arg
);
236 static int option_parse_n(const struct option
*opt
,
237 const char *arg
, int unset
)
240 show_diffstat
= unset
;
244 static struct option builtin_merge_options
[] = {
245 OPT_CALLBACK_F('n', NULL
, NULL
, NULL
,
246 N_("do not show a diffstat at the end of the merge"),
247 PARSE_OPT_NOARG
, option_parse_n
),
248 OPT_BOOL(0, "stat", &show_diffstat
,
249 N_("show a diffstat at the end of the merge")),
250 OPT_BOOL(0, "summary", &show_diffstat
, N_("(synonym to --stat)")),
251 { OPTION_INTEGER
, 0, "log", &shortlog_len
, N_("n"),
252 N_("add (at most <n>) entries from shortlog to merge commit message"),
253 PARSE_OPT_OPTARG
, NULL
, DEFAULT_MERGE_LOG_LEN
},
254 OPT_BOOL(0, "squash", &squash
,
255 N_("create a single commit instead of doing a merge")),
256 OPT_BOOL(0, "commit", &option_commit
,
257 N_("perform a commit if the merge succeeds (default)")),
258 OPT_BOOL('e', "edit", &option_edit
,
259 N_("edit message before committing")),
260 OPT_CLEANUP(&cleanup_arg
),
261 OPT_SET_INT(0, "ff", &fast_forward
, N_("allow fast-forward (default)"), FF_ALLOW
),
262 OPT_SET_INT_F(0, "ff-only", &fast_forward
,
263 N_("abort if fast-forward is not possible"),
264 FF_ONLY
, PARSE_OPT_NONEG
),
265 OPT_RERERE_AUTOUPDATE(&allow_rerere_auto
),
266 OPT_BOOL(0, "verify-signatures", &verify_signatures
,
267 N_("verify that the named commit has a valid GPG signature")),
268 OPT_CALLBACK('s', "strategy", &use_strategies
, N_("strategy"),
269 N_("merge strategy to use"), option_parse_strategy
),
270 OPT_CALLBACK('X', "strategy-option", &xopts
, N_("option=value"),
271 N_("option for selected merge strategy"), option_parse_x
),
272 OPT_CALLBACK('m', "message", &merge_msg
, N_("message"),
273 N_("merge commit message (for a non-fast-forward merge)"),
274 option_parse_message
),
275 { OPTION_LOWLEVEL_CALLBACK
, 'F', "file", &merge_msg
, N_("path"),
276 N_("read message from file"), PARSE_OPT_NONEG
,
277 NULL
, 0, option_read_message
},
278 OPT__VERBOSITY(&verbosity
),
279 OPT_BOOL(0, "abort", &abort_current_merge
,
280 N_("abort the current in-progress merge")),
281 OPT_BOOL(0, "quit", &quit_current_merge
,
282 N_("--abort but leave index and working tree alone")),
283 OPT_BOOL(0, "continue", &continue_current_merge
,
284 N_("continue the current in-progress merge")),
285 OPT_BOOL(0, "allow-unrelated-histories", &allow_unrelated_histories
,
286 N_("allow merging unrelated histories")),
287 OPT_SET_INT(0, "progress", &show_progress
, N_("force progress reporting"), 1),
288 { OPTION_STRING
, 'S', "gpg-sign", &sign_commit
, N_("key-id"),
289 N_("GPG sign commit"), PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
290 OPT_AUTOSTASH(&autostash
),
291 OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore
, N_("update ignored files (default)")),
292 OPT_BOOL(0, "signoff", &signoff
, N_("add Signed-off-by:")),
293 OPT_BOOL(0, "no-verify", &no_verify
, N_("bypass pre-merge-commit and commit-msg hooks")),
297 static int save_state(struct object_id
*stash
)
300 struct child_process cp
= CHILD_PROCESS_INIT
;
301 struct strbuf buffer
= STRBUF_INIT
;
302 const char *argv
[] = {"stash", "create", NULL
};
309 if (start_command(&cp
))
310 die(_("could not run stash."));
311 len
= strbuf_read(&buffer
, cp
.out
, 1024);
314 if (finish_command(&cp
) || len
< 0)
315 die(_("stash failed"));
316 else if (!len
) /* no changes */
318 strbuf_setlen(&buffer
, buffer
.len
-1);
319 if (get_oid(buffer
.buf
, stash
))
320 die(_("not a valid object: %s"), buffer
.buf
);
323 strbuf_release(&buffer
);
327 static void read_empty(const struct object_id
*oid
, int verbose
)
332 args
[i
++] = "read-tree";
337 args
[i
++] = empty_tree_oid_hex();
338 args
[i
++] = oid_to_hex(oid
);
341 if (run_command_v_opt(args
, RUN_GIT_CMD
))
342 die(_("read-tree failed"));
345 static void reset_hard(const struct object_id
*oid
, int verbose
)
350 args
[i
++] = "read-tree";
353 args
[i
++] = "--reset";
355 args
[i
++] = oid_to_hex(oid
);
358 if (run_command_v_opt(args
, RUN_GIT_CMD
))
359 die(_("read-tree failed"));
362 static void restore_state(const struct object_id
*head
,
363 const struct object_id
*stash
)
365 struct strbuf sb
= STRBUF_INIT
;
366 const char *args
[] = { "stash", "apply", NULL
, NULL
};
368 if (is_null_oid(stash
))
373 args
[2] = oid_to_hex(stash
);
376 * It is OK to ignore error here, for example when there was
377 * nothing to restore.
379 run_command_v_opt(args
, RUN_GIT_CMD
);
382 refresh_cache(REFRESH_QUIET
);
385 /* This is called when no merge was necessary. */
386 static void finish_up_to_date(const char *msg
)
389 printf("%s%s\n", squash
? _(" (nothing to squash)") : "", msg
);
390 remove_merge_branch_state(the_repository
);
393 static void squash_message(struct commit
*commit
, struct commit_list
*remoteheads
)
396 struct strbuf out
= STRBUF_INIT
;
397 struct commit_list
*j
;
398 struct pretty_print_context ctx
= {0};
400 printf(_("Squash commit -- not updating HEAD\n"));
402 repo_init_revisions(the_repository
, &rev
, NULL
);
403 rev
.ignore_merges
= 1;
404 rev
.commit_format
= CMIT_FMT_MEDIUM
;
406 commit
->object
.flags
|= UNINTERESTING
;
407 add_pending_object(&rev
, &commit
->object
, NULL
);
409 for (j
= remoteheads
; j
; j
= j
->next
)
410 add_pending_object(&rev
, &j
->item
->object
, NULL
);
412 setup_revisions(0, NULL
, &rev
, NULL
);
413 if (prepare_revision_walk(&rev
))
414 die(_("revision walk setup failed"));
416 ctx
.abbrev
= rev
.abbrev
;
417 ctx
.date_mode
= rev
.date_mode
;
418 ctx
.fmt
= rev
.commit_format
;
420 strbuf_addstr(&out
, "Squashed commit of the following:\n");
421 while ((commit
= get_revision(&rev
)) != NULL
) {
422 strbuf_addch(&out
, '\n');
423 strbuf_addf(&out
, "commit %s\n",
424 oid_to_hex(&commit
->object
.oid
));
425 pretty_print_commit(&ctx
, commit
, &out
);
427 write_file_buf(git_path_squash_msg(the_repository
), out
.buf
, out
.len
);
428 strbuf_release(&out
);
431 static void finish(struct commit
*head_commit
,
432 struct commit_list
*remoteheads
,
433 const struct object_id
*new_head
, const char *msg
)
435 struct strbuf reflog_message
= STRBUF_INIT
;
436 const struct object_id
*head
= &head_commit
->object
.oid
;
439 strbuf_addstr(&reflog_message
, getenv("GIT_REFLOG_ACTION"));
443 strbuf_addf(&reflog_message
, "%s: %s",
444 getenv("GIT_REFLOG_ACTION"), msg
);
447 squash_message(head_commit
, remoteheads
);
449 if (verbosity
>= 0 && !merge_msg
.len
)
450 printf(_("No merge message -- not updating HEAD\n"));
452 update_ref(reflog_message
.buf
, "HEAD", new_head
, head
,
453 0, UPDATE_REFS_DIE_ON_ERR
);
455 * We ignore errors in 'gc --auto', since the
456 * user should see them.
458 close_object_store(the_repository
->objects
);
459 run_auto_gc(verbosity
< 0);
462 if (new_head
&& show_diffstat
) {
463 struct diff_options opts
;
464 repo_diff_setup(the_repository
, &opts
);
465 opts
.stat_width
= -1; /* use full terminal width */
466 opts
.stat_graph_width
= -1; /* respect statGraphWidth config */
467 opts
.output_format
|=
468 DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
469 opts
.detect_rename
= DIFF_DETECT_RENAME
;
470 diff_setup_done(&opts
);
471 diff_tree_oid(head
, new_head
, "", &opts
);
476 /* Run a post-merge hook */
477 run_hook_le(NULL
, "post-merge", squash
? "1" : "0", NULL
);
479 apply_autostash(git_path_merge_autostash(the_repository
));
480 strbuf_release(&reflog_message
);
483 /* Get the name for the merge commit's message. */
484 static void merge_name(const char *remote
, struct strbuf
*msg
)
486 struct commit
*remote_head
;
487 struct object_id branch_head
;
488 struct strbuf buf
= STRBUF_INIT
;
489 struct strbuf bname
= STRBUF_INIT
;
490 struct merge_remote_desc
*desc
;
495 strbuf_branchname(&bname
, remote
, 0);
498 oidclr(&branch_head
);
499 remote_head
= get_merge_parent(remote
);
501 die(_("'%s' does not point to a commit"), remote
);
503 if (dwim_ref(remote
, strlen(remote
), &branch_head
, &found_ref
) > 0) {
504 if (starts_with(found_ref
, "refs/heads/")) {
505 strbuf_addf(msg
, "%s\t\tbranch '%s' of .\n",
506 oid_to_hex(&branch_head
), remote
);
509 if (starts_with(found_ref
, "refs/tags/")) {
510 strbuf_addf(msg
, "%s\t\ttag '%s' of .\n",
511 oid_to_hex(&branch_head
), remote
);
514 if (starts_with(found_ref
, "refs/remotes/")) {
515 strbuf_addf(msg
, "%s\t\tremote-tracking branch '%s' of .\n",
516 oid_to_hex(&branch_head
), remote
);
521 /* See if remote matches <name>^^^.. or <name>~<number> */
522 for (len
= 0, ptr
= remote
+ strlen(remote
);
523 remote
< ptr
&& ptr
[-1] == '^';
530 ptr
= strrchr(remote
, '~');
532 int seen_nonzero
= 0;
535 while (*++ptr
&& isdigit(*ptr
)) {
536 seen_nonzero
|= (*ptr
!= '0');
540 len
= 0; /* not ...~<number> */
541 else if (seen_nonzero
)
544 early
= 1; /* "name~" is "name~1"! */
548 struct strbuf truname
= STRBUF_INIT
;
549 strbuf_addf(&truname
, "refs/heads/%s", remote
);
550 strbuf_setlen(&truname
, truname
.len
- len
);
551 if (ref_exists(truname
.buf
)) {
553 "%s\t\tbranch '%s'%s of .\n",
554 oid_to_hex(&remote_head
->object
.oid
),
556 (early
? " (early part)" : ""));
557 strbuf_release(&truname
);
560 strbuf_release(&truname
);
563 desc
= merge_remote_util(remote_head
);
564 if (desc
&& desc
->obj
&& desc
->obj
->type
== OBJ_TAG
) {
565 strbuf_addf(msg
, "%s\t\t%s '%s'\n",
566 oid_to_hex(&desc
->obj
->oid
),
567 type_name(desc
->obj
->type
),
572 strbuf_addf(msg
, "%s\t\tcommit '%s'\n",
573 oid_to_hex(&remote_head
->object
.oid
), remote
);
575 strbuf_release(&buf
);
576 strbuf_release(&bname
);
579 static void parse_branch_merge_options(char *bmo
)
586 argc
= split_cmdline(bmo
, &argv
);
588 die(_("Bad branch.%s.mergeoptions string: %s"), branch
,
589 _(split_cmdline_strerror(argc
)));
590 REALLOC_ARRAY(argv
, argc
+ 2);
591 MOVE_ARRAY(argv
+ 1, argv
, argc
+ 1);
593 argv
[0] = "branch.*.mergeoptions";
594 parse_options(argc
, argv
, NULL
, builtin_merge_options
,
595 builtin_merge_usage
, 0);
599 static int git_merge_config(const char *k
, const char *v
, void *cb
)
605 skip_prefix(k
, "branch.", &str
) &&
606 skip_prefix(str
, branch
, &str
) &&
607 !strcmp(str
, ".mergeoptions")) {
608 free(branch_mergeoptions
);
609 branch_mergeoptions
= xstrdup(v
);
613 if (!strcmp(k
, "merge.diffstat") || !strcmp(k
, "merge.stat"))
614 show_diffstat
= git_config_bool(k
, v
);
615 else if (!strcmp(k
, "merge.verifysignatures"))
616 verify_signatures
= git_config_bool(k
, v
);
617 else if (!strcmp(k
, "pull.twohead"))
618 return git_config_string(&pull_twohead
, k
, v
);
619 else if (!strcmp(k
, "pull.octopus"))
620 return git_config_string(&pull_octopus
, k
, v
);
621 else if (!strcmp(k
, "commit.cleanup"))
622 return git_config_string(&cleanup_arg
, k
, v
);
623 else if (!strcmp(k
, "merge.ff")) {
624 int boolval
= git_parse_maybe_bool(v
);
626 fast_forward
= boolval
? FF_ALLOW
: FF_NO
;
627 } else if (v
&& !strcmp(v
, "only")) {
628 fast_forward
= FF_ONLY
;
629 } /* do not barf on values from future versions of git */
631 } else if (!strcmp(k
, "merge.defaulttoupstream")) {
632 default_to_upstream
= git_config_bool(k
, v
);
634 } else if (!strcmp(k
, "commit.gpgsign")) {
635 sign_commit
= git_config_bool(k
, v
) ? "" : NULL
;
637 } else if (!strcmp(k
, "gpg.mintrustlevel")) {
638 check_trust_level
= 0;
639 } else if (!strcmp(k
, "merge.autostash")) {
640 autostash
= git_config_bool(k
, v
);
644 status
= fmt_merge_msg_config(k
, v
, cb
);
647 status
= git_gpg_config(k
, v
, NULL
);
650 return git_diff_ui_config(k
, v
, cb
);
653 static int read_tree_trivial(struct object_id
*common
, struct object_id
*head
,
654 struct object_id
*one
)
657 struct tree
*trees
[MAX_UNPACK_TREES
];
658 struct tree_desc t
[MAX_UNPACK_TREES
];
659 struct unpack_trees_options opts
;
661 memset(&opts
, 0, sizeof(opts
));
663 opts
.src_index
= &the_index
;
664 opts
.dst_index
= &the_index
;
666 opts
.verbose_update
= 1;
667 opts
.trivial_merges_only
= 1;
669 trees
[nr_trees
] = parse_tree_indirect(common
);
670 if (!trees
[nr_trees
++])
672 trees
[nr_trees
] = parse_tree_indirect(head
);
673 if (!trees
[nr_trees
++])
675 trees
[nr_trees
] = parse_tree_indirect(one
);
676 if (!trees
[nr_trees
++])
678 opts
.fn
= threeway_merge
;
679 cache_tree_free(&active_cache_tree
);
680 for (i
= 0; i
< nr_trees
; i
++) {
681 parse_tree(trees
[i
]);
682 init_tree_desc(t
+i
, trees
[i
]->buffer
, trees
[i
]->size
);
684 if (unpack_trees(nr_trees
, t
, &opts
))
689 static void write_tree_trivial(struct object_id
*oid
)
691 if (write_cache_as_tree(oid
, 0, NULL
))
692 die(_("git write-tree failed to write a tree"));
695 static int try_merge_strategy(const char *strategy
, struct commit_list
*common
,
696 struct commit_list
*remoteheads
,
699 const char *head_arg
= "HEAD";
701 if (refresh_and_write_cache(REFRESH_QUIET
, SKIP_IF_UNCHANGED
, 0) < 0)
702 return error(_("Unable to write index."));
704 if (!strcmp(strategy
, "recursive") || !strcmp(strategy
, "subtree")) {
705 struct lock_file lock
= LOCK_INIT
;
707 struct commit
*result
;
708 struct commit_list
*reversed
= NULL
;
709 struct merge_options o
;
710 struct commit_list
*j
;
712 if (remoteheads
->next
) {
713 error(_("Not handling anything other than two heads merge."));
717 init_merge_options(&o
, the_repository
);
718 if (!strcmp(strategy
, "subtree"))
719 o
.subtree_shift
= "";
721 o
.show_rename_progress
=
722 show_progress
== -1 ? isatty(2) : show_progress
;
724 for (x
= 0; x
< xopts_nr
; x
++)
725 if (parse_merge_opt(&o
, xopts
[x
]))
726 die(_("Unknown option for merge-recursive: -X%s"), xopts
[x
]);
728 o
.branch1
= head_arg
;
729 o
.branch2
= merge_remote_util(remoteheads
->item
)->name
;
731 for (j
= common
; j
; j
= j
->next
)
732 commit_list_insert(j
->item
, &reversed
);
734 hold_locked_index(&lock
, LOCK_DIE_ON_ERROR
);
735 clean
= merge_recursive(&o
, head
,
736 remoteheads
->item
, reversed
, &result
);
739 if (write_locked_index(&the_index
, &lock
,
740 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
741 die(_("unable to write %s"), get_index_file());
742 return clean
? 0 : 1;
744 return try_merge_command(the_repository
,
745 strategy
, xopts_nr
, xopts
,
746 common
, head_arg
, remoteheads
);
750 static void count_diff_files(struct diff_queue_struct
*q
,
751 struct diff_options
*opt
, void *data
)
758 static int count_unmerged_entries(void)
762 for (i
= 0; i
< active_nr
; i
++)
763 if (ce_stage(active_cache
[i
]))
769 static void add_strategies(const char *string
, unsigned attr
)
774 struct string_list list
= STRING_LIST_INIT_DUP
;
775 struct string_list_item
*item
;
776 string_list_split(&list
, string
, ' ', -1);
777 for_each_string_list_item(item
, &list
)
778 append_strategy(get_strategy(item
->string
));
779 string_list_clear(&list
, 0);
782 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
783 if (all_strategy
[i
].attr
& attr
)
784 append_strategy(&all_strategy
[i
]);
788 static void read_merge_msg(struct strbuf
*msg
)
790 const char *filename
= git_path_merge_msg(the_repository
);
792 if (strbuf_read_file(msg
, filename
, 0) < 0)
793 die_errno(_("Could not read from '%s'"), filename
);
796 static void write_merge_state(struct commit_list
*);
797 static void abort_commit(struct commit_list
*remoteheads
, const char *err_msg
)
800 error("%s", err_msg
);
802 _("Not committing merge; use 'git commit' to complete the merge.\n"));
803 write_merge_state(remoteheads
);
807 static const char merge_editor_comment
[] =
808 N_("Please enter a commit message to explain why this merge is necessary,\n"
809 "especially if it merges an updated upstream into a topic branch.\n"
812 static const char scissors_editor_comment
[] =
813 N_("An empty message aborts the commit.\n");
815 static const char no_scissors_editor_comment
[] =
816 N_("Lines starting with '%c' will be ignored, and an empty message aborts\n"
819 static void write_merge_heads(struct commit_list
*);
820 static void prepare_to_commit(struct commit_list
*remoteheads
)
822 struct strbuf msg
= STRBUF_INIT
;
823 const char *index_file
= get_index_file();
825 if (!no_verify
&& run_commit_hook(0 < option_edit
, index_file
, "pre-merge-commit", NULL
))
826 abort_commit(remoteheads
, NULL
);
828 * Re-read the index as pre-merge-commit hook could have updated it,
829 * and write it out as a tree. We must do this before we invoke
830 * the editor and after we invoke run_status above.
832 if (find_hook("pre-merge-commit"))
834 read_cache_from(index_file
);
835 strbuf_addbuf(&msg
, &merge_msg
);
837 BUG("the control must not reach here under --squash");
838 if (0 < option_edit
) {
839 strbuf_addch(&msg
, '\n');
840 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
) {
841 wt_status_append_cut_line(&msg
);
842 strbuf_commented_addf(&msg
, "\n");
844 strbuf_commented_addf(&msg
, _(merge_editor_comment
));
845 strbuf_commented_addf(&msg
, _(cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
?
846 scissors_editor_comment
:
847 no_scissors_editor_comment
), comment_line_char
);
850 append_signoff(&msg
, ignore_non_trailer(msg
.buf
, msg
.len
), 0);
851 write_merge_heads(remoteheads
);
852 write_file_buf(git_path_merge_msg(the_repository
), msg
.buf
, msg
.len
);
853 if (run_commit_hook(0 < option_edit
, get_index_file(), "prepare-commit-msg",
854 git_path_merge_msg(the_repository
), "merge", NULL
))
855 abort_commit(remoteheads
, NULL
);
856 if (0 < option_edit
) {
857 if (launch_editor(git_path_merge_msg(the_repository
), NULL
, NULL
))
858 abort_commit(remoteheads
, NULL
);
861 if (!no_verify
&& run_commit_hook(0 < option_edit
, get_index_file(),
863 git_path_merge_msg(the_repository
), NULL
))
864 abort_commit(remoteheads
, NULL
);
866 read_merge_msg(&msg
);
867 cleanup_message(&msg
, cleanup_mode
, 0);
869 abort_commit(remoteheads
, _("Empty commit message."));
870 strbuf_release(&merge_msg
);
871 strbuf_addbuf(&merge_msg
, &msg
);
872 strbuf_release(&msg
);
875 static int merge_trivial(struct commit
*head
, struct commit_list
*remoteheads
)
877 struct object_id result_tree
, result_commit
;
878 struct commit_list
*parents
, **pptr
= &parents
;
880 if (refresh_and_write_cache(REFRESH_QUIET
, SKIP_IF_UNCHANGED
, 0) < 0)
881 return error(_("Unable to write index."));
883 write_tree_trivial(&result_tree
);
884 printf(_("Wonderful.\n"));
885 pptr
= commit_list_append(head
, pptr
);
886 pptr
= commit_list_append(remoteheads
->item
, pptr
);
887 prepare_to_commit(remoteheads
);
888 if (commit_tree(merge_msg
.buf
, merge_msg
.len
, &result_tree
, parents
,
889 &result_commit
, NULL
, sign_commit
))
890 die(_("failed to write commit object"));
891 finish(head
, remoteheads
, &result_commit
, "In-index merge");
892 remove_merge_branch_state(the_repository
);
896 static int finish_automerge(struct commit
*head
,
898 struct commit_list
*common
,
899 struct commit_list
*remoteheads
,
900 struct object_id
*result_tree
,
901 const char *wt_strategy
)
903 struct commit_list
*parents
= NULL
;
904 struct strbuf buf
= STRBUF_INIT
;
905 struct object_id result_commit
;
907 write_tree_trivial(result_tree
);
908 free_commit_list(common
);
909 parents
= remoteheads
;
910 if (!head_subsumed
|| fast_forward
== FF_NO
)
911 commit_list_insert(head
, &parents
);
912 prepare_to_commit(remoteheads
);
913 if (commit_tree(merge_msg
.buf
, merge_msg
.len
, result_tree
, parents
,
914 &result_commit
, NULL
, sign_commit
))
915 die(_("failed to write commit object"));
916 strbuf_addf(&buf
, "Merge made by the '%s' strategy.", wt_strategy
);
917 finish(head
, remoteheads
, &result_commit
, buf
.buf
);
918 strbuf_release(&buf
);
919 remove_merge_branch_state(the_repository
);
923 static int suggest_conflicts(void)
925 const char *filename
;
927 struct strbuf msgbuf
= STRBUF_INIT
;
929 filename
= git_path_merge_msg(the_repository
);
930 fp
= xfopen(filename
, "a");
933 * We can't use cleanup_mode because if we're not using the editor,
934 * get_cleanup_mode will return COMMIT_MSG_CLEANUP_SPACE instead, even
935 * though the message is meant to be processed later by git-commit.
936 * Thus, we will get the cleanup mode which is returned when we _are_
939 append_conflicts_hint(&the_index
, &msgbuf
,
940 get_cleanup_mode(cleanup_arg
, 1));
941 fputs(msgbuf
.buf
, fp
);
942 strbuf_release(&msgbuf
);
944 repo_rerere(the_repository
, allow_rerere_auto
);
945 printf(_("Automatic merge failed; "
946 "fix conflicts and then commit the result.\n"));
950 static int evaluate_result(void)
955 /* Check how many files differ. */
956 repo_init_revisions(the_repository
, &rev
, "");
957 setup_revisions(0, NULL
, &rev
, NULL
);
958 rev
.diffopt
.output_format
|=
959 DIFF_FORMAT_CALLBACK
;
960 rev
.diffopt
.format_callback
= count_diff_files
;
961 rev
.diffopt
.format_callback_data
= &cnt
;
962 run_diff_files(&rev
, 0);
965 * Check how many unmerged entries are
968 cnt
+= count_unmerged_entries();
974 * Pretend as if the user told us to merge with the remote-tracking
975 * branch we have for the upstream of the current branch
977 static int setup_with_upstream(const char ***argv
)
979 struct branch
*branch
= branch_get(NULL
);
984 die(_("No current branch."));
985 if (!branch
->remote_name
)
986 die(_("No remote for the current branch."));
987 if (!branch
->merge_nr
)
988 die(_("No default upstream defined for the current branch."));
990 args
= xcalloc(st_add(branch
->merge_nr
, 1), sizeof(char *));
991 for (i
= 0; i
< branch
->merge_nr
; i
++) {
992 if (!branch
->merge
[i
]->dst
)
993 die(_("No remote-tracking branch for %s from %s"),
994 branch
->merge
[i
]->src
, branch
->remote_name
);
995 args
[i
] = branch
->merge
[i
]->dst
;
1002 static void write_merge_heads(struct commit_list
*remoteheads
)
1004 struct commit_list
*j
;
1005 struct strbuf buf
= STRBUF_INIT
;
1007 for (j
= remoteheads
; j
; j
= j
->next
) {
1008 struct object_id
*oid
;
1009 struct commit
*c
= j
->item
;
1010 struct merge_remote_desc
*desc
;
1012 desc
= merge_remote_util(c
);
1013 if (desc
&& desc
->obj
) {
1014 oid
= &desc
->obj
->oid
;
1016 oid
= &c
->object
.oid
;
1018 strbuf_addf(&buf
, "%s\n", oid_to_hex(oid
));
1020 write_file_buf(git_path_merge_head(the_repository
), buf
.buf
, buf
.len
);
1023 if (fast_forward
== FF_NO
)
1024 strbuf_addstr(&buf
, "no-ff");
1025 write_file_buf(git_path_merge_mode(the_repository
), buf
.buf
, buf
.len
);
1026 strbuf_release(&buf
);
1029 static void write_merge_state(struct commit_list
*remoteheads
)
1031 write_merge_heads(remoteheads
);
1032 strbuf_addch(&merge_msg
, '\n');
1033 write_file_buf(git_path_merge_msg(the_repository
), merge_msg
.buf
,
1037 static int default_edit_option(void)
1039 static const char name
[] = "GIT_MERGE_AUTOEDIT";
1040 const char *e
= getenv(name
);
1041 struct stat st_stdin
, st_stdout
;
1044 /* an explicit -m msg without --[no-]edit */
1048 int v
= git_parse_maybe_bool(e
);
1050 die(_("Bad value '%s' in environment '%s'"), e
, name
);
1054 /* Use editor if stdin and stdout are the same and is a tty */
1055 return (!fstat(0, &st_stdin
) &&
1056 !fstat(1, &st_stdout
) &&
1057 isatty(0) && isatty(1) &&
1058 st_stdin
.st_dev
== st_stdout
.st_dev
&&
1059 st_stdin
.st_ino
== st_stdout
.st_ino
&&
1060 st_stdin
.st_mode
== st_stdout
.st_mode
);
1063 static struct commit_list
*reduce_parents(struct commit
*head_commit
,
1065 struct commit_list
*remoteheads
)
1067 struct commit_list
*parents
, **remotes
;
1070 * Is the current HEAD reachable from another commit being
1071 * merged? If so we do not want to record it as a parent of
1072 * the resulting merge, unless --no-ff is given. We will flip
1073 * this variable to 0 when we find HEAD among the independent
1074 * tips being merged.
1078 /* Find what parents to record by checking independent ones. */
1079 parents
= reduce_heads(remoteheads
);
1080 free_commit_list(remoteheads
);
1083 remotes
= &remoteheads
;
1085 struct commit
*commit
= pop_commit(&parents
);
1086 if (commit
== head_commit
)
1089 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1094 static void prepare_merge_message(struct strbuf
*merge_names
, struct strbuf
*merge_msg
)
1096 struct fmt_merge_msg_opts opts
;
1098 memset(&opts
, 0, sizeof(opts
));
1099 opts
.add_title
= !have_message
;
1100 opts
.shortlog_len
= shortlog_len
;
1101 opts
.credit_people
= (0 < option_edit
);
1103 fmt_merge_msg(merge_names
, merge_msg
, &opts
);
1105 strbuf_setlen(merge_msg
, merge_msg
->len
- 1);
1108 static void handle_fetch_head(struct commit_list
**remotes
, struct strbuf
*merge_names
)
1110 const char *filename
;
1112 struct strbuf fetch_head_file
= STRBUF_INIT
;
1113 const unsigned hexsz
= the_hash_algo
->hexsz
;
1116 merge_names
= &fetch_head_file
;
1118 filename
= git_path_fetch_head(the_repository
);
1119 fd
= open(filename
, O_RDONLY
);
1121 die_errno(_("could not open '%s' for reading"), filename
);
1123 if (strbuf_read(merge_names
, fd
, 0) < 0)
1124 die_errno(_("could not read '%s'"), filename
);
1126 die_errno(_("could not close '%s'"), filename
);
1128 for (pos
= 0; pos
< merge_names
->len
; pos
= npos
) {
1129 struct object_id oid
;
1131 struct commit
*commit
;
1133 ptr
= strchr(merge_names
->buf
+ pos
, '\n');
1135 npos
= ptr
- merge_names
->buf
+ 1;
1137 npos
= merge_names
->len
;
1139 if (npos
- pos
< hexsz
+ 2 ||
1140 get_oid_hex(merge_names
->buf
+ pos
, &oid
))
1141 commit
= NULL
; /* bad */
1142 else if (memcmp(merge_names
->buf
+ pos
+ hexsz
, "\t\t", 2))
1143 continue; /* not-for-merge */
1145 char saved
= merge_names
->buf
[pos
+ hexsz
];
1146 merge_names
->buf
[pos
+ hexsz
] = '\0';
1147 commit
= get_merge_parent(merge_names
->buf
+ pos
);
1148 merge_names
->buf
[pos
+ hexsz
] = saved
;
1153 die(_("not something we can merge in %s: %s"),
1154 filename
, merge_names
->buf
+ pos
);
1156 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1159 if (merge_names
== &fetch_head_file
)
1160 strbuf_release(&fetch_head_file
);
1163 static struct commit_list
*collect_parents(struct commit
*head_commit
,
1165 int argc
, const char **argv
,
1166 struct strbuf
*merge_msg
)
1169 struct commit_list
*remoteheads
= NULL
;
1170 struct commit_list
**remotes
= &remoteheads
;
1171 struct strbuf merge_names
= STRBUF_INIT
, *autogen
= NULL
;
1173 if (merge_msg
&& (!have_message
|| shortlog_len
))
1174 autogen
= &merge_names
;
1177 remotes
= &commit_list_insert(head_commit
, remotes
)->next
;
1179 if (argc
== 1 && !strcmp(argv
[0], "FETCH_HEAD")) {
1180 handle_fetch_head(remotes
, autogen
);
1181 remoteheads
= reduce_parents(head_commit
, head_subsumed
, remoteheads
);
1183 for (i
= 0; i
< argc
; i
++) {
1184 struct commit
*commit
= get_merge_parent(argv
[i
]);
1186 help_unknown_ref(argv
[i
], "merge",
1187 _("not something we can merge"));
1188 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1190 remoteheads
= reduce_parents(head_commit
, head_subsumed
, remoteheads
);
1192 struct commit_list
*p
;
1193 for (p
= remoteheads
; p
; p
= p
->next
)
1194 merge_name(merge_remote_util(p
->item
)->name
, autogen
);
1199 prepare_merge_message(autogen
, merge_msg
);
1200 strbuf_release(autogen
);
1206 static int merging_a_throwaway_tag(struct commit
*commit
)
1209 struct object_id oid
;
1210 int is_throwaway_tag
= 0;
1212 /* Are we merging a tag? */
1213 if (!merge_remote_util(commit
) ||
1214 !merge_remote_util(commit
)->obj
||
1215 merge_remote_util(commit
)->obj
->type
!= OBJ_TAG
)
1216 return is_throwaway_tag
;
1219 * Now we know we are merging a tag object. Are we downstream
1220 * and following the tags from upstream? If so, we must have
1221 * the tag object pointed at by "refs/tags/$T" where $T is the
1222 * tagname recorded in the tag object. We want to allow such
1223 * a "just to catch up" merge to fast-forward.
1225 * Otherwise, we are playing an integrator's role, making a
1226 * merge with a throw-away tag from a contributor with
1227 * something like "git pull $contributor $signed_tag".
1228 * We want to forbid such a merge from fast-forwarding
1229 * by default; otherwise we would not keep the signature
1232 tag_ref
= xstrfmt("refs/tags/%s",
1233 ((struct tag
*)merge_remote_util(commit
)->obj
)->tag
);
1234 if (!read_ref(tag_ref
, &oid
) &&
1235 oideq(&oid
, &merge_remote_util(commit
)->obj
->oid
))
1236 is_throwaway_tag
= 0;
1238 is_throwaway_tag
= 1;
1240 return is_throwaway_tag
;
1243 int cmd_merge(int argc
, const char **argv
, const char *prefix
)
1245 struct object_id result_tree
, stash
, head_oid
;
1246 struct commit
*head_commit
;
1247 struct strbuf buf
= STRBUF_INIT
;
1248 int i
, ret
= 0, head_subsumed
;
1249 int best_cnt
= -1, merge_was_ok
= 0, automerge_was_ok
= 0;
1250 struct commit_list
*common
= NULL
;
1251 const char *best_strategy
= NULL
, *wt_strategy
= NULL
;
1252 struct commit_list
*remoteheads
, *p
;
1253 void *branch_to_free
;
1254 int orig_argc
= argc
;
1256 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1257 usage_with_options(builtin_merge_usage
, builtin_merge_options
);
1260 * Check if we are _not_ on a detached HEAD, i.e. if there is a
1263 branch
= branch_to_free
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
1265 skip_prefix(branch
, "refs/heads/", &branch
);
1267 init_diff_ui_defaults();
1268 git_config(git_merge_config
, NULL
);
1270 if (!branch
|| is_null_oid(&head_oid
))
1273 head_commit
= lookup_commit_or_die(&head_oid
, "HEAD");
1275 if (branch_mergeoptions
)
1276 parse_branch_merge_options(branch_mergeoptions
);
1277 argc
= parse_options(argc
, argv
, prefix
, builtin_merge_options
,
1278 builtin_merge_usage
, 0);
1279 if (shortlog_len
< 0)
1280 shortlog_len
= (merge_log_config
> 0) ? merge_log_config
: 0;
1282 if (verbosity
< 0 && show_progress
== -1)
1285 if (abort_current_merge
) {
1287 const char *nargv
[] = {"reset", "--merge", NULL
};
1288 struct strbuf stash_oid
= STRBUF_INIT
;
1291 usage_msg_opt(_("--abort expects no arguments"),
1292 builtin_merge_usage
, builtin_merge_options
);
1294 if (!file_exists(git_path_merge_head(the_repository
)))
1295 die(_("There is no merge to abort (MERGE_HEAD missing)."));
1297 if (read_oneliner(&stash_oid
, git_path_merge_autostash(the_repository
),
1298 READ_ONELINER_SKIP_IF_EMPTY
))
1299 unlink(git_path_merge_autostash(the_repository
));
1301 /* Invoke 'git reset --merge' */
1302 ret
= cmd_reset(nargc
, nargv
, prefix
);
1305 apply_autostash_oid(stash_oid
.buf
);
1307 strbuf_release(&stash_oid
);
1311 if (quit_current_merge
) {
1313 usage_msg_opt(_("--quit expects no arguments"),
1314 builtin_merge_usage
,
1315 builtin_merge_options
);
1317 remove_merge_branch_state(the_repository
);
1321 if (continue_current_merge
) {
1323 const char *nargv
[] = {"commit", NULL
};
1326 usage_msg_opt(_("--continue expects no arguments"),
1327 builtin_merge_usage
, builtin_merge_options
);
1329 if (!file_exists(git_path_merge_head(the_repository
)))
1330 die(_("There is no merge in progress (MERGE_HEAD missing)."));
1332 /* Invoke 'git commit' */
1333 ret
= cmd_commit(nargc
, nargv
, prefix
);
1337 if (read_cache_unmerged())
1338 die_resolve_conflict("merge");
1340 if (file_exists(git_path_merge_head(the_repository
))) {
1342 * There is no unmerged entry, don't advise 'git
1343 * add/rm <file>', just 'git commit'.
1345 if (advice_resolve_conflict
)
1346 die(_("You have not concluded your merge (MERGE_HEAD exists).\n"
1347 "Please, commit your changes before you merge."));
1349 die(_("You have not concluded your merge (MERGE_HEAD exists)."));
1351 if (file_exists(git_path_cherry_pick_head(the_repository
))) {
1352 if (advice_resolve_conflict
)
1353 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
1354 "Please, commit your changes before you merge."));
1356 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists)."));
1358 resolve_undo_clear();
1360 if (option_edit
< 0)
1361 option_edit
= default_edit_option();
1363 cleanup_mode
= get_cleanup_mode(cleanup_arg
, 0 < option_edit
);
1369 if (fast_forward
== FF_NO
)
1370 die(_("You cannot combine --squash with --no-ff."));
1371 if (option_commit
> 0)
1372 die(_("You cannot combine --squash with --commit."));
1374 * squash can now silently disable option_commit - this is not
1375 * a problem as it is only overriding the default, not a user
1381 if (option_commit
< 0)
1385 if (default_to_upstream
)
1386 argc
= setup_with_upstream(&argv
);
1388 die(_("No commit specified and merge.defaultToUpstream not set."));
1389 } else if (argc
== 1 && !strcmp(argv
[0], "-")) {
1394 usage_with_options(builtin_merge_usage
,
1395 builtin_merge_options
);
1399 * If the merged head is a valid one there is no reason
1400 * to forbid "git merge" into a branch yet to be born.
1401 * We do the same for "git pull".
1403 struct object_id
*remote_head_oid
;
1405 die(_("Squash commit into empty head not supported yet"));
1406 if (fast_forward
== FF_NO
)
1407 die(_("Non-fast-forward commit does not make sense into "
1409 remoteheads
= collect_parents(head_commit
, &head_subsumed
,
1412 die(_("%s - not something we can merge"), argv
[0]);
1413 if (remoteheads
->next
)
1414 die(_("Can merge only exactly one commit into empty head"));
1416 if (verify_signatures
)
1417 verify_merge_signature(remoteheads
->item
, verbosity
,
1420 remote_head_oid
= &remoteheads
->item
->object
.oid
;
1421 read_empty(remote_head_oid
, 0);
1422 update_ref("initial pull", "HEAD", remote_head_oid
, NULL
, 0,
1423 UPDATE_REFS_DIE_ON_ERR
);
1428 * All the rest are the commits being merged; prepare
1429 * the standard merge summary message to be appended
1430 * to the given message.
1432 remoteheads
= collect_parents(head_commit
, &head_subsumed
,
1433 argc
, argv
, &merge_msg
);
1435 if (!head_commit
|| !argc
)
1436 usage_with_options(builtin_merge_usage
,
1437 builtin_merge_options
);
1439 if (verify_signatures
) {
1440 for (p
= remoteheads
; p
; p
= p
->next
) {
1441 verify_merge_signature(p
->item
, verbosity
,
1446 strbuf_addstr(&buf
, "merge");
1447 for (p
= remoteheads
; p
; p
= p
->next
)
1448 strbuf_addf(&buf
, " %s", merge_remote_util(p
->item
)->name
);
1449 setenv("GIT_REFLOG_ACTION", buf
.buf
, 0);
1452 for (p
= remoteheads
; p
; p
= p
->next
) {
1453 struct commit
*commit
= p
->item
;
1454 strbuf_addf(&buf
, "GITHEAD_%s",
1455 oid_to_hex(&commit
->object
.oid
));
1456 setenv(buf
.buf
, merge_remote_util(commit
)->name
, 1);
1458 if (fast_forward
!= FF_ONLY
&& merging_a_throwaway_tag(commit
))
1459 fast_forward
= FF_NO
;
1462 if (!use_strategies
) {
1464 ; /* already up-to-date */
1465 else if (!remoteheads
->next
)
1466 add_strategies(pull_twohead
, DEFAULT_TWOHEAD
);
1468 add_strategies(pull_octopus
, DEFAULT_OCTOPUS
);
1471 for (i
= 0; i
< use_strategies_nr
; i
++) {
1472 if (use_strategies
[i
]->attr
& NO_FAST_FORWARD
)
1473 fast_forward
= FF_NO
;
1474 if (use_strategies
[i
]->attr
& NO_TRIVIAL
)
1479 ; /* already up-to-date */
1480 else if (!remoteheads
->next
)
1481 common
= get_merge_bases(head_commit
, remoteheads
->item
);
1483 struct commit_list
*list
= remoteheads
;
1484 commit_list_insert(head_commit
, &list
);
1485 common
= get_octopus_merge_bases(list
);
1489 update_ref("updating ORIG_HEAD", "ORIG_HEAD",
1490 &head_commit
->object
.oid
, NULL
, 0, UPDATE_REFS_DIE_ON_ERR
);
1492 if (remoteheads
&& !common
) {
1493 /* No common ancestors found. */
1494 if (!allow_unrelated_histories
)
1495 die(_("refusing to merge unrelated histories"));
1496 /* otherwise, we need a real merge. */
1497 } else if (!remoteheads
||
1498 (!remoteheads
->next
&& !common
->next
&&
1499 common
->item
== remoteheads
->item
)) {
1501 * If head can reach all the merge then we are up to date.
1502 * but first the most common case of merging one remote.
1504 finish_up_to_date(_("Already up to date."));
1506 } else if (fast_forward
!= FF_NO
&& !remoteheads
->next
&&
1508 oideq(&common
->item
->object
.oid
, &head_commit
->object
.oid
)) {
1509 /* Again the most common case of merging one remote. */
1510 struct strbuf msg
= STRBUF_INIT
;
1511 struct commit
*commit
;
1513 if (verbosity
>= 0) {
1514 printf(_("Updating %s..%s\n"),
1515 find_unique_abbrev(&head_commit
->object
.oid
,
1517 find_unique_abbrev(&remoteheads
->item
->object
.oid
,
1520 strbuf_addstr(&msg
, "Fast-forward");
1523 " (no commit created; -m option ignored)");
1524 commit
= remoteheads
->item
;
1531 create_autostash(the_repository
,
1532 git_path_merge_autostash(the_repository
),
1534 if (checkout_fast_forward(the_repository
,
1535 &head_commit
->object
.oid
,
1536 &commit
->object
.oid
,
1537 overwrite_ignore
)) {
1542 finish(head_commit
, remoteheads
, &commit
->object
.oid
, msg
.buf
);
1543 remove_merge_branch_state(the_repository
);
1545 } else if (!remoteheads
->next
&& common
->next
)
1548 * We are not doing octopus and not fast-forward. Need
1551 else if (!remoteheads
->next
&& !common
->next
&& option_commit
) {
1553 * We are not doing octopus, not fast-forward, and have
1556 refresh_cache(REFRESH_QUIET
);
1557 if (allow_trivial
&& fast_forward
!= FF_ONLY
) {
1558 /* See if it is really trivial. */
1559 git_committer_info(IDENT_STRICT
);
1560 printf(_("Trying really trivial in-index merge...\n"));
1561 if (!read_tree_trivial(&common
->item
->object
.oid
,
1562 &head_commit
->object
.oid
,
1563 &remoteheads
->item
->object
.oid
)) {
1564 ret
= merge_trivial(head_commit
, remoteheads
);
1567 printf(_("Nope.\n"));
1571 * An octopus. If we can reach all the remote we are up
1575 struct commit_list
*j
;
1577 for (j
= remoteheads
; j
; j
= j
->next
) {
1578 struct commit_list
*common_one
;
1581 * Here we *have* to calculate the individual
1582 * merge_bases again, otherwise "git merge HEAD^
1583 * HEAD^^" would be missed.
1585 common_one
= get_merge_bases(head_commit
, j
->item
);
1586 if (!oideq(&common_one
->item
->object
.oid
, &j
->item
->object
.oid
)) {
1592 finish_up_to_date(_("Already up to date. Yeeah!"));
1597 if (fast_forward
== FF_ONLY
)
1598 die(_("Not possible to fast-forward, aborting."));
1601 create_autostash(the_repository
,
1602 git_path_merge_autostash(the_repository
),
1605 /* We are going to make a new commit. */
1606 git_committer_info(IDENT_STRICT
);
1609 * At this point, we need a real merge. No matter what strategy
1610 * we use, it would operate on the index, possibly affecting the
1611 * working tree, and when resolved cleanly, have the desired
1612 * tree in the index -- this means that the index must be in
1613 * sync with the head commit. The strategies are responsible
1616 if (use_strategies_nr
== 1 ||
1618 * Stash away the local changes so that we can try more than one.
1623 for (i
= 0; !merge_was_ok
&& i
< use_strategies_nr
; i
++) {
1626 printf(_("Rewinding the tree to pristine...\n"));
1627 restore_state(&head_commit
->object
.oid
, &stash
);
1629 if (use_strategies_nr
!= 1)
1630 printf(_("Trying merge strategy %s...\n"),
1631 use_strategies
[i
]->name
);
1633 * Remember which strategy left the state in the working
1636 wt_strategy
= use_strategies
[i
]->name
;
1638 ret
= try_merge_strategy(use_strategies
[i
]->name
,
1639 common
, remoteheads
,
1642 * The backend exits with 1 when conflicts are
1643 * left to be resolved, with 2 when it does not
1644 * handle the given merge at all.
1648 if (option_commit
) {
1649 /* Automerge succeeded. */
1650 automerge_was_ok
= 1;
1655 cnt
= (use_strategies_nr
> 1) ? evaluate_result() : 0;
1656 if (best_cnt
<= 0 || cnt
<= best_cnt
) {
1657 best_strategy
= use_strategies
[i
]->name
;
1664 * If we have a resulting tree, that means the strategy module
1665 * auto resolved the merge cleanly.
1667 if (automerge_was_ok
) {
1668 ret
= finish_automerge(head_commit
, head_subsumed
,
1669 common
, remoteheads
,
1670 &result_tree
, wt_strategy
);
1675 * Pick the result from the best strategy and have the user fix
1678 if (!best_strategy
) {
1679 restore_state(&head_commit
->object
.oid
, &stash
);
1680 if (use_strategies_nr
> 1)
1682 _("No merge strategy handled the merge.\n"));
1684 fprintf(stderr
, _("Merge with strategy %s failed.\n"),
1685 use_strategies
[0]->name
);
1688 } else if (best_strategy
== wt_strategy
)
1689 ; /* We already have its result in the working tree. */
1691 printf(_("Rewinding the tree to pristine...\n"));
1692 restore_state(&head_commit
->object
.oid
, &stash
);
1693 printf(_("Using the %s to prepare resolving by hand.\n"),
1695 try_merge_strategy(best_strategy
, common
, remoteheads
,
1700 finish(head_commit
, remoteheads
, NULL
, NULL
);
1702 git_test_write_commit_graph_or_die();
1704 write_merge_state(remoteheads
);
1707 fprintf(stderr
, _("Automatic merge went well; "
1708 "stopped before committing as requested\n"));
1710 ret
= suggest_conflicts();
1713 free(branch_to_free
);