4 * Copyright (c) 2008 Miklos Vajna <vmiklos@frugalware.org>
6 * Based on git-merge.sh by Junio C Hamano.
14 #include "environment.h"
17 #include "object-name.h"
18 #include "parse-options.h"
20 #include "run-command.h"
23 #include "diff-merges.h"
30 #include "unpack-trees.h"
31 #include "cache-tree.h"
37 #include "merge-recursive.h"
38 #include "merge-ort-wrappers.h"
39 #include "resolve-undo.h"
41 #include "fmt-merge-msg.h"
42 #include "sequencer.h"
43 #include "string-list.h"
47 #include "commit-reach.h"
48 #include "wt-status.h"
49 #include "commit-graph.h"
51 #define DEFAULT_TWOHEAD (1<<0)
52 #define DEFAULT_OCTOPUS (1<<1)
53 #define NO_FAST_FORWARD (1<<2)
54 #define NO_TRIVIAL (1<<3)
61 static const char * const builtin_merge_usage
[] = {
62 N_("git merge [<options>] [<commit>...]"),
64 "git merge --continue",
68 static int show_diffstat
= 1, shortlog_len
= -1, squash
;
69 static int option_commit
= -1;
70 static int option_edit
= -1;
71 static int allow_trivial
= 1, have_message
, verify_signatures
;
72 static int check_trust_level
= 1;
73 static int overwrite_ignore
= 1;
74 static struct strbuf merge_msg
= STRBUF_INIT
;
75 static struct strategy
**use_strategies
;
76 static size_t use_strategies_nr
, use_strategies_alloc
;
77 static struct strvec xopts
= STRVEC_INIT
;
78 static const char *branch
;
79 static char *branch_mergeoptions
;
81 static int allow_rerere_auto
;
82 static int abort_current_merge
;
83 static int quit_current_merge
;
84 static int continue_current_merge
;
85 static int allow_unrelated_histories
;
86 static int show_progress
= -1;
87 static int default_to_upstream
= 1;
89 static const char *sign_commit
;
92 static char *into_name
;
94 static struct strategy all_strategy
[] = {
95 { "recursive", NO_TRIVIAL
},
96 { "octopus", DEFAULT_OCTOPUS
},
97 { "ort", DEFAULT_TWOHEAD
| NO_TRIVIAL
},
99 { "ours", NO_FAST_FORWARD
| NO_TRIVIAL
},
100 { "subtree", NO_FAST_FORWARD
| NO_TRIVIAL
},
103 static const char *pull_twohead
, *pull_octopus
;
111 static enum ff_type fast_forward
= FF_ALLOW
;
113 static const char *cleanup_arg
;
114 static enum commit_msg_cleanup_mode cleanup_mode
;
116 static int option_parse_message(const struct option
*opt
,
117 const char *arg
, int unset
)
119 struct strbuf
*buf
= opt
->value
;
122 strbuf_setlen(buf
, 0);
124 strbuf_addf(buf
, "%s%s", buf
->len
? "\n\n" : "", arg
);
127 return error(_("switch `m' requires a value"));
131 static enum parse_opt_result
option_read_message(struct parse_opt_ctx_t
*ctx
,
132 const struct option
*opt
,
133 const char *arg_not_used
,
136 struct strbuf
*buf
= opt
->value
;
139 BUG_ON_OPT_ARG(arg_not_used
);
141 BUG("-F cannot be negated");
146 } else if (ctx
->argc
> 1) {
150 return error(_("option `%s' requires a value"), opt
->long_name
);
153 strbuf_addch(buf
, '\n');
154 if (ctx
->prefix
&& !is_absolute_path(arg
))
155 arg
= prefix_filename(ctx
->prefix
, arg
);
156 if (strbuf_read_file(buf
, arg
, 0) < 0)
157 return error(_("could not read file '%s'"), arg
);
163 static struct strategy
*get_strategy(const char *name
)
166 struct strategy
*ret
;
167 static struct cmdnames main_cmds
, other_cmds
;
169 char *default_strategy
= getenv("GIT_TEST_MERGE_ALGORITHM");
174 if (default_strategy
&&
175 !strcmp(default_strategy
, "ort") &&
176 !strcmp(name
, "recursive")) {
180 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
181 if (!strcmp(name
, all_strategy
[i
].name
))
182 return &all_strategy
[i
];
185 struct cmdnames not_strategies
;
188 memset(¬_strategies
, 0, sizeof(struct cmdnames
));
189 load_command_list("git-merge-", &main_cmds
, &other_cmds
);
190 for (i
= 0; i
< main_cmds
.cnt
; i
++) {
192 struct cmdname
*ent
= main_cmds
.names
[i
];
193 for (j
= 0; !found
&& j
< ARRAY_SIZE(all_strategy
); j
++)
194 if (!xstrncmpz(all_strategy
[j
].name
, ent
->name
, ent
->len
))
197 add_cmdname(¬_strategies
, ent
->name
, ent
->len
);
199 exclude_cmds(&main_cmds
, ¬_strategies
);
201 if (!is_in_cmdlist(&main_cmds
, name
) && !is_in_cmdlist(&other_cmds
, name
)) {
202 fprintf(stderr
, _("Could not find merge strategy '%s'.\n"), name
);
203 fprintf(stderr
, _("Available strategies are:"));
204 for (i
= 0; i
< main_cmds
.cnt
; i
++)
205 fprintf(stderr
, " %s", main_cmds
.names
[i
]->name
);
206 fprintf(stderr
, ".\n");
207 if (other_cmds
.cnt
) {
208 fprintf(stderr
, _("Available custom strategies are:"));
209 for (i
= 0; i
< other_cmds
.cnt
; i
++)
210 fprintf(stderr
, " %s", other_cmds
.names
[i
]->name
);
211 fprintf(stderr
, ".\n");
216 CALLOC_ARRAY(ret
, 1);
217 ret
->name
= xstrdup(name
);
218 ret
->attr
= NO_TRIVIAL
;
222 static void append_strategy(struct strategy
*s
)
224 ALLOC_GROW(use_strategies
, use_strategies_nr
+ 1, use_strategies_alloc
);
225 use_strategies
[use_strategies_nr
++] = s
;
228 static int option_parse_strategy(const struct option
*opt UNUSED
,
229 const char *name
, int unset
)
234 append_strategy(get_strategy(name
));
238 static struct option builtin_merge_options
[] = {
239 OPT_SET_INT('n', NULL
, &show_diffstat
,
240 N_("do not show a diffstat at the end of the merge"), 0),
241 OPT_BOOL(0, "stat", &show_diffstat
,
242 N_("show a diffstat at the end of the merge")),
243 OPT_BOOL(0, "summary", &show_diffstat
, N_("(synonym to --stat)")),
244 { OPTION_INTEGER
, 0, "log", &shortlog_len
, N_("n"),
245 N_("add (at most <n>) entries from shortlog to merge commit message"),
246 PARSE_OPT_OPTARG
, NULL
, DEFAULT_MERGE_LOG_LEN
},
247 OPT_BOOL(0, "squash", &squash
,
248 N_("create a single commit instead of doing a merge")),
249 OPT_BOOL(0, "commit", &option_commit
,
250 N_("perform a commit if the merge succeeds (default)")),
251 OPT_BOOL('e', "edit", &option_edit
,
252 N_("edit message before committing")),
253 OPT_CLEANUP(&cleanup_arg
),
254 OPT_SET_INT(0, "ff", &fast_forward
, N_("allow fast-forward (default)"), FF_ALLOW
),
255 OPT_SET_INT_F(0, "ff-only", &fast_forward
,
256 N_("abort if fast-forward is not possible"),
257 FF_ONLY
, PARSE_OPT_NONEG
),
258 OPT_RERERE_AUTOUPDATE(&allow_rerere_auto
),
259 OPT_BOOL(0, "verify-signatures", &verify_signatures
,
260 N_("verify that the named commit has a valid GPG signature")),
261 OPT_CALLBACK('s', "strategy", NULL
, N_("strategy"),
262 N_("merge strategy to use"), option_parse_strategy
),
263 OPT_STRVEC('X', "strategy-option", &xopts
, N_("option=value"),
264 N_("option for selected merge strategy")),
265 OPT_CALLBACK('m', "message", &merge_msg
, N_("message"),
266 N_("merge commit message (for a non-fast-forward merge)"),
267 option_parse_message
),
268 { OPTION_LOWLEVEL_CALLBACK
, 'F', "file", &merge_msg
, N_("path"),
269 N_("read message from file"), PARSE_OPT_NONEG
,
270 NULL
, 0, option_read_message
},
271 OPT_STRING(0, "into-name", &into_name
, N_("name"),
272 N_("use <name> instead of the real target")),
273 OPT__VERBOSITY(&verbosity
),
274 OPT_BOOL(0, "abort", &abort_current_merge
,
275 N_("abort the current in-progress merge")),
276 OPT_BOOL(0, "quit", &quit_current_merge
,
277 N_("--abort but leave index and working tree alone")),
278 OPT_BOOL(0, "continue", &continue_current_merge
,
279 N_("continue the current in-progress merge")),
280 OPT_BOOL(0, "allow-unrelated-histories", &allow_unrelated_histories
,
281 N_("allow merging unrelated histories")),
282 OPT_SET_INT(0, "progress", &show_progress
, N_("force progress reporting"), 1),
283 { OPTION_STRING
, 'S', "gpg-sign", &sign_commit
, N_("key-id"),
284 N_("GPG sign commit"), PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
285 OPT_AUTOSTASH(&autostash
),
286 OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore
, N_("update ignored files (default)")),
287 OPT_BOOL(0, "signoff", &signoff
, N_("add a Signed-off-by trailer")),
288 OPT_BOOL(0, "no-verify", &no_verify
, N_("bypass pre-merge-commit and commit-msg hooks")),
292 static int save_state(struct object_id
*stash
)
295 struct child_process cp
= CHILD_PROCESS_INIT
;
296 struct strbuf buffer
= STRBUF_INIT
;
297 struct lock_file lock_file
= LOCK_INIT
;
301 fd
= repo_hold_locked_index(the_repository
, &lock_file
, 0);
302 refresh_index(the_repository
->index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
304 repo_update_index_if_able(the_repository
, &lock_file
);
305 rollback_lock_file(&lock_file
);
307 strvec_pushl(&cp
.args
, "stash", "create", NULL
);
311 if (start_command(&cp
))
312 die(_("could not run stash."));
313 len
= strbuf_read(&buffer
, cp
.out
, 1024);
316 if (finish_command(&cp
) || len
< 0)
317 die(_("stash failed"));
318 else if (!len
) /* no changes */
320 strbuf_setlen(&buffer
, buffer
.len
-1);
321 if (repo_get_oid(the_repository
, buffer
.buf
, stash
))
322 die(_("not a valid object: %s"), buffer
.buf
);
325 strbuf_release(&buffer
);
329 static void read_empty(const struct object_id
*oid
)
331 struct child_process cmd
= CHILD_PROCESS_INIT
;
333 strvec_pushl(&cmd
.args
, "read-tree", "-m", "-u", empty_tree_oid_hex(),
334 oid_to_hex(oid
), NULL
);
337 if (run_command(&cmd
))
338 die(_("read-tree failed"));
341 static void reset_hard(const struct object_id
*oid
)
343 struct child_process cmd
= CHILD_PROCESS_INIT
;
345 strvec_pushl(&cmd
.args
, "read-tree", "-v", "--reset", "-u",
346 oid_to_hex(oid
), NULL
);
349 if (run_command(&cmd
))
350 die(_("read-tree failed"));
353 static void restore_state(const struct object_id
*head
,
354 const struct object_id
*stash
)
356 struct child_process cmd
= CHILD_PROCESS_INIT
;
360 if (is_null_oid(stash
))
363 strvec_pushl(&cmd
.args
, "stash", "apply", "--index", "--quiet", NULL
);
364 strvec_push(&cmd
.args
, oid_to_hex(stash
));
367 * It is OK to ignore error here, for example when there was
368 * nothing to restore.
374 discard_index(the_repository
->index
);
375 if (repo_read_index(the_repository
) < 0)
376 die(_("could not read index"));
379 /* This is called when no merge was necessary. */
380 static void finish_up_to_date(void)
382 if (verbosity
>= 0) {
384 puts(_("Already up to date. (nothing to squash)"));
386 puts(_("Already up to date."));
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 diff_merges_suppress(&rev
);
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
);
427 release_revisions(&rev
);
430 static void finish(struct commit
*head_commit
,
431 struct commit_list
*remoteheads
,
432 const struct object_id
*new_head
, const char *msg
)
434 struct strbuf reflog_message
= STRBUF_INIT
;
435 const struct object_id
*head
= &head_commit
->object
.oid
;
438 strbuf_addstr(&reflog_message
, getenv("GIT_REFLOG_ACTION"));
442 strbuf_addf(&reflog_message
, "%s: %s",
443 getenv("GIT_REFLOG_ACTION"), msg
);
446 squash_message(head_commit
, remoteheads
);
448 if (verbosity
>= 0 && !merge_msg
.len
)
449 printf(_("No merge message -- not updating HEAD\n"));
451 refs_update_ref(get_main_ref_store(the_repository
),
452 reflog_message
.buf
, "HEAD", new_head
,
454 0, UPDATE_REFS_DIE_ON_ERR
);
456 * We ignore errors in 'gc --auto', since the
457 * user should see them.
459 run_auto_maintenance(verbosity
< 0);
462 if (new_head
&& show_diffstat
) {
463 struct diff_options opts
;
464 repo_diff_setup(the_repository
, &opts
);
465 init_diffstat_widths(&opts
);
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_hooks_l("post-merge", squash
? "1" : "0", NULL
);
479 apply_autostash_ref(the_repository
, "MERGE_AUTOSTASH");
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 bname
= STRBUF_INIT
;
489 struct merge_remote_desc
*desc
;
491 char *found_ref
= NULL
;
494 strbuf_branchname(&bname
, remote
, 0);
497 oidclr(&branch_head
);
498 remote_head
= get_merge_parent(remote
);
500 die(_("'%s' does not point to a commit"), remote
);
502 if (repo_dwim_ref(the_repository
, remote
, strlen(remote
), &branch_head
,
503 &found_ref
, 0) > 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 (refs_ref_exists(get_main_ref_store(the_repository
), 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
);
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
,
600 const struct config_context
*ctx
, void *cb
)
606 skip_prefix(k
, "branch.", &str
) &&
607 skip_prefix(str
, branch
, &str
) &&
608 !strcmp(str
, ".mergeoptions")) {
609 free(branch_mergeoptions
);
610 branch_mergeoptions
= xstrdup(v
);
614 if (!strcmp(k
, "merge.diffstat") || !strcmp(k
, "merge.stat"))
615 show_diffstat
= git_config_bool(k
, v
);
616 else if (!strcmp(k
, "merge.verifysignatures"))
617 verify_signatures
= git_config_bool(k
, v
);
618 else if (!strcmp(k
, "pull.twohead"))
619 return git_config_string(&pull_twohead
, k
, v
);
620 else if (!strcmp(k
, "pull.octopus"))
621 return git_config_string(&pull_octopus
, k
, v
);
622 else if (!strcmp(k
, "commit.cleanup"))
623 return git_config_string(&cleanup_arg
, k
, v
);
624 else if (!strcmp(k
, "merge.ff")) {
625 int boolval
= git_parse_maybe_bool(v
);
627 fast_forward
= boolval
? FF_ALLOW
: FF_NO
;
628 } else if (v
&& !strcmp(v
, "only")) {
629 fast_forward
= FF_ONLY
;
630 } /* do not barf on values from future versions of git */
632 } else if (!strcmp(k
, "merge.defaulttoupstream")) {
633 default_to_upstream
= git_config_bool(k
, v
);
635 } else if (!strcmp(k
, "commit.gpgsign")) {
636 sign_commit
= git_config_bool(k
, v
) ? "" : NULL
;
638 } else if (!strcmp(k
, "gpg.mintrustlevel")) {
639 check_trust_level
= 0;
640 } else if (!strcmp(k
, "merge.autostash")) {
641 autostash
= git_config_bool(k
, v
);
645 status
= fmt_merge_msg_config(k
, v
, ctx
, cb
);
648 return git_diff_ui_config(k
, v
, ctx
, cb
);
651 static int read_tree_trivial(struct object_id
*common
, struct object_id
*head
,
652 struct object_id
*one
)
655 struct tree
*trees
[MAX_UNPACK_TREES
];
656 struct tree_desc t
[MAX_UNPACK_TREES
];
657 struct unpack_trees_options opts
;
659 memset(&opts
, 0, sizeof(opts
));
661 opts
.src_index
= the_repository
->index
;
662 opts
.dst_index
= the_repository
->index
;
664 opts
.verbose_update
= 1;
665 opts
.trivial_merges_only
= 1;
667 opts
.preserve_ignored
= 0; /* FIXME: !overwrite_ignore */
668 trees
[nr_trees
] = parse_tree_indirect(common
);
669 if (!trees
[nr_trees
++])
671 trees
[nr_trees
] = parse_tree_indirect(head
);
672 if (!trees
[nr_trees
++])
674 trees
[nr_trees
] = parse_tree_indirect(one
);
675 if (!trees
[nr_trees
++])
677 opts
.fn
= threeway_merge
;
678 cache_tree_free(&the_repository
->index
->cache_tree
);
679 for (i
= 0; i
< nr_trees
; i
++) {
680 parse_tree(trees
[i
]);
681 init_tree_desc(t
+i
, &trees
[i
]->object
.oid
,
682 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_index_as_tree(oid
, the_repository
->index
, get_index_file(), 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 (repo_refresh_and_write_index(the_repository
, REFRESH_QUIET
,
702 SKIP_IF_UNCHANGED
, 0, NULL
, NULL
,
704 return error(_("Unable to write index."));
706 if (!strcmp(strategy
, "recursive") || !strcmp(strategy
, "subtree") ||
707 !strcmp(strategy
, "ort")) {
708 struct lock_file lock
= LOCK_INIT
;
710 struct commit
*result
;
711 struct commit_list
*reversed
= NULL
;
712 struct merge_options o
;
713 struct commit_list
*j
;
715 if (remoteheads
->next
) {
716 error(_("Not handling anything other than two heads merge."));
720 init_merge_options(&o
, the_repository
);
721 if (!strcmp(strategy
, "subtree"))
722 o
.subtree_shift
= "";
724 o
.show_rename_progress
=
725 show_progress
== -1 ? isatty(2) : show_progress
;
727 for (x
= 0; x
< xopts
.nr
; x
++)
728 if (parse_merge_opt(&o
, xopts
.v
[x
]))
729 die(_("unknown strategy option: -X%s"), xopts
.v
[x
]);
731 o
.branch1
= head_arg
;
732 o
.branch2
= merge_remote_util(remoteheads
->item
)->name
;
734 for (j
= common
; j
; j
= j
->next
)
735 commit_list_insert(j
->item
, &reversed
);
737 repo_hold_locked_index(the_repository
, &lock
,
739 if (!strcmp(strategy
, "ort"))
740 clean
= merge_ort_recursive(&o
, head
, remoteheads
->item
,
743 clean
= merge_recursive(&o
, head
, remoteheads
->item
,
746 rollback_lock_file(&lock
);
749 if (write_locked_index(the_repository
->index
, &lock
,
750 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
751 die(_("unable to write %s"), get_index_file());
752 return clean
? 0 : 1;
754 return try_merge_command(the_repository
,
755 strategy
, xopts
.nr
, xopts
.v
,
756 common
, head_arg
, remoteheads
);
760 static void count_diff_files(struct diff_queue_struct
*q
,
761 struct diff_options
*opt UNUSED
, void *data
)
768 static int count_unmerged_entries(void)
772 for (i
= 0; i
< the_repository
->index
->cache_nr
; i
++)
773 if (ce_stage(the_repository
->index
->cache
[i
]))
779 static void add_strategies(const char *string
, unsigned attr
)
784 struct string_list list
= STRING_LIST_INIT_DUP
;
785 struct string_list_item
*item
;
786 string_list_split(&list
, string
, ' ', -1);
787 for_each_string_list_item(item
, &list
)
788 append_strategy(get_strategy(item
->string
));
789 string_list_clear(&list
, 0);
792 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
793 if (all_strategy
[i
].attr
& attr
)
794 append_strategy(&all_strategy
[i
]);
798 static void read_merge_msg(struct strbuf
*msg
)
800 const char *filename
= git_path_merge_msg(the_repository
);
802 if (strbuf_read_file(msg
, filename
, 0) < 0)
803 die_errno(_("Could not read from '%s'"), filename
);
806 static void write_merge_state(struct commit_list
*);
807 static void abort_commit(struct commit_list
*remoteheads
, const char *err_msg
)
810 error("%s", err_msg
);
812 _("Not committing merge; use 'git commit' to complete the merge.\n"));
813 write_merge_state(remoteheads
);
817 static const char merge_editor_comment
[] =
818 N_("Please enter a commit message to explain why this merge is necessary,\n"
819 "especially if it merges an updated upstream into a topic branch.\n"
822 static const char scissors_editor_comment
[] =
823 N_("An empty message aborts the commit.\n");
825 static const char no_scissors_editor_comment
[] =
826 N_("Lines starting with '%s' will be ignored, and an empty message aborts\n"
829 static void write_merge_heads(struct commit_list
*);
830 static void prepare_to_commit(struct commit_list
*remoteheads
)
832 struct strbuf msg
= STRBUF_INIT
;
833 const char *index_file
= get_index_file();
838 if (run_commit_hook(0 < option_edit
, index_file
, &invoked_hook
,
839 "pre-merge-commit", NULL
))
840 abort_commit(remoteheads
, NULL
);
842 * Re-read the index as pre-merge-commit hook could have updated it,
843 * and write it out as a tree. We must do this before we invoke
844 * the editor and after we invoke run_status above.
847 discard_index(the_repository
->index
);
849 read_index_from(the_repository
->index
, index_file
, get_git_dir());
850 strbuf_addbuf(&msg
, &merge_msg
);
852 BUG("the control must not reach here under --squash");
853 if (0 < option_edit
) {
854 strbuf_addch(&msg
, '\n');
855 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
) {
856 wt_status_append_cut_line(&msg
);
857 strbuf_commented_addf(&msg
, comment_line_str
, "\n");
859 strbuf_commented_addf(&msg
, comment_line_str
,
860 _(merge_editor_comment
));
861 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
)
862 strbuf_commented_addf(&msg
, comment_line_str
,
863 _(scissors_editor_comment
));
865 strbuf_commented_addf(&msg
, comment_line_str
,
866 _(no_scissors_editor_comment
), comment_line_str
);
869 append_signoff(&msg
, ignored_log_message_bytes(msg
.buf
, msg
.len
), 0);
870 write_merge_heads(remoteheads
);
871 write_file_buf(git_path_merge_msg(the_repository
), msg
.buf
, msg
.len
);
872 if (run_commit_hook(0 < option_edit
, get_index_file(), NULL
,
873 "prepare-commit-msg",
874 git_path_merge_msg(the_repository
), "merge", NULL
))
875 abort_commit(remoteheads
, NULL
);
876 if (0 < option_edit
) {
877 if (launch_editor(git_path_merge_msg(the_repository
), NULL
, NULL
))
878 abort_commit(remoteheads
, NULL
);
881 if (!no_verify
&& run_commit_hook(0 < option_edit
, get_index_file(),
883 git_path_merge_msg(the_repository
), NULL
))
884 abort_commit(remoteheads
, NULL
);
886 read_merge_msg(&msg
);
887 cleanup_message(&msg
, cleanup_mode
, 0);
889 abort_commit(remoteheads
, _("Empty commit message."));
890 strbuf_release(&merge_msg
);
891 strbuf_addbuf(&merge_msg
, &msg
);
892 strbuf_release(&msg
);
895 static int merge_trivial(struct commit
*head
, struct commit_list
*remoteheads
)
897 struct object_id result_tree
, result_commit
;
898 struct commit_list
*parents
, **pptr
= &parents
;
900 if (repo_refresh_and_write_index(the_repository
, REFRESH_QUIET
,
901 SKIP_IF_UNCHANGED
, 0, NULL
, NULL
,
903 return error(_("Unable to write index."));
905 write_tree_trivial(&result_tree
);
906 printf(_("Wonderful.\n"));
907 pptr
= commit_list_append(head
, pptr
);
908 pptr
= commit_list_append(remoteheads
->item
, pptr
);
909 prepare_to_commit(remoteheads
);
910 if (commit_tree(merge_msg
.buf
, merge_msg
.len
, &result_tree
, parents
,
911 &result_commit
, NULL
, sign_commit
))
912 die(_("failed to write commit object"));
913 finish(head
, remoteheads
, &result_commit
, "In-index merge");
914 remove_merge_branch_state(the_repository
);
918 static int finish_automerge(struct commit
*head
,
920 struct commit_list
*common
,
921 struct commit_list
*remoteheads
,
922 struct object_id
*result_tree
,
923 const char *wt_strategy
)
925 struct commit_list
*parents
= NULL
;
926 struct strbuf buf
= STRBUF_INIT
;
927 struct object_id result_commit
;
929 write_tree_trivial(result_tree
);
930 free_commit_list(common
);
931 parents
= remoteheads
;
932 if (!head_subsumed
|| fast_forward
== FF_NO
)
933 commit_list_insert(head
, &parents
);
934 prepare_to_commit(remoteheads
);
935 if (commit_tree(merge_msg
.buf
, merge_msg
.len
, result_tree
, parents
,
936 &result_commit
, NULL
, sign_commit
))
937 die(_("failed to write commit object"));
938 strbuf_addf(&buf
, "Merge made by the '%s' strategy.", wt_strategy
);
939 finish(head
, remoteheads
, &result_commit
, buf
.buf
);
940 strbuf_release(&buf
);
941 remove_merge_branch_state(the_repository
);
945 static int suggest_conflicts(void)
947 const char *filename
;
949 struct strbuf msgbuf
= STRBUF_INIT
;
951 filename
= git_path_merge_msg(the_repository
);
952 fp
= xfopen(filename
, "a");
955 * We can't use cleanup_mode because if we're not using the editor,
956 * get_cleanup_mode will return COMMIT_MSG_CLEANUP_SPACE instead, even
957 * though the message is meant to be processed later by git-commit.
958 * Thus, we will get the cleanup mode which is returned when we _are_
961 append_conflicts_hint(the_repository
->index
, &msgbuf
,
962 get_cleanup_mode(cleanup_arg
, 1));
963 fputs(msgbuf
.buf
, fp
);
964 strbuf_release(&msgbuf
);
966 repo_rerere(the_repository
, allow_rerere_auto
);
967 printf(_("Automatic merge failed; "
968 "fix conflicts and then commit the result.\n"));
972 static int evaluate_result(void)
977 /* Check how many files differ. */
978 repo_init_revisions(the_repository
, &rev
, "");
979 setup_revisions(0, NULL
, &rev
, NULL
);
980 rev
.diffopt
.output_format
|=
981 DIFF_FORMAT_CALLBACK
;
982 rev
.diffopt
.format_callback
= count_diff_files
;
983 rev
.diffopt
.format_callback_data
= &cnt
;
984 run_diff_files(&rev
, 0);
987 * Check how many unmerged entries are
990 cnt
+= count_unmerged_entries();
992 release_revisions(&rev
);
997 * Pretend as if the user told us to merge with the remote-tracking
998 * branch we have for the upstream of the current branch
1000 static int setup_with_upstream(const char ***argv
)
1002 struct branch
*branch
= branch_get(NULL
);
1007 die(_("No current branch."));
1008 if (!branch
->remote_name
)
1009 die(_("No remote for the current branch."));
1010 if (!branch
->merge_nr
)
1011 die(_("No default upstream defined for the current branch."));
1013 args
= xcalloc(st_add(branch
->merge_nr
, 1), sizeof(char *));
1014 for (i
= 0; i
< branch
->merge_nr
; i
++) {
1015 if (!branch
->merge
[i
]->dst
)
1016 die(_("No remote-tracking branch for %s from %s"),
1017 branch
->merge
[i
]->src
, branch
->remote_name
);
1018 args
[i
] = branch
->merge
[i
]->dst
;
1025 static void write_merge_heads(struct commit_list
*remoteheads
)
1027 struct commit_list
*j
;
1028 struct strbuf buf
= STRBUF_INIT
;
1030 for (j
= remoteheads
; j
; j
= j
->next
) {
1031 struct object_id
*oid
;
1032 struct commit
*c
= j
->item
;
1033 struct merge_remote_desc
*desc
;
1035 desc
= merge_remote_util(c
);
1036 if (desc
&& desc
->obj
) {
1037 oid
= &desc
->obj
->oid
;
1039 oid
= &c
->object
.oid
;
1041 strbuf_addf(&buf
, "%s\n", oid_to_hex(oid
));
1043 write_file_buf(git_path_merge_head(the_repository
), buf
.buf
, buf
.len
);
1046 if (fast_forward
== FF_NO
)
1047 strbuf_addstr(&buf
, "no-ff");
1048 write_file_buf(git_path_merge_mode(the_repository
), buf
.buf
, buf
.len
);
1049 strbuf_release(&buf
);
1052 static void write_merge_state(struct commit_list
*remoteheads
)
1054 write_merge_heads(remoteheads
);
1055 strbuf_addch(&merge_msg
, '\n');
1056 write_file_buf(git_path_merge_msg(the_repository
), merge_msg
.buf
,
1060 static int default_edit_option(void)
1062 static const char name
[] = "GIT_MERGE_AUTOEDIT";
1063 const char *e
= getenv(name
);
1064 struct stat st_stdin
, st_stdout
;
1067 /* an explicit -m msg without --[no-]edit */
1071 int v
= git_parse_maybe_bool(e
);
1073 die(_("Bad value '%s' in environment '%s'"), e
, name
);
1077 /* Use editor if stdin and stdout are the same and is a tty */
1078 return (!fstat(0, &st_stdin
) &&
1079 !fstat(1, &st_stdout
) &&
1080 isatty(0) && isatty(1) &&
1081 st_stdin
.st_dev
== st_stdout
.st_dev
&&
1082 st_stdin
.st_ino
== st_stdout
.st_ino
&&
1083 st_stdin
.st_mode
== st_stdout
.st_mode
);
1086 static struct commit_list
*reduce_parents(struct commit
*head_commit
,
1088 struct commit_list
*remoteheads
)
1090 struct commit_list
*parents
, **remotes
;
1093 * Is the current HEAD reachable from another commit being
1094 * merged? If so we do not want to record it as a parent of
1095 * the resulting merge, unless --no-ff is given. We will flip
1096 * this variable to 0 when we find HEAD among the independent
1097 * tips being merged.
1101 /* Find what parents to record by checking independent ones. */
1102 parents
= reduce_heads(remoteheads
);
1103 free_commit_list(remoteheads
);
1106 remotes
= &remoteheads
;
1108 struct commit
*commit
= pop_commit(&parents
);
1109 if (commit
== head_commit
)
1112 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1117 static void prepare_merge_message(struct strbuf
*merge_names
, struct strbuf
*merge_msg
)
1119 struct fmt_merge_msg_opts opts
;
1121 memset(&opts
, 0, sizeof(opts
));
1122 opts
.add_title
= !have_message
;
1123 opts
.shortlog_len
= shortlog_len
;
1124 opts
.credit_people
= (0 < option_edit
);
1125 opts
.into_name
= into_name
;
1127 fmt_merge_msg(merge_names
, merge_msg
, &opts
);
1129 strbuf_setlen(merge_msg
, merge_msg
->len
- 1);
1132 static void handle_fetch_head(struct commit_list
**remotes
, struct strbuf
*merge_names
)
1134 const char *filename
;
1136 struct strbuf fetch_head_file
= STRBUF_INIT
;
1137 const unsigned hexsz
= the_hash_algo
->hexsz
;
1140 merge_names
= &fetch_head_file
;
1142 filename
= git_path_fetch_head(the_repository
);
1143 fd
= xopen(filename
, O_RDONLY
);
1145 if (strbuf_read(merge_names
, fd
, 0) < 0)
1146 die_errno(_("could not read '%s'"), filename
);
1148 die_errno(_("could not close '%s'"), filename
);
1150 for (pos
= 0; pos
< merge_names
->len
; pos
= npos
) {
1151 struct object_id oid
;
1153 struct commit
*commit
;
1155 ptr
= strchr(merge_names
->buf
+ pos
, '\n');
1157 npos
= ptr
- merge_names
->buf
+ 1;
1159 npos
= merge_names
->len
;
1161 if (npos
- pos
< hexsz
+ 2 ||
1162 get_oid_hex(merge_names
->buf
+ pos
, &oid
))
1163 commit
= NULL
; /* bad */
1164 else if (memcmp(merge_names
->buf
+ pos
+ hexsz
, "\t\t", 2))
1165 continue; /* not-for-merge */
1167 char saved
= merge_names
->buf
[pos
+ hexsz
];
1168 merge_names
->buf
[pos
+ hexsz
] = '\0';
1169 commit
= get_merge_parent(merge_names
->buf
+ pos
);
1170 merge_names
->buf
[pos
+ hexsz
] = saved
;
1175 die(_("not something we can merge in %s: %s"),
1176 filename
, merge_names
->buf
+ pos
);
1178 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1181 if (merge_names
== &fetch_head_file
)
1182 strbuf_release(&fetch_head_file
);
1185 static struct commit_list
*collect_parents(struct commit
*head_commit
,
1187 int argc
, const char **argv
,
1188 struct strbuf
*merge_msg
)
1191 struct commit_list
*remoteheads
= NULL
;
1192 struct commit_list
**remotes
= &remoteheads
;
1193 struct strbuf merge_names
= STRBUF_INIT
, *autogen
= NULL
;
1195 if (merge_msg
&& (!have_message
|| shortlog_len
))
1196 autogen
= &merge_names
;
1199 remotes
= &commit_list_insert(head_commit
, remotes
)->next
;
1201 if (argc
== 1 && !strcmp(argv
[0], "FETCH_HEAD")) {
1202 handle_fetch_head(remotes
, autogen
);
1203 remoteheads
= reduce_parents(head_commit
, head_subsumed
, remoteheads
);
1205 for (i
= 0; i
< argc
; i
++) {
1206 struct commit
*commit
= get_merge_parent(argv
[i
]);
1208 help_unknown_ref(argv
[i
], "merge",
1209 _("not something we can merge"));
1210 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1212 remoteheads
= reduce_parents(head_commit
, head_subsumed
, remoteheads
);
1214 struct commit_list
*p
;
1215 for (p
= remoteheads
; p
; p
= p
->next
)
1216 merge_name(merge_remote_util(p
->item
)->name
, autogen
);
1221 prepare_merge_message(autogen
, merge_msg
);
1222 strbuf_release(autogen
);
1228 static int merging_a_throwaway_tag(struct commit
*commit
)
1231 struct object_id oid
;
1232 int is_throwaway_tag
= 0;
1234 /* Are we merging a tag? */
1235 if (!merge_remote_util(commit
) ||
1236 !merge_remote_util(commit
)->obj
||
1237 merge_remote_util(commit
)->obj
->type
!= OBJ_TAG
)
1238 return is_throwaway_tag
;
1241 * Now we know we are merging a tag object. Are we downstream
1242 * and following the tags from upstream? If so, we must have
1243 * the tag object pointed at by "refs/tags/$T" where $T is the
1244 * tagname recorded in the tag object. We want to allow such
1245 * a "just to catch up" merge to fast-forward.
1247 * Otherwise, we are playing an integrator's role, making a
1248 * merge with a throw-away tag from a contributor with
1249 * something like "git pull $contributor $signed_tag".
1250 * We want to forbid such a merge from fast-forwarding
1251 * by default; otherwise we would not keep the signature
1254 tag_ref
= xstrfmt("refs/tags/%s",
1255 ((struct tag
*)merge_remote_util(commit
)->obj
)->tag
);
1256 if (!refs_read_ref(get_main_ref_store(the_repository
), tag_ref
, &oid
) &&
1257 oideq(&oid
, &merge_remote_util(commit
)->obj
->oid
))
1258 is_throwaway_tag
= 0;
1260 is_throwaway_tag
= 1;
1262 return is_throwaway_tag
;
1265 int cmd_merge(int argc
, const char **argv
, const char *prefix
)
1267 struct object_id result_tree
, stash
, head_oid
;
1268 struct commit
*head_commit
;
1269 struct strbuf buf
= STRBUF_INIT
;
1270 int i
, ret
= 0, head_subsumed
;
1271 int best_cnt
= -1, merge_was_ok
= 0, automerge_was_ok
= 0;
1272 struct commit_list
*common
= NULL
;
1273 const char *best_strategy
= NULL
, *wt_strategy
= NULL
;
1274 struct commit_list
*remoteheads
= NULL
, *p
;
1275 void *branch_to_free
;
1276 int orig_argc
= argc
;
1278 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1279 usage_with_options(builtin_merge_usage
, builtin_merge_options
);
1281 prepare_repo_settings(the_repository
);
1282 the_repository
->settings
.command_requires_full_index
= 0;
1285 * Check if we are _not_ on a detached HEAD, i.e. if there is a
1288 branch
= branch_to_free
= refs_resolve_refdup(get_main_ref_store(the_repository
),
1289 "HEAD", 0, &head_oid
,
1292 skip_prefix(branch
, "refs/heads/", &branch
);
1294 if (!pull_twohead
) {
1295 char *default_strategy
= getenv("GIT_TEST_MERGE_ALGORITHM");
1296 if (default_strategy
&& !strcmp(default_strategy
, "ort"))
1297 pull_twohead
= "ort";
1300 init_diff_ui_defaults();
1301 git_config(git_merge_config
, NULL
);
1303 if (!branch
|| is_null_oid(&head_oid
))
1306 head_commit
= lookup_commit_or_die(&head_oid
, "HEAD");
1308 if (branch_mergeoptions
)
1309 parse_branch_merge_options(branch_mergeoptions
);
1310 argc
= parse_options(argc
, argv
, prefix
, builtin_merge_options
,
1311 builtin_merge_usage
, 0);
1312 if (shortlog_len
< 0)
1313 shortlog_len
= (merge_log_config
> 0) ? merge_log_config
: 0;
1315 if (verbosity
< 0 && show_progress
== -1)
1318 if (abort_current_merge
) {
1320 const char *nargv
[] = {"reset", "--merge", NULL
};
1321 char stash_oid_hex
[GIT_MAX_HEXSZ
+ 1];
1322 struct object_id stash_oid
= {0};
1325 usage_msg_opt(_("--abort expects no arguments"),
1326 builtin_merge_usage
, builtin_merge_options
);
1328 if (!file_exists(git_path_merge_head(the_repository
)))
1329 die(_("There is no merge to abort (MERGE_HEAD missing)."));
1331 if (!refs_read_ref(get_main_ref_store(the_repository
), "MERGE_AUTOSTASH", &stash_oid
))
1332 refs_delete_ref(get_main_ref_store(the_repository
),
1333 "", "MERGE_AUTOSTASH", &stash_oid
,
1336 /* Invoke 'git reset --merge' */
1337 ret
= cmd_reset(nargc
, nargv
, prefix
);
1339 if (!is_null_oid(&stash_oid
)) {
1340 oid_to_hex_r(stash_oid_hex
, &stash_oid
);
1341 apply_autostash_oid(stash_oid_hex
);
1347 if (quit_current_merge
) {
1349 usage_msg_opt(_("--quit expects no arguments"),
1350 builtin_merge_usage
,
1351 builtin_merge_options
);
1353 remove_merge_branch_state(the_repository
);
1357 if (continue_current_merge
) {
1359 const char *nargv
[] = {"commit", NULL
};
1362 usage_msg_opt(_("--continue expects no arguments"),
1363 builtin_merge_usage
, builtin_merge_options
);
1365 if (!file_exists(git_path_merge_head(the_repository
)))
1366 die(_("There is no merge in progress (MERGE_HEAD missing)."));
1368 /* Invoke 'git commit' */
1369 ret
= cmd_commit(nargc
, nargv
, prefix
);
1373 if (repo_read_index_unmerged(the_repository
))
1374 die_resolve_conflict("merge");
1376 if (file_exists(git_path_merge_head(the_repository
))) {
1378 * There is no unmerged entry, don't advise 'git
1379 * add/rm <file>', just 'git commit'.
1381 if (advice_enabled(ADVICE_RESOLVE_CONFLICT
))
1382 die(_("You have not concluded your merge (MERGE_HEAD exists).\n"
1383 "Please, commit your changes before you merge."));
1385 die(_("You have not concluded your merge (MERGE_HEAD exists)."));
1387 if (refs_ref_exists(get_main_ref_store(the_repository
), "CHERRY_PICK_HEAD")) {
1388 if (advice_enabled(ADVICE_RESOLVE_CONFLICT
))
1389 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
1390 "Please, commit your changes before you merge."));
1392 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists)."));
1394 resolve_undo_clear_index(the_repository
->index
);
1396 if (option_edit
< 0)
1397 option_edit
= default_edit_option();
1399 cleanup_mode
= get_cleanup_mode(cleanup_arg
, 0 < option_edit
);
1405 if (fast_forward
== FF_NO
)
1406 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--no-ff.");
1407 if (option_commit
> 0)
1408 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--commit.");
1410 * squash can now silently disable option_commit - this is not
1411 * a problem as it is only overriding the default, not a user
1417 if (option_commit
< 0)
1421 if (default_to_upstream
)
1422 argc
= setup_with_upstream(&argv
);
1424 die(_("No commit specified and merge.defaultToUpstream not set."));
1425 } else if (argc
== 1 && !strcmp(argv
[0], "-")) {
1430 usage_with_options(builtin_merge_usage
,
1431 builtin_merge_options
);
1435 * If the merged head is a valid one there is no reason
1436 * to forbid "git merge" into a branch yet to be born.
1437 * We do the same for "git pull".
1439 struct object_id
*remote_head_oid
;
1441 die(_("Squash commit into empty head not supported yet"));
1442 if (fast_forward
== FF_NO
)
1443 die(_("Non-fast-forward commit does not make sense into "
1445 remoteheads
= collect_parents(head_commit
, &head_subsumed
,
1448 die(_("%s - not something we can merge"), argv
[0]);
1449 if (remoteheads
->next
)
1450 die(_("Can merge only exactly one commit into empty head"));
1452 if (verify_signatures
)
1453 verify_merge_signature(remoteheads
->item
, verbosity
,
1456 remote_head_oid
= &remoteheads
->item
->object
.oid
;
1457 read_empty(remote_head_oid
);
1458 refs_update_ref(get_main_ref_store(the_repository
),
1459 "initial pull", "HEAD", remote_head_oid
, NULL
,
1461 UPDATE_REFS_DIE_ON_ERR
);
1466 * All the rest are the commits being merged; prepare
1467 * the standard merge summary message to be appended
1468 * to the given message.
1470 remoteheads
= collect_parents(head_commit
, &head_subsumed
,
1471 argc
, argv
, &merge_msg
);
1473 if (!head_commit
|| !argc
)
1474 usage_with_options(builtin_merge_usage
,
1475 builtin_merge_options
);
1477 if (verify_signatures
) {
1478 for (p
= remoteheads
; p
; p
= p
->next
) {
1479 verify_merge_signature(p
->item
, verbosity
,
1484 strbuf_addstr(&buf
, "merge");
1485 for (p
= remoteheads
; p
; p
= p
->next
)
1486 strbuf_addf(&buf
, " %s", merge_remote_util(p
->item
)->name
);
1487 setenv("GIT_REFLOG_ACTION", buf
.buf
, 0);
1490 for (p
= remoteheads
; p
; p
= p
->next
) {
1491 struct commit
*commit
= p
->item
;
1492 strbuf_addf(&buf
, "GITHEAD_%s",
1493 oid_to_hex(&commit
->object
.oid
));
1494 setenv(buf
.buf
, merge_remote_util(commit
)->name
, 1);
1496 if (fast_forward
!= FF_ONLY
&& merging_a_throwaway_tag(commit
))
1497 fast_forward
= FF_NO
;
1500 if (!use_strategies
&& !pull_twohead
&&
1501 remoteheads
&& !remoteheads
->next
) {
1502 char *default_strategy
= getenv("GIT_TEST_MERGE_ALGORITHM");
1503 if (default_strategy
)
1504 append_strategy(get_strategy(default_strategy
));
1506 if (!use_strategies
) {
1508 ; /* already up-to-date */
1509 else if (!remoteheads
->next
)
1510 add_strategies(pull_twohead
, DEFAULT_TWOHEAD
);
1512 add_strategies(pull_octopus
, DEFAULT_OCTOPUS
);
1515 for (i
= 0; i
< use_strategies_nr
; i
++) {
1516 if (use_strategies
[i
]->attr
& NO_FAST_FORWARD
)
1517 fast_forward
= FF_NO
;
1518 if (use_strategies
[i
]->attr
& NO_TRIVIAL
)
1523 ; /* already up-to-date */
1524 else if (!remoteheads
->next
) {
1525 if (repo_get_merge_bases(the_repository
, head_commit
,
1526 remoteheads
->item
, &common
) < 0) {
1531 struct commit_list
*list
= remoteheads
;
1532 commit_list_insert(head_commit
, &list
);
1533 if (get_octopus_merge_bases(list
, &common
) < 0) {
1541 refs_update_ref(get_main_ref_store(the_repository
),
1542 "updating ORIG_HEAD", "ORIG_HEAD",
1543 &head_commit
->object
.oid
, NULL
, 0,
1544 UPDATE_REFS_DIE_ON_ERR
);
1546 if (remoteheads
&& !common
) {
1547 /* No common ancestors found. */
1548 if (!allow_unrelated_histories
)
1549 die(_("refusing to merge unrelated histories"));
1550 /* otherwise, we need a real merge. */
1551 } else if (!remoteheads
||
1552 (!remoteheads
->next
&& !common
->next
&&
1553 common
->item
== remoteheads
->item
)) {
1555 * If head can reach all the merge then we are up to date.
1556 * but first the most common case of merging one remote.
1558 finish_up_to_date();
1560 } else if (fast_forward
!= FF_NO
&& !remoteheads
->next
&&
1562 oideq(&common
->item
->object
.oid
, &head_commit
->object
.oid
)) {
1563 /* Again the most common case of merging one remote. */
1564 const char *msg
= have_message
?
1565 "Fast-forward (no commit created; -m option ignored)" :
1567 struct commit
*commit
;
1569 if (verbosity
>= 0) {
1570 printf(_("Updating %s..%s\n"),
1571 repo_find_unique_abbrev(the_repository
, &head_commit
->object
.oid
,
1573 repo_find_unique_abbrev(the_repository
, &remoteheads
->item
->object
.oid
,
1576 commit
= remoteheads
->item
;
1583 create_autostash_ref(the_repository
, "MERGE_AUTOSTASH");
1584 if (checkout_fast_forward(the_repository
,
1585 &head_commit
->object
.oid
,
1586 &commit
->object
.oid
,
1587 overwrite_ignore
)) {
1588 apply_autostash_ref(the_repository
, "MERGE_AUTOSTASH");
1593 finish(head_commit
, remoteheads
, &commit
->object
.oid
, msg
);
1594 remove_merge_branch_state(the_repository
);
1596 } else if (!remoteheads
->next
&& common
->next
)
1599 * We are not doing octopus and not fast-forward. Need
1602 else if (!remoteheads
->next
&& !common
->next
&& option_commit
) {
1604 * We are not doing octopus, not fast-forward, and have
1607 refresh_index(the_repository
->index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
1608 if (allow_trivial
&& fast_forward
!= FF_ONLY
) {
1610 * Must first ensure that index matches HEAD before
1611 * attempting a trivial merge.
1613 struct tree
*head_tree
= repo_get_commit_tree(the_repository
,
1615 struct strbuf sb
= STRBUF_INIT
;
1617 if (repo_index_has_changes(the_repository
, head_tree
,
1619 error(_("Your local changes to the following files would be overwritten by merge:\n %s"),
1621 strbuf_release(&sb
);
1626 /* See if it is really trivial. */
1627 git_committer_info(IDENT_STRICT
);
1628 printf(_("Trying really trivial in-index merge...\n"));
1629 if (!read_tree_trivial(&common
->item
->object
.oid
,
1630 &head_commit
->object
.oid
,
1631 &remoteheads
->item
->object
.oid
)) {
1632 ret
= merge_trivial(head_commit
, remoteheads
);
1635 printf(_("Nope.\n"));
1639 * An octopus. If we can reach all the remote we are up
1643 struct commit_list
*j
;
1645 for (j
= remoteheads
; j
; j
= j
->next
) {
1646 struct commit_list
*common_one
= NULL
;
1647 struct commit
*common_item
;
1650 * Here we *have* to calculate the individual
1651 * merge_bases again, otherwise "git merge HEAD^
1652 * HEAD^^" would be missed.
1654 if (repo_get_merge_bases(the_repository
, head_commit
,
1655 j
->item
, &common_one
) < 0)
1658 common_item
= common_one
->item
;
1659 free_commit_list(common_one
);
1660 if (!oideq(&common_item
->object
.oid
, &j
->item
->object
.oid
)) {
1666 finish_up_to_date();
1671 if (fast_forward
== FF_ONLY
)
1672 die_ff_impossible();
1675 create_autostash_ref(the_repository
, "MERGE_AUTOSTASH");
1677 /* We are going to make a new commit. */
1678 git_committer_info(IDENT_STRICT
);
1681 * At this point, we need a real merge. No matter what strategy
1682 * we use, it would operate on the index, possibly affecting the
1683 * working tree, and when resolved cleanly, have the desired
1684 * tree in the index -- this means that the index must be in
1685 * sync with the head commit. The strategies are responsible
1688 * Stash away the local changes so that we can try more than one
1689 * and/or recover from merge strategies bailing while leaving the
1690 * index and working tree polluted.
1692 if (save_state(&stash
))
1695 for (i
= 0; i
< use_strategies_nr
; i
++) {
1698 printf(_("Rewinding the tree to pristine...\n"));
1699 restore_state(&head_commit
->object
.oid
, &stash
);
1701 if (use_strategies_nr
!= 1)
1702 printf(_("Trying merge strategy %s...\n"),
1703 use_strategies
[i
]->name
);
1705 * Remember which strategy left the state in the working
1708 wt_strategy
= use_strategies
[i
]->name
;
1710 ret
= try_merge_strategy(wt_strategy
,
1711 common
, remoteheads
,
1714 * The backend exits with 1 when conflicts are
1715 * left to be resolved, with 2 when it does not
1716 * handle the given merge at all.
1721 * This strategy worked; no point in trying
1725 best_strategy
= wt_strategy
;
1728 cnt
= (use_strategies_nr
> 1) ? evaluate_result() : 0;
1729 if (best_cnt
<= 0 || cnt
<= best_cnt
) {
1730 best_strategy
= wt_strategy
;
1737 * If we have a resulting tree, that means the strategy module
1738 * auto resolved the merge cleanly.
1740 if (merge_was_ok
&& option_commit
) {
1741 automerge_was_ok
= 1;
1742 ret
= finish_automerge(head_commit
, head_subsumed
,
1743 common
, remoteheads
,
1744 &result_tree
, wt_strategy
);
1749 * Pick the result from the best strategy and have the user fix
1752 if (!best_strategy
) {
1753 restore_state(&head_commit
->object
.oid
, &stash
);
1754 if (use_strategies_nr
> 1)
1756 _("No merge strategy handled the merge.\n"));
1758 fprintf(stderr
, _("Merge with strategy %s failed.\n"),
1759 use_strategies
[0]->name
);
1760 apply_autostash_ref(the_repository
, "MERGE_AUTOSTASH");
1763 } else if (best_strategy
== wt_strategy
)
1764 ; /* We already have its result in the working tree. */
1766 printf(_("Rewinding the tree to pristine...\n"));
1767 restore_state(&head_commit
->object
.oid
, &stash
);
1768 printf(_("Using the %s strategy to prepare resolving by hand.\n"),
1770 try_merge_strategy(best_strategy
, common
, remoteheads
,
1775 finish(head_commit
, remoteheads
, NULL
, NULL
);
1777 git_test_write_commit_graph_or_die();
1779 write_merge_state(remoteheads
);
1782 fprintf(stderr
, _("Automatic merge went well; "
1783 "stopped before committing as requested\n"));
1785 ret
= suggest_conflicts();
1787 printf(_("When finished, apply stashed changes with `git stash pop`\n"));
1790 if (!automerge_was_ok
) {
1791 free_commit_list(common
);
1792 free_commit_list(remoteheads
);
1794 strbuf_release(&buf
);
1795 free(branch_to_free
);
1796 discard_index(the_repository
->index
);