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 overwrite_ignore
= 1;
66 static struct strbuf merge_msg
= STRBUF_INIT
;
67 static struct strategy
**use_strategies
;
68 static size_t use_strategies_nr
, use_strategies_alloc
;
69 static const char **xopts
;
70 static size_t xopts_nr
, xopts_alloc
;
71 static const char *branch
;
72 static char *branch_mergeoptions
;
73 static int option_renormalize
;
75 static int allow_rerere_auto
;
76 static int abort_current_merge
;
77 static int quit_current_merge
;
78 static int continue_current_merge
;
79 static int allow_unrelated_histories
;
80 static int show_progress
= -1;
81 static int default_to_upstream
= 1;
83 static const char *sign_commit
;
84 static int verify_msg
= 1;
86 static struct strategy all_strategy
[] = {
87 { "recursive", DEFAULT_TWOHEAD
| NO_TRIVIAL
},
88 { "octopus", DEFAULT_OCTOPUS
},
90 { "ours", NO_FAST_FORWARD
| NO_TRIVIAL
},
91 { "subtree", NO_FAST_FORWARD
| NO_TRIVIAL
},
94 static const char *pull_twohead
, *pull_octopus
;
102 static enum ff_type fast_forward
= FF_ALLOW
;
104 static const char *cleanup_arg
;
105 static enum commit_msg_cleanup_mode cleanup_mode
;
107 static int option_parse_message(const struct option
*opt
,
108 const char *arg
, int unset
)
110 struct strbuf
*buf
= opt
->value
;
113 strbuf_setlen(buf
, 0);
115 strbuf_addf(buf
, "%s%s", buf
->len
? "\n\n" : "", arg
);
118 return error(_("switch `m' requires a value"));
122 static enum parse_opt_result
option_read_message(struct parse_opt_ctx_t
*ctx
,
123 const struct option
*opt
,
124 const char *arg_not_used
,
127 struct strbuf
*buf
= opt
->value
;
130 BUG_ON_OPT_ARG(arg_not_used
);
132 BUG("-F cannot be negated");
137 } else if (ctx
->argc
> 1) {
141 return error(_("option `%s' requires a value"), opt
->long_name
);
144 strbuf_addch(buf
, '\n');
145 if (ctx
->prefix
&& !is_absolute_path(arg
))
146 arg
= prefix_filename(ctx
->prefix
, arg
);
147 if (strbuf_read_file(buf
, arg
, 0) < 0)
148 return error(_("could not read file '%s'"), arg
);
154 static struct strategy
*get_strategy(const char *name
)
157 struct strategy
*ret
;
158 static struct cmdnames main_cmds
, other_cmds
;
164 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
165 if (!strcmp(name
, all_strategy
[i
].name
))
166 return &all_strategy
[i
];
169 struct cmdnames not_strategies
;
172 memset(¬_strategies
, 0, sizeof(struct cmdnames
));
173 load_command_list("git-merge-", &main_cmds
, &other_cmds
);
174 for (i
= 0; i
< main_cmds
.cnt
; i
++) {
176 struct cmdname
*ent
= main_cmds
.names
[i
];
177 for (j
= 0; j
< ARRAY_SIZE(all_strategy
); j
++)
178 if (!strncmp(ent
->name
, all_strategy
[j
].name
, ent
->len
)
179 && !all_strategy
[j
].name
[ent
->len
])
182 add_cmdname(¬_strategies
, ent
->name
, ent
->len
);
184 exclude_cmds(&main_cmds
, ¬_strategies
);
186 if (!is_in_cmdlist(&main_cmds
, name
) && !is_in_cmdlist(&other_cmds
, name
)) {
187 fprintf(stderr
, _("Could not find merge strategy '%s'.\n"), name
);
188 fprintf(stderr
, _("Available strategies are:"));
189 for (i
= 0; i
< main_cmds
.cnt
; i
++)
190 fprintf(stderr
, " %s", main_cmds
.names
[i
]->name
);
191 fprintf(stderr
, ".\n");
192 if (other_cmds
.cnt
) {
193 fprintf(stderr
, _("Available custom strategies are:"));
194 for (i
= 0; i
< other_cmds
.cnt
; i
++)
195 fprintf(stderr
, " %s", other_cmds
.names
[i
]->name
);
196 fprintf(stderr
, ".\n");
201 ret
= xcalloc(1, sizeof(struct strategy
));
202 ret
->name
= xstrdup(name
);
203 ret
->attr
= NO_TRIVIAL
;
207 static void append_strategy(struct strategy
*s
)
209 ALLOC_GROW(use_strategies
, use_strategies_nr
+ 1, use_strategies_alloc
);
210 use_strategies
[use_strategies_nr
++] = s
;
213 static int option_parse_strategy(const struct option
*opt
,
214 const char *name
, int unset
)
219 append_strategy(get_strategy(name
));
223 static int option_parse_x(const struct option
*opt
,
224 const char *arg
, int unset
)
229 ALLOC_GROW(xopts
, xopts_nr
+ 1, xopts_alloc
);
230 xopts
[xopts_nr
++] = xstrdup(arg
);
234 static int option_parse_n(const struct option
*opt
,
235 const char *arg
, int unset
)
238 show_diffstat
= unset
;
242 static struct option builtin_merge_options
[] = {
243 { OPTION_CALLBACK
, 'n', NULL
, NULL
, NULL
,
244 N_("do not show a diffstat at the end of the merge"),
245 PARSE_OPT_NOARG
, option_parse_n
},
246 OPT_BOOL(0, "stat", &show_diffstat
,
247 N_("show a diffstat at the end of the merge")),
248 OPT_BOOL(0, "summary", &show_diffstat
, N_("(synonym to --stat)")),
249 { OPTION_INTEGER
, 0, "log", &shortlog_len
, N_("n"),
250 N_("add (at most <n>) entries from shortlog to merge commit message"),
251 PARSE_OPT_OPTARG
, NULL
, DEFAULT_MERGE_LOG_LEN
},
252 OPT_BOOL(0, "squash", &squash
,
253 N_("create a single commit instead of doing a merge")),
254 OPT_BOOL(0, "commit", &option_commit
,
255 N_("perform a commit if the merge succeeds (default)")),
256 OPT_BOOL('e', "edit", &option_edit
,
257 N_("edit message before committing")),
258 OPT_CLEANUP(&cleanup_arg
),
259 OPT_SET_INT(0, "ff", &fast_forward
, N_("allow fast-forward (default)"), FF_ALLOW
),
260 OPT_SET_INT_F(0, "ff-only", &fast_forward
,
261 N_("abort if fast-forward is not possible"),
262 FF_ONLY
, PARSE_OPT_NONEG
),
263 OPT_RERERE_AUTOUPDATE(&allow_rerere_auto
),
264 OPT_BOOL(0, "verify-signatures", &verify_signatures
,
265 N_("verify that the named commit has a valid GPG signature")),
266 OPT_CALLBACK('s', "strategy", &use_strategies
, N_("strategy"),
267 N_("merge strategy to use"), option_parse_strategy
),
268 OPT_CALLBACK('X', "strategy-option", &xopts
, N_("option=value"),
269 N_("option for selected merge strategy"), option_parse_x
),
270 OPT_CALLBACK('m', "message", &merge_msg
, N_("message"),
271 N_("merge commit message (for a non-fast-forward merge)"),
272 option_parse_message
),
273 { OPTION_LOWLEVEL_CALLBACK
, 'F', "file", &merge_msg
, N_("path"),
274 N_("read message from file"), PARSE_OPT_NONEG
,
275 NULL
, 0, option_read_message
},
276 OPT__VERBOSITY(&verbosity
),
277 OPT_BOOL(0, "abort", &abort_current_merge
,
278 N_("abort the current in-progress merge")),
279 OPT_BOOL(0, "quit", &quit_current_merge
,
280 N_("--abort but leave index and working tree alone")),
281 OPT_BOOL(0, "continue", &continue_current_merge
,
282 N_("continue the current in-progress merge")),
283 OPT_BOOL(0, "allow-unrelated-histories", &allow_unrelated_histories
,
284 N_("allow merging unrelated histories")),
285 OPT_SET_INT(0, "progress", &show_progress
, N_("force progress reporting"), 1),
286 { OPTION_STRING
, 'S', "gpg-sign", &sign_commit
, N_("key-id"),
287 N_("GPG sign commit"), PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
288 OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore
, N_("update ignored files (default)")),
289 OPT_BOOL(0, "signoff", &signoff
, N_("add Signed-off-by:")),
290 OPT_BOOL(0, "verify", &verify_msg
, N_("verify commit-msg hook")),
294 static int save_state(struct object_id
*stash
)
297 struct child_process cp
= CHILD_PROCESS_INIT
;
298 struct strbuf buffer
= STRBUF_INIT
;
299 const char *argv
[] = {"stash", "create", NULL
};
306 if (start_command(&cp
))
307 die(_("could not run stash."));
308 len
= strbuf_read(&buffer
, cp
.out
, 1024);
311 if (finish_command(&cp
) || len
< 0)
312 die(_("stash failed"));
313 else if (!len
) /* no changes */
315 strbuf_setlen(&buffer
, buffer
.len
-1);
316 if (get_oid(buffer
.buf
, stash
))
317 die(_("not a valid object: %s"), buffer
.buf
);
320 strbuf_release(&buffer
);
324 static void read_empty(const struct object_id
*oid
, int verbose
)
329 args
[i
++] = "read-tree";
334 args
[i
++] = empty_tree_oid_hex();
335 args
[i
++] = oid_to_hex(oid
);
338 if (run_command_v_opt(args
, RUN_GIT_CMD
))
339 die(_("read-tree failed"));
342 static void reset_hard(const struct object_id
*oid
, int verbose
)
347 args
[i
++] = "read-tree";
350 args
[i
++] = "--reset";
352 args
[i
++] = oid_to_hex(oid
);
355 if (run_command_v_opt(args
, RUN_GIT_CMD
))
356 die(_("read-tree failed"));
359 static void restore_state(const struct object_id
*head
,
360 const struct object_id
*stash
)
362 struct strbuf sb
= STRBUF_INIT
;
363 const char *args
[] = { "stash", "apply", NULL
, NULL
};
365 if (is_null_oid(stash
))
370 args
[2] = oid_to_hex(stash
);
373 * It is OK to ignore error here, for example when there was
374 * nothing to restore.
376 run_command_v_opt(args
, RUN_GIT_CMD
);
379 refresh_cache(REFRESH_QUIET
);
382 /* This is called when no merge was necessary. */
383 static void finish_up_to_date(const char *msg
)
386 printf("%s%s\n", squash
? _(" (nothing to squash)") : "", msg
);
387 remove_merge_branch_state(the_repository
);
390 static void squash_message(struct commit
*commit
, struct commit_list
*remoteheads
)
393 struct strbuf out
= STRBUF_INIT
;
394 struct commit_list
*j
;
395 struct pretty_print_context ctx
= {0};
397 printf(_("Squash commit -- not updating HEAD\n"));
399 repo_init_revisions(the_repository
, &rev
, NULL
);
400 rev
.ignore_merges
= 1;
401 rev
.commit_format
= CMIT_FMT_MEDIUM
;
403 commit
->object
.flags
|= UNINTERESTING
;
404 add_pending_object(&rev
, &commit
->object
, NULL
);
406 for (j
= remoteheads
; j
; j
= j
->next
)
407 add_pending_object(&rev
, &j
->item
->object
, NULL
);
409 setup_revisions(0, NULL
, &rev
, NULL
);
410 if (prepare_revision_walk(&rev
))
411 die(_("revision walk setup failed"));
413 ctx
.abbrev
= rev
.abbrev
;
414 ctx
.date_mode
= rev
.date_mode
;
415 ctx
.fmt
= rev
.commit_format
;
417 strbuf_addstr(&out
, "Squashed commit of the following:\n");
418 while ((commit
= get_revision(&rev
)) != NULL
) {
419 strbuf_addch(&out
, '\n');
420 strbuf_addf(&out
, "commit %s\n",
421 oid_to_hex(&commit
->object
.oid
));
422 pretty_print_commit(&ctx
, commit
, &out
);
424 write_file_buf(git_path_squash_msg(the_repository
), out
.buf
, out
.len
);
425 strbuf_release(&out
);
428 static void finish(struct commit
*head_commit
,
429 struct commit_list
*remoteheads
,
430 const struct object_id
*new_head
, const char *msg
)
432 struct strbuf reflog_message
= STRBUF_INIT
;
433 const struct object_id
*head
= &head_commit
->object
.oid
;
436 strbuf_addstr(&reflog_message
, getenv("GIT_REFLOG_ACTION"));
440 strbuf_addf(&reflog_message
, "%s: %s",
441 getenv("GIT_REFLOG_ACTION"), msg
);
444 squash_message(head_commit
, remoteheads
);
446 if (verbosity
>= 0 && !merge_msg
.len
)
447 printf(_("No merge message -- not updating HEAD\n"));
449 const char *argv_gc_auto
[] = { "gc", "--auto", NULL
};
450 update_ref(reflog_message
.buf
, "HEAD", new_head
, head
,
451 0, UPDATE_REFS_DIE_ON_ERR
);
453 * We ignore errors in 'gc --auto', since the
454 * user should see them.
456 close_object_store(the_repository
->objects
);
457 run_command_v_opt(argv_gc_auto
, RUN_GIT_CMD
);
460 if (new_head
&& show_diffstat
) {
461 struct diff_options opts
;
462 repo_diff_setup(the_repository
, &opts
);
463 opts
.stat_width
= -1; /* use full terminal width */
464 opts
.stat_graph_width
= -1; /* respect statGraphWidth config */
465 opts
.output_format
|=
466 DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
467 opts
.detect_rename
= DIFF_DETECT_RENAME
;
468 diff_setup_done(&opts
);
469 diff_tree_oid(head
, new_head
, "", &opts
);
474 /* Run a post-merge hook */
475 run_hook_le(NULL
, "post-merge", squash
? "1" : "0", NULL
);
477 strbuf_release(&reflog_message
);
480 /* Get the name for the merge commit's message. */
481 static void merge_name(const char *remote
, struct strbuf
*msg
)
483 struct commit
*remote_head
;
484 struct object_id branch_head
;
485 struct strbuf buf
= STRBUF_INIT
;
486 struct strbuf bname
= STRBUF_INIT
;
487 struct merge_remote_desc
*desc
;
492 strbuf_branchname(&bname
, remote
, 0);
495 oidclr(&branch_head
);
496 remote_head
= get_merge_parent(remote
);
498 die(_("'%s' does not point to a commit"), remote
);
500 if (dwim_ref(remote
, strlen(remote
), &branch_head
, &found_ref
) > 0) {
501 if (starts_with(found_ref
, "refs/heads/")) {
502 strbuf_addf(msg
, "%s\t\tbranch '%s' of .\n",
503 oid_to_hex(&branch_head
), remote
);
506 if (starts_with(found_ref
, "refs/tags/")) {
507 strbuf_addf(msg
, "%s\t\ttag '%s' of .\n",
508 oid_to_hex(&branch_head
), remote
);
511 if (starts_with(found_ref
, "refs/remotes/")) {
512 strbuf_addf(msg
, "%s\t\tremote-tracking branch '%s' of .\n",
513 oid_to_hex(&branch_head
), remote
);
518 /* See if remote matches <name>^^^.. or <name>~<number> */
519 for (len
= 0, ptr
= remote
+ strlen(remote
);
520 remote
< ptr
&& ptr
[-1] == '^';
527 ptr
= strrchr(remote
, '~');
529 int seen_nonzero
= 0;
532 while (*++ptr
&& isdigit(*ptr
)) {
533 seen_nonzero
|= (*ptr
!= '0');
537 len
= 0; /* not ...~<number> */
538 else if (seen_nonzero
)
541 early
= 1; /* "name~" is "name~1"! */
545 struct strbuf truname
= STRBUF_INIT
;
546 strbuf_addf(&truname
, "refs/heads/%s", remote
);
547 strbuf_setlen(&truname
, truname
.len
- len
);
548 if (ref_exists(truname
.buf
)) {
550 "%s\t\tbranch '%s'%s of .\n",
551 oid_to_hex(&remote_head
->object
.oid
),
553 (early
? " (early part)" : ""));
554 strbuf_release(&truname
);
557 strbuf_release(&truname
);
560 desc
= merge_remote_util(remote_head
);
561 if (desc
&& desc
->obj
&& desc
->obj
->type
== OBJ_TAG
) {
562 strbuf_addf(msg
, "%s\t\t%s '%s'\n",
563 oid_to_hex(&desc
->obj
->oid
),
564 type_name(desc
->obj
->type
),
569 strbuf_addf(msg
, "%s\t\tcommit '%s'\n",
570 oid_to_hex(&remote_head
->object
.oid
), remote
);
572 strbuf_release(&buf
);
573 strbuf_release(&bname
);
576 static void parse_branch_merge_options(char *bmo
)
583 argc
= split_cmdline(bmo
, &argv
);
585 die(_("Bad branch.%s.mergeoptions string: %s"), branch
,
586 _(split_cmdline_strerror(argc
)));
587 REALLOC_ARRAY(argv
, argc
+ 2);
588 MOVE_ARRAY(argv
+ 1, argv
, argc
+ 1);
590 argv
[0] = "branch.*.mergeoptions";
591 parse_options(argc
, argv
, NULL
, builtin_merge_options
,
592 builtin_merge_usage
, 0);
596 static int git_merge_config(const char *k
, const char *v
, void *cb
)
600 if (branch
&& starts_with(k
, "branch.") &&
601 starts_with(k
+ 7, branch
) &&
602 !strcmp(k
+ 7 + strlen(branch
), ".mergeoptions")) {
603 free(branch_mergeoptions
);
604 branch_mergeoptions
= xstrdup(v
);
608 if (!strcmp(k
, "merge.diffstat") || !strcmp(k
, "merge.stat"))
609 show_diffstat
= git_config_bool(k
, v
);
610 else if (!strcmp(k
, "merge.verifysignatures"))
611 verify_signatures
= git_config_bool(k
, v
);
612 else if (!strcmp(k
, "pull.twohead"))
613 return git_config_string(&pull_twohead
, k
, v
);
614 else if (!strcmp(k
, "pull.octopus"))
615 return git_config_string(&pull_octopus
, k
, v
);
616 else if (!strcmp(k
, "commit.cleanup"))
617 return git_config_string(&cleanup_arg
, k
, v
);
618 else if (!strcmp(k
, "merge.renormalize"))
619 option_renormalize
= git_config_bool(k
, v
);
620 else if (!strcmp(k
, "merge.ff")) {
621 int boolval
= git_parse_maybe_bool(v
);
623 fast_forward
= boolval
? FF_ALLOW
: FF_NO
;
624 } else if (v
&& !strcmp(v
, "only")) {
625 fast_forward
= FF_ONLY
;
626 } /* do not barf on values from future versions of git */
628 } else if (!strcmp(k
, "merge.defaulttoupstream")) {
629 default_to_upstream
= git_config_bool(k
, v
);
631 } else if (!strcmp(k
, "commit.gpgsign")) {
632 sign_commit
= git_config_bool(k
, v
) ? "" : NULL
;
636 status
= fmt_merge_msg_config(k
, v
, cb
);
639 status
= git_gpg_config(k
, v
, NULL
);
642 return git_diff_ui_config(k
, v
, cb
);
645 static int read_tree_trivial(struct object_id
*common
, struct object_id
*head
,
646 struct object_id
*one
)
649 struct tree
*trees
[MAX_UNPACK_TREES
];
650 struct tree_desc t
[MAX_UNPACK_TREES
];
651 struct unpack_trees_options opts
;
653 memset(&opts
, 0, sizeof(opts
));
655 opts
.src_index
= &the_index
;
656 opts
.dst_index
= &the_index
;
658 opts
.verbose_update
= 1;
659 opts
.trivial_merges_only
= 1;
661 trees
[nr_trees
] = parse_tree_indirect(common
);
662 if (!trees
[nr_trees
++])
664 trees
[nr_trees
] = parse_tree_indirect(head
);
665 if (!trees
[nr_trees
++])
667 trees
[nr_trees
] = parse_tree_indirect(one
);
668 if (!trees
[nr_trees
++])
670 opts
.fn
= threeway_merge
;
671 cache_tree_free(&active_cache_tree
);
672 for (i
= 0; i
< nr_trees
; i
++) {
673 parse_tree(trees
[i
]);
674 init_tree_desc(t
+i
, trees
[i
]->buffer
, trees
[i
]->size
);
676 if (unpack_trees(nr_trees
, t
, &opts
))
681 static void write_tree_trivial(struct object_id
*oid
)
683 if (write_cache_as_tree(oid
, 0, NULL
))
684 die(_("git write-tree failed to write a tree"));
687 static int try_merge_strategy(const char *strategy
, struct commit_list
*common
,
688 struct commit_list
*remoteheads
,
691 struct lock_file lock
= LOCK_INIT
;
692 const char *head_arg
= "HEAD";
694 hold_locked_index(&lock
, LOCK_DIE_ON_ERROR
);
695 refresh_cache(REFRESH_QUIET
);
696 if (write_locked_index(&the_index
, &lock
,
697 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
698 return error(_("Unable to write index."));
700 if (!strcmp(strategy
, "recursive") || !strcmp(strategy
, "subtree")) {
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 strbuf_addbuf(&msg
, &merge_msg
);
821 BUG("the control must not reach here under --squash");
822 if (0 < option_edit
) {
823 strbuf_addch(&msg
, '\n');
824 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
) {
825 wt_status_append_cut_line(&msg
);
826 strbuf_commented_addf(&msg
, "\n");
828 strbuf_commented_addf(&msg
, _(merge_editor_comment
));
829 strbuf_commented_addf(&msg
, _(cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
?
830 scissors_editor_comment
:
831 no_scissors_editor_comment
), comment_line_char
);
834 append_signoff(&msg
, ignore_non_trailer(msg
.buf
, msg
.len
), 0);
835 write_merge_heads(remoteheads
);
836 write_file_buf(git_path_merge_msg(the_repository
), msg
.buf
, msg
.len
);
837 if (run_commit_hook(0 < option_edit
, get_index_file(), "prepare-commit-msg",
838 git_path_merge_msg(the_repository
), "merge", NULL
))
839 abort_commit(remoteheads
, NULL
);
840 if (0 < option_edit
) {
841 if (launch_editor(git_path_merge_msg(the_repository
), NULL
, NULL
))
842 abort_commit(remoteheads
, NULL
);
845 if (verify_msg
&& run_commit_hook(0 < option_edit
, get_index_file(),
847 git_path_merge_msg(the_repository
), NULL
))
848 abort_commit(remoteheads
, NULL
);
850 read_merge_msg(&msg
);
851 cleanup_message(&msg
, cleanup_mode
, 0);
853 abort_commit(remoteheads
, _("Empty commit message."));
854 strbuf_release(&merge_msg
);
855 strbuf_addbuf(&merge_msg
, &msg
);
856 strbuf_release(&msg
);
859 static int merge_trivial(struct commit
*head
, struct commit_list
*remoteheads
)
861 struct object_id result_tree
, result_commit
;
862 struct commit_list
*parents
, **pptr
= &parents
;
863 struct lock_file lock
= LOCK_INIT
;
865 hold_locked_index(&lock
, LOCK_DIE_ON_ERROR
);
866 refresh_cache(REFRESH_QUIET
);
867 if (write_locked_index(&the_index
, &lock
,
868 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
869 return error(_("Unable to write index."));
871 write_tree_trivial(&result_tree
);
872 printf(_("Wonderful.\n"));
873 pptr
= commit_list_append(head
, pptr
);
874 pptr
= commit_list_append(remoteheads
->item
, pptr
);
875 prepare_to_commit(remoteheads
);
876 if (commit_tree(merge_msg
.buf
, merge_msg
.len
, &result_tree
, parents
,
877 &result_commit
, NULL
, sign_commit
))
878 die(_("failed to write commit object"));
879 finish(head
, remoteheads
, &result_commit
, "In-index merge");
880 remove_merge_branch_state(the_repository
);
884 static int finish_automerge(struct commit
*head
,
886 struct commit_list
*common
,
887 struct commit_list
*remoteheads
,
888 struct object_id
*result_tree
,
889 const char *wt_strategy
)
891 struct commit_list
*parents
= NULL
;
892 struct strbuf buf
= STRBUF_INIT
;
893 struct object_id result_commit
;
895 free_commit_list(common
);
896 parents
= remoteheads
;
897 if (!head_subsumed
|| fast_forward
== FF_NO
)
898 commit_list_insert(head
, &parents
);
899 prepare_to_commit(remoteheads
);
900 if (commit_tree(merge_msg
.buf
, merge_msg
.len
, result_tree
, parents
,
901 &result_commit
, NULL
, sign_commit
))
902 die(_("failed to write commit object"));
903 strbuf_addf(&buf
, "Merge made by the '%s' strategy.", wt_strategy
);
904 finish(head
, remoteheads
, &result_commit
, buf
.buf
);
905 strbuf_release(&buf
);
906 remove_merge_branch_state(the_repository
);
910 static int suggest_conflicts(void)
912 const char *filename
;
914 struct strbuf msgbuf
= STRBUF_INIT
;
916 filename
= git_path_merge_msg(the_repository
);
917 fp
= xfopen(filename
, "a");
920 * We can't use cleanup_mode because if we're not using the editor,
921 * get_cleanup_mode will return COMMIT_MSG_CLEANUP_SPACE instead, even
922 * though the message is meant to be processed later by git-commit.
923 * Thus, we will get the cleanup mode which is returned when we _are_
926 append_conflicts_hint(&the_index
, &msgbuf
,
927 get_cleanup_mode(cleanup_arg
, 1));
928 fputs(msgbuf
.buf
, fp
);
929 strbuf_release(&msgbuf
);
931 repo_rerere(the_repository
, allow_rerere_auto
);
932 printf(_("Automatic merge failed; "
933 "fix conflicts and then commit the result.\n"));
937 static int evaluate_result(void)
942 /* Check how many files differ. */
943 repo_init_revisions(the_repository
, &rev
, "");
944 setup_revisions(0, NULL
, &rev
, NULL
);
945 rev
.diffopt
.output_format
|=
946 DIFF_FORMAT_CALLBACK
;
947 rev
.diffopt
.format_callback
= count_diff_files
;
948 rev
.diffopt
.format_callback_data
= &cnt
;
949 run_diff_files(&rev
, 0);
952 * Check how many unmerged entries are
955 cnt
+= count_unmerged_entries();
961 * Pretend as if the user told us to merge with the remote-tracking
962 * branch we have for the upstream of the current branch
964 static int setup_with_upstream(const char ***argv
)
966 struct branch
*branch
= branch_get(NULL
);
971 die(_("No current branch."));
972 if (!branch
->remote_name
)
973 die(_("No remote for the current branch."));
974 if (!branch
->merge_nr
)
975 die(_("No default upstream defined for the current branch."));
977 args
= xcalloc(st_add(branch
->merge_nr
, 1), sizeof(char *));
978 for (i
= 0; i
< branch
->merge_nr
; i
++) {
979 if (!branch
->merge
[i
]->dst
)
980 die(_("No remote-tracking branch for %s from %s"),
981 branch
->merge
[i
]->src
, branch
->remote_name
);
982 args
[i
] = branch
->merge
[i
]->dst
;
989 static void write_merge_heads(struct commit_list
*remoteheads
)
991 struct commit_list
*j
;
992 struct strbuf buf
= STRBUF_INIT
;
994 for (j
= remoteheads
; j
; j
= j
->next
) {
995 struct object_id
*oid
;
996 struct commit
*c
= j
->item
;
997 struct merge_remote_desc
*desc
;
999 desc
= merge_remote_util(c
);
1000 if (desc
&& desc
->obj
) {
1001 oid
= &desc
->obj
->oid
;
1003 oid
= &c
->object
.oid
;
1005 strbuf_addf(&buf
, "%s\n", oid_to_hex(oid
));
1007 write_file_buf(git_path_merge_head(the_repository
), buf
.buf
, buf
.len
);
1010 if (fast_forward
== FF_NO
)
1011 strbuf_addstr(&buf
, "no-ff");
1012 write_file_buf(git_path_merge_mode(the_repository
), buf
.buf
, buf
.len
);
1013 strbuf_release(&buf
);
1016 static void write_merge_state(struct commit_list
*remoteheads
)
1018 write_merge_heads(remoteheads
);
1019 strbuf_addch(&merge_msg
, '\n');
1020 write_file_buf(git_path_merge_msg(the_repository
), merge_msg
.buf
,
1024 static int default_edit_option(void)
1026 static const char name
[] = "GIT_MERGE_AUTOEDIT";
1027 const char *e
= getenv(name
);
1028 struct stat st_stdin
, st_stdout
;
1031 /* an explicit -m msg without --[no-]edit */
1035 int v
= git_parse_maybe_bool(e
);
1037 die(_("Bad value '%s' in environment '%s'"), e
, name
);
1041 /* Use editor if stdin and stdout are the same and is a tty */
1042 return (!fstat(0, &st_stdin
) &&
1043 !fstat(1, &st_stdout
) &&
1044 isatty(0) && isatty(1) &&
1045 st_stdin
.st_dev
== st_stdout
.st_dev
&&
1046 st_stdin
.st_ino
== st_stdout
.st_ino
&&
1047 st_stdin
.st_mode
== st_stdout
.st_mode
);
1050 static struct commit_list
*reduce_parents(struct commit
*head_commit
,
1052 struct commit_list
*remoteheads
)
1054 struct commit_list
*parents
, **remotes
;
1057 * Is the current HEAD reachable from another commit being
1058 * merged? If so we do not want to record it as a parent of
1059 * the resulting merge, unless --no-ff is given. We will flip
1060 * this variable to 0 when we find HEAD among the independent
1061 * tips being merged.
1065 /* Find what parents to record by checking independent ones. */
1066 parents
= reduce_heads(remoteheads
);
1067 free_commit_list(remoteheads
);
1070 remotes
= &remoteheads
;
1072 struct commit
*commit
= pop_commit(&parents
);
1073 if (commit
== head_commit
)
1076 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1081 static void prepare_merge_message(struct strbuf
*merge_names
, struct strbuf
*merge_msg
)
1083 struct fmt_merge_msg_opts opts
;
1085 memset(&opts
, 0, sizeof(opts
));
1086 opts
.add_title
= !have_message
;
1087 opts
.shortlog_len
= shortlog_len
;
1088 opts
.credit_people
= (0 < option_edit
);
1090 fmt_merge_msg(merge_names
, merge_msg
, &opts
);
1092 strbuf_setlen(merge_msg
, merge_msg
->len
- 1);
1095 static void handle_fetch_head(struct commit_list
**remotes
, struct strbuf
*merge_names
)
1097 const char *filename
;
1099 struct strbuf fetch_head_file
= STRBUF_INIT
;
1100 const unsigned hexsz
= the_hash_algo
->hexsz
;
1103 merge_names
= &fetch_head_file
;
1105 filename
= git_path_fetch_head(the_repository
);
1106 fd
= open(filename
, O_RDONLY
);
1108 die_errno(_("could not open '%s' for reading"), filename
);
1110 if (strbuf_read(merge_names
, fd
, 0) < 0)
1111 die_errno(_("could not read '%s'"), filename
);
1113 die_errno(_("could not close '%s'"), filename
);
1115 for (pos
= 0; pos
< merge_names
->len
; pos
= npos
) {
1116 struct object_id oid
;
1118 struct commit
*commit
;
1120 ptr
= strchr(merge_names
->buf
+ pos
, '\n');
1122 npos
= ptr
- merge_names
->buf
+ 1;
1124 npos
= merge_names
->len
;
1126 if (npos
- pos
< hexsz
+ 2 ||
1127 get_oid_hex(merge_names
->buf
+ pos
, &oid
))
1128 commit
= NULL
; /* bad */
1129 else if (memcmp(merge_names
->buf
+ pos
+ hexsz
, "\t\t", 2))
1130 continue; /* not-for-merge */
1132 char saved
= merge_names
->buf
[pos
+ hexsz
];
1133 merge_names
->buf
[pos
+ hexsz
] = '\0';
1134 commit
= get_merge_parent(merge_names
->buf
+ pos
);
1135 merge_names
->buf
[pos
+ hexsz
] = saved
;
1140 die(_("not something we can merge in %s: %s"),
1141 filename
, merge_names
->buf
+ pos
);
1143 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1146 if (merge_names
== &fetch_head_file
)
1147 strbuf_release(&fetch_head_file
);
1150 static struct commit_list
*collect_parents(struct commit
*head_commit
,
1152 int argc
, const char **argv
,
1153 struct strbuf
*merge_msg
)
1156 struct commit_list
*remoteheads
= NULL
;
1157 struct commit_list
**remotes
= &remoteheads
;
1158 struct strbuf merge_names
= STRBUF_INIT
, *autogen
= NULL
;
1160 if (merge_msg
&& (!have_message
|| shortlog_len
))
1161 autogen
= &merge_names
;
1164 remotes
= &commit_list_insert(head_commit
, remotes
)->next
;
1166 if (argc
== 1 && !strcmp(argv
[0], "FETCH_HEAD")) {
1167 handle_fetch_head(remotes
, autogen
);
1168 remoteheads
= reduce_parents(head_commit
, head_subsumed
, remoteheads
);
1170 for (i
= 0; i
< argc
; i
++) {
1171 struct commit
*commit
= get_merge_parent(argv
[i
]);
1173 help_unknown_ref(argv
[i
], "merge",
1174 _("not something we can merge"));
1175 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1177 remoteheads
= reduce_parents(head_commit
, head_subsumed
, remoteheads
);
1179 struct commit_list
*p
;
1180 for (p
= remoteheads
; p
; p
= p
->next
)
1181 merge_name(merge_remote_util(p
->item
)->name
, autogen
);
1186 prepare_merge_message(autogen
, merge_msg
);
1187 strbuf_release(autogen
);
1193 static int merging_a_throwaway_tag(struct commit
*commit
)
1196 struct object_id oid
;
1197 int is_throwaway_tag
= 0;
1199 /* Are we merging a tag? */
1200 if (!merge_remote_util(commit
) ||
1201 !merge_remote_util(commit
)->obj
||
1202 merge_remote_util(commit
)->obj
->type
!= OBJ_TAG
)
1203 return is_throwaway_tag
;
1206 * Now we know we are merging a tag object. Are we downstream
1207 * and following the tags from upstream? If so, we must have
1208 * the tag object pointed at by "refs/tags/$T" where $T is the
1209 * tagname recorded in the tag object. We want to allow such
1210 * a "just to catch up" merge to fast-forward.
1212 * Otherwise, we are playing an integrator's role, making a
1213 * merge with a throw-away tag from a contributor with
1214 * something like "git pull $contributor $signed_tag".
1215 * We want to forbid such a merge from fast-forwarding
1216 * by default; otherwise we would not keep the signature
1219 tag_ref
= xstrfmt("refs/tags/%s",
1220 ((struct tag
*)merge_remote_util(commit
)->obj
)->tag
);
1221 if (!read_ref(tag_ref
, &oid
) &&
1222 oideq(&oid
, &merge_remote_util(commit
)->obj
->oid
))
1223 is_throwaway_tag
= 0;
1225 is_throwaway_tag
= 1;
1227 return is_throwaway_tag
;
1230 int cmd_merge(int argc
, const char **argv
, const char *prefix
)
1232 struct object_id result_tree
, stash
, head_oid
;
1233 struct commit
*head_commit
;
1234 struct strbuf buf
= STRBUF_INIT
;
1235 int i
, ret
= 0, head_subsumed
;
1236 int best_cnt
= -1, merge_was_ok
= 0, automerge_was_ok
= 0;
1237 struct commit_list
*common
= NULL
;
1238 const char *best_strategy
= NULL
, *wt_strategy
= NULL
;
1239 struct commit_list
*remoteheads
, *p
;
1240 void *branch_to_free
;
1241 int orig_argc
= argc
;
1243 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1244 usage_with_options(builtin_merge_usage
, builtin_merge_options
);
1247 * Check if we are _not_ on a detached HEAD, i.e. if there is a
1250 branch
= branch_to_free
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
1252 skip_prefix(branch
, "refs/heads/", &branch
);
1254 init_diff_ui_defaults();
1255 git_config(git_merge_config
, NULL
);
1257 if (!branch
|| is_null_oid(&head_oid
))
1260 head_commit
= lookup_commit_or_die(&head_oid
, "HEAD");
1262 if (branch_mergeoptions
)
1263 parse_branch_merge_options(branch_mergeoptions
);
1264 argc
= parse_options(argc
, argv
, prefix
, builtin_merge_options
,
1265 builtin_merge_usage
, 0);
1266 if (shortlog_len
< 0)
1267 shortlog_len
= (merge_log_config
> 0) ? merge_log_config
: 0;
1269 if (verbosity
< 0 && show_progress
== -1)
1272 if (abort_current_merge
) {
1274 const char *nargv
[] = {"reset", "--merge", NULL
};
1277 usage_msg_opt(_("--abort expects no arguments"),
1278 builtin_merge_usage
, builtin_merge_options
);
1280 if (!file_exists(git_path_merge_head(the_repository
)))
1281 die(_("There is no merge to abort (MERGE_HEAD missing)."));
1283 /* Invoke 'git reset --merge' */
1284 ret
= cmd_reset(nargc
, nargv
, prefix
);
1288 if (quit_current_merge
) {
1290 usage_msg_opt(_("--quit expects no arguments"),
1291 builtin_merge_usage
,
1292 builtin_merge_options
);
1294 remove_merge_branch_state(the_repository
);
1298 if (continue_current_merge
) {
1300 const char *nargv
[] = {"commit", NULL
};
1303 usage_msg_opt(_("--continue expects no arguments"),
1304 builtin_merge_usage
, builtin_merge_options
);
1306 if (!file_exists(git_path_merge_head(the_repository
)))
1307 die(_("There is no merge in progress (MERGE_HEAD missing)."));
1309 /* Invoke 'git commit' */
1310 ret
= cmd_commit(nargc
, nargv
, prefix
);
1314 if (read_cache_unmerged())
1315 die_resolve_conflict("merge");
1317 if (file_exists(git_path_merge_head(the_repository
))) {
1319 * There is no unmerged entry, don't advise 'git
1320 * add/rm <file>', just 'git commit'.
1322 if (advice_resolve_conflict
)
1323 die(_("You have not concluded your merge (MERGE_HEAD exists).\n"
1324 "Please, commit your changes before you merge."));
1326 die(_("You have not concluded your merge (MERGE_HEAD exists)."));
1328 if (file_exists(git_path_cherry_pick_head(the_repository
))) {
1329 if (advice_resolve_conflict
)
1330 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
1331 "Please, commit your changes before you merge."));
1333 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists)."));
1335 resolve_undo_clear();
1337 if (option_edit
< 0)
1338 option_edit
= default_edit_option();
1340 cleanup_mode
= get_cleanup_mode(cleanup_arg
, 0 < option_edit
);
1346 if (fast_forward
== FF_NO
)
1347 die(_("You cannot combine --squash with --no-ff."));
1348 if (option_commit
> 0)
1349 die(_("You cannot combine --squash with --commit."));
1351 * squash can now silently disable option_commit - this is not
1352 * a problem as it is only overriding the default, not a user
1358 if (option_commit
< 0)
1362 if (default_to_upstream
)
1363 argc
= setup_with_upstream(&argv
);
1365 die(_("No commit specified and merge.defaultToUpstream not set."));
1366 } else if (argc
== 1 && !strcmp(argv
[0], "-")) {
1371 usage_with_options(builtin_merge_usage
,
1372 builtin_merge_options
);
1376 * If the merged head is a valid one there is no reason
1377 * to forbid "git merge" into a branch yet to be born.
1378 * We do the same for "git pull".
1380 struct object_id
*remote_head_oid
;
1382 die(_("Squash commit into empty head not supported yet"));
1383 if (fast_forward
== FF_NO
)
1384 die(_("Non-fast-forward commit does not make sense into "
1386 remoteheads
= collect_parents(head_commit
, &head_subsumed
,
1389 die(_("%s - not something we can merge"), argv
[0]);
1390 if (remoteheads
->next
)
1391 die(_("Can merge only exactly one commit into empty head"));
1393 if (verify_signatures
)
1394 verify_merge_signature(remoteheads
->item
, verbosity
);
1396 remote_head_oid
= &remoteheads
->item
->object
.oid
;
1397 read_empty(remote_head_oid
, 0);
1398 update_ref("initial pull", "HEAD", remote_head_oid
, NULL
, 0,
1399 UPDATE_REFS_DIE_ON_ERR
);
1404 * All the rest are the commits being merged; prepare
1405 * the standard merge summary message to be appended
1406 * to the given message.
1408 remoteheads
= collect_parents(head_commit
, &head_subsumed
,
1409 argc
, argv
, &merge_msg
);
1411 if (!head_commit
|| !argc
)
1412 usage_with_options(builtin_merge_usage
,
1413 builtin_merge_options
);
1415 if (verify_signatures
) {
1416 for (p
= remoteheads
; p
; p
= p
->next
) {
1417 verify_merge_signature(p
->item
, verbosity
);
1421 strbuf_addstr(&buf
, "merge");
1422 for (p
= remoteheads
; p
; p
= p
->next
)
1423 strbuf_addf(&buf
, " %s", merge_remote_util(p
->item
)->name
);
1424 setenv("GIT_REFLOG_ACTION", buf
.buf
, 0);
1427 for (p
= remoteheads
; p
; p
= p
->next
) {
1428 struct commit
*commit
= p
->item
;
1429 strbuf_addf(&buf
, "GITHEAD_%s",
1430 oid_to_hex(&commit
->object
.oid
));
1431 setenv(buf
.buf
, merge_remote_util(commit
)->name
, 1);
1433 if (fast_forward
!= FF_ONLY
&& merging_a_throwaway_tag(commit
))
1434 fast_forward
= FF_NO
;
1437 if (!use_strategies
) {
1439 ; /* already up-to-date */
1440 else if (!remoteheads
->next
)
1441 add_strategies(pull_twohead
, DEFAULT_TWOHEAD
);
1443 add_strategies(pull_octopus
, DEFAULT_OCTOPUS
);
1446 for (i
= 0; i
< use_strategies_nr
; i
++) {
1447 if (use_strategies
[i
]->attr
& NO_FAST_FORWARD
)
1448 fast_forward
= FF_NO
;
1449 if (use_strategies
[i
]->attr
& NO_TRIVIAL
)
1454 ; /* already up-to-date */
1455 else if (!remoteheads
->next
)
1456 common
= get_merge_bases(head_commit
, remoteheads
->item
);
1458 struct commit_list
*list
= remoteheads
;
1459 commit_list_insert(head_commit
, &list
);
1460 common
= get_octopus_merge_bases(list
);
1464 update_ref("updating ORIG_HEAD", "ORIG_HEAD",
1465 &head_commit
->object
.oid
, NULL
, 0, UPDATE_REFS_DIE_ON_ERR
);
1467 if (remoteheads
&& !common
) {
1468 /* No common ancestors found. */
1469 if (!allow_unrelated_histories
)
1470 die(_("refusing to merge unrelated histories"));
1471 /* otherwise, we need a real merge. */
1472 } else if (!remoteheads
||
1473 (!remoteheads
->next
&& !common
->next
&&
1474 common
->item
== remoteheads
->item
)) {
1476 * If head can reach all the merge then we are up to date.
1477 * but first the most common case of merging one remote.
1479 finish_up_to_date(_("Already up to date."));
1481 } else if (fast_forward
!= FF_NO
&& !remoteheads
->next
&&
1483 oideq(&common
->item
->object
.oid
, &head_commit
->object
.oid
)) {
1484 /* Again the most common case of merging one remote. */
1485 struct strbuf msg
= STRBUF_INIT
;
1486 struct commit
*commit
;
1488 if (verbosity
>= 0) {
1489 printf(_("Updating %s..%s\n"),
1490 find_unique_abbrev(&head_commit
->object
.oid
,
1492 find_unique_abbrev(&remoteheads
->item
->object
.oid
,
1495 strbuf_addstr(&msg
, "Fast-forward");
1498 " (no commit created; -m option ignored)");
1499 commit
= remoteheads
->item
;
1505 if (checkout_fast_forward(the_repository
,
1506 &head_commit
->object
.oid
,
1507 &commit
->object
.oid
,
1508 overwrite_ignore
)) {
1513 finish(head_commit
, remoteheads
, &commit
->object
.oid
, msg
.buf
);
1514 remove_merge_branch_state(the_repository
);
1516 } else if (!remoteheads
->next
&& common
->next
)
1519 * We are not doing octopus and not fast-forward. Need
1522 else if (!remoteheads
->next
&& !common
->next
&& option_commit
) {
1524 * We are not doing octopus, not fast-forward, and have
1527 refresh_cache(REFRESH_QUIET
);
1528 if (allow_trivial
&& fast_forward
!= FF_ONLY
) {
1529 /* See if it is really trivial. */
1530 git_committer_info(IDENT_STRICT
);
1531 printf(_("Trying really trivial in-index merge...\n"));
1532 if (!read_tree_trivial(&common
->item
->object
.oid
,
1533 &head_commit
->object
.oid
,
1534 &remoteheads
->item
->object
.oid
)) {
1535 ret
= merge_trivial(head_commit
, remoteheads
);
1538 printf(_("Nope.\n"));
1542 * An octopus. If we can reach all the remote we are up
1546 struct commit_list
*j
;
1548 for (j
= remoteheads
; j
; j
= j
->next
) {
1549 struct commit_list
*common_one
;
1552 * Here we *have* to calculate the individual
1553 * merge_bases again, otherwise "git merge HEAD^
1554 * HEAD^^" would be missed.
1556 common_one
= get_merge_bases(head_commit
, j
->item
);
1557 if (!oideq(&common_one
->item
->object
.oid
, &j
->item
->object
.oid
)) {
1563 finish_up_to_date(_("Already up to date. Yeeah!"));
1568 if (fast_forward
== FF_ONLY
)
1569 die(_("Not possible to fast-forward, aborting."));
1571 /* We are going to make a new commit. */
1572 git_committer_info(IDENT_STRICT
);
1575 * At this point, we need a real merge. No matter what strategy
1576 * we use, it would operate on the index, possibly affecting the
1577 * working tree, and when resolved cleanly, have the desired
1578 * tree in the index -- this means that the index must be in
1579 * sync with the head commit. The strategies are responsible
1582 if (use_strategies_nr
== 1 ||
1584 * Stash away the local changes so that we can try more than one.
1589 for (i
= 0; i
< use_strategies_nr
; i
++) {
1592 printf(_("Rewinding the tree to pristine...\n"));
1593 restore_state(&head_commit
->object
.oid
, &stash
);
1595 if (use_strategies_nr
!= 1)
1596 printf(_("Trying merge strategy %s...\n"),
1597 use_strategies
[i
]->name
);
1599 * Remember which strategy left the state in the working
1602 wt_strategy
= use_strategies
[i
]->name
;
1604 ret
= try_merge_strategy(use_strategies
[i
]->name
,
1605 common
, remoteheads
,
1607 if (!option_commit
&& !ret
) {
1610 * This is necessary here just to avoid writing
1611 * the tree, but later we will *not* exit with
1612 * status code 1 because merge_was_ok is set.
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.
1624 int cnt
= evaluate_result();
1626 if (best_cnt
<= 0 || cnt
<= best_cnt
) {
1627 best_strategy
= use_strategies
[i
]->name
;
1637 /* Automerge succeeded. */
1638 write_tree_trivial(&result_tree
);
1639 automerge_was_ok
= 1;
1644 * If we have a resulting tree, that means the strategy module
1645 * auto resolved the merge cleanly.
1647 if (automerge_was_ok
) {
1648 ret
= finish_automerge(head_commit
, head_subsumed
,
1649 common
, remoteheads
,
1650 &result_tree
, wt_strategy
);
1655 * Pick the result from the best strategy and have the user fix
1658 if (!best_strategy
) {
1659 restore_state(&head_commit
->object
.oid
, &stash
);
1660 if (use_strategies_nr
> 1)
1662 _("No merge strategy handled the merge.\n"));
1664 fprintf(stderr
, _("Merge with strategy %s failed.\n"),
1665 use_strategies
[0]->name
);
1668 } else if (best_strategy
== wt_strategy
)
1669 ; /* We already have its result in the working tree. */
1671 printf(_("Rewinding the tree to pristine...\n"));
1672 restore_state(&head_commit
->object
.oid
, &stash
);
1673 printf(_("Using the %s to prepare resolving by hand.\n"),
1675 try_merge_strategy(best_strategy
, common
, remoteheads
,
1680 finish(head_commit
, remoteheads
, NULL
, NULL
);
1682 write_merge_state(remoteheads
);
1685 fprintf(stderr
, _("Automatic merge went well; "
1686 "stopped before committing as requested\n"));
1688 ret
= suggest_conflicts();
1691 free(branch_to_free
);