4 * Copyright (c) 2008 Miklos Vajna <vmiklos@frugalware.org>
6 * Based on git-merge.sh by Junio C Hamano.
9 #define USE_THE_INDEX_COMPATIBILITY_MACROS
12 #include "parse-options.h"
15 #include "run-command.h"
18 #include "diff-merges.h"
24 #include "unpack-trees.h"
25 #include "cache-tree.h"
32 #include "merge-recursive.h"
33 #include "merge-ort-wrappers.h"
34 #include "resolve-undo.h"
36 #include "fmt-merge-msg.h"
37 #include "gpg-interface.h"
38 #include "sequencer.h"
39 #include "string-list.h"
44 #include "commit-reach.h"
45 #include "wt-status.h"
46 #include "commit-graph.h"
48 #define DEFAULT_TWOHEAD (1<<0)
49 #define DEFAULT_OCTOPUS (1<<1)
50 #define NO_FAST_FORWARD (1<<2)
51 #define NO_TRIVIAL (1<<3)
58 static const char * const builtin_merge_usage
[] = {
59 N_("git merge [<options>] [<commit>...]"),
61 "git merge --continue",
65 static int show_diffstat
= 1, shortlog_len
= -1, squash
;
66 static int option_commit
= -1;
67 static int option_edit
= -1;
68 static int allow_trivial
= 1, have_message
, verify_signatures
;
69 static int check_trust_level
= 1;
70 static int overwrite_ignore
= 1;
71 static struct strbuf merge_msg
= STRBUF_INIT
;
72 static struct strategy
**use_strategies
;
73 static size_t use_strategies_nr
, use_strategies_alloc
;
74 static const char **xopts
;
75 static size_t xopts_nr
, xopts_alloc
;
76 static const char *branch
;
77 static char *branch_mergeoptions
;
79 static int allow_rerere_auto
;
80 static int abort_current_merge
;
81 static int quit_current_merge
;
82 static int continue_current_merge
;
83 static int allow_unrelated_histories
;
84 static int show_progress
= -1;
85 static int default_to_upstream
= 1;
87 static const char *sign_commit
;
90 static char *into_name
;
92 static struct strategy all_strategy
[] = {
93 { "recursive", NO_TRIVIAL
},
94 { "octopus", DEFAULT_OCTOPUS
},
95 { "ort", DEFAULT_TWOHEAD
| NO_TRIVIAL
},
97 { "ours", NO_FAST_FORWARD
| NO_TRIVIAL
},
98 { "subtree", NO_FAST_FORWARD
| NO_TRIVIAL
},
101 static const char *pull_twohead
, *pull_octopus
;
109 static enum ff_type fast_forward
= FF_ALLOW
;
111 static const char *cleanup_arg
;
112 static enum commit_msg_cleanup_mode cleanup_mode
;
114 static int option_parse_message(const struct option
*opt
,
115 const char *arg
, int unset
)
117 struct strbuf
*buf
= opt
->value
;
120 strbuf_setlen(buf
, 0);
122 strbuf_addf(buf
, "%s%s", buf
->len
? "\n\n" : "", arg
);
125 return error(_("switch `m' requires a value"));
129 static enum parse_opt_result
option_read_message(struct parse_opt_ctx_t
*ctx
,
130 const struct option
*opt
,
131 const char *arg_not_used
,
134 struct strbuf
*buf
= opt
->value
;
137 BUG_ON_OPT_ARG(arg_not_used
);
139 BUG("-F cannot be negated");
144 } else if (ctx
->argc
> 1) {
148 return error(_("option `%s' requires a value"), opt
->long_name
);
151 strbuf_addch(buf
, '\n');
152 if (ctx
->prefix
&& !is_absolute_path(arg
))
153 arg
= prefix_filename(ctx
->prefix
, arg
);
154 if (strbuf_read_file(buf
, arg
, 0) < 0)
155 return error(_("could not read file '%s'"), arg
);
161 static struct strategy
*get_strategy(const char *name
)
164 struct strategy
*ret
;
165 static struct cmdnames main_cmds
, other_cmds
;
167 char *default_strategy
= getenv("GIT_TEST_MERGE_ALGORITHM");
172 if (default_strategy
&&
173 !strcmp(default_strategy
, "ort") &&
174 !strcmp(name
, "recursive")) {
178 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
179 if (!strcmp(name
, all_strategy
[i
].name
))
180 return &all_strategy
[i
];
183 struct cmdnames not_strategies
;
186 memset(¬_strategies
, 0, sizeof(struct cmdnames
));
187 load_command_list("git-merge-", &main_cmds
, &other_cmds
);
188 for (i
= 0; i
< main_cmds
.cnt
; i
++) {
190 struct cmdname
*ent
= main_cmds
.names
[i
];
191 for (j
= 0; j
< ARRAY_SIZE(all_strategy
); j
++)
192 if (!strncmp(ent
->name
, all_strategy
[j
].name
, ent
->len
)
193 && !all_strategy
[j
].name
[ent
->len
])
196 add_cmdname(¬_strategies
, ent
->name
, ent
->len
);
198 exclude_cmds(&main_cmds
, ¬_strategies
);
200 if (!is_in_cmdlist(&main_cmds
, name
) && !is_in_cmdlist(&other_cmds
, name
)) {
201 fprintf(stderr
, _("Could not find merge strategy '%s'.\n"), name
);
202 fprintf(stderr
, _("Available strategies are:"));
203 for (i
= 0; i
< main_cmds
.cnt
; i
++)
204 fprintf(stderr
, " %s", main_cmds
.names
[i
]->name
);
205 fprintf(stderr
, ".\n");
206 if (other_cmds
.cnt
) {
207 fprintf(stderr
, _("Available custom strategies are:"));
208 for (i
= 0; i
< other_cmds
.cnt
; i
++)
209 fprintf(stderr
, " %s", other_cmds
.names
[i
]->name
);
210 fprintf(stderr
, ".\n");
215 CALLOC_ARRAY(ret
, 1);
216 ret
->name
= xstrdup(name
);
217 ret
->attr
= NO_TRIVIAL
;
221 static void append_strategy(struct strategy
*s
)
223 ALLOC_GROW(use_strategies
, use_strategies_nr
+ 1, use_strategies_alloc
);
224 use_strategies
[use_strategies_nr
++] = s
;
227 static int option_parse_strategy(const struct option
*opt
,
228 const char *name
, int unset
)
233 append_strategy(get_strategy(name
));
237 static int option_parse_x(const struct option
*opt
,
238 const char *arg
, int unset
)
243 ALLOC_GROW(xopts
, xopts_nr
+ 1, xopts_alloc
);
244 xopts
[xopts_nr
++] = xstrdup(arg
);
248 static int option_parse_n(const struct option
*opt
,
249 const char *arg
, int unset
)
252 show_diffstat
= unset
;
256 static struct option builtin_merge_options
[] = {
257 OPT_CALLBACK_F('n', NULL
, NULL
, NULL
,
258 N_("do not show a diffstat at the end of the merge"),
259 PARSE_OPT_NOARG
, option_parse_n
),
260 OPT_BOOL(0, "stat", &show_diffstat
,
261 N_("show a diffstat at the end of the merge")),
262 OPT_BOOL(0, "summary", &show_diffstat
, N_("(synonym to --stat)")),
263 { OPTION_INTEGER
, 0, "log", &shortlog_len
, N_("n"),
264 N_("add (at most <n>) entries from shortlog to merge commit message"),
265 PARSE_OPT_OPTARG
, NULL
, DEFAULT_MERGE_LOG_LEN
},
266 OPT_BOOL(0, "squash", &squash
,
267 N_("create a single commit instead of doing a merge")),
268 OPT_BOOL(0, "commit", &option_commit
,
269 N_("perform a commit if the merge succeeds (default)")),
270 OPT_BOOL('e', "edit", &option_edit
,
271 N_("edit message before committing")),
272 OPT_CLEANUP(&cleanup_arg
),
273 OPT_SET_INT(0, "ff", &fast_forward
, N_("allow fast-forward (default)"), FF_ALLOW
),
274 OPT_SET_INT_F(0, "ff-only", &fast_forward
,
275 N_("abort if fast-forward is not possible"),
276 FF_ONLY
, PARSE_OPT_NONEG
),
277 OPT_RERERE_AUTOUPDATE(&allow_rerere_auto
),
278 OPT_BOOL(0, "verify-signatures", &verify_signatures
,
279 N_("verify that the named commit has a valid GPG signature")),
280 OPT_CALLBACK('s', "strategy", &use_strategies
, N_("strategy"),
281 N_("merge strategy to use"), option_parse_strategy
),
282 OPT_CALLBACK('X', "strategy-option", &xopts
, N_("option=value"),
283 N_("option for selected merge strategy"), option_parse_x
),
284 OPT_CALLBACK('m', "message", &merge_msg
, N_("message"),
285 N_("merge commit message (for a non-fast-forward merge)"),
286 option_parse_message
),
287 { OPTION_LOWLEVEL_CALLBACK
, 'F', "file", &merge_msg
, N_("path"),
288 N_("read message from file"), PARSE_OPT_NONEG
,
289 NULL
, 0, option_read_message
},
290 OPT_STRING(0, "into-name", &into_name
, N_("name"),
291 N_("use <name> instead of the real target")),
292 OPT__VERBOSITY(&verbosity
),
293 OPT_BOOL(0, "abort", &abort_current_merge
,
294 N_("abort the current in-progress merge")),
295 OPT_BOOL(0, "quit", &quit_current_merge
,
296 N_("--abort but leave index and working tree alone")),
297 OPT_BOOL(0, "continue", &continue_current_merge
,
298 N_("continue the current in-progress merge")),
299 OPT_BOOL(0, "allow-unrelated-histories", &allow_unrelated_histories
,
300 N_("allow merging unrelated histories")),
301 OPT_SET_INT(0, "progress", &show_progress
, N_("force progress reporting"), 1),
302 { OPTION_STRING
, 'S', "gpg-sign", &sign_commit
, N_("key-id"),
303 N_("GPG sign commit"), PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
304 OPT_AUTOSTASH(&autostash
),
305 OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore
, N_("update ignored files (default)")),
306 OPT_BOOL(0, "signoff", &signoff
, N_("add a Signed-off-by trailer")),
307 OPT_BOOL(0, "no-verify", &no_verify
, N_("bypass pre-merge-commit and commit-msg hooks")),
311 static int save_state(struct object_id
*stash
)
314 struct child_process cp
= CHILD_PROCESS_INIT
;
315 struct strbuf buffer
= STRBUF_INIT
;
318 strvec_pushl(&cp
.args
, "stash", "create", NULL
);
322 if (start_command(&cp
))
323 die(_("could not run stash."));
324 len
= strbuf_read(&buffer
, cp
.out
, 1024);
327 if (finish_command(&cp
) || len
< 0)
328 die(_("stash failed"));
329 else if (!len
) /* no changes */
331 strbuf_setlen(&buffer
, buffer
.len
-1);
332 if (get_oid(buffer
.buf
, stash
))
333 die(_("not a valid object: %s"), buffer
.buf
);
336 strbuf_release(&buffer
);
340 static void read_empty(const struct object_id
*oid
, int verbose
)
345 args
[i
++] = "read-tree";
350 args
[i
++] = empty_tree_oid_hex();
351 args
[i
++] = oid_to_hex(oid
);
354 if (run_command_v_opt(args
, RUN_GIT_CMD
))
355 die(_("read-tree failed"));
358 static void reset_hard(const struct object_id
*oid
, int verbose
)
363 args
[i
++] = "read-tree";
366 args
[i
++] = "--reset";
368 args
[i
++] = oid_to_hex(oid
);
371 if (run_command_v_opt(args
, RUN_GIT_CMD
))
372 die(_("read-tree failed"));
375 static void restore_state(const struct object_id
*head
,
376 const struct object_id
*stash
)
378 struct strbuf sb
= STRBUF_INIT
;
379 const char *args
[] = { "stash", "apply", NULL
, NULL
};
381 if (is_null_oid(stash
))
386 args
[2] = oid_to_hex(stash
);
389 * It is OK to ignore error here, for example when there was
390 * nothing to restore.
392 run_command_v_opt(args
, RUN_GIT_CMD
);
395 refresh_cache(REFRESH_QUIET
);
398 /* This is called when no merge was necessary. */
399 static void finish_up_to_date(void)
401 if (verbosity
>= 0) {
403 puts(_("Already up to date. (nothing to squash)"));
405 puts(_("Already up to date."));
407 remove_merge_branch_state(the_repository
);
410 static void squash_message(struct commit
*commit
, struct commit_list
*remoteheads
)
413 struct strbuf out
= STRBUF_INIT
;
414 struct commit_list
*j
;
415 struct pretty_print_context ctx
= {0};
417 printf(_("Squash commit -- not updating HEAD\n"));
419 repo_init_revisions(the_repository
, &rev
, NULL
);
420 diff_merges_suppress(&rev
);
421 rev
.commit_format
= CMIT_FMT_MEDIUM
;
423 commit
->object
.flags
|= UNINTERESTING
;
424 add_pending_object(&rev
, &commit
->object
, NULL
);
426 for (j
= remoteheads
; j
; j
= j
->next
)
427 add_pending_object(&rev
, &j
->item
->object
, NULL
);
429 setup_revisions(0, NULL
, &rev
, NULL
);
430 if (prepare_revision_walk(&rev
))
431 die(_("revision walk setup failed"));
433 ctx
.abbrev
= rev
.abbrev
;
434 ctx
.date_mode
= rev
.date_mode
;
435 ctx
.fmt
= rev
.commit_format
;
437 strbuf_addstr(&out
, "Squashed commit of the following:\n");
438 while ((commit
= get_revision(&rev
)) != NULL
) {
439 strbuf_addch(&out
, '\n');
440 strbuf_addf(&out
, "commit %s\n",
441 oid_to_hex(&commit
->object
.oid
));
442 pretty_print_commit(&ctx
, commit
, &out
);
444 write_file_buf(git_path_squash_msg(the_repository
), out
.buf
, out
.len
);
445 strbuf_release(&out
);
448 static void finish(struct commit
*head_commit
,
449 struct commit_list
*remoteheads
,
450 const struct object_id
*new_head
, const char *msg
)
452 struct strbuf reflog_message
= STRBUF_INIT
;
453 const struct object_id
*head
= &head_commit
->object
.oid
;
456 strbuf_addstr(&reflog_message
, getenv("GIT_REFLOG_ACTION"));
460 strbuf_addf(&reflog_message
, "%s: %s",
461 getenv("GIT_REFLOG_ACTION"), msg
);
464 squash_message(head_commit
, remoteheads
);
466 if (verbosity
>= 0 && !merge_msg
.len
)
467 printf(_("No merge message -- not updating HEAD\n"));
469 update_ref(reflog_message
.buf
, "HEAD", new_head
, head
,
470 0, UPDATE_REFS_DIE_ON_ERR
);
472 * We ignore errors in 'gc --auto', since the
473 * user should see them.
475 run_auto_maintenance(verbosity
< 0);
478 if (new_head
&& show_diffstat
) {
479 struct diff_options opts
;
480 repo_diff_setup(the_repository
, &opts
);
481 opts
.stat_width
= -1; /* use full terminal width */
482 opts
.stat_graph_width
= -1; /* respect statGraphWidth config */
483 opts
.output_format
|=
484 DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
485 opts
.detect_rename
= DIFF_DETECT_RENAME
;
486 diff_setup_done(&opts
);
487 diff_tree_oid(head
, new_head
, "", &opts
);
492 /* Run a post-merge hook */
493 run_hooks_l("post-merge", squash
? "1" : "0", NULL
);
495 apply_autostash(git_path_merge_autostash(the_repository
));
496 strbuf_release(&reflog_message
);
499 /* Get the name for the merge commit's message. */
500 static void merge_name(const char *remote
, struct strbuf
*msg
)
502 struct commit
*remote_head
;
503 struct object_id branch_head
;
504 struct strbuf buf
= STRBUF_INIT
;
505 struct strbuf bname
= STRBUF_INIT
;
506 struct merge_remote_desc
*desc
;
508 char *found_ref
= NULL
;
511 strbuf_branchname(&bname
, remote
, 0);
514 oidclr(&branch_head
);
515 remote_head
= get_merge_parent(remote
);
517 die(_("'%s' does not point to a commit"), remote
);
519 if (dwim_ref(remote
, strlen(remote
), &branch_head
, &found_ref
, 0) > 0) {
520 if (starts_with(found_ref
, "refs/heads/")) {
521 strbuf_addf(msg
, "%s\t\tbranch '%s' of .\n",
522 oid_to_hex(&branch_head
), remote
);
525 if (starts_with(found_ref
, "refs/tags/")) {
526 strbuf_addf(msg
, "%s\t\ttag '%s' of .\n",
527 oid_to_hex(&branch_head
), remote
);
530 if (starts_with(found_ref
, "refs/remotes/")) {
531 strbuf_addf(msg
, "%s\t\tremote-tracking branch '%s' of .\n",
532 oid_to_hex(&branch_head
), remote
);
537 /* See if remote matches <name>^^^.. or <name>~<number> */
538 for (len
= 0, ptr
= remote
+ strlen(remote
);
539 remote
< ptr
&& ptr
[-1] == '^';
546 ptr
= strrchr(remote
, '~');
548 int seen_nonzero
= 0;
551 while (*++ptr
&& isdigit(*ptr
)) {
552 seen_nonzero
|= (*ptr
!= '0');
556 len
= 0; /* not ...~<number> */
557 else if (seen_nonzero
)
560 early
= 1; /* "name~" is "name~1"! */
564 struct strbuf truname
= STRBUF_INIT
;
565 strbuf_addf(&truname
, "refs/heads/%s", remote
);
566 strbuf_setlen(&truname
, truname
.len
- len
);
567 if (ref_exists(truname
.buf
)) {
569 "%s\t\tbranch '%s'%s of .\n",
570 oid_to_hex(&remote_head
->object
.oid
),
572 (early
? " (early part)" : ""));
573 strbuf_release(&truname
);
576 strbuf_release(&truname
);
579 desc
= merge_remote_util(remote_head
);
580 if (desc
&& desc
->obj
&& desc
->obj
->type
== OBJ_TAG
) {
581 strbuf_addf(msg
, "%s\t\t%s '%s'\n",
582 oid_to_hex(&desc
->obj
->oid
),
583 type_name(desc
->obj
->type
),
588 strbuf_addf(msg
, "%s\t\tcommit '%s'\n",
589 oid_to_hex(&remote_head
->object
.oid
), remote
);
592 strbuf_release(&buf
);
593 strbuf_release(&bname
);
596 static void parse_branch_merge_options(char *bmo
)
603 argc
= split_cmdline(bmo
, &argv
);
605 die(_("Bad branch.%s.mergeoptions string: %s"), branch
,
606 _(split_cmdline_strerror(argc
)));
607 REALLOC_ARRAY(argv
, argc
+ 2);
608 MOVE_ARRAY(argv
+ 1, argv
, argc
+ 1);
610 argv
[0] = "branch.*.mergeoptions";
611 parse_options(argc
, argv
, NULL
, builtin_merge_options
,
612 builtin_merge_usage
, 0);
616 static int git_merge_config(const char *k
, const char *v
, void *cb
)
622 skip_prefix(k
, "branch.", &str
) &&
623 skip_prefix(str
, branch
, &str
) &&
624 !strcmp(str
, ".mergeoptions")) {
625 free(branch_mergeoptions
);
626 branch_mergeoptions
= xstrdup(v
);
630 if (!strcmp(k
, "merge.diffstat") || !strcmp(k
, "merge.stat"))
631 show_diffstat
= git_config_bool(k
, v
);
632 else if (!strcmp(k
, "merge.verifysignatures"))
633 verify_signatures
= git_config_bool(k
, v
);
634 else if (!strcmp(k
, "pull.twohead"))
635 return git_config_string(&pull_twohead
, k
, v
);
636 else if (!strcmp(k
, "pull.octopus"))
637 return git_config_string(&pull_octopus
, k
, v
);
638 else if (!strcmp(k
, "commit.cleanup"))
639 return git_config_string(&cleanup_arg
, k
, v
);
640 else if (!strcmp(k
, "merge.ff")) {
641 int boolval
= git_parse_maybe_bool(v
);
643 fast_forward
= boolval
? FF_ALLOW
: FF_NO
;
644 } else if (v
&& !strcmp(v
, "only")) {
645 fast_forward
= FF_ONLY
;
646 } /* do not barf on values from future versions of git */
648 } else if (!strcmp(k
, "merge.defaulttoupstream")) {
649 default_to_upstream
= git_config_bool(k
, v
);
651 } else if (!strcmp(k
, "commit.gpgsign")) {
652 sign_commit
= git_config_bool(k
, v
) ? "" : NULL
;
654 } else if (!strcmp(k
, "gpg.mintrustlevel")) {
655 check_trust_level
= 0;
656 } else if (!strcmp(k
, "merge.autostash")) {
657 autostash
= git_config_bool(k
, v
);
661 status
= fmt_merge_msg_config(k
, v
, cb
);
664 status
= git_gpg_config(k
, v
, NULL
);
667 return git_diff_ui_config(k
, v
, cb
);
670 static int read_tree_trivial(struct object_id
*common
, struct object_id
*head
,
671 struct object_id
*one
)
674 struct tree
*trees
[MAX_UNPACK_TREES
];
675 struct tree_desc t
[MAX_UNPACK_TREES
];
676 struct unpack_trees_options opts
;
678 memset(&opts
, 0, sizeof(opts
));
680 opts
.src_index
= &the_index
;
681 opts
.dst_index
= &the_index
;
683 opts
.verbose_update
= 1;
684 opts
.trivial_merges_only
= 1;
686 opts
.preserve_ignored
= 0; /* FIXME: !overwrite_ignore */
687 trees
[nr_trees
] = parse_tree_indirect(common
);
688 if (!trees
[nr_trees
++])
690 trees
[nr_trees
] = parse_tree_indirect(head
);
691 if (!trees
[nr_trees
++])
693 trees
[nr_trees
] = parse_tree_indirect(one
);
694 if (!trees
[nr_trees
++])
696 opts
.fn
= threeway_merge
;
697 cache_tree_free(&active_cache_tree
);
698 for (i
= 0; i
< nr_trees
; i
++) {
699 parse_tree(trees
[i
]);
700 init_tree_desc(t
+i
, trees
[i
]->buffer
, trees
[i
]->size
);
702 if (unpack_trees(nr_trees
, t
, &opts
))
707 static void write_tree_trivial(struct object_id
*oid
)
709 if (write_cache_as_tree(oid
, 0, NULL
))
710 die(_("git write-tree failed to write a tree"));
713 static int try_merge_strategy(const char *strategy
, struct commit_list
*common
,
714 struct commit_list
*remoteheads
,
717 const char *head_arg
= "HEAD";
719 if (refresh_and_write_cache(REFRESH_QUIET
, SKIP_IF_UNCHANGED
, 0) < 0)
720 return error(_("Unable to write index."));
722 if (!strcmp(strategy
, "recursive") || !strcmp(strategy
, "subtree") ||
723 !strcmp(strategy
, "ort")) {
724 struct lock_file lock
= LOCK_INIT
;
726 struct commit
*result
;
727 struct commit_list
*reversed
= NULL
;
728 struct merge_options o
;
729 struct commit_list
*j
;
731 if (remoteheads
->next
) {
732 error(_("Not handling anything other than two heads merge."));
736 init_merge_options(&o
, the_repository
);
737 if (!strcmp(strategy
, "subtree"))
738 o
.subtree_shift
= "";
740 o
.show_rename_progress
=
741 show_progress
== -1 ? isatty(2) : show_progress
;
743 for (x
= 0; x
< xopts_nr
; x
++)
744 if (parse_merge_opt(&o
, xopts
[x
]))
745 die(_("unknown strategy option: -X%s"), xopts
[x
]);
747 o
.branch1
= head_arg
;
748 o
.branch2
= merge_remote_util(remoteheads
->item
)->name
;
750 for (j
= common
; j
; j
= j
->next
)
751 commit_list_insert(j
->item
, &reversed
);
753 hold_locked_index(&lock
, LOCK_DIE_ON_ERROR
);
754 if (!strcmp(strategy
, "ort"))
755 clean
= merge_ort_recursive(&o
, head
, remoteheads
->item
,
758 clean
= merge_recursive(&o
, head
, remoteheads
->item
,
762 if (write_locked_index(&the_index
, &lock
,
763 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
764 die(_("unable to write %s"), get_index_file());
765 return clean
? 0 : 1;
767 return try_merge_command(the_repository
,
768 strategy
, xopts_nr
, xopts
,
769 common
, head_arg
, remoteheads
);
773 static void count_diff_files(struct diff_queue_struct
*q
,
774 struct diff_options
*opt
, void *data
)
781 static int count_unmerged_entries(void)
785 for (i
= 0; i
< active_nr
; i
++)
786 if (ce_stage(active_cache
[i
]))
792 static void add_strategies(const char *string
, unsigned attr
)
797 struct string_list list
= STRING_LIST_INIT_DUP
;
798 struct string_list_item
*item
;
799 string_list_split(&list
, string
, ' ', -1);
800 for_each_string_list_item(item
, &list
)
801 append_strategy(get_strategy(item
->string
));
802 string_list_clear(&list
, 0);
805 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
806 if (all_strategy
[i
].attr
& attr
)
807 append_strategy(&all_strategy
[i
]);
811 static void read_merge_msg(struct strbuf
*msg
)
813 const char *filename
= git_path_merge_msg(the_repository
);
815 if (strbuf_read_file(msg
, filename
, 0) < 0)
816 die_errno(_("Could not read from '%s'"), filename
);
819 static void write_merge_state(struct commit_list
*);
820 static void abort_commit(struct commit_list
*remoteheads
, const char *err_msg
)
823 error("%s", err_msg
);
825 _("Not committing merge; use 'git commit' to complete the merge.\n"));
826 write_merge_state(remoteheads
);
830 static const char merge_editor_comment
[] =
831 N_("Please enter a commit message to explain why this merge is necessary,\n"
832 "especially if it merges an updated upstream into a topic branch.\n"
835 static const char scissors_editor_comment
[] =
836 N_("An empty message aborts the commit.\n");
838 static const char no_scissors_editor_comment
[] =
839 N_("Lines starting with '%c' will be ignored, and an empty message aborts\n"
842 static void write_merge_heads(struct commit_list
*);
843 static void prepare_to_commit(struct commit_list
*remoteheads
)
845 struct strbuf msg
= STRBUF_INIT
;
846 const char *index_file
= get_index_file();
851 if (run_commit_hook(0 < option_edit
, index_file
, &invoked_hook
,
852 "pre-merge-commit", NULL
))
853 abort_commit(remoteheads
, NULL
);
855 * Re-read the index as pre-merge-commit hook could have updated it,
856 * and write it out as a tree. We must do this before we invoke
857 * the editor and after we invoke run_status above.
862 read_cache_from(index_file
);
863 strbuf_addbuf(&msg
, &merge_msg
);
865 BUG("the control must not reach here under --squash");
866 if (0 < option_edit
) {
867 strbuf_addch(&msg
, '\n');
868 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
) {
869 wt_status_append_cut_line(&msg
);
870 strbuf_commented_addf(&msg
, "\n");
872 strbuf_commented_addf(&msg
, _(merge_editor_comment
));
873 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
)
874 strbuf_commented_addf(&msg
, _(scissors_editor_comment
));
876 strbuf_commented_addf(&msg
,
877 _(no_scissors_editor_comment
), comment_line_char
);
880 append_signoff(&msg
, ignore_non_trailer(msg
.buf
, msg
.len
), 0);
881 write_merge_heads(remoteheads
);
882 write_file_buf(git_path_merge_msg(the_repository
), msg
.buf
, msg
.len
);
883 if (run_commit_hook(0 < option_edit
, get_index_file(), NULL
,
884 "prepare-commit-msg",
885 git_path_merge_msg(the_repository
), "merge", NULL
))
886 abort_commit(remoteheads
, NULL
);
887 if (0 < option_edit
) {
888 if (launch_editor(git_path_merge_msg(the_repository
), NULL
, NULL
))
889 abort_commit(remoteheads
, NULL
);
892 if (!no_verify
&& run_commit_hook(0 < option_edit
, get_index_file(),
894 git_path_merge_msg(the_repository
), NULL
))
895 abort_commit(remoteheads
, NULL
);
897 read_merge_msg(&msg
);
898 cleanup_message(&msg
, cleanup_mode
, 0);
900 abort_commit(remoteheads
, _("Empty commit message."));
901 strbuf_release(&merge_msg
);
902 strbuf_addbuf(&merge_msg
, &msg
);
903 strbuf_release(&msg
);
906 static int merge_trivial(struct commit
*head
, struct commit_list
*remoteheads
)
908 struct object_id result_tree
, result_commit
;
909 struct commit_list
*parents
, **pptr
= &parents
;
911 if (refresh_and_write_cache(REFRESH_QUIET
, SKIP_IF_UNCHANGED
, 0) < 0)
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");
923 remove_merge_branch_state(the_repository
);
927 static int finish_automerge(struct commit
*head
,
929 struct commit_list
*common
,
930 struct commit_list
*remoteheads
,
931 struct object_id
*result_tree
,
932 const char *wt_strategy
)
934 struct commit_list
*parents
= NULL
;
935 struct strbuf buf
= STRBUF_INIT
;
936 struct object_id result_commit
;
938 write_tree_trivial(result_tree
);
939 free_commit_list(common
);
940 parents
= remoteheads
;
941 if (!head_subsumed
|| fast_forward
== FF_NO
)
942 commit_list_insert(head
, &parents
);
943 prepare_to_commit(remoteheads
);
944 if (commit_tree(merge_msg
.buf
, merge_msg
.len
, result_tree
, parents
,
945 &result_commit
, NULL
, sign_commit
))
946 die(_("failed to write commit object"));
947 strbuf_addf(&buf
, "Merge made by the '%s' strategy.", wt_strategy
);
948 finish(head
, remoteheads
, &result_commit
, buf
.buf
);
949 strbuf_release(&buf
);
950 remove_merge_branch_state(the_repository
);
954 static int suggest_conflicts(void)
956 const char *filename
;
958 struct strbuf msgbuf
= STRBUF_INIT
;
960 filename
= git_path_merge_msg(the_repository
);
961 fp
= xfopen(filename
, "a");
964 * We can't use cleanup_mode because if we're not using the editor,
965 * get_cleanup_mode will return COMMIT_MSG_CLEANUP_SPACE instead, even
966 * though the message is meant to be processed later by git-commit.
967 * Thus, we will get the cleanup mode which is returned when we _are_
970 append_conflicts_hint(&the_index
, &msgbuf
,
971 get_cleanup_mode(cleanup_arg
, 1));
972 fputs(msgbuf
.buf
, fp
);
973 strbuf_release(&msgbuf
);
975 repo_rerere(the_repository
, allow_rerere_auto
);
976 printf(_("Automatic merge failed; "
977 "fix conflicts and then commit the result.\n"));
981 static int evaluate_result(void)
986 /* Check how many files differ. */
987 repo_init_revisions(the_repository
, &rev
, "");
988 setup_revisions(0, NULL
, &rev
, NULL
);
989 rev
.diffopt
.output_format
|=
990 DIFF_FORMAT_CALLBACK
;
991 rev
.diffopt
.format_callback
= count_diff_files
;
992 rev
.diffopt
.format_callback_data
= &cnt
;
993 run_diff_files(&rev
, 0);
996 * Check how many unmerged entries are
999 cnt
+= count_unmerged_entries();
1005 * Pretend as if the user told us to merge with the remote-tracking
1006 * branch we have for the upstream of the current branch
1008 static int setup_with_upstream(const char ***argv
)
1010 struct branch
*branch
= branch_get(NULL
);
1015 die(_("No current branch."));
1016 if (!branch
->remote_name
)
1017 die(_("No remote for the current branch."));
1018 if (!branch
->merge_nr
)
1019 die(_("No default upstream defined for the current branch."));
1021 args
= xcalloc(st_add(branch
->merge_nr
, 1), sizeof(char *));
1022 for (i
= 0; i
< branch
->merge_nr
; i
++) {
1023 if (!branch
->merge
[i
]->dst
)
1024 die(_("No remote-tracking branch for %s from %s"),
1025 branch
->merge
[i
]->src
, branch
->remote_name
);
1026 args
[i
] = branch
->merge
[i
]->dst
;
1033 static void write_merge_heads(struct commit_list
*remoteheads
)
1035 struct commit_list
*j
;
1036 struct strbuf buf
= STRBUF_INIT
;
1038 for (j
= remoteheads
; j
; j
= j
->next
) {
1039 struct object_id
*oid
;
1040 struct commit
*c
= j
->item
;
1041 struct merge_remote_desc
*desc
;
1043 desc
= merge_remote_util(c
);
1044 if (desc
&& desc
->obj
) {
1045 oid
= &desc
->obj
->oid
;
1047 oid
= &c
->object
.oid
;
1049 strbuf_addf(&buf
, "%s\n", oid_to_hex(oid
));
1051 write_file_buf(git_path_merge_head(the_repository
), buf
.buf
, buf
.len
);
1054 if (fast_forward
== FF_NO
)
1055 strbuf_addstr(&buf
, "no-ff");
1056 write_file_buf(git_path_merge_mode(the_repository
), buf
.buf
, buf
.len
);
1057 strbuf_release(&buf
);
1060 static void write_merge_state(struct commit_list
*remoteheads
)
1062 write_merge_heads(remoteheads
);
1063 strbuf_addch(&merge_msg
, '\n');
1064 write_file_buf(git_path_merge_msg(the_repository
), merge_msg
.buf
,
1068 static int default_edit_option(void)
1070 static const char name
[] = "GIT_MERGE_AUTOEDIT";
1071 const char *e
= getenv(name
);
1072 struct stat st_stdin
, st_stdout
;
1075 /* an explicit -m msg without --[no-]edit */
1079 int v
= git_parse_maybe_bool(e
);
1081 die(_("Bad value '%s' in environment '%s'"), e
, name
);
1085 /* Use editor if stdin and stdout are the same and is a tty */
1086 return (!fstat(0, &st_stdin
) &&
1087 !fstat(1, &st_stdout
) &&
1088 isatty(0) && isatty(1) &&
1089 st_stdin
.st_dev
== st_stdout
.st_dev
&&
1090 st_stdin
.st_ino
== st_stdout
.st_ino
&&
1091 st_stdin
.st_mode
== st_stdout
.st_mode
);
1094 static struct commit_list
*reduce_parents(struct commit
*head_commit
,
1096 struct commit_list
*remoteheads
)
1098 struct commit_list
*parents
, **remotes
;
1101 * Is the current HEAD reachable from another commit being
1102 * merged? If so we do not want to record it as a parent of
1103 * the resulting merge, unless --no-ff is given. We will flip
1104 * this variable to 0 when we find HEAD among the independent
1105 * tips being merged.
1109 /* Find what parents to record by checking independent ones. */
1110 parents
= reduce_heads(remoteheads
);
1111 free_commit_list(remoteheads
);
1114 remotes
= &remoteheads
;
1116 struct commit
*commit
= pop_commit(&parents
);
1117 if (commit
== head_commit
)
1120 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1125 static void prepare_merge_message(struct strbuf
*merge_names
, struct strbuf
*merge_msg
)
1127 struct fmt_merge_msg_opts opts
;
1129 memset(&opts
, 0, sizeof(opts
));
1130 opts
.add_title
= !have_message
;
1131 opts
.shortlog_len
= shortlog_len
;
1132 opts
.credit_people
= (0 < option_edit
);
1133 opts
.into_name
= into_name
;
1135 fmt_merge_msg(merge_names
, merge_msg
, &opts
);
1137 strbuf_setlen(merge_msg
, merge_msg
->len
- 1);
1140 static void handle_fetch_head(struct commit_list
**remotes
, struct strbuf
*merge_names
)
1142 const char *filename
;
1144 struct strbuf fetch_head_file
= STRBUF_INIT
;
1145 const unsigned hexsz
= the_hash_algo
->hexsz
;
1148 merge_names
= &fetch_head_file
;
1150 filename
= git_path_fetch_head(the_repository
);
1151 fd
= xopen(filename
, O_RDONLY
);
1153 if (strbuf_read(merge_names
, fd
, 0) < 0)
1154 die_errno(_("could not read '%s'"), filename
);
1156 die_errno(_("could not close '%s'"), filename
);
1158 for (pos
= 0; pos
< merge_names
->len
; pos
= npos
) {
1159 struct object_id oid
;
1161 struct commit
*commit
;
1163 ptr
= strchr(merge_names
->buf
+ pos
, '\n');
1165 npos
= ptr
- merge_names
->buf
+ 1;
1167 npos
= merge_names
->len
;
1169 if (npos
- pos
< hexsz
+ 2 ||
1170 get_oid_hex(merge_names
->buf
+ pos
, &oid
))
1171 commit
= NULL
; /* bad */
1172 else if (memcmp(merge_names
->buf
+ pos
+ hexsz
, "\t\t", 2))
1173 continue; /* not-for-merge */
1175 char saved
= merge_names
->buf
[pos
+ hexsz
];
1176 merge_names
->buf
[pos
+ hexsz
] = '\0';
1177 commit
= get_merge_parent(merge_names
->buf
+ pos
);
1178 merge_names
->buf
[pos
+ hexsz
] = saved
;
1183 die(_("not something we can merge in %s: %s"),
1184 filename
, merge_names
->buf
+ pos
);
1186 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1189 if (merge_names
== &fetch_head_file
)
1190 strbuf_release(&fetch_head_file
);
1193 static struct commit_list
*collect_parents(struct commit
*head_commit
,
1195 int argc
, const char **argv
,
1196 struct strbuf
*merge_msg
)
1199 struct commit_list
*remoteheads
= NULL
;
1200 struct commit_list
**remotes
= &remoteheads
;
1201 struct strbuf merge_names
= STRBUF_INIT
, *autogen
= NULL
;
1203 if (merge_msg
&& (!have_message
|| shortlog_len
))
1204 autogen
= &merge_names
;
1207 remotes
= &commit_list_insert(head_commit
, remotes
)->next
;
1209 if (argc
== 1 && !strcmp(argv
[0], "FETCH_HEAD")) {
1210 handle_fetch_head(remotes
, autogen
);
1211 remoteheads
= reduce_parents(head_commit
, head_subsumed
, remoteheads
);
1213 for (i
= 0; i
< argc
; i
++) {
1214 struct commit
*commit
= get_merge_parent(argv
[i
]);
1216 help_unknown_ref(argv
[i
], "merge",
1217 _("not something we can merge"));
1218 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1220 remoteheads
= reduce_parents(head_commit
, head_subsumed
, remoteheads
);
1222 struct commit_list
*p
;
1223 for (p
= remoteheads
; p
; p
= p
->next
)
1224 merge_name(merge_remote_util(p
->item
)->name
, autogen
);
1229 prepare_merge_message(autogen
, merge_msg
);
1230 strbuf_release(autogen
);
1236 static int merging_a_throwaway_tag(struct commit
*commit
)
1239 struct object_id oid
;
1240 int is_throwaway_tag
= 0;
1242 /* Are we merging a tag? */
1243 if (!merge_remote_util(commit
) ||
1244 !merge_remote_util(commit
)->obj
||
1245 merge_remote_util(commit
)->obj
->type
!= OBJ_TAG
)
1246 return is_throwaway_tag
;
1249 * Now we know we are merging a tag object. Are we downstream
1250 * and following the tags from upstream? If so, we must have
1251 * the tag object pointed at by "refs/tags/$T" where $T is the
1252 * tagname recorded in the tag object. We want to allow such
1253 * a "just to catch up" merge to fast-forward.
1255 * Otherwise, we are playing an integrator's role, making a
1256 * merge with a throw-away tag from a contributor with
1257 * something like "git pull $contributor $signed_tag".
1258 * We want to forbid such a merge from fast-forwarding
1259 * by default; otherwise we would not keep the signature
1262 tag_ref
= xstrfmt("refs/tags/%s",
1263 ((struct tag
*)merge_remote_util(commit
)->obj
)->tag
);
1264 if (!read_ref(tag_ref
, &oid
) &&
1265 oideq(&oid
, &merge_remote_util(commit
)->obj
->oid
))
1266 is_throwaway_tag
= 0;
1268 is_throwaway_tag
= 1;
1270 return is_throwaway_tag
;
1273 int cmd_merge(int argc
, const char **argv
, const char *prefix
)
1275 struct object_id result_tree
, stash
, head_oid
;
1276 struct commit
*head_commit
;
1277 struct strbuf buf
= STRBUF_INIT
;
1278 int i
, ret
= 0, head_subsumed
;
1279 int best_cnt
= -1, merge_was_ok
= 0, automerge_was_ok
= 0;
1280 struct commit_list
*common
= NULL
;
1281 const char *best_strategy
= NULL
, *wt_strategy
= NULL
;
1282 struct commit_list
*remoteheads
= NULL
, *p
;
1283 void *branch_to_free
;
1284 int orig_argc
= argc
;
1286 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1287 usage_with_options(builtin_merge_usage
, builtin_merge_options
);
1289 prepare_repo_settings(the_repository
);
1290 the_repository
->settings
.command_requires_full_index
= 0;
1293 * Check if we are _not_ on a detached HEAD, i.e. if there is a
1296 branch
= branch_to_free
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
1298 skip_prefix(branch
, "refs/heads/", &branch
);
1300 if (!pull_twohead
) {
1301 char *default_strategy
= getenv("GIT_TEST_MERGE_ALGORITHM");
1302 if (default_strategy
&& !strcmp(default_strategy
, "ort"))
1303 pull_twohead
= "ort";
1306 init_diff_ui_defaults();
1307 git_config(git_merge_config
, NULL
);
1309 if (!branch
|| is_null_oid(&head_oid
))
1312 head_commit
= lookup_commit_or_die(&head_oid
, "HEAD");
1314 if (branch_mergeoptions
)
1315 parse_branch_merge_options(branch_mergeoptions
);
1316 argc
= parse_options(argc
, argv
, prefix
, builtin_merge_options
,
1317 builtin_merge_usage
, 0);
1318 if (shortlog_len
< 0)
1319 shortlog_len
= (merge_log_config
> 0) ? merge_log_config
: 0;
1321 if (verbosity
< 0 && show_progress
== -1)
1324 if (abort_current_merge
) {
1326 const char *nargv
[] = {"reset", "--merge", NULL
};
1327 struct strbuf stash_oid
= STRBUF_INIT
;
1330 usage_msg_opt(_("--abort expects no arguments"),
1331 builtin_merge_usage
, builtin_merge_options
);
1333 if (!file_exists(git_path_merge_head(the_repository
)))
1334 die(_("There is no merge to abort (MERGE_HEAD missing)."));
1336 if (read_oneliner(&stash_oid
, git_path_merge_autostash(the_repository
),
1337 READ_ONELINER_SKIP_IF_EMPTY
))
1338 unlink(git_path_merge_autostash(the_repository
));
1340 /* Invoke 'git reset --merge' */
1341 ret
= cmd_reset(nargc
, nargv
, prefix
);
1344 apply_autostash_oid(stash_oid
.buf
);
1346 strbuf_release(&stash_oid
);
1350 if (quit_current_merge
) {
1352 usage_msg_opt(_("--quit expects no arguments"),
1353 builtin_merge_usage
,
1354 builtin_merge_options
);
1356 remove_merge_branch_state(the_repository
);
1360 if (continue_current_merge
) {
1362 const char *nargv
[] = {"commit", NULL
};
1365 usage_msg_opt(_("--continue expects no arguments"),
1366 builtin_merge_usage
, builtin_merge_options
);
1368 if (!file_exists(git_path_merge_head(the_repository
)))
1369 die(_("There is no merge in progress (MERGE_HEAD missing)."));
1371 /* Invoke 'git commit' */
1372 ret
= cmd_commit(nargc
, nargv
, prefix
);
1376 if (read_cache_unmerged())
1377 die_resolve_conflict("merge");
1379 if (file_exists(git_path_merge_head(the_repository
))) {
1381 * There is no unmerged entry, don't advise 'git
1382 * add/rm <file>', just 'git commit'.
1384 if (advice_enabled(ADVICE_RESOLVE_CONFLICT
))
1385 die(_("You have not concluded your merge (MERGE_HEAD exists).\n"
1386 "Please, commit your changes before you merge."));
1388 die(_("You have not concluded your merge (MERGE_HEAD exists)."));
1390 if (ref_exists("CHERRY_PICK_HEAD")) {
1391 if (advice_enabled(ADVICE_RESOLVE_CONFLICT
))
1392 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
1393 "Please, commit your changes before you merge."));
1395 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists)."));
1397 resolve_undo_clear();
1399 if (option_edit
< 0)
1400 option_edit
= default_edit_option();
1402 cleanup_mode
= get_cleanup_mode(cleanup_arg
, 0 < option_edit
);
1408 if (fast_forward
== FF_NO
)
1409 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--no-ff.");
1410 if (option_commit
> 0)
1411 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--commit.");
1413 * squash can now silently disable option_commit - this is not
1414 * a problem as it is only overriding the default, not a user
1420 if (option_commit
< 0)
1424 if (default_to_upstream
)
1425 argc
= setup_with_upstream(&argv
);
1427 die(_("No commit specified and merge.defaultToUpstream not set."));
1428 } else if (argc
== 1 && !strcmp(argv
[0], "-")) {
1433 usage_with_options(builtin_merge_usage
,
1434 builtin_merge_options
);
1438 * If the merged head is a valid one there is no reason
1439 * to forbid "git merge" into a branch yet to be born.
1440 * We do the same for "git pull".
1442 struct object_id
*remote_head_oid
;
1444 die(_("Squash commit into empty head not supported yet"));
1445 if (fast_forward
== FF_NO
)
1446 die(_("Non-fast-forward commit does not make sense into "
1448 remoteheads
= collect_parents(head_commit
, &head_subsumed
,
1451 die(_("%s - not something we can merge"), argv
[0]);
1452 if (remoteheads
->next
)
1453 die(_("Can merge only exactly one commit into empty head"));
1455 if (verify_signatures
)
1456 verify_merge_signature(remoteheads
->item
, verbosity
,
1459 remote_head_oid
= &remoteheads
->item
->object
.oid
;
1460 read_empty(remote_head_oid
, 0);
1461 update_ref("initial pull", "HEAD", remote_head_oid
, NULL
, 0,
1462 UPDATE_REFS_DIE_ON_ERR
);
1467 * All the rest are the commits being merged; prepare
1468 * the standard merge summary message to be appended
1469 * to the given message.
1471 remoteheads
= collect_parents(head_commit
, &head_subsumed
,
1472 argc
, argv
, &merge_msg
);
1474 if (!head_commit
|| !argc
)
1475 usage_with_options(builtin_merge_usage
,
1476 builtin_merge_options
);
1478 if (verify_signatures
) {
1479 for (p
= remoteheads
; p
; p
= p
->next
) {
1480 verify_merge_signature(p
->item
, verbosity
,
1485 strbuf_addstr(&buf
, "merge");
1486 for (p
= remoteheads
; p
; p
= p
->next
)
1487 strbuf_addf(&buf
, " %s", merge_remote_util(p
->item
)->name
);
1488 setenv("GIT_REFLOG_ACTION", buf
.buf
, 0);
1491 for (p
= remoteheads
; p
; p
= p
->next
) {
1492 struct commit
*commit
= p
->item
;
1493 strbuf_addf(&buf
, "GITHEAD_%s",
1494 oid_to_hex(&commit
->object
.oid
));
1495 setenv(buf
.buf
, merge_remote_util(commit
)->name
, 1);
1497 if (fast_forward
!= FF_ONLY
&& merging_a_throwaway_tag(commit
))
1498 fast_forward
= FF_NO
;
1501 if (!use_strategies
&& !pull_twohead
&&
1502 remoteheads
&& !remoteheads
->next
) {
1503 char *default_strategy
= getenv("GIT_TEST_MERGE_ALGORITHM");
1504 if (default_strategy
)
1505 append_strategy(get_strategy(default_strategy
));
1507 if (!use_strategies
) {
1509 ; /* already up-to-date */
1510 else if (!remoteheads
->next
)
1511 add_strategies(pull_twohead
, DEFAULT_TWOHEAD
);
1513 add_strategies(pull_octopus
, DEFAULT_OCTOPUS
);
1516 for (i
= 0; i
< use_strategies_nr
; i
++) {
1517 if (use_strategies
[i
]->attr
& NO_FAST_FORWARD
)
1518 fast_forward
= FF_NO
;
1519 if (use_strategies
[i
]->attr
& NO_TRIVIAL
)
1524 ; /* already up-to-date */
1525 else if (!remoteheads
->next
)
1526 common
= get_merge_bases(head_commit
, remoteheads
->item
);
1528 struct commit_list
*list
= remoteheads
;
1529 commit_list_insert(head_commit
, &list
);
1530 common
= get_octopus_merge_bases(list
);
1534 update_ref("updating ORIG_HEAD", "ORIG_HEAD",
1535 &head_commit
->object
.oid
, NULL
, 0, UPDATE_REFS_DIE_ON_ERR
);
1537 if (remoteheads
&& !common
) {
1538 /* No common ancestors found. */
1539 if (!allow_unrelated_histories
)
1540 die(_("refusing to merge unrelated histories"));
1541 /* otherwise, we need a real merge. */
1542 } else if (!remoteheads
||
1543 (!remoteheads
->next
&& !common
->next
&&
1544 common
->item
== remoteheads
->item
)) {
1546 * If head can reach all the merge then we are up to date.
1547 * but first the most common case of merging one remote.
1549 finish_up_to_date();
1551 } else if (fast_forward
!= FF_NO
&& !remoteheads
->next
&&
1553 oideq(&common
->item
->object
.oid
, &head_commit
->object
.oid
)) {
1554 /* Again the most common case of merging one remote. */
1555 struct strbuf msg
= STRBUF_INIT
;
1556 struct commit
*commit
;
1558 if (verbosity
>= 0) {
1559 printf(_("Updating %s..%s\n"),
1560 find_unique_abbrev(&head_commit
->object
.oid
,
1562 find_unique_abbrev(&remoteheads
->item
->object
.oid
,
1565 strbuf_addstr(&msg
, "Fast-forward");
1568 " (no commit created; -m option ignored)");
1569 commit
= remoteheads
->item
;
1576 create_autostash(the_repository
,
1577 git_path_merge_autostash(the_repository
));
1578 if (checkout_fast_forward(the_repository
,
1579 &head_commit
->object
.oid
,
1580 &commit
->object
.oid
,
1581 overwrite_ignore
)) {
1582 apply_autostash(git_path_merge_autostash(the_repository
));
1587 finish(head_commit
, remoteheads
, &commit
->object
.oid
, msg
.buf
);
1588 remove_merge_branch_state(the_repository
);
1589 strbuf_release(&msg
);
1591 } else if (!remoteheads
->next
&& common
->next
)
1594 * We are not doing octopus and not fast-forward. Need
1597 else if (!remoteheads
->next
&& !common
->next
&& option_commit
) {
1599 * We are not doing octopus, not fast-forward, and have
1602 refresh_cache(REFRESH_QUIET
);
1603 if (allow_trivial
&& fast_forward
!= FF_ONLY
) {
1604 /* See if it is really trivial. */
1605 git_committer_info(IDENT_STRICT
);
1606 printf(_("Trying really trivial in-index merge...\n"));
1607 if (!read_tree_trivial(&common
->item
->object
.oid
,
1608 &head_commit
->object
.oid
,
1609 &remoteheads
->item
->object
.oid
)) {
1610 ret
= merge_trivial(head_commit
, remoteheads
);
1613 printf(_("Nope.\n"));
1617 * An octopus. If we can reach all the remote we are up
1621 struct commit_list
*j
;
1623 for (j
= remoteheads
; j
; j
= j
->next
) {
1624 struct commit_list
*common_one
;
1627 * Here we *have* to calculate the individual
1628 * merge_bases again, otherwise "git merge HEAD^
1629 * HEAD^^" would be missed.
1631 common_one
= get_merge_bases(head_commit
, j
->item
);
1632 if (!oideq(&common_one
->item
->object
.oid
, &j
->item
->object
.oid
)) {
1638 finish_up_to_date();
1643 if (fast_forward
== FF_ONLY
)
1644 die_ff_impossible();
1647 create_autostash(the_repository
,
1648 git_path_merge_autostash(the_repository
));
1650 /* We are going to make a new commit. */
1651 git_committer_info(IDENT_STRICT
);
1654 * At this point, we need a real merge. No matter what strategy
1655 * we use, it would operate on the index, possibly affecting the
1656 * working tree, and when resolved cleanly, have the desired
1657 * tree in the index -- this means that the index must be in
1658 * sync with the head commit. The strategies are responsible
1661 if (use_strategies_nr
== 1 ||
1663 * Stash away the local changes so that we can try more than one.
1668 for (i
= 0; !merge_was_ok
&& i
< use_strategies_nr
; i
++) {
1671 printf(_("Rewinding the tree to pristine...\n"));
1672 restore_state(&head_commit
->object
.oid
, &stash
);
1674 if (use_strategies_nr
!= 1)
1675 printf(_("Trying merge strategy %s...\n"),
1676 use_strategies
[i
]->name
);
1678 * Remember which strategy left the state in the working
1681 wt_strategy
= use_strategies
[i
]->name
;
1683 ret
= try_merge_strategy(use_strategies
[i
]->name
,
1684 common
, remoteheads
,
1687 * The backend exits with 1 when conflicts are
1688 * left to be resolved, with 2 when it does not
1689 * handle the given merge at all.
1693 if (option_commit
) {
1694 /* Automerge succeeded. */
1695 automerge_was_ok
= 1;
1700 cnt
= (use_strategies_nr
> 1) ? evaluate_result() : 0;
1701 if (best_cnt
<= 0 || cnt
<= best_cnt
) {
1702 best_strategy
= use_strategies
[i
]->name
;
1709 * If we have a resulting tree, that means the strategy module
1710 * auto resolved the merge cleanly.
1712 if (automerge_was_ok
) {
1713 ret
= finish_automerge(head_commit
, head_subsumed
,
1714 common
, remoteheads
,
1715 &result_tree
, wt_strategy
);
1720 * Pick the result from the best strategy and have the user fix
1723 if (!best_strategy
) {
1724 restore_state(&head_commit
->object
.oid
, &stash
);
1725 if (use_strategies_nr
> 1)
1727 _("No merge strategy handled the merge.\n"));
1729 fprintf(stderr
, _("Merge with strategy %s failed.\n"),
1730 use_strategies
[0]->name
);
1731 apply_autostash(git_path_merge_autostash(the_repository
));
1734 } else if (best_strategy
== wt_strategy
)
1735 ; /* We already have its result in the working tree. */
1737 printf(_("Rewinding the tree to pristine...\n"));
1738 restore_state(&head_commit
->object
.oid
, &stash
);
1739 printf(_("Using the %s strategy to prepare resolving by hand.\n"),
1741 try_merge_strategy(best_strategy
, common
, remoteheads
,
1746 finish(head_commit
, remoteheads
, NULL
, NULL
);
1748 git_test_write_commit_graph_or_die();
1750 write_merge_state(remoteheads
);
1753 fprintf(stderr
, _("Automatic merge went well; "
1754 "stopped before committing as requested\n"));
1756 ret
= suggest_conflicts();
1759 if (!automerge_was_ok
) {
1760 free_commit_list(common
);
1761 free_commit_list(remoteheads
);
1763 strbuf_release(&buf
);
1764 free(branch_to_free
);