4 * Copyright (c) 2008 Miklos Vajna <vmiklos@frugalware.org>
6 * Based on git-merge.sh by Junio C Hamano.
9 #define USE_THE_REPOSITORY_VARIABLE
10 #define DISABLE_SIGN_COMPARE_WARNINGS
18 #include "environment.h"
21 #include "object-name.h"
22 #include "parse-options.h"
24 #include "repository.h"
25 #include "run-command.h"
28 #include "diff-merges.h"
35 #include "unpack-trees.h"
36 #include "cache-tree.h"
42 #include "merge-ort-wrappers.h"
43 #include "resolve-undo.h"
45 #include "fmt-merge-msg.h"
46 #include "sequencer.h"
47 #include "string-list.h"
51 #include "commit-reach.h"
52 #include "wt-status.h"
53 #include "commit-graph.h"
55 #define DEFAULT_TWOHEAD (1<<0)
56 #define DEFAULT_OCTOPUS (1<<1)
57 #define NO_FAST_FORWARD (1<<2)
58 #define NO_TRIVIAL (1<<3)
65 static const char * const builtin_merge_usage
[] = {
66 N_("git merge [<options>] [<commit>...]"),
68 "git merge --continue",
72 #define MERGE_SHOW_DIFFSTAT 1
73 #define MERGE_SHOW_COMPACTSUMMARY 2
75 static int show_diffstat
= MERGE_SHOW_DIFFSTAT
, shortlog_len
= -1, squash
;
76 static int option_commit
= -1;
77 static int option_edit
= -1;
78 static int allow_trivial
= 1, have_message
, verify_signatures
;
79 static int check_trust_level
= 1;
80 static int overwrite_ignore
= 1;
81 static struct strbuf merge_msg
= STRBUF_INIT
;
82 static struct strategy
**use_strategies
;
83 static size_t use_strategies_nr
, use_strategies_alloc
;
84 static struct strvec xopts
= STRVEC_INIT
;
85 static const char *branch
;
86 static char *branch_mergeoptions
;
88 static int allow_rerere_auto
;
89 static int abort_current_merge
;
90 static int quit_current_merge
;
91 static int continue_current_merge
;
92 static int allow_unrelated_histories
;
93 static int show_progress
= -1;
94 static int default_to_upstream
= 1;
96 static const char *sign_commit
;
99 static char *into_name
;
101 static struct strategy all_strategy
[] = {
102 { "recursive", NO_TRIVIAL
},
103 { "octopus", DEFAULT_OCTOPUS
},
104 { "ort", DEFAULT_TWOHEAD
| NO_TRIVIAL
},
106 { "ours", NO_FAST_FORWARD
| NO_TRIVIAL
},
107 { "subtree", NO_FAST_FORWARD
| NO_TRIVIAL
},
110 static char *pull_twohead
, *pull_octopus
;
118 static enum ff_type fast_forward
= FF_ALLOW
;
120 static char *cleanup_arg
;
121 static enum commit_msg_cleanup_mode cleanup_mode
;
123 static int option_parse_message(const struct option
*opt
,
124 const char *arg
, int unset
)
126 struct strbuf
*buf
= opt
->value
;
129 strbuf_setlen(buf
, 0);
131 strbuf_addf(buf
, "%s%s", buf
->len
? "\n\n" : "", arg
);
134 return error(_("switch `m' requires a value"));
138 static enum parse_opt_result
option_read_message(struct parse_opt_ctx_t
*ctx
,
139 const struct option
*opt
,
140 const char *arg_not_used
,
143 struct strbuf
*buf
= opt
->value
;
146 BUG_ON_OPT_ARG(arg_not_used
);
148 BUG("-F cannot be negated");
153 } else if (ctx
->argc
> 1) {
157 return error(_("option `%s' requires a value"), opt
->long_name
);
160 strbuf_addch(buf
, '\n');
161 if (ctx
->prefix
&& !is_absolute_path(arg
))
162 arg
= prefix_filename(ctx
->prefix
, arg
);
163 if (strbuf_read_file(buf
, arg
, 0) < 0)
164 return error(_("could not read file '%s'"), arg
);
170 static struct strategy
*get_strategy(const char *name
)
173 struct strategy
*ret
;
174 static struct cmdnames main_cmds
= {0}, other_cmds
= {0};
176 char *default_strategy
= NULL
;
181 if (default_strategy
&&
182 !strcmp(default_strategy
, "ort") &&
183 !strcmp(name
, "recursive")) {
187 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
188 if (!strcmp(name
, all_strategy
[i
].name
))
189 return &all_strategy
[i
];
192 struct cmdnames not_strategies
= {0};
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 (!xstrncmpz(all_strategy
[j
].name
, ent
->name
, ent
->len
))
203 add_cmdname(¬_strategies
, ent
->name
, ent
->len
);
205 exclude_cmds(&main_cmds
, ¬_strategies
);
207 cmdnames_release(¬_strategies
);
209 if (!is_in_cmdlist(&main_cmds
, name
) && !is_in_cmdlist(&other_cmds
, name
)) {
210 fprintf(stderr
, _("Could not find merge strategy '%s'.\n"), name
);
211 fprintf(stderr
, _("Available strategies are:"));
212 for (i
= 0; i
< main_cmds
.cnt
; i
++)
213 fprintf(stderr
, " %s", main_cmds
.names
[i
]->name
);
214 fprintf(stderr
, ".\n");
215 if (other_cmds
.cnt
) {
216 fprintf(stderr
, _("Available custom strategies are:"));
217 for (i
= 0; i
< other_cmds
.cnt
; i
++)
218 fprintf(stderr
, " %s", other_cmds
.names
[i
]->name
);
219 fprintf(stderr
, ".\n");
224 CALLOC_ARRAY(ret
, 1);
225 ret
->name
= xstrdup(name
);
226 ret
->attr
= NO_TRIVIAL
;
228 cmdnames_release(&main_cmds
);
229 cmdnames_release(&other_cmds
);
233 static void append_strategy(struct strategy
*s
)
235 ALLOC_GROW(use_strategies
, use_strategies_nr
+ 1, use_strategies_alloc
);
236 use_strategies
[use_strategies_nr
++] = s
;
239 static int option_parse_strategy(const struct option
*opt UNUSED
,
240 const char *name
, int unset
)
245 append_strategy(get_strategy(name
));
249 static int option_parse_compact_summary(const struct option
*opt
,
250 const char *name UNUSED
, int unset
)
252 int *setting
= opt
->value
;
257 *setting
= MERGE_SHOW_COMPACTSUMMARY
;
261 static struct option builtin_merge_options
[] = {
262 OPT_SET_INT('n', NULL
, &show_diffstat
,
263 N_("do not show a diffstat at the end of the merge"), 0),
264 OPT_BOOL(0, "stat", &show_diffstat
,
265 N_("show a diffstat at the end of the merge")),
266 OPT_BOOL(0, "summary", &show_diffstat
, N_("(synonym to --stat)")),
267 OPT_CALLBACK_F(0, "compact-summary", &show_diffstat
, N_("compact-summary"),
268 N_("show a compact-summary at the end of the merge"),
270 option_parse_compact_summary
),
272 .type
= OPTION_INTEGER
,
274 .value
= &shortlog_len
,
275 .precision
= sizeof(shortlog_len
),
277 .help
= N_("add (at most <n>) entries from shortlog to merge commit message"),
278 .flags
= PARSE_OPT_OPTARG
,
279 .defval
= DEFAULT_MERGE_LOG_LEN
,
281 OPT_BOOL(0, "squash", &squash
,
282 N_("create a single commit instead of doing a merge")),
283 OPT_BOOL(0, "commit", &option_commit
,
284 N_("perform a commit if the merge succeeds (default)")),
285 OPT_BOOL('e', "edit", &option_edit
,
286 N_("edit message before committing")),
287 OPT_CLEANUP(&cleanup_arg
),
288 OPT_SET_INT(0, "ff", &fast_forward
, N_("allow fast-forward (default)"), FF_ALLOW
),
289 OPT_SET_INT_F(0, "ff-only", &fast_forward
,
290 N_("abort if fast-forward is not possible"),
291 FF_ONLY
, PARSE_OPT_NONEG
),
292 OPT_RERERE_AUTOUPDATE(&allow_rerere_auto
),
293 OPT_BOOL(0, "verify-signatures", &verify_signatures
,
294 N_("verify that the named commit has a valid GPG signature")),
295 OPT_CALLBACK('s', "strategy", NULL
, N_("strategy"),
296 N_("merge strategy to use"), option_parse_strategy
),
297 OPT_STRVEC('X', "strategy-option", &xopts
, N_("option=value"),
298 N_("option for selected merge strategy")),
299 OPT_CALLBACK('m', "message", &merge_msg
, N_("message"),
300 N_("merge commit message (for a non-fast-forward merge)"),
301 option_parse_message
),
303 .type
= OPTION_LOWLEVEL_CALLBACK
,
308 .help
= N_("read message from file"),
309 .flags
= PARSE_OPT_NONEG
,
310 .ll_callback
= option_read_message
,
312 OPT_STRING(0, "into-name", &into_name
, N_("name"),
313 N_("use <name> instead of the real target")),
314 OPT__VERBOSITY(&verbosity
),
315 OPT_BOOL(0, "abort", &abort_current_merge
,
316 N_("abort the current in-progress merge")),
317 OPT_BOOL(0, "quit", &quit_current_merge
,
318 N_("--abort but leave index and working tree alone")),
319 OPT_BOOL(0, "continue", &continue_current_merge
,
320 N_("continue the current in-progress merge")),
321 OPT_BOOL(0, "allow-unrelated-histories", &allow_unrelated_histories
,
322 N_("allow merging unrelated histories")),
323 OPT_SET_INT(0, "progress", &show_progress
, N_("force progress reporting"), 1),
325 .type
= OPTION_STRING
,
327 .long_name
= "gpg-sign",
328 .value
= &sign_commit
,
329 .argh
= N_("key-id"),
330 .help
= N_("GPG sign commit"),
331 .flags
= PARSE_OPT_OPTARG
,
332 .defval
= (intptr_t) "",
334 OPT_AUTOSTASH(&autostash
),
335 OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore
, N_("update ignored files (default)")),
336 OPT_BOOL(0, "signoff", &signoff
, N_("add a Signed-off-by trailer")),
337 OPT_BOOL(0, "no-verify", &no_verify
, N_("bypass pre-merge-commit and commit-msg hooks")),
341 static int save_state(struct object_id
*stash
)
344 struct child_process cp
= CHILD_PROCESS_INIT
;
345 struct strbuf buffer
= STRBUF_INIT
;
346 struct lock_file lock_file
= LOCK_INIT
;
350 fd
= repo_hold_locked_index(the_repository
, &lock_file
, 0);
351 refresh_index(the_repository
->index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
353 repo_update_index_if_able(the_repository
, &lock_file
);
354 rollback_lock_file(&lock_file
);
356 strvec_pushl(&cp
.args
, "stash", "create", NULL
);
360 if (start_command(&cp
))
361 die(_("could not run stash."));
362 len
= strbuf_read(&buffer
, cp
.out
, 1024);
365 if (finish_command(&cp
) || len
< 0)
366 die(_("stash failed"));
367 else if (!len
) /* no changes */
369 strbuf_setlen(&buffer
, buffer
.len
-1);
370 if (repo_get_oid(the_repository
, buffer
.buf
, stash
))
371 die(_("not a valid object: %s"), buffer
.buf
);
374 strbuf_release(&buffer
);
378 static void read_empty(const struct object_id
*oid
)
380 struct child_process cmd
= CHILD_PROCESS_INIT
;
382 strvec_pushl(&cmd
.args
, "read-tree", "-m", "-u",
383 empty_tree_oid_hex(the_repository
->hash_algo
),
384 oid_to_hex(oid
), NULL
);
387 if (run_command(&cmd
))
388 die(_("read-tree failed"));
391 static void reset_hard(const struct object_id
*oid
)
393 struct child_process cmd
= CHILD_PROCESS_INIT
;
395 strvec_pushl(&cmd
.args
, "read-tree", "-v", "--reset", "-u",
396 oid_to_hex(oid
), NULL
);
399 if (run_command(&cmd
))
400 die(_("read-tree failed"));
403 static void restore_state(const struct object_id
*head
,
404 const struct object_id
*stash
)
406 struct child_process cmd
= CHILD_PROCESS_INIT
;
410 if (is_null_oid(stash
))
413 strvec_pushl(&cmd
.args
, "stash", "apply", "--index", "--quiet", NULL
);
414 strvec_push(&cmd
.args
, oid_to_hex(stash
));
417 * It is OK to ignore error here, for example when there was
418 * nothing to restore.
424 discard_index(the_repository
->index
);
425 if (repo_read_index(the_repository
) < 0)
426 die(_("could not read index"));
429 /* This is called when no merge was necessary. */
430 static void finish_up_to_date(void)
432 if (verbosity
>= 0) {
434 puts(_("Already up to date. (nothing to squash)"));
436 puts(_("Already up to date."));
438 remove_merge_branch_state(the_repository
);
441 static void squash_message(struct commit
*commit
, struct commit_list
*remoteheads
)
444 struct strbuf out
= STRBUF_INIT
;
445 struct commit_list
*j
;
446 struct pretty_print_context ctx
= {0};
448 printf(_("Squash commit -- not updating HEAD\n"));
450 repo_init_revisions(the_repository
, &rev
, NULL
);
451 diff_merges_suppress(&rev
);
452 rev
.commit_format
= CMIT_FMT_MEDIUM
;
454 commit
->object
.flags
|= UNINTERESTING
;
455 add_pending_object(&rev
, &commit
->object
, NULL
);
457 for (j
= remoteheads
; j
; j
= j
->next
)
458 add_pending_object(&rev
, &j
->item
->object
, NULL
);
460 setup_revisions(0, NULL
, &rev
, NULL
);
461 if (prepare_revision_walk(&rev
))
462 die(_("revision walk setup failed"));
464 ctx
.abbrev
= rev
.abbrev
;
465 ctx
.date_mode
= rev
.date_mode
;
466 ctx
.fmt
= rev
.commit_format
;
468 strbuf_addstr(&out
, "Squashed commit of the following:\n");
469 while ((commit
= get_revision(&rev
)) != NULL
) {
470 strbuf_addch(&out
, '\n');
471 strbuf_addf(&out
, "commit %s\n",
472 oid_to_hex(&commit
->object
.oid
));
473 pretty_print_commit(&ctx
, commit
, &out
);
475 write_file_buf(git_path_squash_msg(the_repository
), out
.buf
, out
.len
);
476 strbuf_release(&out
);
477 release_revisions(&rev
);
480 static void finish(struct commit
*head_commit
,
481 struct commit_list
*remoteheads
,
482 const struct object_id
*new_head
, const char *msg
)
484 struct strbuf reflog_message
= STRBUF_INIT
;
485 const struct object_id
*head
= &head_commit
->object
.oid
;
488 strbuf_addstr(&reflog_message
, getenv("GIT_REFLOG_ACTION"));
492 strbuf_addf(&reflog_message
, "%s: %s",
493 getenv("GIT_REFLOG_ACTION"), msg
);
496 squash_message(head_commit
, remoteheads
);
498 if (verbosity
>= 0 && !merge_msg
.len
)
499 printf(_("No merge message -- not updating HEAD\n"));
501 refs_update_ref(get_main_ref_store(the_repository
),
502 reflog_message
.buf
, "HEAD", new_head
,
504 0, UPDATE_REFS_DIE_ON_ERR
);
506 * We ignore errors in 'gc --auto', since the
507 * user should see them.
509 run_auto_maintenance(verbosity
< 0);
512 if (new_head
&& show_diffstat
) {
513 struct diff_options opts
;
514 repo_diff_setup(the_repository
, &opts
);
515 init_diffstat_widths(&opts
);
517 switch (show_diffstat
) {
518 case MERGE_SHOW_DIFFSTAT
: /* 1 */
519 opts
.output_format
|=
520 DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
522 case MERGE_SHOW_COMPACTSUMMARY
: /* 2 */
523 opts
.output_format
|= DIFF_FORMAT_DIFFSTAT
;
524 opts
.flags
.stat_with_summary
= 1;
529 opts
.detect_rename
= DIFF_DETECT_RENAME
;
530 diff_setup_done(&opts
);
531 diff_tree_oid(head
, new_head
, "", &opts
);
536 /* Run a post-merge hook */
537 run_hooks_l(the_repository
, "post-merge", squash
? "1" : "0", NULL
);
540 apply_autostash_ref(the_repository
, "MERGE_AUTOSTASH");
541 strbuf_release(&reflog_message
);
544 /* Get the name for the merge commit's message. */
545 static void merge_name(const char *remote
, struct strbuf
*msg
)
547 struct commit
*remote_head
;
548 struct object_id branch_head
;
549 struct strbuf bname
= STRBUF_INIT
;
550 struct merge_remote_desc
*desc
;
552 char *found_ref
= NULL
;
555 copy_branchname(&bname
, remote
, 0);
558 oidclr(&branch_head
, the_repository
->hash_algo
);
559 remote_head
= get_merge_parent(remote
);
561 die(_("'%s' does not point to a commit"), remote
);
563 if (repo_dwim_ref(the_repository
, remote
, strlen(remote
), &branch_head
,
564 &found_ref
, 0) > 0) {
565 if (starts_with(found_ref
, "refs/heads/")) {
566 strbuf_addf(msg
, "%s\t\tbranch '%s' of .\n",
567 oid_to_hex(&branch_head
), remote
);
570 if (starts_with(found_ref
, "refs/tags/")) {
571 strbuf_addf(msg
, "%s\t\ttag '%s' of .\n",
572 oid_to_hex(&branch_head
), remote
);
575 if (starts_with(found_ref
, "refs/remotes/")) {
576 strbuf_addf(msg
, "%s\t\tremote-tracking branch '%s' of .\n",
577 oid_to_hex(&branch_head
), remote
);
582 /* See if remote matches <name>^^^.. or <name>~<number> */
583 for (len
= 0, ptr
= remote
+ strlen(remote
);
584 remote
< ptr
&& ptr
[-1] == '^';
591 ptr
= strrchr(remote
, '~');
593 int seen_nonzero
= 0;
596 while (*++ptr
&& isdigit(*ptr
)) {
597 seen_nonzero
|= (*ptr
!= '0');
601 len
= 0; /* not ...~<number> */
602 else if (seen_nonzero
)
605 early
= 1; /* "name~" is "name~1"! */
609 struct strbuf truname
= STRBUF_INIT
;
610 strbuf_addf(&truname
, "refs/heads/%s", remote
);
611 strbuf_setlen(&truname
, truname
.len
- len
);
612 if (refs_ref_exists(get_main_ref_store(the_repository
), truname
.buf
)) {
614 "%s\t\tbranch '%s'%s of .\n",
615 oid_to_hex(&remote_head
->object
.oid
),
617 (early
? " (early part)" : ""));
618 strbuf_release(&truname
);
621 strbuf_release(&truname
);
624 desc
= merge_remote_util(remote_head
);
625 if (desc
&& desc
->obj
&& desc
->obj
->type
== OBJ_TAG
) {
626 strbuf_addf(msg
, "%s\t\t%s '%s'\n",
627 oid_to_hex(&desc
->obj
->oid
),
628 type_name(desc
->obj
->type
),
633 strbuf_addf(msg
, "%s\t\tcommit '%s'\n",
634 oid_to_hex(&remote_head
->object
.oid
), remote
);
637 strbuf_release(&bname
);
640 static void parse_branch_merge_options(char *bmo
)
647 argc
= split_cmdline(bmo
, &argv
);
649 die(_("Bad branch.%s.mergeoptions string: %s"), branch
,
650 _(split_cmdline_strerror(argc
)));
651 REALLOC_ARRAY(argv
, argc
+ 2);
652 MOVE_ARRAY(argv
+ 1, argv
, argc
+ 1);
654 argv
[0] = "branch.*.mergeoptions";
655 parse_options(argc
, argv
, NULL
, builtin_merge_options
,
656 builtin_merge_usage
, 0);
660 static int git_merge_config(const char *k
, const char *v
,
661 const struct config_context
*ctx
, void *cb
)
667 skip_prefix(k
, "branch.", &str
) &&
668 skip_prefix(str
, branch
, &str
) &&
669 !strcmp(str
, ".mergeoptions")) {
670 free(branch_mergeoptions
);
671 branch_mergeoptions
= xstrdup(v
);
675 if (!strcmp(k
, "merge.diffstat") || !strcmp(k
, "merge.stat")) {
676 int val
= git_parse_maybe_bool_text(v
);
682 show_diffstat
= MERGE_SHOW_DIFFSTAT
;
685 if (!strcmp(v
, "compact"))
686 show_diffstat
= MERGE_SHOW_COMPACTSUMMARY
;
688 * We do not need to have an explicit
690 * else if (!strcmp(v, "diffstat"))
691 * show_diffstat = MERGE_SHOW_DIFFSTAT;
693 * here, because the catch-all uses the
694 * diffstat style anyway.
698 * A setting from a future? It is not an
699 * error grave enough to fail the command.
700 * proceed using the default one.
702 show_diffstat
= MERGE_SHOW_DIFFSTAT
;
705 } else if (!strcmp(k
, "merge.verifysignatures")) {
706 verify_signatures
= git_config_bool(k
, v
);
707 } else if (!strcmp(k
, "pull.twohead")) {
708 FREE_AND_NULL(pull_twohead
);
709 return git_config_string(&pull_twohead
, k
, v
);
710 } else if (!strcmp(k
, "pull.octopus")) {
711 FREE_AND_NULL(pull_octopus
);
712 return git_config_string(&pull_octopus
, k
, v
);
713 } else if (!strcmp(k
, "commit.cleanup")) {
714 return git_config_string(&cleanup_arg
, k
, v
);
715 } else if (!strcmp(k
, "merge.ff")) {
716 int boolval
= git_parse_maybe_bool(v
);
718 fast_forward
= boolval
? FF_ALLOW
: FF_NO
;
719 } else if (v
&& !strcmp(v
, "only")) {
720 fast_forward
= FF_ONLY
;
721 } /* do not barf on values from future versions of git */
723 } else if (!strcmp(k
, "merge.defaulttoupstream")) {
724 default_to_upstream
= git_config_bool(k
, v
);
726 } else if (!strcmp(k
, "commit.gpgsign")) {
727 sign_commit
= git_config_bool(k
, v
) ? "" : NULL
;
729 } else if (!strcmp(k
, "gpg.mintrustlevel")) {
730 check_trust_level
= 0;
731 } else if (!strcmp(k
, "merge.autostash")) {
732 autostash
= git_config_bool(k
, v
);
736 status
= fmt_merge_msg_config(k
, v
, ctx
, cb
);
739 return git_diff_ui_config(k
, v
, ctx
, cb
);
742 static int read_tree_trivial(struct object_id
*common
, struct object_id
*head
,
743 struct object_id
*one
)
746 struct tree
*trees
[MAX_UNPACK_TREES
];
747 struct tree_desc t
[MAX_UNPACK_TREES
];
748 struct unpack_trees_options opts
;
750 memset(&opts
, 0, sizeof(opts
));
752 opts
.src_index
= the_repository
->index
;
753 opts
.dst_index
= the_repository
->index
;
755 opts
.verbose_update
= 1;
756 opts
.trivial_merges_only
= 1;
758 opts
.preserve_ignored
= 0; /* FIXME: !overwrite_ignore */
759 trees
[nr_trees
] = parse_tree_indirect(common
);
760 if (!trees
[nr_trees
++])
762 trees
[nr_trees
] = parse_tree_indirect(head
);
763 if (!trees
[nr_trees
++])
765 trees
[nr_trees
] = parse_tree_indirect(one
);
766 if (!trees
[nr_trees
++])
768 opts
.fn
= threeway_merge
;
769 cache_tree_free(&the_repository
->index
->cache_tree
);
770 for (i
= 0; i
< nr_trees
; i
++) {
771 parse_tree(trees
[i
]);
772 init_tree_desc(t
+i
, &trees
[i
]->object
.oid
,
773 trees
[i
]->buffer
, trees
[i
]->size
);
775 if (unpack_trees(nr_trees
, t
, &opts
))
780 static void write_tree_trivial(struct object_id
*oid
)
782 if (write_index_as_tree(oid
, the_repository
->index
,
783 repo_get_index_file(the_repository
),
785 die(_("git write-tree failed to write a tree"));
788 static int try_merge_strategy(const char *strategy
, struct commit_list
*common
,
789 struct commit_list
*remoteheads
,
792 const char *head_arg
= "HEAD";
794 if (repo_refresh_and_write_index(the_repository
, REFRESH_QUIET
,
795 SKIP_IF_UNCHANGED
, 0, NULL
, NULL
,
797 die(_("Unable to write index."));
799 if (!strcmp(strategy
, "recursive") || !strcmp(strategy
, "subtree") ||
800 !strcmp(strategy
, "ort")) {
801 struct lock_file lock
= LOCK_INIT
;
803 struct commit
*result
;
804 struct commit_list
*reversed
= NULL
;
805 struct merge_options o
;
806 struct commit_list
*j
;
808 if (remoteheads
->next
) {
809 error(_("Not handling anything other than two heads merge."));
813 init_ui_merge_options(&o
, the_repository
);
814 if (!strcmp(strategy
, "subtree"))
815 o
.subtree_shift
= "";
817 o
.show_rename_progress
=
818 show_progress
== -1 ? isatty(2) : show_progress
;
820 for (x
= 0; x
< xopts
.nr
; x
++)
821 if (parse_merge_opt(&o
, xopts
.v
[x
]))
822 die(_("unknown strategy option: -X%s"), xopts
.v
[x
]);
824 o
.branch1
= head_arg
;
825 o
.branch2
= merge_remote_util(remoteheads
->item
)->name
;
827 for (j
= common
; j
; j
= j
->next
)
828 commit_list_insert(j
->item
, &reversed
);
830 repo_hold_locked_index(the_repository
, &lock
,
832 clean
= merge_ort_recursive(&o
, head
, remoteheads
->item
,
834 free_commit_list(reversed
);
835 strbuf_release(&o
.obuf
);
838 rollback_lock_file(&lock
);
841 if (write_locked_index(the_repository
->index
, &lock
,
842 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
843 die(_("unable to write %s"), repo_get_index_file(the_repository
));
844 return clean
? 0 : 1;
846 return try_merge_command(the_repository
,
847 strategy
, xopts
.nr
, xopts
.v
,
848 common
, head_arg
, remoteheads
);
852 static void count_diff_files(struct diff_queue_struct
*q
,
853 struct diff_options
*opt UNUSED
, void *data
)
860 static int count_unmerged_entries(void)
864 for (i
= 0; i
< the_repository
->index
->cache_nr
; i
++)
865 if (ce_stage(the_repository
->index
->cache
[i
]))
871 static void add_strategies(const char *string
, unsigned attr
)
876 struct string_list list
= STRING_LIST_INIT_DUP
;
877 struct string_list_item
*item
;
878 string_list_split(&list
, string
, ' ', -1);
879 for_each_string_list_item(item
, &list
)
880 append_strategy(get_strategy(item
->string
));
881 string_list_clear(&list
, 0);
884 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
885 if (all_strategy
[i
].attr
& attr
)
886 append_strategy(&all_strategy
[i
]);
890 static void read_merge_msg(struct strbuf
*msg
)
892 const char *filename
= git_path_merge_msg(the_repository
);
894 if (strbuf_read_file(msg
, filename
, 0) < 0)
895 die_errno(_("Could not read from '%s'"), filename
);
898 static void write_merge_state(struct commit_list
*);
899 static void abort_commit(struct commit_list
*remoteheads
, const char *err_msg
)
902 error("%s", err_msg
);
904 _("Not committing merge; use 'git commit' to complete the merge.\n"));
905 write_merge_state(remoteheads
);
909 static const char merge_editor_comment
[] =
910 N_("Please enter a commit message to explain why this merge is necessary,\n"
911 "especially if it merges an updated upstream into a topic branch.\n"
914 static const char scissors_editor_comment
[] =
915 N_("An empty message aborts the commit.\n");
917 static const char no_scissors_editor_comment
[] =
918 N_("Lines starting with '%s' will be ignored, and an empty message aborts\n"
921 static void write_merge_heads(struct commit_list
*);
922 static void prepare_to_commit(struct commit_list
*remoteheads
)
924 struct strbuf msg
= STRBUF_INIT
;
925 const char *index_file
= repo_get_index_file(the_repository
);
930 if (run_commit_hook(0 < option_edit
, index_file
, &invoked_hook
,
931 "pre-merge-commit", NULL
))
932 abort_commit(remoteheads
, NULL
);
934 * Re-read the index as pre-merge-commit hook could have updated it,
935 * and write it out as a tree. We must do this before we invoke
936 * the editor and after we invoke run_status above.
939 discard_index(the_repository
->index
);
941 read_index_from(the_repository
->index
, index_file
,
942 repo_get_git_dir(the_repository
));
943 strbuf_addbuf(&msg
, &merge_msg
);
945 BUG("the control must not reach here under --squash");
946 if (0 < option_edit
) {
947 strbuf_addch(&msg
, '\n');
948 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
) {
949 wt_status_append_cut_line(&msg
);
950 strbuf_commented_addf(&msg
, comment_line_str
, "\n");
952 strbuf_commented_addf(&msg
, comment_line_str
,
953 _(merge_editor_comment
));
954 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
)
955 strbuf_commented_addf(&msg
, comment_line_str
,
956 _(scissors_editor_comment
));
958 strbuf_commented_addf(&msg
, comment_line_str
,
959 _(no_scissors_editor_comment
), comment_line_str
);
962 append_signoff(&msg
, ignored_log_message_bytes(msg
.buf
, msg
.len
), 0);
963 write_merge_heads(remoteheads
);
964 write_file_buf(git_path_merge_msg(the_repository
), msg
.buf
, msg
.len
);
965 if (run_commit_hook(0 < option_edit
, repo_get_index_file(the_repository
),
966 NULL
, "prepare-commit-msg",
967 git_path_merge_msg(the_repository
), "merge", NULL
))
968 abort_commit(remoteheads
, NULL
);
969 if (0 < option_edit
) {
970 if (launch_editor(git_path_merge_msg(the_repository
), NULL
, NULL
))
971 abort_commit(remoteheads
, NULL
);
974 if (!no_verify
&& run_commit_hook(0 < option_edit
, repo_get_index_file(the_repository
),
976 git_path_merge_msg(the_repository
), NULL
))
977 abort_commit(remoteheads
, NULL
);
979 read_merge_msg(&msg
);
980 cleanup_message(&msg
, cleanup_mode
, 0);
982 abort_commit(remoteheads
, _("Empty commit message."));
983 strbuf_release(&merge_msg
);
984 strbuf_addbuf(&merge_msg
, &msg
);
985 strbuf_release(&msg
);
988 static int merge_trivial(struct commit
*head
, struct commit_list
*remoteheads
)
990 struct object_id result_tree
, result_commit
;
991 struct commit_list
*parents
= NULL
, **pptr
= &parents
;
993 if (repo_refresh_and_write_index(the_repository
, REFRESH_QUIET
,
994 SKIP_IF_UNCHANGED
, 0, NULL
, NULL
,
996 return error(_("Unable to write index."));
998 write_tree_trivial(&result_tree
);
999 printf(_("Wonderful.\n"));
1000 pptr
= commit_list_append(head
, pptr
);
1001 pptr
= commit_list_append(remoteheads
->item
, pptr
);
1002 prepare_to_commit(remoteheads
);
1003 if (commit_tree(merge_msg
.buf
, merge_msg
.len
, &result_tree
, parents
,
1004 &result_commit
, NULL
, sign_commit
))
1005 die(_("failed to write commit object"));
1006 finish(head
, remoteheads
, &result_commit
, "In-index merge");
1008 remove_merge_branch_state(the_repository
);
1009 free_commit_list(parents
);
1013 static int finish_automerge(struct commit
*head
,
1015 struct commit_list
*common
,
1016 struct commit_list
*remoteheads
,
1017 struct object_id
*result_tree
,
1018 const char *wt_strategy
)
1020 struct commit_list
*parents
= NULL
;
1021 struct strbuf buf
= STRBUF_INIT
;
1022 struct object_id result_commit
;
1024 write_tree_trivial(result_tree
);
1025 free_commit_list(common
);
1026 parents
= remoteheads
;
1027 if (!head_subsumed
|| fast_forward
== FF_NO
)
1028 commit_list_insert(head
, &parents
);
1029 prepare_to_commit(remoteheads
);
1030 if (commit_tree(merge_msg
.buf
, merge_msg
.len
, result_tree
, parents
,
1031 &result_commit
, NULL
, sign_commit
))
1032 die(_("failed to write commit object"));
1033 strbuf_addf(&buf
, "Merge made by the '%s' strategy.", wt_strategy
);
1034 finish(head
, remoteheads
, &result_commit
, buf
.buf
);
1036 strbuf_release(&buf
);
1037 remove_merge_branch_state(the_repository
);
1038 free_commit_list(parents
);
1042 static int suggest_conflicts(void)
1044 const char *filename
;
1046 struct strbuf msgbuf
= STRBUF_INIT
;
1048 filename
= git_path_merge_msg(the_repository
);
1049 fp
= xfopen(filename
, "a");
1052 * We can't use cleanup_mode because if we're not using the editor,
1053 * get_cleanup_mode will return COMMIT_MSG_CLEANUP_SPACE instead, even
1054 * though the message is meant to be processed later by git-commit.
1055 * Thus, we will get the cleanup mode which is returned when we _are_
1058 append_conflicts_hint(the_repository
->index
, &msgbuf
,
1059 get_cleanup_mode(cleanup_arg
, 1));
1060 fputs(msgbuf
.buf
, fp
);
1061 strbuf_release(&msgbuf
);
1063 repo_rerere(the_repository
, allow_rerere_auto
);
1064 printf(_("Automatic merge failed; "
1065 "fix conflicts and then commit the result.\n"));
1069 static int evaluate_result(void)
1072 struct rev_info rev
;
1074 /* Check how many files differ. */
1075 repo_init_revisions(the_repository
, &rev
, "");
1076 setup_revisions(0, NULL
, &rev
, NULL
);
1077 rev
.diffopt
.output_format
|=
1078 DIFF_FORMAT_CALLBACK
;
1079 rev
.diffopt
.format_callback
= count_diff_files
;
1080 rev
.diffopt
.format_callback_data
= &cnt
;
1081 run_diff_files(&rev
, 0);
1084 * Check how many unmerged entries are
1087 cnt
+= count_unmerged_entries();
1089 release_revisions(&rev
);
1094 * Pretend as if the user told us to merge with the remote-tracking
1095 * branch we have for the upstream of the current branch
1097 static int setup_with_upstream(const char ***argv
)
1099 struct branch
*branch
= branch_get(NULL
);
1104 die(_("No current branch."));
1105 if (!branch
->remote_name
)
1106 die(_("No remote for the current branch."));
1107 if (!branch
->merge_nr
)
1108 die(_("No default upstream defined for the current branch."));
1110 args
= xcalloc(st_add(branch
->merge_nr
, 1), sizeof(char *));
1111 for (i
= 0; i
< branch
->merge_nr
; i
++) {
1112 if (!branch
->merge
[i
]->dst
)
1113 die(_("No remote-tracking branch for %s from %s"),
1114 branch
->merge
[i
]->src
, branch
->remote_name
);
1115 args
[i
] = branch
->merge
[i
]->dst
;
1122 static void write_merge_heads(struct commit_list
*remoteheads
)
1124 struct commit_list
*j
;
1125 struct strbuf buf
= STRBUF_INIT
;
1127 for (j
= remoteheads
; j
; j
= j
->next
) {
1128 struct object_id
*oid
;
1129 struct commit
*c
= j
->item
;
1130 struct merge_remote_desc
*desc
;
1132 desc
= merge_remote_util(c
);
1133 if (desc
&& desc
->obj
) {
1134 oid
= &desc
->obj
->oid
;
1136 oid
= &c
->object
.oid
;
1138 strbuf_addf(&buf
, "%s\n", oid_to_hex(oid
));
1140 write_file_buf(git_path_merge_head(the_repository
), buf
.buf
, buf
.len
);
1143 if (fast_forward
== FF_NO
)
1144 strbuf_addstr(&buf
, "no-ff");
1145 write_file_buf(git_path_merge_mode(the_repository
), buf
.buf
, buf
.len
);
1146 strbuf_release(&buf
);
1149 static void write_merge_state(struct commit_list
*remoteheads
)
1151 write_merge_heads(remoteheads
);
1152 strbuf_addch(&merge_msg
, '\n');
1153 write_file_buf(git_path_merge_msg(the_repository
), merge_msg
.buf
,
1157 static int default_edit_option(void)
1159 static const char name
[] = "GIT_MERGE_AUTOEDIT";
1160 const char *e
= getenv(name
);
1161 struct stat st_stdin
, st_stdout
;
1164 /* an explicit -m msg without --[no-]edit */
1168 int v
= git_parse_maybe_bool(e
);
1170 die(_("Bad value '%s' in environment '%s'"), e
, name
);
1174 /* Use editor if stdin and stdout are the same and is a tty */
1175 return (!fstat(0, &st_stdin
) &&
1176 !fstat(1, &st_stdout
) &&
1177 isatty(0) && isatty(1) &&
1178 st_stdin
.st_dev
== st_stdout
.st_dev
&&
1179 st_stdin
.st_ino
== st_stdout
.st_ino
&&
1180 st_stdin
.st_mode
== st_stdout
.st_mode
);
1183 static struct commit_list
*reduce_parents(struct commit
*head_commit
,
1185 struct commit_list
*remoteheads
)
1187 struct commit_list
*parents
, **remotes
;
1190 * Is the current HEAD reachable from another commit being
1191 * merged? If so we do not want to record it as a parent of
1192 * the resulting merge, unless --no-ff is given. We will flip
1193 * this variable to 0 when we find HEAD among the independent
1194 * tips being merged.
1198 /* Find what parents to record by checking independent ones. */
1199 parents
= reduce_heads(remoteheads
);
1200 free_commit_list(remoteheads
);
1203 remotes
= &remoteheads
;
1205 struct commit
*commit
= pop_commit(&parents
);
1206 if (commit
== head_commit
)
1209 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1214 static void prepare_merge_message(struct strbuf
*merge_names
, struct strbuf
*merge_msg
)
1216 struct fmt_merge_msg_opts opts
;
1218 memset(&opts
, 0, sizeof(opts
));
1219 opts
.add_title
= !have_message
;
1220 opts
.shortlog_len
= shortlog_len
;
1221 opts
.credit_people
= (0 < option_edit
);
1222 opts
.into_name
= into_name
;
1224 fmt_merge_msg(merge_names
, merge_msg
, &opts
);
1226 strbuf_setlen(merge_msg
, merge_msg
->len
- 1);
1229 static void handle_fetch_head(struct commit_list
**remotes
, struct strbuf
*merge_names
)
1231 const char *filename
;
1233 struct strbuf fetch_head_file
= STRBUF_INIT
;
1234 const unsigned hexsz
= the_hash_algo
->hexsz
;
1237 merge_names
= &fetch_head_file
;
1239 filename
= git_path_fetch_head(the_repository
);
1240 fd
= xopen(filename
, O_RDONLY
);
1242 if (strbuf_read(merge_names
, fd
, 0) < 0)
1243 die_errno(_("could not read '%s'"), filename
);
1245 die_errno(_("could not close '%s'"), filename
);
1247 for (pos
= 0; pos
< merge_names
->len
; pos
= npos
) {
1248 struct object_id oid
;
1250 struct commit
*commit
;
1252 ptr
= strchr(merge_names
->buf
+ pos
, '\n');
1254 npos
= ptr
- merge_names
->buf
+ 1;
1256 npos
= merge_names
->len
;
1258 if (npos
- pos
< hexsz
+ 2 ||
1259 get_oid_hex(merge_names
->buf
+ pos
, &oid
))
1260 commit
= NULL
; /* bad */
1261 else if (memcmp(merge_names
->buf
+ pos
+ hexsz
, "\t\t", 2))
1262 continue; /* not-for-merge */
1264 char saved
= merge_names
->buf
[pos
+ hexsz
];
1265 merge_names
->buf
[pos
+ hexsz
] = '\0';
1266 commit
= get_merge_parent(merge_names
->buf
+ pos
);
1267 merge_names
->buf
[pos
+ hexsz
] = saved
;
1272 die(_("not something we can merge in %s: %s"),
1273 filename
, merge_names
->buf
+ pos
);
1275 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1278 if (merge_names
== &fetch_head_file
)
1279 strbuf_release(&fetch_head_file
);
1282 static struct commit_list
*collect_parents(struct commit
*head_commit
,
1284 int argc
, const char **argv
,
1285 struct strbuf
*merge_msg
)
1288 struct commit_list
*remoteheads
= NULL
;
1289 struct commit_list
**remotes
= &remoteheads
;
1290 struct strbuf merge_names
= STRBUF_INIT
, *autogen
= NULL
;
1292 if (merge_msg
&& (!have_message
|| shortlog_len
))
1293 autogen
= &merge_names
;
1296 remotes
= &commit_list_insert(head_commit
, remotes
)->next
;
1298 if (argc
== 1 && !strcmp(argv
[0], "FETCH_HEAD")) {
1299 handle_fetch_head(remotes
, autogen
);
1300 remoteheads
= reduce_parents(head_commit
, head_subsumed
, remoteheads
);
1302 for (i
= 0; i
< argc
; i
++) {
1303 struct commit
*commit
= get_merge_parent(argv
[i
]);
1305 help_unknown_ref(argv
[i
], "merge",
1306 _("not something we can merge"));
1307 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1309 remoteheads
= reduce_parents(head_commit
, head_subsumed
, remoteheads
);
1311 struct commit_list
*p
;
1312 for (p
= remoteheads
; p
; p
= p
->next
)
1313 merge_name(merge_remote_util(p
->item
)->name
, autogen
);
1318 prepare_merge_message(autogen
, merge_msg
);
1319 strbuf_release(autogen
);
1325 static int merging_a_throwaway_tag(struct commit
*commit
)
1328 struct object_id oid
;
1329 int is_throwaway_tag
= 0;
1331 /* Are we merging a tag? */
1332 if (!merge_remote_util(commit
) ||
1333 !merge_remote_util(commit
)->obj
||
1334 merge_remote_util(commit
)->obj
->type
!= OBJ_TAG
)
1335 return is_throwaway_tag
;
1338 * Now we know we are merging a tag object. Are we downstream
1339 * and following the tags from upstream? If so, we must have
1340 * the tag object pointed at by "refs/tags/$T" where $T is the
1341 * tagname recorded in the tag object. We want to allow such
1342 * a "just to catch up" merge to fast-forward.
1344 * Otherwise, we are playing an integrator's role, making a
1345 * merge with a throw-away tag from a contributor with
1346 * something like "git pull $contributor $signed_tag".
1347 * We want to forbid such a merge from fast-forwarding
1348 * by default; otherwise we would not keep the signature
1351 tag_ref
= xstrfmt("refs/tags/%s",
1352 ((struct tag
*)merge_remote_util(commit
)->obj
)->tag
);
1353 if (!refs_read_ref(get_main_ref_store(the_repository
), tag_ref
, &oid
) &&
1354 oideq(&oid
, &merge_remote_util(commit
)->obj
->oid
))
1355 is_throwaway_tag
= 0;
1357 is_throwaway_tag
= 1;
1359 return is_throwaway_tag
;
1362 int cmd_merge(int argc
,
1365 struct repository
*repo UNUSED
)
1367 struct object_id result_tree
, stash
, head_oid
;
1368 struct commit
*head_commit
;
1369 struct strbuf buf
= STRBUF_INIT
;
1370 int i
, ret
= 0, head_subsumed
;
1371 int best_cnt
= -1, merge_was_ok
= 0, automerge_was_ok
= 0;
1372 struct commit_list
*common
= NULL
;
1373 const char *best_strategy
= NULL
, *wt_strategy
= NULL
;
1374 struct commit_list
*remoteheads
= NULL
, *p
;
1375 void *branch_to_free
;
1376 int orig_argc
= argc
;
1378 show_usage_with_options_if_asked(argc
, argv
,
1379 builtin_merge_usage
, builtin_merge_options
);
1381 prepare_repo_settings(the_repository
);
1382 the_repository
->settings
.command_requires_full_index
= 0;
1385 * Check if we are _not_ on a detached HEAD, i.e. if there is a
1388 branch
= branch_to_free
= refs_resolve_refdup(get_main_ref_store(the_repository
),
1389 "HEAD", 0, &head_oid
,
1392 skip_prefix(branch
, "refs/heads/", &branch
);
1394 init_diff_ui_defaults();
1395 git_config(git_merge_config
, NULL
);
1397 if (!branch
|| is_null_oid(&head_oid
))
1400 head_commit
= lookup_commit_or_die(&head_oid
, "HEAD");
1402 if (branch_mergeoptions
)
1403 parse_branch_merge_options(branch_mergeoptions
);
1404 argc
= parse_options(argc
, argv
, prefix
, builtin_merge_options
,
1405 builtin_merge_usage
, 0);
1406 if (shortlog_len
< 0)
1407 shortlog_len
= (merge_log_config
> 0) ? merge_log_config
: 0;
1409 if (verbosity
< 0 && show_progress
== -1)
1412 if (abort_current_merge
) {
1414 const char *nargv
[] = {"reset", "--merge", NULL
};
1415 char stash_oid_hex
[GIT_MAX_HEXSZ
+ 1];
1416 struct object_id stash_oid
= {0};
1419 usage_msg_opt(_("--abort expects no arguments"),
1420 builtin_merge_usage
, builtin_merge_options
);
1422 if (!file_exists(git_path_merge_head(the_repository
)))
1423 die(_("There is no merge to abort (MERGE_HEAD missing)."));
1425 if (!refs_read_ref(get_main_ref_store(the_repository
), "MERGE_AUTOSTASH", &stash_oid
))
1426 refs_delete_ref(get_main_ref_store(the_repository
),
1427 "", "MERGE_AUTOSTASH", &stash_oid
,
1430 /* Invoke 'git reset --merge' */
1431 ret
= cmd_reset(nargc
, nargv
, prefix
, the_repository
);
1433 if (!is_null_oid(&stash_oid
)) {
1434 oid_to_hex_r(stash_oid_hex
, &stash_oid
);
1435 apply_autostash_oid(stash_oid_hex
);
1441 if (quit_current_merge
) {
1443 usage_msg_opt(_("--quit expects no arguments"),
1444 builtin_merge_usage
,
1445 builtin_merge_options
);
1447 remove_merge_branch_state(the_repository
);
1451 if (continue_current_merge
) {
1453 const char *nargv
[] = {"commit", NULL
};
1456 usage_msg_opt(_("--continue expects no arguments"),
1457 builtin_merge_usage
, builtin_merge_options
);
1459 if (!file_exists(git_path_merge_head(the_repository
)))
1460 die(_("There is no merge in progress (MERGE_HEAD missing)."));
1462 /* Invoke 'git commit' */
1463 ret
= cmd_commit(nargc
, nargv
, prefix
, the_repository
);
1467 if (repo_read_index_unmerged(the_repository
))
1468 die_resolve_conflict("merge");
1470 if (file_exists(git_path_merge_head(the_repository
))) {
1472 * There is no unmerged entry, don't advise 'git
1473 * add/rm <file>', just 'git commit'.
1475 if (advice_enabled(ADVICE_RESOLVE_CONFLICT
))
1476 die(_("You have not concluded your merge (MERGE_HEAD exists).\n"
1477 "Please, commit your changes before you merge."));
1479 die(_("You have not concluded your merge (MERGE_HEAD exists)."));
1481 if (refs_ref_exists(get_main_ref_store(the_repository
), "CHERRY_PICK_HEAD")) {
1482 if (advice_enabled(ADVICE_RESOLVE_CONFLICT
))
1483 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
1484 "Please, commit your changes before you merge."));
1486 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists)."));
1488 resolve_undo_clear_index(the_repository
->index
);
1490 if (option_edit
< 0)
1491 option_edit
= default_edit_option();
1493 cleanup_mode
= get_cleanup_mode(cleanup_arg
, 0 < option_edit
);
1499 if (fast_forward
== FF_NO
)
1500 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--no-ff.");
1501 if (option_commit
> 0)
1502 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--commit.");
1504 * squash can now silently disable option_commit - this is not
1505 * a problem as it is only overriding the default, not a user
1511 if (option_commit
< 0)
1515 if (default_to_upstream
)
1516 argc
= setup_with_upstream(&argv
);
1518 die(_("No commit specified and merge.defaultToUpstream not set."));
1519 } else if (argc
== 1 && !strcmp(argv
[0], "-")) {
1524 usage_with_options(builtin_merge_usage
,
1525 builtin_merge_options
);
1529 * If the merged head is a valid one there is no reason
1530 * to forbid "git merge" into a branch yet to be born.
1531 * We do the same for "git pull".
1533 struct object_id
*remote_head_oid
;
1535 die(_("Squash commit into empty head not supported yet"));
1536 if (fast_forward
== FF_NO
)
1537 die(_("Non-fast-forward commit does not make sense into "
1539 remoteheads
= collect_parents(head_commit
, &head_subsumed
,
1542 die(_("%s - not something we can merge"), argv
[0]);
1543 if (remoteheads
->next
)
1544 die(_("Can merge only exactly one commit into empty head"));
1546 if (verify_signatures
)
1547 verify_merge_signature(remoteheads
->item
, verbosity
,
1550 remote_head_oid
= &remoteheads
->item
->object
.oid
;
1551 read_empty(remote_head_oid
);
1552 refs_update_ref(get_main_ref_store(the_repository
),
1553 "initial pull", "HEAD", remote_head_oid
, NULL
,
1555 UPDATE_REFS_DIE_ON_ERR
);
1560 * All the rest are the commits being merged; prepare
1561 * the standard merge summary message to be appended
1562 * to the given message.
1564 remoteheads
= collect_parents(head_commit
, &head_subsumed
,
1565 argc
, argv
, &merge_msg
);
1567 if (!head_commit
|| !argc
)
1568 usage_with_options(builtin_merge_usage
,
1569 builtin_merge_options
);
1571 if (verify_signatures
) {
1572 for (p
= remoteheads
; p
; p
= p
->next
) {
1573 verify_merge_signature(p
->item
, verbosity
,
1578 strbuf_addstr(&buf
, "merge");
1579 for (p
= remoteheads
; p
; p
= p
->next
)
1580 strbuf_addf(&buf
, " %s", merge_remote_util(p
->item
)->name
);
1581 setenv("GIT_REFLOG_ACTION", buf
.buf
, 0);
1584 for (p
= remoteheads
; p
; p
= p
->next
) {
1585 struct commit
*commit
= p
->item
;
1586 strbuf_addf(&buf
, "GITHEAD_%s",
1587 oid_to_hex(&commit
->object
.oid
));
1588 setenv(buf
.buf
, merge_remote_util(commit
)->name
, 1);
1590 if (fast_forward
!= FF_ONLY
&& merging_a_throwaway_tag(commit
))
1591 fast_forward
= FF_NO
;
1594 if (!use_strategies
) {
1596 ; /* already up-to-date */
1597 else if (!remoteheads
->next
)
1598 add_strategies(pull_twohead
, DEFAULT_TWOHEAD
);
1600 add_strategies(pull_octopus
, DEFAULT_OCTOPUS
);
1603 for (i
= 0; i
< use_strategies_nr
; i
++) {
1604 if (use_strategies
[i
]->attr
& NO_FAST_FORWARD
)
1605 fast_forward
= FF_NO
;
1606 if (use_strategies
[i
]->attr
& NO_TRIVIAL
)
1611 ; /* already up-to-date */
1612 else if (!remoteheads
->next
) {
1613 if (repo_get_merge_bases(the_repository
, head_commit
,
1614 remoteheads
->item
, &common
) < 0) {
1619 struct commit_list
*list
= remoteheads
;
1620 commit_list_insert(head_commit
, &list
);
1621 if (get_octopus_merge_bases(list
, &common
) < 0) {
1629 refs_update_ref(get_main_ref_store(the_repository
),
1630 "updating ORIG_HEAD", "ORIG_HEAD",
1631 &head_commit
->object
.oid
, NULL
, 0,
1632 UPDATE_REFS_DIE_ON_ERR
);
1634 if (remoteheads
&& !common
) {
1635 /* No common ancestors found. */
1636 if (!allow_unrelated_histories
)
1637 die(_("refusing to merge unrelated histories"));
1638 /* otherwise, we need a real merge. */
1639 } else if (!remoteheads
||
1640 (!remoteheads
->next
&& !common
->next
&&
1641 common
->item
== remoteheads
->item
)) {
1643 * If head can reach all the merge then we are up to date.
1644 * but first the most common case of merging one remote.
1646 finish_up_to_date();
1648 } else if (fast_forward
!= FF_NO
&& !remoteheads
->next
&&
1650 oideq(&common
->item
->object
.oid
, &head_commit
->object
.oid
)) {
1651 /* Again the most common case of merging one remote. */
1652 const char *msg
= have_message
?
1653 "Fast-forward (no commit created; -m option ignored)" :
1655 struct commit
*commit
;
1657 if (verbosity
>= 0) {
1658 printf(_("Updating %s..%s\n"),
1659 repo_find_unique_abbrev(the_repository
, &head_commit
->object
.oid
,
1661 repo_find_unique_abbrev(the_repository
, &remoteheads
->item
->object
.oid
,
1664 commit
= remoteheads
->item
;
1671 create_autostash_ref(the_repository
, "MERGE_AUTOSTASH");
1672 if (checkout_fast_forward(the_repository
,
1673 &head_commit
->object
.oid
,
1674 &commit
->object
.oid
,
1675 overwrite_ignore
)) {
1676 apply_autostash_ref(the_repository
, "MERGE_AUTOSTASH");
1681 finish(head_commit
, remoteheads
, &commit
->object
.oid
, msg
);
1682 remove_merge_branch_state(the_repository
);
1684 } else if (!remoteheads
->next
&& common
->next
)
1687 * We are not doing octopus and not fast-forward. Need
1690 else if (!remoteheads
->next
&& !common
->next
&& option_commit
) {
1692 * We are not doing octopus, not fast-forward, and have
1695 refresh_index(the_repository
->index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
1696 if (allow_trivial
&& fast_forward
!= FF_ONLY
) {
1698 * Must first ensure that index matches HEAD before
1699 * attempting a trivial merge.
1701 struct tree
*head_tree
= repo_get_commit_tree(the_repository
,
1703 struct strbuf sb
= STRBUF_INIT
;
1705 if (repo_index_has_changes(the_repository
, head_tree
,
1707 error(_("Your local changes to the following files would be overwritten by merge:\n %s"),
1709 strbuf_release(&sb
);
1714 /* See if it is really trivial. */
1715 git_committer_info(IDENT_STRICT
);
1716 printf(_("Trying really trivial in-index merge...\n"));
1717 if (!read_tree_trivial(&common
->item
->object
.oid
,
1718 &head_commit
->object
.oid
,
1719 &remoteheads
->item
->object
.oid
)) {
1720 ret
= merge_trivial(head_commit
, remoteheads
);
1723 printf(_("Nope.\n"));
1727 * An octopus. If we can reach all the remote we are up
1731 struct commit_list
*j
;
1733 for (j
= remoteheads
; j
; j
= j
->next
) {
1734 struct commit_list
*common_one
= NULL
;
1735 struct commit
*common_item
;
1738 * Here we *have* to calculate the individual
1739 * merge_bases again, otherwise "git merge HEAD^
1740 * HEAD^^" would be missed.
1742 if (repo_get_merge_bases(the_repository
, head_commit
,
1743 j
->item
, &common_one
) < 0)
1746 common_item
= common_one
->item
;
1747 free_commit_list(common_one
);
1748 if (!oideq(&common_item
->object
.oid
, &j
->item
->object
.oid
)) {
1754 finish_up_to_date();
1759 if (fast_forward
== FF_ONLY
)
1760 die_ff_impossible();
1763 create_autostash_ref(the_repository
, "MERGE_AUTOSTASH");
1765 /* We are going to make a new commit. */
1766 git_committer_info(IDENT_STRICT
);
1769 * At this point, we need a real merge. No matter what strategy
1770 * we use, it would operate on the index, possibly affecting the
1771 * working tree, and when resolved cleanly, have the desired
1772 * tree in the index -- this means that the index must be in
1773 * sync with the head commit. The strategies are responsible
1776 * Stash away the local changes so that we can try more than one
1777 * and/or recover from merge strategies bailing while leaving the
1778 * index and working tree polluted.
1780 if (save_state(&stash
))
1781 oidclr(&stash
, the_repository
->hash_algo
);
1783 for (i
= 0; i
< use_strategies_nr
; i
++) {
1786 printf(_("Rewinding the tree to pristine...\n"));
1787 restore_state(&head_commit
->object
.oid
, &stash
);
1789 if (use_strategies_nr
!= 1)
1790 printf(_("Trying merge strategy %s...\n"),
1791 use_strategies
[i
]->name
);
1793 * Remember which strategy left the state in the working
1796 wt_strategy
= use_strategies
[i
]->name
;
1798 ret
= try_merge_strategy(wt_strategy
,
1799 common
, remoteheads
,
1802 * The backend exits with 1 when conflicts are
1803 * left to be resolved, with 2 when it does not
1804 * handle the given merge at all.
1809 * This strategy worked; no point in trying
1813 best_strategy
= wt_strategy
;
1816 cnt
= (use_strategies_nr
> 1) ? evaluate_result() : 0;
1817 if (best_cnt
<= 0 || cnt
<= best_cnt
) {
1818 best_strategy
= wt_strategy
;
1825 * If we have a resulting tree, that means the strategy module
1826 * auto resolved the merge cleanly.
1828 if (merge_was_ok
&& option_commit
) {
1829 automerge_was_ok
= 1;
1830 ret
= finish_automerge(head_commit
, head_subsumed
,
1831 common
, remoteheads
,
1832 &result_tree
, wt_strategy
);
1837 * Pick the result from the best strategy and have the user fix
1840 if (!best_strategy
) {
1841 restore_state(&head_commit
->object
.oid
, &stash
);
1842 if (use_strategies_nr
> 1)
1844 _("No merge strategy handled the merge.\n"));
1846 fprintf(stderr
, _("Merge with strategy %s failed.\n"),
1847 use_strategies
[0]->name
);
1848 apply_autostash_ref(the_repository
, "MERGE_AUTOSTASH");
1851 } else if (best_strategy
== wt_strategy
)
1852 ; /* We already have its result in the working tree. */
1854 printf(_("Rewinding the tree to pristine...\n"));
1855 restore_state(&head_commit
->object
.oid
, &stash
);
1856 printf(_("Using the %s strategy to prepare resolving by hand.\n"),
1858 try_merge_strategy(best_strategy
, common
, remoteheads
,
1863 finish(head_commit
, remoteheads
, NULL
, NULL
);
1865 git_test_write_commit_graph_or_die();
1867 write_merge_state(remoteheads
);
1870 fprintf(stderr
, _("Automatic merge went well; "
1871 "stopped before committing as requested\n"));
1873 ret
= suggest_conflicts();
1875 printf(_("When finished, apply stashed changes with `git stash pop`\n"));
1878 if (!automerge_was_ok
) {
1879 free_commit_list(common
);
1880 free_commit_list(remoteheads
);
1882 strbuf_release(&buf
);
1883 free(branch_to_free
);
1886 discard_index(the_repository
->index
);