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 char *pull_twohead
, *pull_octopus
;
111 static enum ff_type fast_forward
= FF_ALLOW
;
113 static 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
= {0}, other_cmds
= {0};
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
= {0};
188 load_command_list("git-merge-", &main_cmds
, &other_cmds
);
189 for (i
= 0; i
< main_cmds
.cnt
; i
++) {
191 struct cmdname
*ent
= main_cmds
.names
[i
];
192 for (j
= 0; !found
&& j
< ARRAY_SIZE(all_strategy
); j
++)
193 if (!xstrncmpz(all_strategy
[j
].name
, ent
->name
, ent
->len
))
196 add_cmdname(¬_strategies
, ent
->name
, ent
->len
);
198 exclude_cmds(&main_cmds
, ¬_strategies
);
200 cmdnames_release(¬_strategies
);
202 if (!is_in_cmdlist(&main_cmds
, name
) && !is_in_cmdlist(&other_cmds
, name
)) {
203 fprintf(stderr
, _("Could not find merge strategy '%s'.\n"), name
);
204 fprintf(stderr
, _("Available strategies are:"));
205 for (i
= 0; i
< main_cmds
.cnt
; i
++)
206 fprintf(stderr
, " %s", main_cmds
.names
[i
]->name
);
207 fprintf(stderr
, ".\n");
208 if (other_cmds
.cnt
) {
209 fprintf(stderr
, _("Available custom strategies are:"));
210 for (i
= 0; i
< other_cmds
.cnt
; i
++)
211 fprintf(stderr
, " %s", other_cmds
.names
[i
]->name
);
212 fprintf(stderr
, ".\n");
217 CALLOC_ARRAY(ret
, 1);
218 ret
->name
= xstrdup(name
);
219 ret
->attr
= NO_TRIVIAL
;
221 cmdnames_release(&main_cmds
);
222 cmdnames_release(&other_cmds
);
226 static void append_strategy(struct strategy
*s
)
228 ALLOC_GROW(use_strategies
, use_strategies_nr
+ 1, use_strategies_alloc
);
229 use_strategies
[use_strategies_nr
++] = s
;
232 static int option_parse_strategy(const struct option
*opt UNUSED
,
233 const char *name
, int unset
)
238 append_strategy(get_strategy(name
));
242 static struct option builtin_merge_options
[] = {
243 OPT_SET_INT('n', NULL
, &show_diffstat
,
244 N_("do not show a diffstat at the end of the merge"), 0),
245 OPT_BOOL(0, "stat", &show_diffstat
,
246 N_("show a diffstat at the end of the merge")),
247 OPT_BOOL(0, "summary", &show_diffstat
, N_("(synonym to --stat)")),
248 { OPTION_INTEGER
, 0, "log", &shortlog_len
, N_("n"),
249 N_("add (at most <n>) entries from shortlog to merge commit message"),
250 PARSE_OPT_OPTARG
, NULL
, DEFAULT_MERGE_LOG_LEN
},
251 OPT_BOOL(0, "squash", &squash
,
252 N_("create a single commit instead of doing a merge")),
253 OPT_BOOL(0, "commit", &option_commit
,
254 N_("perform a commit if the merge succeeds (default)")),
255 OPT_BOOL('e', "edit", &option_edit
,
256 N_("edit message before committing")),
257 OPT_CLEANUP(&cleanup_arg
),
258 OPT_SET_INT(0, "ff", &fast_forward
, N_("allow fast-forward (default)"), FF_ALLOW
),
259 OPT_SET_INT_F(0, "ff-only", &fast_forward
,
260 N_("abort if fast-forward is not possible"),
261 FF_ONLY
, PARSE_OPT_NONEG
),
262 OPT_RERERE_AUTOUPDATE(&allow_rerere_auto
),
263 OPT_BOOL(0, "verify-signatures", &verify_signatures
,
264 N_("verify that the named commit has a valid GPG signature")),
265 OPT_CALLBACK('s', "strategy", NULL
, N_("strategy"),
266 N_("merge strategy to use"), option_parse_strategy
),
267 OPT_STRVEC('X', "strategy-option", &xopts
, N_("option=value"),
268 N_("option for selected merge strategy")),
269 OPT_CALLBACK('m', "message", &merge_msg
, N_("message"),
270 N_("merge commit message (for a non-fast-forward merge)"),
271 option_parse_message
),
272 { OPTION_LOWLEVEL_CALLBACK
, 'F', "file", &merge_msg
, N_("path"),
273 N_("read message from file"), PARSE_OPT_NONEG
,
274 NULL
, 0, option_read_message
},
275 OPT_STRING(0, "into-name", &into_name
, N_("name"),
276 N_("use <name> instead of the real target")),
277 OPT__VERBOSITY(&verbosity
),
278 OPT_BOOL(0, "abort", &abort_current_merge
,
279 N_("abort the current in-progress merge")),
280 OPT_BOOL(0, "quit", &quit_current_merge
,
281 N_("--abort but leave index and working tree alone")),
282 OPT_BOOL(0, "continue", &continue_current_merge
,
283 N_("continue the current in-progress merge")),
284 OPT_BOOL(0, "allow-unrelated-histories", &allow_unrelated_histories
,
285 N_("allow merging unrelated histories")),
286 OPT_SET_INT(0, "progress", &show_progress
, N_("force progress reporting"), 1),
287 { OPTION_STRING
, 'S', "gpg-sign", &sign_commit
, N_("key-id"),
288 N_("GPG sign commit"), PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
289 OPT_AUTOSTASH(&autostash
),
290 OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore
, N_("update ignored files (default)")),
291 OPT_BOOL(0, "signoff", &signoff
, N_("add a Signed-off-by trailer")),
292 OPT_BOOL(0, "no-verify", &no_verify
, N_("bypass pre-merge-commit and commit-msg hooks")),
296 static int save_state(struct object_id
*stash
)
299 struct child_process cp
= CHILD_PROCESS_INIT
;
300 struct strbuf buffer
= STRBUF_INIT
;
301 struct lock_file lock_file
= LOCK_INIT
;
305 fd
= repo_hold_locked_index(the_repository
, &lock_file
, 0);
306 refresh_index(the_repository
->index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
308 repo_update_index_if_able(the_repository
, &lock_file
);
309 rollback_lock_file(&lock_file
);
311 strvec_pushl(&cp
.args
, "stash", "create", NULL
);
315 if (start_command(&cp
))
316 die(_("could not run stash."));
317 len
= strbuf_read(&buffer
, cp
.out
, 1024);
320 if (finish_command(&cp
) || len
< 0)
321 die(_("stash failed"));
322 else if (!len
) /* no changes */
324 strbuf_setlen(&buffer
, buffer
.len
-1);
325 if (repo_get_oid(the_repository
, buffer
.buf
, stash
))
326 die(_("not a valid object: %s"), buffer
.buf
);
329 strbuf_release(&buffer
);
333 static void read_empty(const struct object_id
*oid
)
335 struct child_process cmd
= CHILD_PROCESS_INIT
;
337 strvec_pushl(&cmd
.args
, "read-tree", "-m", "-u",
338 empty_tree_oid_hex(the_repository
->hash_algo
),
339 oid_to_hex(oid
), NULL
);
342 if (run_command(&cmd
))
343 die(_("read-tree failed"));
346 static void reset_hard(const struct object_id
*oid
)
348 struct child_process cmd
= CHILD_PROCESS_INIT
;
350 strvec_pushl(&cmd
.args
, "read-tree", "-v", "--reset", "-u",
351 oid_to_hex(oid
), NULL
);
354 if (run_command(&cmd
))
355 die(_("read-tree failed"));
358 static void restore_state(const struct object_id
*head
,
359 const struct object_id
*stash
)
361 struct child_process cmd
= CHILD_PROCESS_INIT
;
365 if (is_null_oid(stash
))
368 strvec_pushl(&cmd
.args
, "stash", "apply", "--index", "--quiet", NULL
);
369 strvec_push(&cmd
.args
, oid_to_hex(stash
));
372 * It is OK to ignore error here, for example when there was
373 * nothing to restore.
379 discard_index(the_repository
->index
);
380 if (repo_read_index(the_repository
) < 0)
381 die(_("could not read index"));
384 /* This is called when no merge was necessary. */
385 static void finish_up_to_date(void)
387 if (verbosity
>= 0) {
389 puts(_("Already up to date. (nothing to squash)"));
391 puts(_("Already up to date."));
393 remove_merge_branch_state(the_repository
);
396 static void squash_message(struct commit
*commit
, struct commit_list
*remoteheads
)
399 struct strbuf out
= STRBUF_INIT
;
400 struct commit_list
*j
;
401 struct pretty_print_context ctx
= {0};
403 printf(_("Squash commit -- not updating HEAD\n"));
405 repo_init_revisions(the_repository
, &rev
, NULL
);
406 diff_merges_suppress(&rev
);
407 rev
.commit_format
= CMIT_FMT_MEDIUM
;
409 commit
->object
.flags
|= UNINTERESTING
;
410 add_pending_object(&rev
, &commit
->object
, NULL
);
412 for (j
= remoteheads
; j
; j
= j
->next
)
413 add_pending_object(&rev
, &j
->item
->object
, NULL
);
415 setup_revisions(0, NULL
, &rev
, NULL
);
416 if (prepare_revision_walk(&rev
))
417 die(_("revision walk setup failed"));
419 ctx
.abbrev
= rev
.abbrev
;
420 ctx
.date_mode
= rev
.date_mode
;
421 ctx
.fmt
= rev
.commit_format
;
423 strbuf_addstr(&out
, "Squashed commit of the following:\n");
424 while ((commit
= get_revision(&rev
)) != NULL
) {
425 strbuf_addch(&out
, '\n');
426 strbuf_addf(&out
, "commit %s\n",
427 oid_to_hex(&commit
->object
.oid
));
428 pretty_print_commit(&ctx
, commit
, &out
);
430 write_file_buf(git_path_squash_msg(the_repository
), out
.buf
, out
.len
);
431 strbuf_release(&out
);
432 release_revisions(&rev
);
435 static void finish(struct commit
*head_commit
,
436 struct commit_list
*remoteheads
,
437 const struct object_id
*new_head
, const char *msg
)
439 struct strbuf reflog_message
= STRBUF_INIT
;
440 const struct object_id
*head
= &head_commit
->object
.oid
;
443 strbuf_addstr(&reflog_message
, getenv("GIT_REFLOG_ACTION"));
447 strbuf_addf(&reflog_message
, "%s: %s",
448 getenv("GIT_REFLOG_ACTION"), msg
);
451 squash_message(head_commit
, remoteheads
);
453 if (verbosity
>= 0 && !merge_msg
.len
)
454 printf(_("No merge message -- not updating HEAD\n"));
456 refs_update_ref(get_main_ref_store(the_repository
),
457 reflog_message
.buf
, "HEAD", new_head
,
459 0, UPDATE_REFS_DIE_ON_ERR
);
461 * We ignore errors in 'gc --auto', since the
462 * user should see them.
464 run_auto_maintenance(verbosity
< 0);
467 if (new_head
&& show_diffstat
) {
468 struct diff_options opts
;
469 repo_diff_setup(the_repository
, &opts
);
470 init_diffstat_widths(&opts
);
471 opts
.output_format
|=
472 DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
473 opts
.detect_rename
= DIFF_DETECT_RENAME
;
474 diff_setup_done(&opts
);
475 diff_tree_oid(head
, new_head
, "", &opts
);
480 /* Run a post-merge hook */
481 run_hooks_l(the_repository
, "post-merge", squash
? "1" : "0", NULL
);
484 apply_autostash_ref(the_repository
, "MERGE_AUTOSTASH");
485 strbuf_release(&reflog_message
);
488 /* Get the name for the merge commit's message. */
489 static void merge_name(const char *remote
, struct strbuf
*msg
)
491 struct commit
*remote_head
;
492 struct object_id branch_head
;
493 struct strbuf bname
= STRBUF_INIT
;
494 struct merge_remote_desc
*desc
;
496 char *found_ref
= NULL
;
499 strbuf_branchname(&bname
, remote
, 0);
502 oidclr(&branch_head
, the_repository
->hash_algo
);
503 remote_head
= get_merge_parent(remote
);
505 die(_("'%s' does not point to a commit"), remote
);
507 if (repo_dwim_ref(the_repository
, remote
, strlen(remote
), &branch_head
,
508 &found_ref
, 0) > 0) {
509 if (starts_with(found_ref
, "refs/heads/")) {
510 strbuf_addf(msg
, "%s\t\tbranch '%s' of .\n",
511 oid_to_hex(&branch_head
), remote
);
514 if (starts_with(found_ref
, "refs/tags/")) {
515 strbuf_addf(msg
, "%s\t\ttag '%s' of .\n",
516 oid_to_hex(&branch_head
), remote
);
519 if (starts_with(found_ref
, "refs/remotes/")) {
520 strbuf_addf(msg
, "%s\t\tremote-tracking branch '%s' of .\n",
521 oid_to_hex(&branch_head
), remote
);
526 /* See if remote matches <name>^^^.. or <name>~<number> */
527 for (len
= 0, ptr
= remote
+ strlen(remote
);
528 remote
< ptr
&& ptr
[-1] == '^';
535 ptr
= strrchr(remote
, '~');
537 int seen_nonzero
= 0;
540 while (*++ptr
&& isdigit(*ptr
)) {
541 seen_nonzero
|= (*ptr
!= '0');
545 len
= 0; /* not ...~<number> */
546 else if (seen_nonzero
)
549 early
= 1; /* "name~" is "name~1"! */
553 struct strbuf truname
= STRBUF_INIT
;
554 strbuf_addf(&truname
, "refs/heads/%s", remote
);
555 strbuf_setlen(&truname
, truname
.len
- len
);
556 if (refs_ref_exists(get_main_ref_store(the_repository
), truname
.buf
)) {
558 "%s\t\tbranch '%s'%s of .\n",
559 oid_to_hex(&remote_head
->object
.oid
),
561 (early
? " (early part)" : ""));
562 strbuf_release(&truname
);
565 strbuf_release(&truname
);
568 desc
= merge_remote_util(remote_head
);
569 if (desc
&& desc
->obj
&& desc
->obj
->type
== OBJ_TAG
) {
570 strbuf_addf(msg
, "%s\t\t%s '%s'\n",
571 oid_to_hex(&desc
->obj
->oid
),
572 type_name(desc
->obj
->type
),
577 strbuf_addf(msg
, "%s\t\tcommit '%s'\n",
578 oid_to_hex(&remote_head
->object
.oid
), remote
);
581 strbuf_release(&bname
);
584 static void parse_branch_merge_options(char *bmo
)
591 argc
= split_cmdline(bmo
, &argv
);
593 die(_("Bad branch.%s.mergeoptions string: %s"), branch
,
594 _(split_cmdline_strerror(argc
)));
595 REALLOC_ARRAY(argv
, argc
+ 2);
596 MOVE_ARRAY(argv
+ 1, argv
, argc
+ 1);
598 argv
[0] = "branch.*.mergeoptions";
599 parse_options(argc
, argv
, NULL
, builtin_merge_options
,
600 builtin_merge_usage
, 0);
604 static int git_merge_config(const char *k
, const char *v
,
605 const struct config_context
*ctx
, void *cb
)
611 skip_prefix(k
, "branch.", &str
) &&
612 skip_prefix(str
, branch
, &str
) &&
613 !strcmp(str
, ".mergeoptions")) {
614 free(branch_mergeoptions
);
615 branch_mergeoptions
= xstrdup(v
);
619 if (!strcmp(k
, "merge.diffstat") || !strcmp(k
, "merge.stat")) {
620 show_diffstat
= git_config_bool(k
, v
);
621 } else if (!strcmp(k
, "merge.verifysignatures")) {
622 verify_signatures
= git_config_bool(k
, v
);
623 } else if (!strcmp(k
, "pull.twohead")) {
624 FREE_AND_NULL(pull_twohead
);
625 return git_config_string(&pull_twohead
, k
, v
);
626 } else if (!strcmp(k
, "pull.octopus")) {
627 FREE_AND_NULL(pull_octopus
);
628 return git_config_string(&pull_octopus
, k
, v
);
629 } else if (!strcmp(k
, "commit.cleanup")) {
630 return git_config_string(&cleanup_arg
, k
, v
);
631 } else if (!strcmp(k
, "merge.ff")) {
632 int boolval
= git_parse_maybe_bool(v
);
634 fast_forward
= boolval
? FF_ALLOW
: FF_NO
;
635 } else if (v
&& !strcmp(v
, "only")) {
636 fast_forward
= FF_ONLY
;
637 } /* do not barf on values from future versions of git */
639 } else if (!strcmp(k
, "merge.defaulttoupstream")) {
640 default_to_upstream
= git_config_bool(k
, v
);
642 } else if (!strcmp(k
, "commit.gpgsign")) {
643 sign_commit
= git_config_bool(k
, v
) ? "" : NULL
;
645 } else if (!strcmp(k
, "gpg.mintrustlevel")) {
646 check_trust_level
= 0;
647 } else if (!strcmp(k
, "merge.autostash")) {
648 autostash
= git_config_bool(k
, v
);
652 status
= fmt_merge_msg_config(k
, v
, ctx
, cb
);
655 return git_diff_ui_config(k
, v
, ctx
, cb
);
658 static int read_tree_trivial(struct object_id
*common
, struct object_id
*head
,
659 struct object_id
*one
)
662 struct tree
*trees
[MAX_UNPACK_TREES
];
663 struct tree_desc t
[MAX_UNPACK_TREES
];
664 struct unpack_trees_options opts
;
666 memset(&opts
, 0, sizeof(opts
));
668 opts
.src_index
= the_repository
->index
;
669 opts
.dst_index
= the_repository
->index
;
671 opts
.verbose_update
= 1;
672 opts
.trivial_merges_only
= 1;
674 opts
.preserve_ignored
= 0; /* FIXME: !overwrite_ignore */
675 trees
[nr_trees
] = parse_tree_indirect(common
);
676 if (!trees
[nr_trees
++])
678 trees
[nr_trees
] = parse_tree_indirect(head
);
679 if (!trees
[nr_trees
++])
681 trees
[nr_trees
] = parse_tree_indirect(one
);
682 if (!trees
[nr_trees
++])
684 opts
.fn
= threeway_merge
;
685 cache_tree_free(&the_repository
->index
->cache_tree
);
686 for (i
= 0; i
< nr_trees
; i
++) {
687 parse_tree(trees
[i
]);
688 init_tree_desc(t
+i
, &trees
[i
]->object
.oid
,
689 trees
[i
]->buffer
, trees
[i
]->size
);
691 if (unpack_trees(nr_trees
, t
, &opts
))
696 static void write_tree_trivial(struct object_id
*oid
)
698 if (write_index_as_tree(oid
, the_repository
->index
, get_index_file(), 0, NULL
))
699 die(_("git write-tree failed to write a tree"));
702 static int try_merge_strategy(const char *strategy
, struct commit_list
*common
,
703 struct commit_list
*remoteheads
,
706 const char *head_arg
= "HEAD";
708 if (repo_refresh_and_write_index(the_repository
, REFRESH_QUIET
,
709 SKIP_IF_UNCHANGED
, 0, NULL
, NULL
,
711 die(_("Unable to write index."));
713 if (!strcmp(strategy
, "recursive") || !strcmp(strategy
, "subtree") ||
714 !strcmp(strategy
, "ort")) {
715 struct lock_file lock
= LOCK_INIT
;
717 struct commit
*result
;
718 struct commit_list
*reversed
= NULL
;
719 struct merge_options o
;
720 struct commit_list
*j
;
722 if (remoteheads
->next
) {
723 error(_("Not handling anything other than two heads merge."));
727 init_ui_merge_options(&o
, the_repository
);
728 if (!strcmp(strategy
, "subtree"))
729 o
.subtree_shift
= "";
731 o
.show_rename_progress
=
732 show_progress
== -1 ? isatty(2) : show_progress
;
734 for (x
= 0; x
< xopts
.nr
; x
++)
735 if (parse_merge_opt(&o
, xopts
.v
[x
]))
736 die(_("unknown strategy option: -X%s"), xopts
.v
[x
]);
738 o
.branch1
= head_arg
;
739 o
.branch2
= merge_remote_util(remoteheads
->item
)->name
;
741 for (j
= common
; j
; j
= j
->next
)
742 commit_list_insert(j
->item
, &reversed
);
744 repo_hold_locked_index(the_repository
, &lock
,
746 if (!strcmp(strategy
, "ort"))
747 clean
= merge_ort_recursive(&o
, head
, remoteheads
->item
,
750 clean
= merge_recursive(&o
, head
, remoteheads
->item
,
752 free_commit_list(reversed
);
755 rollback_lock_file(&lock
);
758 if (write_locked_index(the_repository
->index
, &lock
,
759 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
760 die(_("unable to write %s"), get_index_file());
761 return clean
? 0 : 1;
763 return try_merge_command(the_repository
,
764 strategy
, xopts
.nr
, xopts
.v
,
765 common
, head_arg
, remoteheads
);
769 static void count_diff_files(struct diff_queue_struct
*q
,
770 struct diff_options
*opt UNUSED
, void *data
)
777 static int count_unmerged_entries(void)
781 for (i
= 0; i
< the_repository
->index
->cache_nr
; i
++)
782 if (ce_stage(the_repository
->index
->cache
[i
]))
788 static void add_strategies(const char *string
, unsigned attr
)
793 struct string_list list
= STRING_LIST_INIT_DUP
;
794 struct string_list_item
*item
;
795 string_list_split(&list
, string
, ' ', -1);
796 for_each_string_list_item(item
, &list
)
797 append_strategy(get_strategy(item
->string
));
798 string_list_clear(&list
, 0);
801 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
802 if (all_strategy
[i
].attr
& attr
)
803 append_strategy(&all_strategy
[i
]);
807 static void read_merge_msg(struct strbuf
*msg
)
809 const char *filename
= git_path_merge_msg(the_repository
);
811 if (strbuf_read_file(msg
, filename
, 0) < 0)
812 die_errno(_("Could not read from '%s'"), filename
);
815 static void write_merge_state(struct commit_list
*);
816 static void abort_commit(struct commit_list
*remoteheads
, const char *err_msg
)
819 error("%s", err_msg
);
821 _("Not committing merge; use 'git commit' to complete the merge.\n"));
822 write_merge_state(remoteheads
);
826 static const char merge_editor_comment
[] =
827 N_("Please enter a commit message to explain why this merge is necessary,\n"
828 "especially if it merges an updated upstream into a topic branch.\n"
831 static const char scissors_editor_comment
[] =
832 N_("An empty message aborts the commit.\n");
834 static const char no_scissors_editor_comment
[] =
835 N_("Lines starting with '%s' will be ignored, and an empty message aborts\n"
838 static void write_merge_heads(struct commit_list
*);
839 static void prepare_to_commit(struct commit_list
*remoteheads
)
841 struct strbuf msg
= STRBUF_INIT
;
842 const char *index_file
= get_index_file();
847 if (run_commit_hook(0 < option_edit
, index_file
, &invoked_hook
,
848 "pre-merge-commit", NULL
))
849 abort_commit(remoteheads
, NULL
);
851 * Re-read the index as pre-merge-commit hook could have updated it,
852 * and write it out as a tree. We must do this before we invoke
853 * the editor and after we invoke run_status above.
856 discard_index(the_repository
->index
);
858 read_index_from(the_repository
->index
, index_file
, get_git_dir());
859 strbuf_addbuf(&msg
, &merge_msg
);
861 BUG("the control must not reach here under --squash");
862 if (0 < option_edit
) {
863 strbuf_addch(&msg
, '\n');
864 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
) {
865 wt_status_append_cut_line(&msg
);
866 strbuf_commented_addf(&msg
, comment_line_str
, "\n");
868 strbuf_commented_addf(&msg
, comment_line_str
,
869 _(merge_editor_comment
));
870 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
)
871 strbuf_commented_addf(&msg
, comment_line_str
,
872 _(scissors_editor_comment
));
874 strbuf_commented_addf(&msg
, comment_line_str
,
875 _(no_scissors_editor_comment
), comment_line_str
);
878 append_signoff(&msg
, ignored_log_message_bytes(msg
.buf
, msg
.len
), 0);
879 write_merge_heads(remoteheads
);
880 write_file_buf(git_path_merge_msg(the_repository
), msg
.buf
, msg
.len
);
881 if (run_commit_hook(0 < option_edit
, get_index_file(), NULL
,
882 "prepare-commit-msg",
883 git_path_merge_msg(the_repository
), "merge", NULL
))
884 abort_commit(remoteheads
, NULL
);
885 if (0 < option_edit
) {
886 if (launch_editor(git_path_merge_msg(the_repository
), NULL
, NULL
))
887 abort_commit(remoteheads
, NULL
);
890 if (!no_verify
&& run_commit_hook(0 < option_edit
, get_index_file(),
892 git_path_merge_msg(the_repository
), NULL
))
893 abort_commit(remoteheads
, NULL
);
895 read_merge_msg(&msg
);
896 cleanup_message(&msg
, cleanup_mode
, 0);
898 abort_commit(remoteheads
, _("Empty commit message."));
899 strbuf_release(&merge_msg
);
900 strbuf_addbuf(&merge_msg
, &msg
);
901 strbuf_release(&msg
);
904 static int merge_trivial(struct commit
*head
, struct commit_list
*remoteheads
)
906 struct object_id result_tree
, result_commit
;
907 struct commit_list
*parents
= NULL
, **pptr
= &parents
;
909 if (repo_refresh_and_write_index(the_repository
, REFRESH_QUIET
,
910 SKIP_IF_UNCHANGED
, 0, NULL
, NULL
,
912 return error(_("Unable to write index."));
914 write_tree_trivial(&result_tree
);
915 printf(_("Wonderful.\n"));
916 pptr
= commit_list_append(head
, pptr
);
917 pptr
= commit_list_append(remoteheads
->item
, pptr
);
918 prepare_to_commit(remoteheads
);
919 if (commit_tree(merge_msg
.buf
, merge_msg
.len
, &result_tree
, parents
,
920 &result_commit
, NULL
, sign_commit
))
921 die(_("failed to write commit object"));
922 finish(head
, remoteheads
, &result_commit
, "In-index merge");
924 remove_merge_branch_state(the_repository
);
925 free_commit_list(parents
);
929 static int finish_automerge(struct commit
*head
,
931 struct commit_list
*common
,
932 struct commit_list
*remoteheads
,
933 struct object_id
*result_tree
,
934 const char *wt_strategy
)
936 struct commit_list
*parents
= NULL
;
937 struct strbuf buf
= STRBUF_INIT
;
938 struct object_id result_commit
;
940 write_tree_trivial(result_tree
);
941 free_commit_list(common
);
942 parents
= remoteheads
;
943 if (!head_subsumed
|| fast_forward
== FF_NO
)
944 commit_list_insert(head
, &parents
);
945 prepare_to_commit(remoteheads
);
946 if (commit_tree(merge_msg
.buf
, merge_msg
.len
, result_tree
, parents
,
947 &result_commit
, NULL
, sign_commit
))
948 die(_("failed to write commit object"));
949 strbuf_addf(&buf
, "Merge made by the '%s' strategy.", wt_strategy
);
950 finish(head
, remoteheads
, &result_commit
, buf
.buf
);
952 strbuf_release(&buf
);
953 remove_merge_branch_state(the_repository
);
954 free_commit_list(parents
);
958 static int suggest_conflicts(void)
960 const char *filename
;
962 struct strbuf msgbuf
= STRBUF_INIT
;
964 filename
= git_path_merge_msg(the_repository
);
965 fp
= xfopen(filename
, "a");
968 * We can't use cleanup_mode because if we're not using the editor,
969 * get_cleanup_mode will return COMMIT_MSG_CLEANUP_SPACE instead, even
970 * though the message is meant to be processed later by git-commit.
971 * Thus, we will get the cleanup mode which is returned when we _are_
974 append_conflicts_hint(the_repository
->index
, &msgbuf
,
975 get_cleanup_mode(cleanup_arg
, 1));
976 fputs(msgbuf
.buf
, fp
);
977 strbuf_release(&msgbuf
);
979 repo_rerere(the_repository
, allow_rerere_auto
);
980 printf(_("Automatic merge failed; "
981 "fix conflicts and then commit the result.\n"));
985 static int evaluate_result(void)
990 /* Check how many files differ. */
991 repo_init_revisions(the_repository
, &rev
, "");
992 setup_revisions(0, NULL
, &rev
, NULL
);
993 rev
.diffopt
.output_format
|=
994 DIFF_FORMAT_CALLBACK
;
995 rev
.diffopt
.format_callback
= count_diff_files
;
996 rev
.diffopt
.format_callback_data
= &cnt
;
997 run_diff_files(&rev
, 0);
1000 * Check how many unmerged entries are
1003 cnt
+= count_unmerged_entries();
1005 release_revisions(&rev
);
1010 * Pretend as if the user told us to merge with the remote-tracking
1011 * branch we have for the upstream of the current branch
1013 static int setup_with_upstream(const char ***argv
)
1015 struct branch
*branch
= branch_get(NULL
);
1020 die(_("No current branch."));
1021 if (!branch
->remote_name
)
1022 die(_("No remote for the current branch."));
1023 if (!branch
->merge_nr
)
1024 die(_("No default upstream defined for the current branch."));
1026 args
= xcalloc(st_add(branch
->merge_nr
, 1), sizeof(char *));
1027 for (i
= 0; i
< branch
->merge_nr
; i
++) {
1028 if (!branch
->merge
[i
]->dst
)
1029 die(_("No remote-tracking branch for %s from %s"),
1030 branch
->merge
[i
]->src
, branch
->remote_name
);
1031 args
[i
] = branch
->merge
[i
]->dst
;
1038 static void write_merge_heads(struct commit_list
*remoteheads
)
1040 struct commit_list
*j
;
1041 struct strbuf buf
= STRBUF_INIT
;
1043 for (j
= remoteheads
; j
; j
= j
->next
) {
1044 struct object_id
*oid
;
1045 struct commit
*c
= j
->item
;
1046 struct merge_remote_desc
*desc
;
1048 desc
= merge_remote_util(c
);
1049 if (desc
&& desc
->obj
) {
1050 oid
= &desc
->obj
->oid
;
1052 oid
= &c
->object
.oid
;
1054 strbuf_addf(&buf
, "%s\n", oid_to_hex(oid
));
1056 write_file_buf(git_path_merge_head(the_repository
), buf
.buf
, buf
.len
);
1059 if (fast_forward
== FF_NO
)
1060 strbuf_addstr(&buf
, "no-ff");
1061 write_file_buf(git_path_merge_mode(the_repository
), buf
.buf
, buf
.len
);
1062 strbuf_release(&buf
);
1065 static void write_merge_state(struct commit_list
*remoteheads
)
1067 write_merge_heads(remoteheads
);
1068 strbuf_addch(&merge_msg
, '\n');
1069 write_file_buf(git_path_merge_msg(the_repository
), merge_msg
.buf
,
1073 static int default_edit_option(void)
1075 static const char name
[] = "GIT_MERGE_AUTOEDIT";
1076 const char *e
= getenv(name
);
1077 struct stat st_stdin
, st_stdout
;
1080 /* an explicit -m msg without --[no-]edit */
1084 int v
= git_parse_maybe_bool(e
);
1086 die(_("Bad value '%s' in environment '%s'"), e
, name
);
1090 /* Use editor if stdin and stdout are the same and is a tty */
1091 return (!fstat(0, &st_stdin
) &&
1092 !fstat(1, &st_stdout
) &&
1093 isatty(0) && isatty(1) &&
1094 st_stdin
.st_dev
== st_stdout
.st_dev
&&
1095 st_stdin
.st_ino
== st_stdout
.st_ino
&&
1096 st_stdin
.st_mode
== st_stdout
.st_mode
);
1099 static struct commit_list
*reduce_parents(struct commit
*head_commit
,
1101 struct commit_list
*remoteheads
)
1103 struct commit_list
*parents
, **remotes
;
1106 * Is the current HEAD reachable from another commit being
1107 * merged? If so we do not want to record it as a parent of
1108 * the resulting merge, unless --no-ff is given. We will flip
1109 * this variable to 0 when we find HEAD among the independent
1110 * tips being merged.
1114 /* Find what parents to record by checking independent ones. */
1115 parents
= reduce_heads(remoteheads
);
1116 free_commit_list(remoteheads
);
1119 remotes
= &remoteheads
;
1121 struct commit
*commit
= pop_commit(&parents
);
1122 if (commit
== head_commit
)
1125 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1130 static void prepare_merge_message(struct strbuf
*merge_names
, struct strbuf
*merge_msg
)
1132 struct fmt_merge_msg_opts opts
;
1134 memset(&opts
, 0, sizeof(opts
));
1135 opts
.add_title
= !have_message
;
1136 opts
.shortlog_len
= shortlog_len
;
1137 opts
.credit_people
= (0 < option_edit
);
1138 opts
.into_name
= into_name
;
1140 fmt_merge_msg(merge_names
, merge_msg
, &opts
);
1142 strbuf_setlen(merge_msg
, merge_msg
->len
- 1);
1145 static void handle_fetch_head(struct commit_list
**remotes
, struct strbuf
*merge_names
)
1147 const char *filename
;
1149 struct strbuf fetch_head_file
= STRBUF_INIT
;
1150 const unsigned hexsz
= the_hash_algo
->hexsz
;
1153 merge_names
= &fetch_head_file
;
1155 filename
= git_path_fetch_head(the_repository
);
1156 fd
= xopen(filename
, O_RDONLY
);
1158 if (strbuf_read(merge_names
, fd
, 0) < 0)
1159 die_errno(_("could not read '%s'"), filename
);
1161 die_errno(_("could not close '%s'"), filename
);
1163 for (pos
= 0; pos
< merge_names
->len
; pos
= npos
) {
1164 struct object_id oid
;
1166 struct commit
*commit
;
1168 ptr
= strchr(merge_names
->buf
+ pos
, '\n');
1170 npos
= ptr
- merge_names
->buf
+ 1;
1172 npos
= merge_names
->len
;
1174 if (npos
- pos
< hexsz
+ 2 ||
1175 get_oid_hex(merge_names
->buf
+ pos
, &oid
))
1176 commit
= NULL
; /* bad */
1177 else if (memcmp(merge_names
->buf
+ pos
+ hexsz
, "\t\t", 2))
1178 continue; /* not-for-merge */
1180 char saved
= merge_names
->buf
[pos
+ hexsz
];
1181 merge_names
->buf
[pos
+ hexsz
] = '\0';
1182 commit
= get_merge_parent(merge_names
->buf
+ pos
);
1183 merge_names
->buf
[pos
+ hexsz
] = saved
;
1188 die(_("not something we can merge in %s: %s"),
1189 filename
, merge_names
->buf
+ pos
);
1191 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1194 if (merge_names
== &fetch_head_file
)
1195 strbuf_release(&fetch_head_file
);
1198 static struct commit_list
*collect_parents(struct commit
*head_commit
,
1200 int argc
, const char **argv
,
1201 struct strbuf
*merge_msg
)
1204 struct commit_list
*remoteheads
= NULL
;
1205 struct commit_list
**remotes
= &remoteheads
;
1206 struct strbuf merge_names
= STRBUF_INIT
, *autogen
= NULL
;
1208 if (merge_msg
&& (!have_message
|| shortlog_len
))
1209 autogen
= &merge_names
;
1212 remotes
= &commit_list_insert(head_commit
, remotes
)->next
;
1214 if (argc
== 1 && !strcmp(argv
[0], "FETCH_HEAD")) {
1215 handle_fetch_head(remotes
, autogen
);
1216 remoteheads
= reduce_parents(head_commit
, head_subsumed
, remoteheads
);
1218 for (i
= 0; i
< argc
; i
++) {
1219 struct commit
*commit
= get_merge_parent(argv
[i
]);
1221 help_unknown_ref(argv
[i
], "merge",
1222 _("not something we can merge"));
1223 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1225 remoteheads
= reduce_parents(head_commit
, head_subsumed
, remoteheads
);
1227 struct commit_list
*p
;
1228 for (p
= remoteheads
; p
; p
= p
->next
)
1229 merge_name(merge_remote_util(p
->item
)->name
, autogen
);
1234 prepare_merge_message(autogen
, merge_msg
);
1235 strbuf_release(autogen
);
1241 static int merging_a_throwaway_tag(struct commit
*commit
)
1244 struct object_id oid
;
1245 int is_throwaway_tag
= 0;
1247 /* Are we merging a tag? */
1248 if (!merge_remote_util(commit
) ||
1249 !merge_remote_util(commit
)->obj
||
1250 merge_remote_util(commit
)->obj
->type
!= OBJ_TAG
)
1251 return is_throwaway_tag
;
1254 * Now we know we are merging a tag object. Are we downstream
1255 * and following the tags from upstream? If so, we must have
1256 * the tag object pointed at by "refs/tags/$T" where $T is the
1257 * tagname recorded in the tag object. We want to allow such
1258 * a "just to catch up" merge to fast-forward.
1260 * Otherwise, we are playing an integrator's role, making a
1261 * merge with a throw-away tag from a contributor with
1262 * something like "git pull $contributor $signed_tag".
1263 * We want to forbid such a merge from fast-forwarding
1264 * by default; otherwise we would not keep the signature
1267 tag_ref
= xstrfmt("refs/tags/%s",
1268 ((struct tag
*)merge_remote_util(commit
)->obj
)->tag
);
1269 if (!refs_read_ref(get_main_ref_store(the_repository
), tag_ref
, &oid
) &&
1270 oideq(&oid
, &merge_remote_util(commit
)->obj
->oid
))
1271 is_throwaway_tag
= 0;
1273 is_throwaway_tag
= 1;
1275 return is_throwaway_tag
;
1278 int cmd_merge(int argc
, const char **argv
, const char *prefix
)
1280 struct object_id result_tree
, stash
, head_oid
;
1281 struct commit
*head_commit
;
1282 struct strbuf buf
= STRBUF_INIT
;
1283 int i
, ret
= 0, head_subsumed
;
1284 int best_cnt
= -1, merge_was_ok
= 0, automerge_was_ok
= 0;
1285 struct commit_list
*common
= NULL
;
1286 const char *best_strategy
= NULL
, *wt_strategy
= NULL
;
1287 struct commit_list
*remoteheads
= NULL
, *p
;
1288 void *branch_to_free
;
1289 int orig_argc
= argc
;
1291 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1292 usage_with_options(builtin_merge_usage
, builtin_merge_options
);
1294 prepare_repo_settings(the_repository
);
1295 the_repository
->settings
.command_requires_full_index
= 0;
1298 * Check if we are _not_ on a detached HEAD, i.e. if there is a
1301 branch
= branch_to_free
= refs_resolve_refdup(get_main_ref_store(the_repository
),
1302 "HEAD", 0, &head_oid
,
1305 skip_prefix(branch
, "refs/heads/", &branch
);
1307 if (!pull_twohead
) {
1308 char *default_strategy
= getenv("GIT_TEST_MERGE_ALGORITHM");
1309 if (default_strategy
&& !strcmp(default_strategy
, "ort"))
1310 pull_twohead
= xstrdup("ort");
1313 init_diff_ui_defaults();
1314 git_config(git_merge_config
, NULL
);
1316 if (!branch
|| is_null_oid(&head_oid
))
1319 head_commit
= lookup_commit_or_die(&head_oid
, "HEAD");
1321 if (branch_mergeoptions
)
1322 parse_branch_merge_options(branch_mergeoptions
);
1323 argc
= parse_options(argc
, argv
, prefix
, builtin_merge_options
,
1324 builtin_merge_usage
, 0);
1325 if (shortlog_len
< 0)
1326 shortlog_len
= (merge_log_config
> 0) ? merge_log_config
: 0;
1328 if (verbosity
< 0 && show_progress
== -1)
1331 if (abort_current_merge
) {
1333 const char *nargv
[] = {"reset", "--merge", NULL
};
1334 char stash_oid_hex
[GIT_MAX_HEXSZ
+ 1];
1335 struct object_id stash_oid
= {0};
1338 usage_msg_opt(_("--abort expects no arguments"),
1339 builtin_merge_usage
, builtin_merge_options
);
1341 if (!file_exists(git_path_merge_head(the_repository
)))
1342 die(_("There is no merge to abort (MERGE_HEAD missing)."));
1344 if (!refs_read_ref(get_main_ref_store(the_repository
), "MERGE_AUTOSTASH", &stash_oid
))
1345 refs_delete_ref(get_main_ref_store(the_repository
),
1346 "", "MERGE_AUTOSTASH", &stash_oid
,
1349 /* Invoke 'git reset --merge' */
1350 ret
= cmd_reset(nargc
, nargv
, prefix
);
1352 if (!is_null_oid(&stash_oid
)) {
1353 oid_to_hex_r(stash_oid_hex
, &stash_oid
);
1354 apply_autostash_oid(stash_oid_hex
);
1360 if (quit_current_merge
) {
1362 usage_msg_opt(_("--quit expects no arguments"),
1363 builtin_merge_usage
,
1364 builtin_merge_options
);
1366 remove_merge_branch_state(the_repository
);
1370 if (continue_current_merge
) {
1372 const char *nargv
[] = {"commit", NULL
};
1375 usage_msg_opt(_("--continue expects no arguments"),
1376 builtin_merge_usage
, builtin_merge_options
);
1378 if (!file_exists(git_path_merge_head(the_repository
)))
1379 die(_("There is no merge in progress (MERGE_HEAD missing)."));
1381 /* Invoke 'git commit' */
1382 ret
= cmd_commit(nargc
, nargv
, prefix
);
1386 if (repo_read_index_unmerged(the_repository
))
1387 die_resolve_conflict("merge");
1389 if (file_exists(git_path_merge_head(the_repository
))) {
1391 * There is no unmerged entry, don't advise 'git
1392 * add/rm <file>', just 'git commit'.
1394 if (advice_enabled(ADVICE_RESOLVE_CONFLICT
))
1395 die(_("You have not concluded your merge (MERGE_HEAD exists).\n"
1396 "Please, commit your changes before you merge."));
1398 die(_("You have not concluded your merge (MERGE_HEAD exists)."));
1400 if (refs_ref_exists(get_main_ref_store(the_repository
), "CHERRY_PICK_HEAD")) {
1401 if (advice_enabled(ADVICE_RESOLVE_CONFLICT
))
1402 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
1403 "Please, commit your changes before you merge."));
1405 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists)."));
1407 resolve_undo_clear_index(the_repository
->index
);
1409 if (option_edit
< 0)
1410 option_edit
= default_edit_option();
1412 cleanup_mode
= get_cleanup_mode(cleanup_arg
, 0 < option_edit
);
1418 if (fast_forward
== FF_NO
)
1419 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--no-ff.");
1420 if (option_commit
> 0)
1421 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--commit.");
1423 * squash can now silently disable option_commit - this is not
1424 * a problem as it is only overriding the default, not a user
1430 if (option_commit
< 0)
1434 if (default_to_upstream
)
1435 argc
= setup_with_upstream(&argv
);
1437 die(_("No commit specified and merge.defaultToUpstream not set."));
1438 } else if (argc
== 1 && !strcmp(argv
[0], "-")) {
1443 usage_with_options(builtin_merge_usage
,
1444 builtin_merge_options
);
1448 * If the merged head is a valid one there is no reason
1449 * to forbid "git merge" into a branch yet to be born.
1450 * We do the same for "git pull".
1452 struct object_id
*remote_head_oid
;
1454 die(_("Squash commit into empty head not supported yet"));
1455 if (fast_forward
== FF_NO
)
1456 die(_("Non-fast-forward commit does not make sense into "
1458 remoteheads
= collect_parents(head_commit
, &head_subsumed
,
1461 die(_("%s - not something we can merge"), argv
[0]);
1462 if (remoteheads
->next
)
1463 die(_("Can merge only exactly one commit into empty head"));
1465 if (verify_signatures
)
1466 verify_merge_signature(remoteheads
->item
, verbosity
,
1469 remote_head_oid
= &remoteheads
->item
->object
.oid
;
1470 read_empty(remote_head_oid
);
1471 refs_update_ref(get_main_ref_store(the_repository
),
1472 "initial pull", "HEAD", remote_head_oid
, NULL
,
1474 UPDATE_REFS_DIE_ON_ERR
);
1479 * All the rest are the commits being merged; prepare
1480 * the standard merge summary message to be appended
1481 * to the given message.
1483 remoteheads
= collect_parents(head_commit
, &head_subsumed
,
1484 argc
, argv
, &merge_msg
);
1486 if (!head_commit
|| !argc
)
1487 usage_with_options(builtin_merge_usage
,
1488 builtin_merge_options
);
1490 if (verify_signatures
) {
1491 for (p
= remoteheads
; p
; p
= p
->next
) {
1492 verify_merge_signature(p
->item
, verbosity
,
1497 strbuf_addstr(&buf
, "merge");
1498 for (p
= remoteheads
; p
; p
= p
->next
)
1499 strbuf_addf(&buf
, " %s", merge_remote_util(p
->item
)->name
);
1500 setenv("GIT_REFLOG_ACTION", buf
.buf
, 0);
1503 for (p
= remoteheads
; p
; p
= p
->next
) {
1504 struct commit
*commit
= p
->item
;
1505 strbuf_addf(&buf
, "GITHEAD_%s",
1506 oid_to_hex(&commit
->object
.oid
));
1507 setenv(buf
.buf
, merge_remote_util(commit
)->name
, 1);
1509 if (fast_forward
!= FF_ONLY
&& merging_a_throwaway_tag(commit
))
1510 fast_forward
= FF_NO
;
1513 if (!use_strategies
&& !pull_twohead
&&
1514 remoteheads
&& !remoteheads
->next
) {
1515 char *default_strategy
= getenv("GIT_TEST_MERGE_ALGORITHM");
1516 if (default_strategy
)
1517 append_strategy(get_strategy(default_strategy
));
1519 if (!use_strategies
) {
1521 ; /* already up-to-date */
1522 else if (!remoteheads
->next
)
1523 add_strategies(pull_twohead
, DEFAULT_TWOHEAD
);
1525 add_strategies(pull_octopus
, DEFAULT_OCTOPUS
);
1528 for (i
= 0; i
< use_strategies_nr
; i
++) {
1529 if (use_strategies
[i
]->attr
& NO_FAST_FORWARD
)
1530 fast_forward
= FF_NO
;
1531 if (use_strategies
[i
]->attr
& NO_TRIVIAL
)
1536 ; /* already up-to-date */
1537 else if (!remoteheads
->next
) {
1538 if (repo_get_merge_bases(the_repository
, head_commit
,
1539 remoteheads
->item
, &common
) < 0) {
1544 struct commit_list
*list
= remoteheads
;
1545 commit_list_insert(head_commit
, &list
);
1546 if (get_octopus_merge_bases(list
, &common
) < 0) {
1554 refs_update_ref(get_main_ref_store(the_repository
),
1555 "updating ORIG_HEAD", "ORIG_HEAD",
1556 &head_commit
->object
.oid
, NULL
, 0,
1557 UPDATE_REFS_DIE_ON_ERR
);
1559 if (remoteheads
&& !common
) {
1560 /* No common ancestors found. */
1561 if (!allow_unrelated_histories
)
1562 die(_("refusing to merge unrelated histories"));
1563 /* otherwise, we need a real merge. */
1564 } else if (!remoteheads
||
1565 (!remoteheads
->next
&& !common
->next
&&
1566 common
->item
== remoteheads
->item
)) {
1568 * If head can reach all the merge then we are up to date.
1569 * but first the most common case of merging one remote.
1571 finish_up_to_date();
1573 } else if (fast_forward
!= FF_NO
&& !remoteheads
->next
&&
1575 oideq(&common
->item
->object
.oid
, &head_commit
->object
.oid
)) {
1576 /* Again the most common case of merging one remote. */
1577 const char *msg
= have_message
?
1578 "Fast-forward (no commit created; -m option ignored)" :
1580 struct commit
*commit
;
1582 if (verbosity
>= 0) {
1583 printf(_("Updating %s..%s\n"),
1584 repo_find_unique_abbrev(the_repository
, &head_commit
->object
.oid
,
1586 repo_find_unique_abbrev(the_repository
, &remoteheads
->item
->object
.oid
,
1589 commit
= remoteheads
->item
;
1596 create_autostash_ref(the_repository
, "MERGE_AUTOSTASH");
1597 if (checkout_fast_forward(the_repository
,
1598 &head_commit
->object
.oid
,
1599 &commit
->object
.oid
,
1600 overwrite_ignore
)) {
1601 apply_autostash_ref(the_repository
, "MERGE_AUTOSTASH");
1606 finish(head_commit
, remoteheads
, &commit
->object
.oid
, msg
);
1607 remove_merge_branch_state(the_repository
);
1609 } else if (!remoteheads
->next
&& common
->next
)
1612 * We are not doing octopus and not fast-forward. Need
1615 else if (!remoteheads
->next
&& !common
->next
&& option_commit
) {
1617 * We are not doing octopus, not fast-forward, and have
1620 refresh_index(the_repository
->index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
1621 if (allow_trivial
&& fast_forward
!= FF_ONLY
) {
1623 * Must first ensure that index matches HEAD before
1624 * attempting a trivial merge.
1626 struct tree
*head_tree
= repo_get_commit_tree(the_repository
,
1628 struct strbuf sb
= STRBUF_INIT
;
1630 if (repo_index_has_changes(the_repository
, head_tree
,
1632 error(_("Your local changes to the following files would be overwritten by merge:\n %s"),
1634 strbuf_release(&sb
);
1639 /* See if it is really trivial. */
1640 git_committer_info(IDENT_STRICT
);
1641 printf(_("Trying really trivial in-index merge...\n"));
1642 if (!read_tree_trivial(&common
->item
->object
.oid
,
1643 &head_commit
->object
.oid
,
1644 &remoteheads
->item
->object
.oid
)) {
1645 ret
= merge_trivial(head_commit
, remoteheads
);
1648 printf(_("Nope.\n"));
1652 * An octopus. If we can reach all the remote we are up
1656 struct commit_list
*j
;
1658 for (j
= remoteheads
; j
; j
= j
->next
) {
1659 struct commit_list
*common_one
= NULL
;
1660 struct commit
*common_item
;
1663 * Here we *have* to calculate the individual
1664 * merge_bases again, otherwise "git merge HEAD^
1665 * HEAD^^" would be missed.
1667 if (repo_get_merge_bases(the_repository
, head_commit
,
1668 j
->item
, &common_one
) < 0)
1671 common_item
= common_one
->item
;
1672 free_commit_list(common_one
);
1673 if (!oideq(&common_item
->object
.oid
, &j
->item
->object
.oid
)) {
1679 finish_up_to_date();
1684 if (fast_forward
== FF_ONLY
)
1685 die_ff_impossible();
1688 create_autostash_ref(the_repository
, "MERGE_AUTOSTASH");
1690 /* We are going to make a new commit. */
1691 git_committer_info(IDENT_STRICT
);
1694 * At this point, we need a real merge. No matter what strategy
1695 * we use, it would operate on the index, possibly affecting the
1696 * working tree, and when resolved cleanly, have the desired
1697 * tree in the index -- this means that the index must be in
1698 * sync with the head commit. The strategies are responsible
1701 * Stash away the local changes so that we can try more than one
1702 * and/or recover from merge strategies bailing while leaving the
1703 * index and working tree polluted.
1705 if (save_state(&stash
))
1706 oidclr(&stash
, the_repository
->hash_algo
);
1708 for (i
= 0; i
< use_strategies_nr
; i
++) {
1711 printf(_("Rewinding the tree to pristine...\n"));
1712 restore_state(&head_commit
->object
.oid
, &stash
);
1714 if (use_strategies_nr
!= 1)
1715 printf(_("Trying merge strategy %s...\n"),
1716 use_strategies
[i
]->name
);
1718 * Remember which strategy left the state in the working
1721 wt_strategy
= use_strategies
[i
]->name
;
1723 ret
= try_merge_strategy(wt_strategy
,
1724 common
, remoteheads
,
1727 * The backend exits with 1 when conflicts are
1728 * left to be resolved, with 2 when it does not
1729 * handle the given merge at all.
1734 * This strategy worked; no point in trying
1738 best_strategy
= wt_strategy
;
1741 cnt
= (use_strategies_nr
> 1) ? evaluate_result() : 0;
1742 if (best_cnt
<= 0 || cnt
<= best_cnt
) {
1743 best_strategy
= wt_strategy
;
1750 * If we have a resulting tree, that means the strategy module
1751 * auto resolved the merge cleanly.
1753 if (merge_was_ok
&& option_commit
) {
1754 automerge_was_ok
= 1;
1755 ret
= finish_automerge(head_commit
, head_subsumed
,
1756 common
, remoteheads
,
1757 &result_tree
, wt_strategy
);
1762 * Pick the result from the best strategy and have the user fix
1765 if (!best_strategy
) {
1766 restore_state(&head_commit
->object
.oid
, &stash
);
1767 if (use_strategies_nr
> 1)
1769 _("No merge strategy handled the merge.\n"));
1771 fprintf(stderr
, _("Merge with strategy %s failed.\n"),
1772 use_strategies
[0]->name
);
1773 apply_autostash_ref(the_repository
, "MERGE_AUTOSTASH");
1776 } else if (best_strategy
== wt_strategy
)
1777 ; /* We already have its result in the working tree. */
1779 printf(_("Rewinding the tree to pristine...\n"));
1780 restore_state(&head_commit
->object
.oid
, &stash
);
1781 printf(_("Using the %s strategy to prepare resolving by hand.\n"),
1783 try_merge_strategy(best_strategy
, common
, remoteheads
,
1788 finish(head_commit
, remoteheads
, NULL
, NULL
);
1790 git_test_write_commit_graph_or_die();
1792 write_merge_state(remoteheads
);
1795 fprintf(stderr
, _("Automatic merge went well; "
1796 "stopped before committing as requested\n"));
1798 ret
= suggest_conflicts();
1800 printf(_("When finished, apply stashed changes with `git stash pop`\n"));
1803 if (!automerge_was_ok
) {
1804 free_commit_list(common
);
1805 free_commit_list(remoteheads
);
1807 strbuf_release(&buf
);
1808 free(branch_to_free
);
1811 discard_index(the_repository
->index
);