4 * Copyright (c) 2008 Miklos Vajna <vmiklos@frugalware.org>
6 * Based on git-merge.sh by Junio C Hamano.
9 #define USE_THE_INDEX_VARIABLE
15 #include "environment.h"
18 #include "object-name.h"
19 #include "parse-options.h"
21 #include "run-command.h"
24 #include "diff-merges.h"
31 #include "unpack-trees.h"
32 #include "cache-tree.h"
40 #include "merge-recursive.h"
41 #include "merge-ort-wrappers.h"
42 #include "resolve-undo.h"
44 #include "fmt-merge-msg.h"
45 #include "gpg-interface.h"
46 #include "sequencer.h"
47 #include "string-list.h"
52 #include "commit-reach.h"
53 #include "wt-status.h"
54 #include "commit-graph.h"
56 #define DEFAULT_TWOHEAD (1<<0)
57 #define DEFAULT_OCTOPUS (1<<1)
58 #define NO_FAST_FORWARD (1<<2)
59 #define NO_TRIVIAL (1<<3)
66 static const char * const builtin_merge_usage
[] = {
67 N_("git merge [<options>] [<commit>...]"),
69 "git merge --continue",
73 static int show_diffstat
= 1, shortlog_len
= -1, squash
;
74 static int option_commit
= -1;
75 static int option_edit
= -1;
76 static int allow_trivial
= 1, have_message
, verify_signatures
;
77 static int check_trust_level
= 1;
78 static int overwrite_ignore
= 1;
79 static struct strbuf merge_msg
= STRBUF_INIT
;
80 static struct strategy
**use_strategies
;
81 static size_t use_strategies_nr
, use_strategies_alloc
;
82 static const char **xopts
;
83 static size_t xopts_nr
, xopts_alloc
;
84 static const char *branch
;
85 static char *branch_mergeoptions
;
87 static int allow_rerere_auto
;
88 static int abort_current_merge
;
89 static int quit_current_merge
;
90 static int continue_current_merge
;
91 static int allow_unrelated_histories
;
92 static int show_progress
= -1;
93 static int default_to_upstream
= 1;
95 static const char *sign_commit
;
98 static char *into_name
;
100 static struct strategy all_strategy
[] = {
101 { "recursive", NO_TRIVIAL
},
102 { "octopus", DEFAULT_OCTOPUS
},
103 { "ort", DEFAULT_TWOHEAD
| NO_TRIVIAL
},
105 { "ours", NO_FAST_FORWARD
| NO_TRIVIAL
},
106 { "subtree", NO_FAST_FORWARD
| NO_TRIVIAL
},
109 static const char *pull_twohead
, *pull_octopus
;
117 static enum ff_type fast_forward
= FF_ALLOW
;
119 static const char *cleanup_arg
;
120 static enum commit_msg_cleanup_mode cleanup_mode
;
122 static int option_parse_message(const struct option
*opt
,
123 const char *arg
, int unset
)
125 struct strbuf
*buf
= opt
->value
;
128 strbuf_setlen(buf
, 0);
130 strbuf_addf(buf
, "%s%s", buf
->len
? "\n\n" : "", arg
);
133 return error(_("switch `m' requires a value"));
137 static enum parse_opt_result
option_read_message(struct parse_opt_ctx_t
*ctx
,
138 const struct option
*opt
,
139 const char *arg_not_used
,
142 struct strbuf
*buf
= opt
->value
;
145 BUG_ON_OPT_ARG(arg_not_used
);
147 BUG("-F cannot be negated");
152 } else if (ctx
->argc
> 1) {
156 return error(_("option `%s' requires a value"), opt
->long_name
);
159 strbuf_addch(buf
, '\n');
160 if (ctx
->prefix
&& !is_absolute_path(arg
))
161 arg
= prefix_filename(ctx
->prefix
, arg
);
162 if (strbuf_read_file(buf
, arg
, 0) < 0)
163 return error(_("could not read file '%s'"), arg
);
169 static struct strategy
*get_strategy(const char *name
)
172 struct strategy
*ret
;
173 static struct cmdnames main_cmds
, other_cmds
;
175 char *default_strategy
= getenv("GIT_TEST_MERGE_ALGORITHM");
180 if (default_strategy
&&
181 !strcmp(default_strategy
, "ort") &&
182 !strcmp(name
, "recursive")) {
186 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
187 if (!strcmp(name
, all_strategy
[i
].name
))
188 return &all_strategy
[i
];
191 struct cmdnames not_strategies
;
194 memset(¬_strategies
, 0, sizeof(struct cmdnames
));
195 load_command_list("git-merge-", &main_cmds
, &other_cmds
);
196 for (i
= 0; i
< main_cmds
.cnt
; i
++) {
198 struct cmdname
*ent
= main_cmds
.names
[i
];
199 for (j
= 0; !found
&& j
< ARRAY_SIZE(all_strategy
); j
++)
200 if (!strncmp(ent
->name
, all_strategy
[j
].name
, ent
->len
)
201 && !all_strategy
[j
].name
[ent
->len
])
204 add_cmdname(¬_strategies
, ent
->name
, ent
->len
);
206 exclude_cmds(&main_cmds
, ¬_strategies
);
208 if (!is_in_cmdlist(&main_cmds
, name
) && !is_in_cmdlist(&other_cmds
, name
)) {
209 fprintf(stderr
, _("Could not find merge strategy '%s'.\n"), name
);
210 fprintf(stderr
, _("Available strategies are:"));
211 for (i
= 0; i
< main_cmds
.cnt
; i
++)
212 fprintf(stderr
, " %s", main_cmds
.names
[i
]->name
);
213 fprintf(stderr
, ".\n");
214 if (other_cmds
.cnt
) {
215 fprintf(stderr
, _("Available custom strategies are:"));
216 for (i
= 0; i
< other_cmds
.cnt
; i
++)
217 fprintf(stderr
, " %s", other_cmds
.names
[i
]->name
);
218 fprintf(stderr
, ".\n");
223 CALLOC_ARRAY(ret
, 1);
224 ret
->name
= xstrdup(name
);
225 ret
->attr
= NO_TRIVIAL
;
229 static void append_strategy(struct strategy
*s
)
231 ALLOC_GROW(use_strategies
, use_strategies_nr
+ 1, use_strategies_alloc
);
232 use_strategies
[use_strategies_nr
++] = s
;
235 static int option_parse_strategy(const struct option
*opt
,
236 const char *name
, int unset
)
241 append_strategy(get_strategy(name
));
245 static int option_parse_x(const struct option
*opt
,
246 const char *arg
, int unset
)
251 ALLOC_GROW(xopts
, xopts_nr
+ 1, xopts_alloc
);
252 xopts
[xopts_nr
++] = xstrdup(arg
);
256 static int option_parse_n(const struct option
*opt
,
257 const char *arg
, int unset
)
260 show_diffstat
= unset
;
264 static struct option builtin_merge_options
[] = {
265 OPT_CALLBACK_F('n', NULL
, NULL
, NULL
,
266 N_("do not show a diffstat at the end of the merge"),
267 PARSE_OPT_NOARG
, option_parse_n
),
268 OPT_BOOL(0, "stat", &show_diffstat
,
269 N_("show a diffstat at the end of the merge")),
270 OPT_BOOL(0, "summary", &show_diffstat
, N_("(synonym to --stat)")),
271 { OPTION_INTEGER
, 0, "log", &shortlog_len
, N_("n"),
272 N_("add (at most <n>) entries from shortlog to merge commit message"),
273 PARSE_OPT_OPTARG
, NULL
, DEFAULT_MERGE_LOG_LEN
},
274 OPT_BOOL(0, "squash", &squash
,
275 N_("create a single commit instead of doing a merge")),
276 OPT_BOOL(0, "commit", &option_commit
,
277 N_("perform a commit if the merge succeeds (default)")),
278 OPT_BOOL('e', "edit", &option_edit
,
279 N_("edit message before committing")),
280 OPT_CLEANUP(&cleanup_arg
),
281 OPT_SET_INT(0, "ff", &fast_forward
, N_("allow fast-forward (default)"), FF_ALLOW
),
282 OPT_SET_INT_F(0, "ff-only", &fast_forward
,
283 N_("abort if fast-forward is not possible"),
284 FF_ONLY
, PARSE_OPT_NONEG
),
285 OPT_RERERE_AUTOUPDATE(&allow_rerere_auto
),
286 OPT_BOOL(0, "verify-signatures", &verify_signatures
,
287 N_("verify that the named commit has a valid GPG signature")),
288 OPT_CALLBACK('s', "strategy", &use_strategies
, N_("strategy"),
289 N_("merge strategy to use"), option_parse_strategy
),
290 OPT_CALLBACK('X', "strategy-option", &xopts
, N_("option=value"),
291 N_("option for selected merge strategy"), option_parse_x
),
292 OPT_CALLBACK('m', "message", &merge_msg
, N_("message"),
293 N_("merge commit message (for a non-fast-forward merge)"),
294 option_parse_message
),
295 { OPTION_LOWLEVEL_CALLBACK
, 'F', "file", &merge_msg
, N_("path"),
296 N_("read message from file"), PARSE_OPT_NONEG
,
297 NULL
, 0, option_read_message
},
298 OPT_STRING(0, "into-name", &into_name
, N_("name"),
299 N_("use <name> instead of the real target")),
300 OPT__VERBOSITY(&verbosity
),
301 OPT_BOOL(0, "abort", &abort_current_merge
,
302 N_("abort the current in-progress merge")),
303 OPT_BOOL(0, "quit", &quit_current_merge
,
304 N_("--abort but leave index and working tree alone")),
305 OPT_BOOL(0, "continue", &continue_current_merge
,
306 N_("continue the current in-progress merge")),
307 OPT_BOOL(0, "allow-unrelated-histories", &allow_unrelated_histories
,
308 N_("allow merging unrelated histories")),
309 OPT_SET_INT(0, "progress", &show_progress
, N_("force progress reporting"), 1),
310 { OPTION_STRING
, 'S', "gpg-sign", &sign_commit
, N_("key-id"),
311 N_("GPG sign commit"), PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
312 OPT_AUTOSTASH(&autostash
),
313 OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore
, N_("update ignored files (default)")),
314 OPT_BOOL(0, "signoff", &signoff
, N_("add a Signed-off-by trailer")),
315 OPT_BOOL(0, "no-verify", &no_verify
, N_("bypass pre-merge-commit and commit-msg hooks")),
319 static int save_state(struct object_id
*stash
)
322 struct child_process cp
= CHILD_PROCESS_INIT
;
323 struct strbuf buffer
= STRBUF_INIT
;
324 struct lock_file lock_file
= LOCK_INIT
;
328 fd
= repo_hold_locked_index(the_repository
, &lock_file
, 0);
329 refresh_index(&the_index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
331 repo_update_index_if_able(the_repository
, &lock_file
);
332 rollback_lock_file(&lock_file
);
334 strvec_pushl(&cp
.args
, "stash", "create", NULL
);
338 if (start_command(&cp
))
339 die(_("could not run stash."));
340 len
= strbuf_read(&buffer
, cp
.out
, 1024);
343 if (finish_command(&cp
) || len
< 0)
344 die(_("stash failed"));
345 else if (!len
) /* no changes */
347 strbuf_setlen(&buffer
, buffer
.len
-1);
348 if (repo_get_oid(the_repository
, buffer
.buf
, stash
))
349 die(_("not a valid object: %s"), buffer
.buf
);
352 strbuf_release(&buffer
);
356 static void read_empty(const struct object_id
*oid
)
358 struct child_process cmd
= CHILD_PROCESS_INIT
;
360 strvec_pushl(&cmd
.args
, "read-tree", "-m", "-u", empty_tree_oid_hex(),
361 oid_to_hex(oid
), NULL
);
364 if (run_command(&cmd
))
365 die(_("read-tree failed"));
368 static void reset_hard(const struct object_id
*oid
)
370 struct child_process cmd
= CHILD_PROCESS_INIT
;
372 strvec_pushl(&cmd
.args
, "read-tree", "-v", "--reset", "-u",
373 oid_to_hex(oid
), NULL
);
376 if (run_command(&cmd
))
377 die(_("read-tree failed"));
380 static void restore_state(const struct object_id
*head
,
381 const struct object_id
*stash
)
383 struct child_process cmd
= CHILD_PROCESS_INIT
;
387 if (is_null_oid(stash
))
390 strvec_pushl(&cmd
.args
, "stash", "apply", "--index", "--quiet", NULL
);
391 strvec_push(&cmd
.args
, oid_to_hex(stash
));
394 * It is OK to ignore error here, for example when there was
395 * nothing to restore.
401 discard_index(&the_index
);
402 if (repo_read_index(the_repository
) < 0)
403 die(_("could not read index"));
406 /* This is called when no merge was necessary. */
407 static void finish_up_to_date(void)
409 if (verbosity
>= 0) {
411 puts(_("Already up to date. (nothing to squash)"));
413 puts(_("Already up to date."));
415 remove_merge_branch_state(the_repository
);
418 static void squash_message(struct commit
*commit
, struct commit_list
*remoteheads
)
421 struct strbuf out
= STRBUF_INIT
;
422 struct commit_list
*j
;
423 struct pretty_print_context ctx
= {0};
425 printf(_("Squash commit -- not updating HEAD\n"));
427 repo_init_revisions(the_repository
, &rev
, NULL
);
428 diff_merges_suppress(&rev
);
429 rev
.commit_format
= CMIT_FMT_MEDIUM
;
431 commit
->object
.flags
|= UNINTERESTING
;
432 add_pending_object(&rev
, &commit
->object
, NULL
);
434 for (j
= remoteheads
; j
; j
= j
->next
)
435 add_pending_object(&rev
, &j
->item
->object
, NULL
);
437 setup_revisions(0, NULL
, &rev
, NULL
);
438 if (prepare_revision_walk(&rev
))
439 die(_("revision walk setup failed"));
441 ctx
.abbrev
= rev
.abbrev
;
442 ctx
.date_mode
= rev
.date_mode
;
443 ctx
.fmt
= rev
.commit_format
;
445 strbuf_addstr(&out
, "Squashed commit of the following:\n");
446 while ((commit
= get_revision(&rev
)) != NULL
) {
447 strbuf_addch(&out
, '\n');
448 strbuf_addf(&out
, "commit %s\n",
449 oid_to_hex(&commit
->object
.oid
));
450 pretty_print_commit(&ctx
, commit
, &out
);
452 write_file_buf(git_path_squash_msg(the_repository
), out
.buf
, out
.len
);
453 strbuf_release(&out
);
454 release_revisions(&rev
);
457 static void finish(struct commit
*head_commit
,
458 struct commit_list
*remoteheads
,
459 const struct object_id
*new_head
, const char *msg
)
461 struct strbuf reflog_message
= STRBUF_INIT
;
462 const struct object_id
*head
= &head_commit
->object
.oid
;
465 strbuf_addstr(&reflog_message
, getenv("GIT_REFLOG_ACTION"));
469 strbuf_addf(&reflog_message
, "%s: %s",
470 getenv("GIT_REFLOG_ACTION"), msg
);
473 squash_message(head_commit
, remoteheads
);
475 if (verbosity
>= 0 && !merge_msg
.len
)
476 printf(_("No merge message -- not updating HEAD\n"));
478 update_ref(reflog_message
.buf
, "HEAD", new_head
, head
,
479 0, UPDATE_REFS_DIE_ON_ERR
);
481 * We ignore errors in 'gc --auto', since the
482 * user should see them.
484 run_auto_maintenance(verbosity
< 0);
487 if (new_head
&& show_diffstat
) {
488 struct diff_options opts
;
489 repo_diff_setup(the_repository
, &opts
);
490 opts
.stat_width
= -1; /* use full terminal width */
491 opts
.stat_graph_width
= -1; /* respect statGraphWidth config */
492 opts
.output_format
|=
493 DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
494 opts
.detect_rename
= DIFF_DETECT_RENAME
;
495 diff_setup_done(&opts
);
496 diff_tree_oid(head
, new_head
, "", &opts
);
501 /* Run a post-merge hook */
502 run_hooks_l("post-merge", squash
? "1" : "0", NULL
);
505 apply_autostash(git_path_merge_autostash(the_repository
));
506 strbuf_release(&reflog_message
);
509 /* Get the name for the merge commit's message. */
510 static void merge_name(const char *remote
, struct strbuf
*msg
)
512 struct commit
*remote_head
;
513 struct object_id branch_head
;
514 struct strbuf bname
= STRBUF_INIT
;
515 struct merge_remote_desc
*desc
;
517 char *found_ref
= NULL
;
520 strbuf_branchname(&bname
, remote
, 0);
523 oidclr(&branch_head
);
524 remote_head
= get_merge_parent(remote
);
526 die(_("'%s' does not point to a commit"), remote
);
528 if (repo_dwim_ref(the_repository
, remote
, strlen(remote
), &branch_head
,
529 &found_ref
, 0) > 0) {
530 if (starts_with(found_ref
, "refs/heads/")) {
531 strbuf_addf(msg
, "%s\t\tbranch '%s' of .\n",
532 oid_to_hex(&branch_head
), remote
);
535 if (starts_with(found_ref
, "refs/tags/")) {
536 strbuf_addf(msg
, "%s\t\ttag '%s' of .\n",
537 oid_to_hex(&branch_head
), remote
);
540 if (starts_with(found_ref
, "refs/remotes/")) {
541 strbuf_addf(msg
, "%s\t\tremote-tracking branch '%s' of .\n",
542 oid_to_hex(&branch_head
), remote
);
547 /* See if remote matches <name>^^^.. or <name>~<number> */
548 for (len
= 0, ptr
= remote
+ strlen(remote
);
549 remote
< ptr
&& ptr
[-1] == '^';
556 ptr
= strrchr(remote
, '~');
558 int seen_nonzero
= 0;
561 while (*++ptr
&& isdigit(*ptr
)) {
562 seen_nonzero
|= (*ptr
!= '0');
566 len
= 0; /* not ...~<number> */
567 else if (seen_nonzero
)
570 early
= 1; /* "name~" is "name~1"! */
574 struct strbuf truname
= STRBUF_INIT
;
575 strbuf_addf(&truname
, "refs/heads/%s", remote
);
576 strbuf_setlen(&truname
, truname
.len
- len
);
577 if (ref_exists(truname
.buf
)) {
579 "%s\t\tbranch '%s'%s of .\n",
580 oid_to_hex(&remote_head
->object
.oid
),
582 (early
? " (early part)" : ""));
583 strbuf_release(&truname
);
586 strbuf_release(&truname
);
589 desc
= merge_remote_util(remote_head
);
590 if (desc
&& desc
->obj
&& desc
->obj
->type
== OBJ_TAG
) {
591 strbuf_addf(msg
, "%s\t\t%s '%s'\n",
592 oid_to_hex(&desc
->obj
->oid
),
593 type_name(desc
->obj
->type
),
598 strbuf_addf(msg
, "%s\t\tcommit '%s'\n",
599 oid_to_hex(&remote_head
->object
.oid
), remote
);
602 strbuf_release(&bname
);
605 static void parse_branch_merge_options(char *bmo
)
612 argc
= split_cmdline(bmo
, &argv
);
614 die(_("Bad branch.%s.mergeoptions string: %s"), branch
,
615 _(split_cmdline_strerror(argc
)));
616 REALLOC_ARRAY(argv
, argc
+ 2);
617 MOVE_ARRAY(argv
+ 1, argv
, argc
+ 1);
619 argv
[0] = "branch.*.mergeoptions";
620 parse_options(argc
, argv
, NULL
, builtin_merge_options
,
621 builtin_merge_usage
, 0);
625 static int git_merge_config(const char *k
, const char *v
,
626 const struct config_context
*ctx
, void *cb
)
632 skip_prefix(k
, "branch.", &str
) &&
633 skip_prefix(str
, branch
, &str
) &&
634 !strcmp(str
, ".mergeoptions")) {
635 free(branch_mergeoptions
);
636 branch_mergeoptions
= xstrdup(v
);
640 if (!strcmp(k
, "merge.diffstat") || !strcmp(k
, "merge.stat"))
641 show_diffstat
= git_config_bool(k
, v
);
642 else if (!strcmp(k
, "merge.verifysignatures"))
643 verify_signatures
= git_config_bool(k
, v
);
644 else if (!strcmp(k
, "pull.twohead"))
645 return git_config_string(&pull_twohead
, k
, v
);
646 else if (!strcmp(k
, "pull.octopus"))
647 return git_config_string(&pull_octopus
, k
, v
);
648 else if (!strcmp(k
, "commit.cleanup"))
649 return git_config_string(&cleanup_arg
, k
, v
);
650 else if (!strcmp(k
, "merge.ff")) {
651 int boolval
= git_parse_maybe_bool(v
);
653 fast_forward
= boolval
? FF_ALLOW
: FF_NO
;
654 } else if (v
&& !strcmp(v
, "only")) {
655 fast_forward
= FF_ONLY
;
656 } /* do not barf on values from future versions of git */
658 } else if (!strcmp(k
, "merge.defaulttoupstream")) {
659 default_to_upstream
= git_config_bool(k
, v
);
661 } else if (!strcmp(k
, "commit.gpgsign")) {
662 sign_commit
= git_config_bool(k
, v
) ? "" : NULL
;
664 } else if (!strcmp(k
, "gpg.mintrustlevel")) {
665 check_trust_level
= 0;
666 } else if (!strcmp(k
, "merge.autostash")) {
667 autostash
= git_config_bool(k
, v
);
671 status
= fmt_merge_msg_config(k
, v
, ctx
, cb
);
674 return git_diff_ui_config(k
, v
, ctx
, cb
);
677 static int read_tree_trivial(struct object_id
*common
, struct object_id
*head
,
678 struct object_id
*one
)
681 struct tree
*trees
[MAX_UNPACK_TREES
];
682 struct tree_desc t
[MAX_UNPACK_TREES
];
683 struct unpack_trees_options opts
;
685 memset(&opts
, 0, sizeof(opts
));
687 opts
.src_index
= &the_index
;
688 opts
.dst_index
= &the_index
;
690 opts
.verbose_update
= 1;
691 opts
.trivial_merges_only
= 1;
693 opts
.preserve_ignored
= 0; /* FIXME: !overwrite_ignore */
694 trees
[nr_trees
] = parse_tree_indirect(common
);
695 if (!trees
[nr_trees
++])
697 trees
[nr_trees
] = parse_tree_indirect(head
);
698 if (!trees
[nr_trees
++])
700 trees
[nr_trees
] = parse_tree_indirect(one
);
701 if (!trees
[nr_trees
++])
703 opts
.fn
= threeway_merge
;
704 cache_tree_free(&the_index
.cache_tree
);
705 for (i
= 0; i
< nr_trees
; i
++) {
706 parse_tree(trees
[i
]);
707 init_tree_desc(t
+i
, trees
[i
]->buffer
, trees
[i
]->size
);
709 if (unpack_trees(nr_trees
, t
, &opts
))
714 static void write_tree_trivial(struct object_id
*oid
)
716 if (write_index_as_tree(oid
, &the_index
, get_index_file(), 0, NULL
))
717 die(_("git write-tree failed to write a tree"));
720 static int try_merge_strategy(const char *strategy
, struct commit_list
*common
,
721 struct commit_list
*remoteheads
,
724 const char *head_arg
= "HEAD";
726 if (repo_refresh_and_write_index(the_repository
, REFRESH_QUIET
,
727 SKIP_IF_UNCHANGED
, 0, NULL
, NULL
,
729 return error(_("Unable to write index."));
731 if (!strcmp(strategy
, "recursive") || !strcmp(strategy
, "subtree") ||
732 !strcmp(strategy
, "ort")) {
733 struct lock_file lock
= LOCK_INIT
;
735 struct commit
*result
;
736 struct commit_list
*reversed
= NULL
;
737 struct merge_options o
;
738 struct commit_list
*j
;
740 if (remoteheads
->next
) {
741 error(_("Not handling anything other than two heads merge."));
745 init_merge_options(&o
, the_repository
);
746 if (!strcmp(strategy
, "subtree"))
747 o
.subtree_shift
= "";
749 o
.show_rename_progress
=
750 show_progress
== -1 ? isatty(2) : show_progress
;
752 for (x
= 0; x
< xopts_nr
; x
++)
753 if (parse_merge_opt(&o
, xopts
[x
]))
754 die(_("unknown strategy option: -X%s"), xopts
[x
]);
756 o
.branch1
= head_arg
;
757 o
.branch2
= merge_remote_util(remoteheads
->item
)->name
;
759 for (j
= common
; j
; j
= j
->next
)
760 commit_list_insert(j
->item
, &reversed
);
762 repo_hold_locked_index(the_repository
, &lock
,
764 if (!strcmp(strategy
, "ort"))
765 clean
= merge_ort_recursive(&o
, head
, remoteheads
->item
,
768 clean
= merge_recursive(&o
, head
, remoteheads
->item
,
771 rollback_lock_file(&lock
);
774 if (write_locked_index(&the_index
, &lock
,
775 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
776 die(_("unable to write %s"), get_index_file());
777 return clean
? 0 : 1;
779 return try_merge_command(the_repository
,
780 strategy
, xopts_nr
, xopts
,
781 common
, head_arg
, remoteheads
);
785 static void count_diff_files(struct diff_queue_struct
*q
,
786 struct diff_options
*opt UNUSED
, void *data
)
793 static int count_unmerged_entries(void)
797 for (i
= 0; i
< the_index
.cache_nr
; i
++)
798 if (ce_stage(the_index
.cache
[i
]))
804 static void add_strategies(const char *string
, unsigned attr
)
809 struct string_list list
= STRING_LIST_INIT_DUP
;
810 struct string_list_item
*item
;
811 string_list_split(&list
, string
, ' ', -1);
812 for_each_string_list_item(item
, &list
)
813 append_strategy(get_strategy(item
->string
));
814 string_list_clear(&list
, 0);
817 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
818 if (all_strategy
[i
].attr
& attr
)
819 append_strategy(&all_strategy
[i
]);
823 static void read_merge_msg(struct strbuf
*msg
)
825 const char *filename
= git_path_merge_msg(the_repository
);
827 if (strbuf_read_file(msg
, filename
, 0) < 0)
828 die_errno(_("Could not read from '%s'"), filename
);
831 static void write_merge_state(struct commit_list
*);
832 static void abort_commit(struct commit_list
*remoteheads
, const char *err_msg
)
835 error("%s", err_msg
);
837 _("Not committing merge; use 'git commit' to complete the merge.\n"));
838 write_merge_state(remoteheads
);
842 static const char merge_editor_comment
[] =
843 N_("Please enter a commit message to explain why this merge is necessary,\n"
844 "especially if it merges an updated upstream into a topic branch.\n"
847 static const char scissors_editor_comment
[] =
848 N_("An empty message aborts the commit.\n");
850 static const char no_scissors_editor_comment
[] =
851 N_("Lines starting with '%c' will be ignored, and an empty message aborts\n"
854 static void write_merge_heads(struct commit_list
*);
855 static void prepare_to_commit(struct commit_list
*remoteheads
)
857 struct strbuf msg
= STRBUF_INIT
;
858 const char *index_file
= get_index_file();
863 if (run_commit_hook(0 < option_edit
, index_file
, &invoked_hook
,
864 "pre-merge-commit", NULL
))
865 abort_commit(remoteheads
, NULL
);
867 * Re-read the index as pre-merge-commit hook could have updated it,
868 * and write it out as a tree. We must do this before we invoke
869 * the editor and after we invoke run_status above.
872 discard_index(&the_index
);
874 read_index_from(&the_index
, index_file
, get_git_dir());
875 strbuf_addbuf(&msg
, &merge_msg
);
877 BUG("the control must not reach here under --squash");
878 if (0 < option_edit
) {
879 strbuf_addch(&msg
, '\n');
880 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
) {
881 wt_status_append_cut_line(&msg
);
882 strbuf_commented_addf(&msg
, comment_line_char
, "\n");
884 strbuf_commented_addf(&msg
, comment_line_char
,
885 _(merge_editor_comment
));
886 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
)
887 strbuf_commented_addf(&msg
, comment_line_char
,
888 _(scissors_editor_comment
));
890 strbuf_commented_addf(&msg
, comment_line_char
,
891 _(no_scissors_editor_comment
), comment_line_char
);
894 append_signoff(&msg
, ignore_non_trailer(msg
.buf
, msg
.len
), 0);
895 write_merge_heads(remoteheads
);
896 write_file_buf(git_path_merge_msg(the_repository
), msg
.buf
, msg
.len
);
897 if (run_commit_hook(0 < option_edit
, get_index_file(), NULL
,
898 "prepare-commit-msg",
899 git_path_merge_msg(the_repository
), "merge", NULL
))
900 abort_commit(remoteheads
, NULL
);
901 if (0 < option_edit
) {
902 if (launch_editor(git_path_merge_msg(the_repository
), NULL
, NULL
))
903 abort_commit(remoteheads
, NULL
);
906 if (!no_verify
&& run_commit_hook(0 < option_edit
, get_index_file(),
908 git_path_merge_msg(the_repository
), NULL
))
909 abort_commit(remoteheads
, NULL
);
911 read_merge_msg(&msg
);
912 cleanup_message(&msg
, cleanup_mode
, 0);
914 abort_commit(remoteheads
, _("Empty commit message."));
915 strbuf_release(&merge_msg
);
916 strbuf_addbuf(&merge_msg
, &msg
);
917 strbuf_release(&msg
);
920 static int merge_trivial(struct commit
*head
, struct commit_list
*remoteheads
)
922 struct object_id result_tree
, result_commit
;
923 struct commit_list
*parents
, **pptr
= &parents
;
925 if (repo_refresh_and_write_index(the_repository
, REFRESH_QUIET
,
926 SKIP_IF_UNCHANGED
, 0, NULL
, NULL
,
928 return error(_("Unable to write index."));
930 write_tree_trivial(&result_tree
);
931 printf(_("Wonderful.\n"));
932 pptr
= commit_list_append(head
, pptr
);
933 pptr
= commit_list_append(remoteheads
->item
, pptr
);
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 finish(head
, remoteheads
, &result_commit
, "In-index merge");
939 remove_merge_branch_state(the_repository
);
943 static int finish_automerge(struct commit
*head
,
945 struct commit_list
*common
,
946 struct commit_list
*remoteheads
,
947 struct object_id
*result_tree
,
948 const char *wt_strategy
)
950 struct commit_list
*parents
= NULL
;
951 struct strbuf buf
= STRBUF_INIT
;
952 struct object_id result_commit
;
954 write_tree_trivial(result_tree
);
955 free_commit_list(common
);
956 parents
= remoteheads
;
957 if (!head_subsumed
|| fast_forward
== FF_NO
)
958 commit_list_insert(head
, &parents
);
959 prepare_to_commit(remoteheads
);
960 if (commit_tree(merge_msg
.buf
, merge_msg
.len
, result_tree
, parents
,
961 &result_commit
, NULL
, sign_commit
))
962 die(_("failed to write commit object"));
963 strbuf_addf(&buf
, "Merge made by the '%s' strategy.", wt_strategy
);
964 finish(head
, remoteheads
, &result_commit
, buf
.buf
);
965 strbuf_release(&buf
);
966 remove_merge_branch_state(the_repository
);
970 static int suggest_conflicts(void)
972 const char *filename
;
974 struct strbuf msgbuf
= STRBUF_INIT
;
976 filename
= git_path_merge_msg(the_repository
);
977 fp
= xfopen(filename
, "a");
980 * We can't use cleanup_mode because if we're not using the editor,
981 * get_cleanup_mode will return COMMIT_MSG_CLEANUP_SPACE instead, even
982 * though the message is meant to be processed later by git-commit.
983 * Thus, we will get the cleanup mode which is returned when we _are_
986 append_conflicts_hint(&the_index
, &msgbuf
,
987 get_cleanup_mode(cleanup_arg
, 1));
988 fputs(msgbuf
.buf
, fp
);
989 strbuf_release(&msgbuf
);
991 repo_rerere(the_repository
, allow_rerere_auto
);
992 printf(_("Automatic merge failed; "
993 "fix conflicts and then commit the result.\n"));
997 static int evaluate_result(void)
1000 struct rev_info rev
;
1002 /* Check how many files differ. */
1003 repo_init_revisions(the_repository
, &rev
, "");
1004 setup_revisions(0, NULL
, &rev
, NULL
);
1005 rev
.diffopt
.output_format
|=
1006 DIFF_FORMAT_CALLBACK
;
1007 rev
.diffopt
.format_callback
= count_diff_files
;
1008 rev
.diffopt
.format_callback_data
= &cnt
;
1009 run_diff_files(&rev
, 0);
1012 * Check how many unmerged entries are
1015 cnt
+= count_unmerged_entries();
1017 release_revisions(&rev
);
1022 * Pretend as if the user told us to merge with the remote-tracking
1023 * branch we have for the upstream of the current branch
1025 static int setup_with_upstream(const char ***argv
)
1027 struct branch
*branch
= branch_get(NULL
);
1032 die(_("No current branch."));
1033 if (!branch
->remote_name
)
1034 die(_("No remote for the current branch."));
1035 if (!branch
->merge_nr
)
1036 die(_("No default upstream defined for the current branch."));
1038 args
= xcalloc(st_add(branch
->merge_nr
, 1), sizeof(char *));
1039 for (i
= 0; i
< branch
->merge_nr
; i
++) {
1040 if (!branch
->merge
[i
]->dst
)
1041 die(_("No remote-tracking branch for %s from %s"),
1042 branch
->merge
[i
]->src
, branch
->remote_name
);
1043 args
[i
] = branch
->merge
[i
]->dst
;
1050 static void write_merge_heads(struct commit_list
*remoteheads
)
1052 struct commit_list
*j
;
1053 struct strbuf buf
= STRBUF_INIT
;
1055 for (j
= remoteheads
; j
; j
= j
->next
) {
1056 struct object_id
*oid
;
1057 struct commit
*c
= j
->item
;
1058 struct merge_remote_desc
*desc
;
1060 desc
= merge_remote_util(c
);
1061 if (desc
&& desc
->obj
) {
1062 oid
= &desc
->obj
->oid
;
1064 oid
= &c
->object
.oid
;
1066 strbuf_addf(&buf
, "%s\n", oid_to_hex(oid
));
1068 write_file_buf(git_path_merge_head(the_repository
), buf
.buf
, buf
.len
);
1071 if (fast_forward
== FF_NO
)
1072 strbuf_addstr(&buf
, "no-ff");
1073 write_file_buf(git_path_merge_mode(the_repository
), buf
.buf
, buf
.len
);
1074 strbuf_release(&buf
);
1077 static void write_merge_state(struct commit_list
*remoteheads
)
1079 write_merge_heads(remoteheads
);
1080 strbuf_addch(&merge_msg
, '\n');
1081 write_file_buf(git_path_merge_msg(the_repository
), merge_msg
.buf
,
1085 static int default_edit_option(void)
1087 static const char name
[] = "GIT_MERGE_AUTOEDIT";
1088 const char *e
= getenv(name
);
1089 struct stat st_stdin
, st_stdout
;
1092 /* an explicit -m msg without --[no-]edit */
1096 int v
= git_parse_maybe_bool(e
);
1098 die(_("Bad value '%s' in environment '%s'"), e
, name
);
1102 /* Use editor if stdin and stdout are the same and is a tty */
1103 return (!fstat(0, &st_stdin
) &&
1104 !fstat(1, &st_stdout
) &&
1105 isatty(0) && isatty(1) &&
1106 st_stdin
.st_dev
== st_stdout
.st_dev
&&
1107 st_stdin
.st_ino
== st_stdout
.st_ino
&&
1108 st_stdin
.st_mode
== st_stdout
.st_mode
);
1111 static struct commit_list
*reduce_parents(struct commit
*head_commit
,
1113 struct commit_list
*remoteheads
)
1115 struct commit_list
*parents
, **remotes
;
1118 * Is the current HEAD reachable from another commit being
1119 * merged? If so we do not want to record it as a parent of
1120 * the resulting merge, unless --no-ff is given. We will flip
1121 * this variable to 0 when we find HEAD among the independent
1122 * tips being merged.
1126 /* Find what parents to record by checking independent ones. */
1127 parents
= reduce_heads(remoteheads
);
1128 free_commit_list(remoteheads
);
1131 remotes
= &remoteheads
;
1133 struct commit
*commit
= pop_commit(&parents
);
1134 if (commit
== head_commit
)
1137 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1142 static void prepare_merge_message(struct strbuf
*merge_names
, struct strbuf
*merge_msg
)
1144 struct fmt_merge_msg_opts opts
;
1146 memset(&opts
, 0, sizeof(opts
));
1147 opts
.add_title
= !have_message
;
1148 opts
.shortlog_len
= shortlog_len
;
1149 opts
.credit_people
= (0 < option_edit
);
1150 opts
.into_name
= into_name
;
1152 fmt_merge_msg(merge_names
, merge_msg
, &opts
);
1154 strbuf_setlen(merge_msg
, merge_msg
->len
- 1);
1157 static void handle_fetch_head(struct commit_list
**remotes
, struct strbuf
*merge_names
)
1159 const char *filename
;
1161 struct strbuf fetch_head_file
= STRBUF_INIT
;
1162 const unsigned hexsz
= the_hash_algo
->hexsz
;
1165 merge_names
= &fetch_head_file
;
1167 filename
= git_path_fetch_head(the_repository
);
1168 fd
= xopen(filename
, O_RDONLY
);
1170 if (strbuf_read(merge_names
, fd
, 0) < 0)
1171 die_errno(_("could not read '%s'"), filename
);
1173 die_errno(_("could not close '%s'"), filename
);
1175 for (pos
= 0; pos
< merge_names
->len
; pos
= npos
) {
1176 struct object_id oid
;
1178 struct commit
*commit
;
1180 ptr
= strchr(merge_names
->buf
+ pos
, '\n');
1182 npos
= ptr
- merge_names
->buf
+ 1;
1184 npos
= merge_names
->len
;
1186 if (npos
- pos
< hexsz
+ 2 ||
1187 get_oid_hex(merge_names
->buf
+ pos
, &oid
))
1188 commit
= NULL
; /* bad */
1189 else if (memcmp(merge_names
->buf
+ pos
+ hexsz
, "\t\t", 2))
1190 continue; /* not-for-merge */
1192 char saved
= merge_names
->buf
[pos
+ hexsz
];
1193 merge_names
->buf
[pos
+ hexsz
] = '\0';
1194 commit
= get_merge_parent(merge_names
->buf
+ pos
);
1195 merge_names
->buf
[pos
+ hexsz
] = saved
;
1200 die(_("not something we can merge in %s: %s"),
1201 filename
, merge_names
->buf
+ pos
);
1203 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1206 if (merge_names
== &fetch_head_file
)
1207 strbuf_release(&fetch_head_file
);
1210 static struct commit_list
*collect_parents(struct commit
*head_commit
,
1212 int argc
, const char **argv
,
1213 struct strbuf
*merge_msg
)
1216 struct commit_list
*remoteheads
= NULL
;
1217 struct commit_list
**remotes
= &remoteheads
;
1218 struct strbuf merge_names
= STRBUF_INIT
, *autogen
= NULL
;
1220 if (merge_msg
&& (!have_message
|| shortlog_len
))
1221 autogen
= &merge_names
;
1224 remotes
= &commit_list_insert(head_commit
, remotes
)->next
;
1226 if (argc
== 1 && !strcmp(argv
[0], "FETCH_HEAD")) {
1227 handle_fetch_head(remotes
, autogen
);
1228 remoteheads
= reduce_parents(head_commit
, head_subsumed
, remoteheads
);
1230 for (i
= 0; i
< argc
; i
++) {
1231 struct commit
*commit
= get_merge_parent(argv
[i
]);
1233 help_unknown_ref(argv
[i
], "merge",
1234 _("not something we can merge"));
1235 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1237 remoteheads
= reduce_parents(head_commit
, head_subsumed
, remoteheads
);
1239 struct commit_list
*p
;
1240 for (p
= remoteheads
; p
; p
= p
->next
)
1241 merge_name(merge_remote_util(p
->item
)->name
, autogen
);
1246 prepare_merge_message(autogen
, merge_msg
);
1247 strbuf_release(autogen
);
1253 static int merging_a_throwaway_tag(struct commit
*commit
)
1256 struct object_id oid
;
1257 int is_throwaway_tag
= 0;
1259 /* Are we merging a tag? */
1260 if (!merge_remote_util(commit
) ||
1261 !merge_remote_util(commit
)->obj
||
1262 merge_remote_util(commit
)->obj
->type
!= OBJ_TAG
)
1263 return is_throwaway_tag
;
1266 * Now we know we are merging a tag object. Are we downstream
1267 * and following the tags from upstream? If so, we must have
1268 * the tag object pointed at by "refs/tags/$T" where $T is the
1269 * tagname recorded in the tag object. We want to allow such
1270 * a "just to catch up" merge to fast-forward.
1272 * Otherwise, we are playing an integrator's role, making a
1273 * merge with a throw-away tag from a contributor with
1274 * something like "git pull $contributor $signed_tag".
1275 * We want to forbid such a merge from fast-forwarding
1276 * by default; otherwise we would not keep the signature
1279 tag_ref
= xstrfmt("refs/tags/%s",
1280 ((struct tag
*)merge_remote_util(commit
)->obj
)->tag
);
1281 if (!read_ref(tag_ref
, &oid
) &&
1282 oideq(&oid
, &merge_remote_util(commit
)->obj
->oid
))
1283 is_throwaway_tag
= 0;
1285 is_throwaway_tag
= 1;
1287 return is_throwaway_tag
;
1290 int cmd_merge(int argc
, const char **argv
, const char *prefix
)
1292 struct object_id result_tree
, stash
, head_oid
;
1293 struct commit
*head_commit
;
1294 struct strbuf buf
= STRBUF_INIT
;
1295 int i
, ret
= 0, head_subsumed
;
1296 int best_cnt
= -1, merge_was_ok
= 0, automerge_was_ok
= 0;
1297 struct commit_list
*common
= NULL
;
1298 const char *best_strategy
= NULL
, *wt_strategy
= NULL
;
1299 struct commit_list
*remoteheads
= NULL
, *p
;
1300 void *branch_to_free
;
1301 int orig_argc
= argc
;
1303 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1304 usage_with_options(builtin_merge_usage
, builtin_merge_options
);
1306 prepare_repo_settings(the_repository
);
1307 the_repository
->settings
.command_requires_full_index
= 0;
1310 * Check if we are _not_ on a detached HEAD, i.e. if there is a
1313 branch
= branch_to_free
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
1315 skip_prefix(branch
, "refs/heads/", &branch
);
1317 if (!pull_twohead
) {
1318 char *default_strategy
= getenv("GIT_TEST_MERGE_ALGORITHM");
1319 if (default_strategy
&& !strcmp(default_strategy
, "ort"))
1320 pull_twohead
= "ort";
1323 init_diff_ui_defaults();
1324 git_config(git_merge_config
, NULL
);
1326 if (!branch
|| is_null_oid(&head_oid
))
1329 head_commit
= lookup_commit_or_die(&head_oid
, "HEAD");
1331 if (branch_mergeoptions
)
1332 parse_branch_merge_options(branch_mergeoptions
);
1333 argc
= parse_options(argc
, argv
, prefix
, builtin_merge_options
,
1334 builtin_merge_usage
, 0);
1335 if (shortlog_len
< 0)
1336 shortlog_len
= (merge_log_config
> 0) ? merge_log_config
: 0;
1338 if (verbosity
< 0 && show_progress
== -1)
1341 if (abort_current_merge
) {
1343 const char *nargv
[] = {"reset", "--merge", NULL
};
1344 struct strbuf stash_oid
= STRBUF_INIT
;
1347 usage_msg_opt(_("--abort expects no arguments"),
1348 builtin_merge_usage
, builtin_merge_options
);
1350 if (!file_exists(git_path_merge_head(the_repository
)))
1351 die(_("There is no merge to abort (MERGE_HEAD missing)."));
1353 if (read_oneliner(&stash_oid
, git_path_merge_autostash(the_repository
),
1354 READ_ONELINER_SKIP_IF_EMPTY
))
1355 unlink(git_path_merge_autostash(the_repository
));
1357 /* Invoke 'git reset --merge' */
1358 ret
= cmd_reset(nargc
, nargv
, prefix
);
1361 apply_autostash_oid(stash_oid
.buf
);
1363 strbuf_release(&stash_oid
);
1367 if (quit_current_merge
) {
1369 usage_msg_opt(_("--quit expects no arguments"),
1370 builtin_merge_usage
,
1371 builtin_merge_options
);
1373 remove_merge_branch_state(the_repository
);
1377 if (continue_current_merge
) {
1379 const char *nargv
[] = {"commit", NULL
};
1382 usage_msg_opt(_("--continue expects no arguments"),
1383 builtin_merge_usage
, builtin_merge_options
);
1385 if (!file_exists(git_path_merge_head(the_repository
)))
1386 die(_("There is no merge in progress (MERGE_HEAD missing)."));
1388 /* Invoke 'git commit' */
1389 ret
= cmd_commit(nargc
, nargv
, prefix
);
1393 if (repo_read_index_unmerged(the_repository
))
1394 die_resolve_conflict("merge");
1396 if (file_exists(git_path_merge_head(the_repository
))) {
1398 * There is no unmerged entry, don't advise 'git
1399 * add/rm <file>', just 'git commit'.
1401 if (advice_enabled(ADVICE_RESOLVE_CONFLICT
))
1402 die(_("You have not concluded your merge (MERGE_HEAD exists).\n"
1403 "Please, commit your changes before you merge."));
1405 die(_("You have not concluded your merge (MERGE_HEAD exists)."));
1407 if (ref_exists("CHERRY_PICK_HEAD")) {
1408 if (advice_enabled(ADVICE_RESOLVE_CONFLICT
))
1409 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
1410 "Please, commit your changes before you merge."));
1412 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists)."));
1414 resolve_undo_clear_index(&the_index
);
1416 if (option_edit
< 0)
1417 option_edit
= default_edit_option();
1419 cleanup_mode
= get_cleanup_mode(cleanup_arg
, 0 < option_edit
);
1425 if (fast_forward
== FF_NO
)
1426 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--no-ff.");
1427 if (option_commit
> 0)
1428 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--commit.");
1430 * squash can now silently disable option_commit - this is not
1431 * a problem as it is only overriding the default, not a user
1437 if (option_commit
< 0)
1441 if (default_to_upstream
)
1442 argc
= setup_with_upstream(&argv
);
1444 die(_("No commit specified and merge.defaultToUpstream not set."));
1445 } else if (argc
== 1 && !strcmp(argv
[0], "-")) {
1450 usage_with_options(builtin_merge_usage
,
1451 builtin_merge_options
);
1455 * If the merged head is a valid one there is no reason
1456 * to forbid "git merge" into a branch yet to be born.
1457 * We do the same for "git pull".
1459 struct object_id
*remote_head_oid
;
1461 die(_("Squash commit into empty head not supported yet"));
1462 if (fast_forward
== FF_NO
)
1463 die(_("Non-fast-forward commit does not make sense into "
1465 remoteheads
= collect_parents(head_commit
, &head_subsumed
,
1468 die(_("%s - not something we can merge"), argv
[0]);
1469 if (remoteheads
->next
)
1470 die(_("Can merge only exactly one commit into empty head"));
1472 if (verify_signatures
)
1473 verify_merge_signature(remoteheads
->item
, verbosity
,
1476 remote_head_oid
= &remoteheads
->item
->object
.oid
;
1477 read_empty(remote_head_oid
);
1478 update_ref("initial pull", "HEAD", remote_head_oid
, NULL
, 0,
1479 UPDATE_REFS_DIE_ON_ERR
);
1484 * All the rest are the commits being merged; prepare
1485 * the standard merge summary message to be appended
1486 * to the given message.
1488 remoteheads
= collect_parents(head_commit
, &head_subsumed
,
1489 argc
, argv
, &merge_msg
);
1491 if (!head_commit
|| !argc
)
1492 usage_with_options(builtin_merge_usage
,
1493 builtin_merge_options
);
1495 if (verify_signatures
) {
1496 for (p
= remoteheads
; p
; p
= p
->next
) {
1497 verify_merge_signature(p
->item
, verbosity
,
1502 strbuf_addstr(&buf
, "merge");
1503 for (p
= remoteheads
; p
; p
= p
->next
)
1504 strbuf_addf(&buf
, " %s", merge_remote_util(p
->item
)->name
);
1505 setenv("GIT_REFLOG_ACTION", buf
.buf
, 0);
1508 for (p
= remoteheads
; p
; p
= p
->next
) {
1509 struct commit
*commit
= p
->item
;
1510 strbuf_addf(&buf
, "GITHEAD_%s",
1511 oid_to_hex(&commit
->object
.oid
));
1512 setenv(buf
.buf
, merge_remote_util(commit
)->name
, 1);
1514 if (fast_forward
!= FF_ONLY
&& merging_a_throwaway_tag(commit
))
1515 fast_forward
= FF_NO
;
1518 if (!use_strategies
&& !pull_twohead
&&
1519 remoteheads
&& !remoteheads
->next
) {
1520 char *default_strategy
= getenv("GIT_TEST_MERGE_ALGORITHM");
1521 if (default_strategy
)
1522 append_strategy(get_strategy(default_strategy
));
1524 if (!use_strategies
) {
1526 ; /* already up-to-date */
1527 else if (!remoteheads
->next
)
1528 add_strategies(pull_twohead
, DEFAULT_TWOHEAD
);
1530 add_strategies(pull_octopus
, DEFAULT_OCTOPUS
);
1533 for (i
= 0; i
< use_strategies_nr
; i
++) {
1534 if (use_strategies
[i
]->attr
& NO_FAST_FORWARD
)
1535 fast_forward
= FF_NO
;
1536 if (use_strategies
[i
]->attr
& NO_TRIVIAL
)
1541 ; /* already up-to-date */
1542 else if (!remoteheads
->next
)
1543 common
= repo_get_merge_bases(the_repository
, head_commit
,
1546 struct commit_list
*list
= remoteheads
;
1547 commit_list_insert(head_commit
, &list
);
1548 common
= get_octopus_merge_bases(list
);
1552 update_ref("updating ORIG_HEAD", "ORIG_HEAD",
1553 &head_commit
->object
.oid
, NULL
, 0, UPDATE_REFS_DIE_ON_ERR
);
1555 if (remoteheads
&& !common
) {
1556 /* No common ancestors found. */
1557 if (!allow_unrelated_histories
)
1558 die(_("refusing to merge unrelated histories"));
1559 /* otherwise, we need a real merge. */
1560 } else if (!remoteheads
||
1561 (!remoteheads
->next
&& !common
->next
&&
1562 common
->item
== remoteheads
->item
)) {
1564 * If head can reach all the merge then we are up to date.
1565 * but first the most common case of merging one remote.
1567 finish_up_to_date();
1569 } else if (fast_forward
!= FF_NO
&& !remoteheads
->next
&&
1571 oideq(&common
->item
->object
.oid
, &head_commit
->object
.oid
)) {
1572 /* Again the most common case of merging one remote. */
1573 const char *msg
= have_message
?
1574 "Fast-forward (no commit created; -m option ignored)" :
1576 struct commit
*commit
;
1578 if (verbosity
>= 0) {
1579 printf(_("Updating %s..%s\n"),
1580 repo_find_unique_abbrev(the_repository
, &head_commit
->object
.oid
,
1582 repo_find_unique_abbrev(the_repository
, &remoteheads
->item
->object
.oid
,
1585 commit
= remoteheads
->item
;
1592 create_autostash(the_repository
,
1593 git_path_merge_autostash(the_repository
));
1594 if (checkout_fast_forward(the_repository
,
1595 &head_commit
->object
.oid
,
1596 &commit
->object
.oid
,
1597 overwrite_ignore
)) {
1598 apply_autostash(git_path_merge_autostash(the_repository
));
1603 finish(head_commit
, remoteheads
, &commit
->object
.oid
, msg
);
1604 remove_merge_branch_state(the_repository
);
1606 } else if (!remoteheads
->next
&& common
->next
)
1609 * We are not doing octopus and not fast-forward. Need
1612 else if (!remoteheads
->next
&& !common
->next
&& option_commit
) {
1614 * We are not doing octopus, not fast-forward, and have
1617 refresh_index(&the_index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
1618 if (allow_trivial
&& fast_forward
!= FF_ONLY
) {
1620 * Must first ensure that index matches HEAD before
1621 * attempting a trivial merge.
1623 struct tree
*head_tree
= repo_get_commit_tree(the_repository
,
1625 struct strbuf sb
= STRBUF_INIT
;
1627 if (repo_index_has_changes(the_repository
, head_tree
,
1629 error(_("Your local changes to the following files would be overwritten by merge:\n %s"),
1631 strbuf_release(&sb
);
1636 /* See if it is really trivial. */
1637 git_committer_info(IDENT_STRICT
);
1638 printf(_("Trying really trivial in-index merge...\n"));
1639 if (!read_tree_trivial(&common
->item
->object
.oid
,
1640 &head_commit
->object
.oid
,
1641 &remoteheads
->item
->object
.oid
)) {
1642 ret
= merge_trivial(head_commit
, remoteheads
);
1645 printf(_("Nope.\n"));
1649 * An octopus. If we can reach all the remote we are up
1653 struct commit_list
*j
;
1655 for (j
= remoteheads
; j
; j
= j
->next
) {
1656 struct commit_list
*common_one
;
1659 * Here we *have* to calculate the individual
1660 * merge_bases again, otherwise "git merge HEAD^
1661 * HEAD^^" would be missed.
1663 common_one
= repo_get_merge_bases(the_repository
,
1666 if (!oideq(&common_one
->item
->object
.oid
, &j
->item
->object
.oid
)) {
1672 finish_up_to_date();
1677 if (fast_forward
== FF_ONLY
)
1678 die_ff_impossible();
1681 create_autostash(the_repository
,
1682 git_path_merge_autostash(the_repository
));
1684 /* We are going to make a new commit. */
1685 git_committer_info(IDENT_STRICT
);
1688 * At this point, we need a real merge. No matter what strategy
1689 * we use, it would operate on the index, possibly affecting the
1690 * working tree, and when resolved cleanly, have the desired
1691 * tree in the index -- this means that the index must be in
1692 * sync with the head commit. The strategies are responsible
1695 * Stash away the local changes so that we can try more than one
1696 * and/or recover from merge strategies bailing while leaving the
1697 * index and working tree polluted.
1699 if (save_state(&stash
))
1702 for (i
= 0; i
< use_strategies_nr
; i
++) {
1705 printf(_("Rewinding the tree to pristine...\n"));
1706 restore_state(&head_commit
->object
.oid
, &stash
);
1708 if (use_strategies_nr
!= 1)
1709 printf(_("Trying merge strategy %s...\n"),
1710 use_strategies
[i
]->name
);
1712 * Remember which strategy left the state in the working
1715 wt_strategy
= use_strategies
[i
]->name
;
1717 ret
= try_merge_strategy(wt_strategy
,
1718 common
, remoteheads
,
1721 * The backend exits with 1 when conflicts are
1722 * left to be resolved, with 2 when it does not
1723 * handle the given merge at all.
1728 * This strategy worked; no point in trying
1732 best_strategy
= wt_strategy
;
1735 cnt
= (use_strategies_nr
> 1) ? evaluate_result() : 0;
1736 if (best_cnt
<= 0 || cnt
<= best_cnt
) {
1737 best_strategy
= wt_strategy
;
1744 * If we have a resulting tree, that means the strategy module
1745 * auto resolved the merge cleanly.
1747 if (merge_was_ok
&& option_commit
) {
1748 automerge_was_ok
= 1;
1749 ret
= finish_automerge(head_commit
, head_subsumed
,
1750 common
, remoteheads
,
1751 &result_tree
, wt_strategy
);
1756 * Pick the result from the best strategy and have the user fix
1759 if (!best_strategy
) {
1760 restore_state(&head_commit
->object
.oid
, &stash
);
1761 if (use_strategies_nr
> 1)
1763 _("No merge strategy handled the merge.\n"));
1765 fprintf(stderr
, _("Merge with strategy %s failed.\n"),
1766 use_strategies
[0]->name
);
1767 apply_autostash(git_path_merge_autostash(the_repository
));
1770 } else if (best_strategy
== wt_strategy
)
1771 ; /* We already have its result in the working tree. */
1773 printf(_("Rewinding the tree to pristine...\n"));
1774 restore_state(&head_commit
->object
.oid
, &stash
);
1775 printf(_("Using the %s strategy to prepare resolving by hand.\n"),
1777 try_merge_strategy(best_strategy
, common
, remoteheads
,
1782 finish(head_commit
, remoteheads
, NULL
, NULL
);
1784 git_test_write_commit_graph_or_die();
1786 write_merge_state(remoteheads
);
1789 fprintf(stderr
, _("Automatic merge went well; "
1790 "stopped before committing as requested\n"));
1792 ret
= suggest_conflicts();
1794 printf(_("When finished, apply stashed changes with `git stash pop`\n"));
1797 if (!automerge_was_ok
) {
1798 free_commit_list(common
);
1799 free_commit_list(remoteheads
);
1801 strbuf_release(&buf
);
1802 free(branch_to_free
);
1803 discard_index(&the_index
);