4 * Copyright (c) 2008 Miklos Vajna <vmiklos@frugalware.org>
6 * Based on git-merge.sh by Junio C Hamano.
9 #define USE_THE_INDEX_VARIABLE
13 #include "parse-options.h"
16 #include "run-command.h"
19 #include "diff-merges.h"
25 #include "unpack-trees.h"
26 #include "cache-tree.h"
33 #include "merge-recursive.h"
34 #include "merge-ort-wrappers.h"
35 #include "resolve-undo.h"
37 #include "fmt-merge-msg.h"
38 #include "gpg-interface.h"
39 #include "sequencer.h"
40 #include "string-list.h"
45 #include "commit-reach.h"
46 #include "wt-status.h"
47 #include "commit-graph.h"
49 #define DEFAULT_TWOHEAD (1<<0)
50 #define DEFAULT_OCTOPUS (1<<1)
51 #define NO_FAST_FORWARD (1<<2)
52 #define NO_TRIVIAL (1<<3)
59 static const char * const builtin_merge_usage
[] = {
60 N_("git merge [<options>] [<commit>...]"),
62 "git merge --continue",
66 static int show_diffstat
= 1, shortlog_len
= -1, squash
;
67 static int option_commit
= -1;
68 static int option_edit
= -1;
69 static int allow_trivial
= 1, have_message
, verify_signatures
;
70 static int check_trust_level
= 1;
71 static int overwrite_ignore
= 1;
72 static struct strbuf merge_msg
= STRBUF_INIT
;
73 static struct strategy
**use_strategies
;
74 static size_t use_strategies_nr
, use_strategies_alloc
;
75 static const char **xopts
;
76 static size_t xopts_nr
, xopts_alloc
;
77 static const char *branch
;
78 static char *branch_mergeoptions
;
80 static int allow_rerere_auto
;
81 static int abort_current_merge
;
82 static int quit_current_merge
;
83 static int continue_current_merge
;
84 static int allow_unrelated_histories
;
85 static int show_progress
= -1;
86 static int default_to_upstream
= 1;
88 static const char *sign_commit
;
91 static char *into_name
;
93 static struct strategy all_strategy
[] = {
94 { "recursive", NO_TRIVIAL
},
95 { "octopus", DEFAULT_OCTOPUS
},
96 { "ort", DEFAULT_TWOHEAD
| NO_TRIVIAL
},
98 { "ours", NO_FAST_FORWARD
| NO_TRIVIAL
},
99 { "subtree", NO_FAST_FORWARD
| NO_TRIVIAL
},
102 static const char *pull_twohead
, *pull_octopus
;
110 static enum ff_type fast_forward
= FF_ALLOW
;
112 static const char *cleanup_arg
;
113 static enum commit_msg_cleanup_mode cleanup_mode
;
115 static int option_parse_message(const struct option
*opt
,
116 const char *arg
, int unset
)
118 struct strbuf
*buf
= opt
->value
;
121 strbuf_setlen(buf
, 0);
123 strbuf_addf(buf
, "%s%s", buf
->len
? "\n\n" : "", arg
);
126 return error(_("switch `m' requires a value"));
130 static enum parse_opt_result
option_read_message(struct parse_opt_ctx_t
*ctx
,
131 const struct option
*opt
,
132 const char *arg_not_used
,
135 struct strbuf
*buf
= opt
->value
;
138 BUG_ON_OPT_ARG(arg_not_used
);
140 BUG("-F cannot be negated");
145 } else if (ctx
->argc
> 1) {
149 return error(_("option `%s' requires a value"), opt
->long_name
);
152 strbuf_addch(buf
, '\n');
153 if (ctx
->prefix
&& !is_absolute_path(arg
))
154 arg
= prefix_filename(ctx
->prefix
, arg
);
155 if (strbuf_read_file(buf
, arg
, 0) < 0)
156 return error(_("could not read file '%s'"), arg
);
162 static struct strategy
*get_strategy(const char *name
)
165 struct strategy
*ret
;
166 static struct cmdnames main_cmds
, other_cmds
;
168 char *default_strategy
= getenv("GIT_TEST_MERGE_ALGORITHM");
173 if (default_strategy
&&
174 !strcmp(default_strategy
, "ort") &&
175 !strcmp(name
, "recursive")) {
179 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
180 if (!strcmp(name
, all_strategy
[i
].name
))
181 return &all_strategy
[i
];
184 struct cmdnames not_strategies
;
187 memset(¬_strategies
, 0, sizeof(struct cmdnames
));
188 load_command_list("git-merge-", &main_cmds
, &other_cmds
);
189 for (i
= 0; i
< main_cmds
.cnt
; i
++) {
191 struct cmdname
*ent
= main_cmds
.names
[i
];
192 for (j
= 0; !found
&& j
< ARRAY_SIZE(all_strategy
); j
++)
193 if (!strncmp(ent
->name
, all_strategy
[j
].name
, ent
->len
)
194 && !all_strategy
[j
].name
[ent
->len
])
197 add_cmdname(¬_strategies
, ent
->name
, ent
->len
);
199 exclude_cmds(&main_cmds
, ¬_strategies
);
201 if (!is_in_cmdlist(&main_cmds
, name
) && !is_in_cmdlist(&other_cmds
, name
)) {
202 fprintf(stderr
, _("Could not find merge strategy '%s'.\n"), name
);
203 fprintf(stderr
, _("Available strategies are:"));
204 for (i
= 0; i
< main_cmds
.cnt
; i
++)
205 fprintf(stderr
, " %s", main_cmds
.names
[i
]->name
);
206 fprintf(stderr
, ".\n");
207 if (other_cmds
.cnt
) {
208 fprintf(stderr
, _("Available custom strategies are:"));
209 for (i
= 0; i
< other_cmds
.cnt
; i
++)
210 fprintf(stderr
, " %s", other_cmds
.names
[i
]->name
);
211 fprintf(stderr
, ".\n");
216 CALLOC_ARRAY(ret
, 1);
217 ret
->name
= xstrdup(name
);
218 ret
->attr
= NO_TRIVIAL
;
222 static void append_strategy(struct strategy
*s
)
224 ALLOC_GROW(use_strategies
, use_strategies_nr
+ 1, use_strategies_alloc
);
225 use_strategies
[use_strategies_nr
++] = s
;
228 static int option_parse_strategy(const struct option
*opt
,
229 const char *name
, int unset
)
234 append_strategy(get_strategy(name
));
238 static int option_parse_x(const struct option
*opt
,
239 const char *arg
, int unset
)
244 ALLOC_GROW(xopts
, xopts_nr
+ 1, xopts_alloc
);
245 xopts
[xopts_nr
++] = xstrdup(arg
);
249 static int option_parse_n(const struct option
*opt
,
250 const char *arg
, int unset
)
253 show_diffstat
= unset
;
257 static struct option builtin_merge_options
[] = {
258 OPT_CALLBACK_F('n', NULL
, NULL
, NULL
,
259 N_("do not show a diffstat at the end of the merge"),
260 PARSE_OPT_NOARG
, option_parse_n
),
261 OPT_BOOL(0, "stat", &show_diffstat
,
262 N_("show a diffstat at the end of the merge")),
263 OPT_BOOL(0, "summary", &show_diffstat
, N_("(synonym to --stat)")),
264 { OPTION_INTEGER
, 0, "log", &shortlog_len
, N_("n"),
265 N_("add (at most <n>) entries from shortlog to merge commit message"),
266 PARSE_OPT_OPTARG
, NULL
, DEFAULT_MERGE_LOG_LEN
},
267 OPT_BOOL(0, "squash", &squash
,
268 N_("create a single commit instead of doing a merge")),
269 OPT_BOOL(0, "commit", &option_commit
,
270 N_("perform a commit if the merge succeeds (default)")),
271 OPT_BOOL('e', "edit", &option_edit
,
272 N_("edit message before committing")),
273 OPT_CLEANUP(&cleanup_arg
),
274 OPT_SET_INT(0, "ff", &fast_forward
, N_("allow fast-forward (default)"), FF_ALLOW
),
275 OPT_SET_INT_F(0, "ff-only", &fast_forward
,
276 N_("abort if fast-forward is not possible"),
277 FF_ONLY
, PARSE_OPT_NONEG
),
278 OPT_RERERE_AUTOUPDATE(&allow_rerere_auto
),
279 OPT_BOOL(0, "verify-signatures", &verify_signatures
,
280 N_("verify that the named commit has a valid GPG signature")),
281 OPT_CALLBACK('s', "strategy", &use_strategies
, N_("strategy"),
282 N_("merge strategy to use"), option_parse_strategy
),
283 OPT_CALLBACK('X', "strategy-option", &xopts
, N_("option=value"),
284 N_("option for selected merge strategy"), option_parse_x
),
285 OPT_CALLBACK('m', "message", &merge_msg
, N_("message"),
286 N_("merge commit message (for a non-fast-forward merge)"),
287 option_parse_message
),
288 { OPTION_LOWLEVEL_CALLBACK
, 'F', "file", &merge_msg
, N_("path"),
289 N_("read message from file"), PARSE_OPT_NONEG
,
290 NULL
, 0, option_read_message
},
291 OPT_STRING(0, "into-name", &into_name
, N_("name"),
292 N_("use <name> instead of the real target")),
293 OPT__VERBOSITY(&verbosity
),
294 OPT_BOOL(0, "abort", &abort_current_merge
,
295 N_("abort the current in-progress merge")),
296 OPT_BOOL(0, "quit", &quit_current_merge
,
297 N_("--abort but leave index and working tree alone")),
298 OPT_BOOL(0, "continue", &continue_current_merge
,
299 N_("continue the current in-progress merge")),
300 OPT_BOOL(0, "allow-unrelated-histories", &allow_unrelated_histories
,
301 N_("allow merging unrelated histories")),
302 OPT_SET_INT(0, "progress", &show_progress
, N_("force progress reporting"), 1),
303 { OPTION_STRING
, 'S', "gpg-sign", &sign_commit
, N_("key-id"),
304 N_("GPG sign commit"), PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
305 OPT_AUTOSTASH(&autostash
),
306 OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore
, N_("update ignored files (default)")),
307 OPT_BOOL(0, "signoff", &signoff
, N_("add a Signed-off-by trailer")),
308 OPT_BOOL(0, "no-verify", &no_verify
, N_("bypass pre-merge-commit and commit-msg hooks")),
312 static int save_state(struct object_id
*stash
)
315 struct child_process cp
= CHILD_PROCESS_INIT
;
316 struct strbuf buffer
= STRBUF_INIT
;
317 struct lock_file lock_file
= LOCK_INIT
;
321 fd
= repo_hold_locked_index(the_repository
, &lock_file
, 0);
322 refresh_index(&the_index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
324 repo_update_index_if_able(the_repository
, &lock_file
);
325 rollback_lock_file(&lock_file
);
327 strvec_pushl(&cp
.args
, "stash", "create", NULL
);
331 if (start_command(&cp
))
332 die(_("could not run stash."));
333 len
= strbuf_read(&buffer
, cp
.out
, 1024);
336 if (finish_command(&cp
) || len
< 0)
337 die(_("stash failed"));
338 else if (!len
) /* no changes */
340 strbuf_setlen(&buffer
, buffer
.len
-1);
341 if (get_oid(buffer
.buf
, stash
))
342 die(_("not a valid object: %s"), buffer
.buf
);
345 strbuf_release(&buffer
);
349 static void read_empty(const struct object_id
*oid
)
351 struct child_process cmd
= CHILD_PROCESS_INIT
;
353 strvec_pushl(&cmd
.args
, "read-tree", "-m", "-u", empty_tree_oid_hex(),
354 oid_to_hex(oid
), NULL
);
357 if (run_command(&cmd
))
358 die(_("read-tree failed"));
361 static void reset_hard(const struct object_id
*oid
)
363 struct child_process cmd
= CHILD_PROCESS_INIT
;
365 strvec_pushl(&cmd
.args
, "read-tree", "-v", "--reset", "-u",
366 oid_to_hex(oid
), NULL
);
369 if (run_command(&cmd
))
370 die(_("read-tree failed"));
373 static void restore_state(const struct object_id
*head
,
374 const struct object_id
*stash
)
376 struct child_process cmd
= CHILD_PROCESS_INIT
;
380 if (is_null_oid(stash
))
383 strvec_pushl(&cmd
.args
, "stash", "apply", "--index", "--quiet", NULL
);
384 strvec_push(&cmd
.args
, oid_to_hex(stash
));
387 * It is OK to ignore error here, for example when there was
388 * nothing to restore.
394 discard_index(&the_index
);
395 if (repo_read_index(the_repository
) < 0)
396 die(_("could not read index"));
399 /* This is called when no merge was necessary. */
400 static void finish_up_to_date(void)
402 if (verbosity
>= 0) {
404 puts(_("Already up to date. (nothing to squash)"));
406 puts(_("Already up to date."));
408 remove_merge_branch_state(the_repository
);
411 static void squash_message(struct commit
*commit
, struct commit_list
*remoteheads
)
414 struct strbuf out
= STRBUF_INIT
;
415 struct commit_list
*j
;
416 struct pretty_print_context ctx
= {0};
418 printf(_("Squash commit -- not updating HEAD\n"));
420 repo_init_revisions(the_repository
, &rev
, NULL
);
421 diff_merges_suppress(&rev
);
422 rev
.commit_format
= CMIT_FMT_MEDIUM
;
424 commit
->object
.flags
|= UNINTERESTING
;
425 add_pending_object(&rev
, &commit
->object
, NULL
);
427 for (j
= remoteheads
; j
; j
= j
->next
)
428 add_pending_object(&rev
, &j
->item
->object
, NULL
);
430 setup_revisions(0, NULL
, &rev
, NULL
);
431 if (prepare_revision_walk(&rev
))
432 die(_("revision walk setup failed"));
434 ctx
.abbrev
= rev
.abbrev
;
435 ctx
.date_mode
= rev
.date_mode
;
436 ctx
.fmt
= rev
.commit_format
;
438 strbuf_addstr(&out
, "Squashed commit of the following:\n");
439 while ((commit
= get_revision(&rev
)) != NULL
) {
440 strbuf_addch(&out
, '\n');
441 strbuf_addf(&out
, "commit %s\n",
442 oid_to_hex(&commit
->object
.oid
));
443 pretty_print_commit(&ctx
, commit
, &out
);
445 write_file_buf(git_path_squash_msg(the_repository
), out
.buf
, out
.len
);
446 strbuf_release(&out
);
447 release_revisions(&rev
);
450 static void finish(struct commit
*head_commit
,
451 struct commit_list
*remoteheads
,
452 const struct object_id
*new_head
, const char *msg
)
454 struct strbuf reflog_message
= STRBUF_INIT
;
455 const struct object_id
*head
= &head_commit
->object
.oid
;
458 strbuf_addstr(&reflog_message
, getenv("GIT_REFLOG_ACTION"));
462 strbuf_addf(&reflog_message
, "%s: %s",
463 getenv("GIT_REFLOG_ACTION"), msg
);
466 squash_message(head_commit
, remoteheads
);
468 if (verbosity
>= 0 && !merge_msg
.len
)
469 printf(_("No merge message -- not updating HEAD\n"));
471 update_ref(reflog_message
.buf
, "HEAD", new_head
, head
,
472 0, UPDATE_REFS_DIE_ON_ERR
);
474 * We ignore errors in 'gc --auto', since the
475 * user should see them.
477 run_auto_maintenance(verbosity
< 0);
480 if (new_head
&& show_diffstat
) {
481 struct diff_options opts
;
482 repo_diff_setup(the_repository
, &opts
);
483 opts
.stat_width
= -1; /* use full terminal width */
484 opts
.stat_graph_width
= -1; /* respect statGraphWidth config */
485 opts
.output_format
|=
486 DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
487 opts
.detect_rename
= DIFF_DETECT_RENAME
;
488 diff_setup_done(&opts
);
489 diff_tree_oid(head
, new_head
, "", &opts
);
494 /* Run a post-merge hook */
495 run_hooks_l("post-merge", squash
? "1" : "0", NULL
);
498 apply_autostash(git_path_merge_autostash(the_repository
));
499 strbuf_release(&reflog_message
);
502 /* Get the name for the merge commit's message. */
503 static void merge_name(const char *remote
, struct strbuf
*msg
)
505 struct commit
*remote_head
;
506 struct object_id branch_head
;
507 struct strbuf bname
= STRBUF_INIT
;
508 struct merge_remote_desc
*desc
;
510 char *found_ref
= NULL
;
513 strbuf_branchname(&bname
, remote
, 0);
516 oidclr(&branch_head
);
517 remote_head
= get_merge_parent(remote
);
519 die(_("'%s' does not point to a commit"), remote
);
521 if (dwim_ref(remote
, strlen(remote
), &branch_head
, &found_ref
, 0) > 0) {
522 if (starts_with(found_ref
, "refs/heads/")) {
523 strbuf_addf(msg
, "%s\t\tbranch '%s' of .\n",
524 oid_to_hex(&branch_head
), remote
);
527 if (starts_with(found_ref
, "refs/tags/")) {
528 strbuf_addf(msg
, "%s\t\ttag '%s' of .\n",
529 oid_to_hex(&branch_head
), remote
);
532 if (starts_with(found_ref
, "refs/remotes/")) {
533 strbuf_addf(msg
, "%s\t\tremote-tracking branch '%s' of .\n",
534 oid_to_hex(&branch_head
), remote
);
539 /* See if remote matches <name>^^^.. or <name>~<number> */
540 for (len
= 0, ptr
= remote
+ strlen(remote
);
541 remote
< ptr
&& ptr
[-1] == '^';
548 ptr
= strrchr(remote
, '~');
550 int seen_nonzero
= 0;
553 while (*++ptr
&& isdigit(*ptr
)) {
554 seen_nonzero
|= (*ptr
!= '0');
558 len
= 0; /* not ...~<number> */
559 else if (seen_nonzero
)
562 early
= 1; /* "name~" is "name~1"! */
566 struct strbuf truname
= STRBUF_INIT
;
567 strbuf_addf(&truname
, "refs/heads/%s", remote
);
568 strbuf_setlen(&truname
, truname
.len
- len
);
569 if (ref_exists(truname
.buf
)) {
571 "%s\t\tbranch '%s'%s of .\n",
572 oid_to_hex(&remote_head
->object
.oid
),
574 (early
? " (early part)" : ""));
575 strbuf_release(&truname
);
578 strbuf_release(&truname
);
581 desc
= merge_remote_util(remote_head
);
582 if (desc
&& desc
->obj
&& desc
->obj
->type
== OBJ_TAG
) {
583 strbuf_addf(msg
, "%s\t\t%s '%s'\n",
584 oid_to_hex(&desc
->obj
->oid
),
585 type_name(desc
->obj
->type
),
590 strbuf_addf(msg
, "%s\t\tcommit '%s'\n",
591 oid_to_hex(&remote_head
->object
.oid
), remote
);
594 strbuf_release(&bname
);
597 static void parse_branch_merge_options(char *bmo
)
604 argc
= split_cmdline(bmo
, &argv
);
606 die(_("Bad branch.%s.mergeoptions string: %s"), branch
,
607 _(split_cmdline_strerror(argc
)));
608 REALLOC_ARRAY(argv
, argc
+ 2);
609 MOVE_ARRAY(argv
+ 1, argv
, argc
+ 1);
611 argv
[0] = "branch.*.mergeoptions";
612 parse_options(argc
, argv
, NULL
, builtin_merge_options
,
613 builtin_merge_usage
, 0);
617 static int git_merge_config(const char *k
, const char *v
, void *cb
)
623 skip_prefix(k
, "branch.", &str
) &&
624 skip_prefix(str
, branch
, &str
) &&
625 !strcmp(str
, ".mergeoptions")) {
626 free(branch_mergeoptions
);
627 branch_mergeoptions
= xstrdup(v
);
631 if (!strcmp(k
, "merge.diffstat") || !strcmp(k
, "merge.stat"))
632 show_diffstat
= git_config_bool(k
, v
);
633 else if (!strcmp(k
, "merge.verifysignatures"))
634 verify_signatures
= git_config_bool(k
, v
);
635 else if (!strcmp(k
, "pull.twohead"))
636 return git_config_string(&pull_twohead
, k
, v
);
637 else if (!strcmp(k
, "pull.octopus"))
638 return git_config_string(&pull_octopus
, k
, v
);
639 else if (!strcmp(k
, "commit.cleanup"))
640 return git_config_string(&cleanup_arg
, k
, v
);
641 else if (!strcmp(k
, "merge.ff")) {
642 int boolval
= git_parse_maybe_bool(v
);
644 fast_forward
= boolval
? FF_ALLOW
: FF_NO
;
645 } else if (v
&& !strcmp(v
, "only")) {
646 fast_forward
= FF_ONLY
;
647 } /* do not barf on values from future versions of git */
649 } else if (!strcmp(k
, "merge.defaulttoupstream")) {
650 default_to_upstream
= git_config_bool(k
, v
);
652 } else if (!strcmp(k
, "commit.gpgsign")) {
653 sign_commit
= git_config_bool(k
, v
) ? "" : NULL
;
655 } else if (!strcmp(k
, "gpg.mintrustlevel")) {
656 check_trust_level
= 0;
657 } else if (!strcmp(k
, "merge.autostash")) {
658 autostash
= git_config_bool(k
, v
);
662 status
= fmt_merge_msg_config(k
, v
, cb
);
665 status
= git_gpg_config(k
, v
, NULL
);
668 return git_diff_ui_config(k
, v
, cb
);
671 static int read_tree_trivial(struct object_id
*common
, struct object_id
*head
,
672 struct object_id
*one
)
675 struct tree
*trees
[MAX_UNPACK_TREES
];
676 struct tree_desc t
[MAX_UNPACK_TREES
];
677 struct unpack_trees_options opts
;
679 memset(&opts
, 0, sizeof(opts
));
681 opts
.src_index
= &the_index
;
682 opts
.dst_index
= &the_index
;
684 opts
.verbose_update
= 1;
685 opts
.trivial_merges_only
= 1;
687 opts
.preserve_ignored
= 0; /* FIXME: !overwrite_ignore */
688 trees
[nr_trees
] = parse_tree_indirect(common
);
689 if (!trees
[nr_trees
++])
691 trees
[nr_trees
] = parse_tree_indirect(head
);
692 if (!trees
[nr_trees
++])
694 trees
[nr_trees
] = parse_tree_indirect(one
);
695 if (!trees
[nr_trees
++])
697 opts
.fn
= threeway_merge
;
698 cache_tree_free(&the_index
.cache_tree
);
699 for (i
= 0; i
< nr_trees
; i
++) {
700 parse_tree(trees
[i
]);
701 init_tree_desc(t
+i
, trees
[i
]->buffer
, trees
[i
]->size
);
703 if (unpack_trees(nr_trees
, t
, &opts
))
708 static void write_tree_trivial(struct object_id
*oid
)
710 if (write_index_as_tree(oid
, &the_index
, get_index_file(), 0, NULL
))
711 die(_("git write-tree failed to write a tree"));
714 static int try_merge_strategy(const char *strategy
, struct commit_list
*common
,
715 struct commit_list
*remoteheads
,
718 const char *head_arg
= "HEAD";
720 if (repo_refresh_and_write_index(the_repository
, REFRESH_QUIET
,
721 SKIP_IF_UNCHANGED
, 0, NULL
, NULL
,
723 return error(_("Unable to write index."));
725 if (!strcmp(strategy
, "recursive") || !strcmp(strategy
, "subtree") ||
726 !strcmp(strategy
, "ort")) {
727 struct lock_file lock
= LOCK_INIT
;
729 struct commit
*result
;
730 struct commit_list
*reversed
= NULL
;
731 struct merge_options o
;
732 struct commit_list
*j
;
734 if (remoteheads
->next
) {
735 error(_("Not handling anything other than two heads merge."));
739 init_merge_options(&o
, the_repository
);
740 if (!strcmp(strategy
, "subtree"))
741 o
.subtree_shift
= "";
743 o
.show_rename_progress
=
744 show_progress
== -1 ? isatty(2) : show_progress
;
746 for (x
= 0; x
< xopts_nr
; x
++)
747 if (parse_merge_opt(&o
, xopts
[x
]))
748 die(_("unknown strategy option: -X%s"), xopts
[x
]);
750 o
.branch1
= head_arg
;
751 o
.branch2
= merge_remote_util(remoteheads
->item
)->name
;
753 for (j
= common
; j
; j
= j
->next
)
754 commit_list_insert(j
->item
, &reversed
);
756 repo_hold_locked_index(the_repository
, &lock
,
758 if (!strcmp(strategy
, "ort"))
759 clean
= merge_ort_recursive(&o
, head
, remoteheads
->item
,
762 clean
= merge_recursive(&o
, head
, remoteheads
->item
,
765 rollback_lock_file(&lock
);
768 if (write_locked_index(&the_index
, &lock
,
769 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
770 die(_("unable to write %s"), get_index_file());
771 return clean
? 0 : 1;
773 return try_merge_command(the_repository
,
774 strategy
, xopts_nr
, xopts
,
775 common
, head_arg
, remoteheads
);
779 static void count_diff_files(struct diff_queue_struct
*q
,
780 struct diff_options
*opt UNUSED
, void *data
)
787 static int count_unmerged_entries(void)
791 for (i
= 0; i
< the_index
.cache_nr
; i
++)
792 if (ce_stage(the_index
.cache
[i
]))
798 static void add_strategies(const char *string
, unsigned attr
)
803 struct string_list list
= STRING_LIST_INIT_DUP
;
804 struct string_list_item
*item
;
805 string_list_split(&list
, string
, ' ', -1);
806 for_each_string_list_item(item
, &list
)
807 append_strategy(get_strategy(item
->string
));
808 string_list_clear(&list
, 0);
811 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
812 if (all_strategy
[i
].attr
& attr
)
813 append_strategy(&all_strategy
[i
]);
817 static void read_merge_msg(struct strbuf
*msg
)
819 const char *filename
= git_path_merge_msg(the_repository
);
821 if (strbuf_read_file(msg
, filename
, 0) < 0)
822 die_errno(_("Could not read from '%s'"), filename
);
825 static void write_merge_state(struct commit_list
*);
826 static void abort_commit(struct commit_list
*remoteheads
, const char *err_msg
)
829 error("%s", err_msg
);
831 _("Not committing merge; use 'git commit' to complete the merge.\n"));
832 write_merge_state(remoteheads
);
836 static const char merge_editor_comment
[] =
837 N_("Please enter a commit message to explain why this merge is necessary,\n"
838 "especially if it merges an updated upstream into a topic branch.\n"
841 static const char scissors_editor_comment
[] =
842 N_("An empty message aborts the commit.\n");
844 static const char no_scissors_editor_comment
[] =
845 N_("Lines starting with '%c' will be ignored, and an empty message aborts\n"
848 static void write_merge_heads(struct commit_list
*);
849 static void prepare_to_commit(struct commit_list
*remoteheads
)
851 struct strbuf msg
= STRBUF_INIT
;
852 const char *index_file
= get_index_file();
857 if (run_commit_hook(0 < option_edit
, index_file
, &invoked_hook
,
858 "pre-merge-commit", NULL
))
859 abort_commit(remoteheads
, NULL
);
861 * Re-read the index as pre-merge-commit hook could have updated it,
862 * and write it out as a tree. We must do this before we invoke
863 * the editor and after we invoke run_status above.
866 discard_index(&the_index
);
868 read_index_from(&the_index
, index_file
, get_git_dir());
869 strbuf_addbuf(&msg
, &merge_msg
);
871 BUG("the control must not reach here under --squash");
872 if (0 < option_edit
) {
873 strbuf_addch(&msg
, '\n');
874 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
) {
875 wt_status_append_cut_line(&msg
);
876 strbuf_commented_addf(&msg
, "\n");
878 strbuf_commented_addf(&msg
, _(merge_editor_comment
));
879 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
)
880 strbuf_commented_addf(&msg
, _(scissors_editor_comment
));
882 strbuf_commented_addf(&msg
,
883 _(no_scissors_editor_comment
), comment_line_char
);
886 append_signoff(&msg
, ignore_non_trailer(msg
.buf
, msg
.len
), 0);
887 write_merge_heads(remoteheads
);
888 write_file_buf(git_path_merge_msg(the_repository
), msg
.buf
, msg
.len
);
889 if (run_commit_hook(0 < option_edit
, get_index_file(), NULL
,
890 "prepare-commit-msg",
891 git_path_merge_msg(the_repository
), "merge", NULL
))
892 abort_commit(remoteheads
, NULL
);
893 if (0 < option_edit
) {
894 if (launch_editor(git_path_merge_msg(the_repository
), NULL
, NULL
))
895 abort_commit(remoteheads
, NULL
);
898 if (!no_verify
&& run_commit_hook(0 < option_edit
, get_index_file(),
900 git_path_merge_msg(the_repository
), NULL
))
901 abort_commit(remoteheads
, NULL
);
903 read_merge_msg(&msg
);
904 cleanup_message(&msg
, cleanup_mode
, 0);
906 abort_commit(remoteheads
, _("Empty commit message."));
907 strbuf_release(&merge_msg
);
908 strbuf_addbuf(&merge_msg
, &msg
);
909 strbuf_release(&msg
);
912 static int merge_trivial(struct commit
*head
, struct commit_list
*remoteheads
)
914 struct object_id result_tree
, result_commit
;
915 struct commit_list
*parents
, **pptr
= &parents
;
917 if (repo_refresh_and_write_index(the_repository
, REFRESH_QUIET
,
918 SKIP_IF_UNCHANGED
, 0, NULL
, NULL
,
920 return error(_("Unable to write index."));
922 write_tree_trivial(&result_tree
);
923 printf(_("Wonderful.\n"));
924 pptr
= commit_list_append(head
, pptr
);
925 pptr
= commit_list_append(remoteheads
->item
, pptr
);
926 prepare_to_commit(remoteheads
);
927 if (commit_tree(merge_msg
.buf
, merge_msg
.len
, &result_tree
, parents
,
928 &result_commit
, NULL
, sign_commit
))
929 die(_("failed to write commit object"));
930 finish(head
, remoteheads
, &result_commit
, "In-index merge");
931 remove_merge_branch_state(the_repository
);
935 static int finish_automerge(struct commit
*head
,
937 struct commit_list
*common
,
938 struct commit_list
*remoteheads
,
939 struct object_id
*result_tree
,
940 const char *wt_strategy
)
942 struct commit_list
*parents
= NULL
;
943 struct strbuf buf
= STRBUF_INIT
;
944 struct object_id result_commit
;
946 write_tree_trivial(result_tree
);
947 free_commit_list(common
);
948 parents
= remoteheads
;
949 if (!head_subsumed
|| fast_forward
== FF_NO
)
950 commit_list_insert(head
, &parents
);
951 prepare_to_commit(remoteheads
);
952 if (commit_tree(merge_msg
.buf
, merge_msg
.len
, result_tree
, parents
,
953 &result_commit
, NULL
, sign_commit
))
954 die(_("failed to write commit object"));
955 strbuf_addf(&buf
, "Merge made by the '%s' strategy.", wt_strategy
);
956 finish(head
, remoteheads
, &result_commit
, buf
.buf
);
957 strbuf_release(&buf
);
958 remove_merge_branch_state(the_repository
);
962 static int suggest_conflicts(void)
964 const char *filename
;
966 struct strbuf msgbuf
= STRBUF_INIT
;
968 filename
= git_path_merge_msg(the_repository
);
969 fp
= xfopen(filename
, "a");
972 * We can't use cleanup_mode because if we're not using the editor,
973 * get_cleanup_mode will return COMMIT_MSG_CLEANUP_SPACE instead, even
974 * though the message is meant to be processed later by git-commit.
975 * Thus, we will get the cleanup mode which is returned when we _are_
978 append_conflicts_hint(&the_index
, &msgbuf
,
979 get_cleanup_mode(cleanup_arg
, 1));
980 fputs(msgbuf
.buf
, fp
);
981 strbuf_release(&msgbuf
);
983 repo_rerere(the_repository
, allow_rerere_auto
);
984 printf(_("Automatic merge failed; "
985 "fix conflicts and then commit the result.\n"));
989 static int evaluate_result(void)
994 /* Check how many files differ. */
995 repo_init_revisions(the_repository
, &rev
, "");
996 setup_revisions(0, NULL
, &rev
, NULL
);
997 rev
.diffopt
.output_format
|=
998 DIFF_FORMAT_CALLBACK
;
999 rev
.diffopt
.format_callback
= count_diff_files
;
1000 rev
.diffopt
.format_callback_data
= &cnt
;
1001 run_diff_files(&rev
, 0);
1004 * Check how many unmerged entries are
1007 cnt
+= count_unmerged_entries();
1009 release_revisions(&rev
);
1014 * Pretend as if the user told us to merge with the remote-tracking
1015 * branch we have for the upstream of the current branch
1017 static int setup_with_upstream(const char ***argv
)
1019 struct branch
*branch
= branch_get(NULL
);
1024 die(_("No current branch."));
1025 if (!branch
->remote_name
)
1026 die(_("No remote for the current branch."));
1027 if (!branch
->merge_nr
)
1028 die(_("No default upstream defined for the current branch."));
1030 args
= xcalloc(st_add(branch
->merge_nr
, 1), sizeof(char *));
1031 for (i
= 0; i
< branch
->merge_nr
; i
++) {
1032 if (!branch
->merge
[i
]->dst
)
1033 die(_("No remote-tracking branch for %s from %s"),
1034 branch
->merge
[i
]->src
, branch
->remote_name
);
1035 args
[i
] = branch
->merge
[i
]->dst
;
1042 static void write_merge_heads(struct commit_list
*remoteheads
)
1044 struct commit_list
*j
;
1045 struct strbuf buf
= STRBUF_INIT
;
1047 for (j
= remoteheads
; j
; j
= j
->next
) {
1048 struct object_id
*oid
;
1049 struct commit
*c
= j
->item
;
1050 struct merge_remote_desc
*desc
;
1052 desc
= merge_remote_util(c
);
1053 if (desc
&& desc
->obj
) {
1054 oid
= &desc
->obj
->oid
;
1056 oid
= &c
->object
.oid
;
1058 strbuf_addf(&buf
, "%s\n", oid_to_hex(oid
));
1060 write_file_buf(git_path_merge_head(the_repository
), buf
.buf
, buf
.len
);
1063 if (fast_forward
== FF_NO
)
1064 strbuf_addstr(&buf
, "no-ff");
1065 write_file_buf(git_path_merge_mode(the_repository
), buf
.buf
, buf
.len
);
1066 strbuf_release(&buf
);
1069 static void write_merge_state(struct commit_list
*remoteheads
)
1071 write_merge_heads(remoteheads
);
1072 strbuf_addch(&merge_msg
, '\n');
1073 write_file_buf(git_path_merge_msg(the_repository
), merge_msg
.buf
,
1077 static int default_edit_option(void)
1079 static const char name
[] = "GIT_MERGE_AUTOEDIT";
1080 const char *e
= getenv(name
);
1081 struct stat st_stdin
, st_stdout
;
1084 /* an explicit -m msg without --[no-]edit */
1088 int v
= git_parse_maybe_bool(e
);
1090 die(_("Bad value '%s' in environment '%s'"), e
, name
);
1094 /* Use editor if stdin and stdout are the same and is a tty */
1095 return (!fstat(0, &st_stdin
) &&
1096 !fstat(1, &st_stdout
) &&
1097 isatty(0) && isatty(1) &&
1098 st_stdin
.st_dev
== st_stdout
.st_dev
&&
1099 st_stdin
.st_ino
== st_stdout
.st_ino
&&
1100 st_stdin
.st_mode
== st_stdout
.st_mode
);
1103 static struct commit_list
*reduce_parents(struct commit
*head_commit
,
1105 struct commit_list
*remoteheads
)
1107 struct commit_list
*parents
, **remotes
;
1110 * Is the current HEAD reachable from another commit being
1111 * merged? If so we do not want to record it as a parent of
1112 * the resulting merge, unless --no-ff is given. We will flip
1113 * this variable to 0 when we find HEAD among the independent
1114 * tips being merged.
1118 /* Find what parents to record by checking independent ones. */
1119 parents
= reduce_heads(remoteheads
);
1120 free_commit_list(remoteheads
);
1123 remotes
= &remoteheads
;
1125 struct commit
*commit
= pop_commit(&parents
);
1126 if (commit
== head_commit
)
1129 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1134 static void prepare_merge_message(struct strbuf
*merge_names
, struct strbuf
*merge_msg
)
1136 struct fmt_merge_msg_opts opts
;
1138 memset(&opts
, 0, sizeof(opts
));
1139 opts
.add_title
= !have_message
;
1140 opts
.shortlog_len
= shortlog_len
;
1141 opts
.credit_people
= (0 < option_edit
);
1142 opts
.into_name
= into_name
;
1144 fmt_merge_msg(merge_names
, merge_msg
, &opts
);
1146 strbuf_setlen(merge_msg
, merge_msg
->len
- 1);
1149 static void handle_fetch_head(struct commit_list
**remotes
, struct strbuf
*merge_names
)
1151 const char *filename
;
1153 struct strbuf fetch_head_file
= STRBUF_INIT
;
1154 const unsigned hexsz
= the_hash_algo
->hexsz
;
1157 merge_names
= &fetch_head_file
;
1159 filename
= git_path_fetch_head(the_repository
);
1160 fd
= xopen(filename
, O_RDONLY
);
1162 if (strbuf_read(merge_names
, fd
, 0) < 0)
1163 die_errno(_("could not read '%s'"), filename
);
1165 die_errno(_("could not close '%s'"), filename
);
1167 for (pos
= 0; pos
< merge_names
->len
; pos
= npos
) {
1168 struct object_id oid
;
1170 struct commit
*commit
;
1172 ptr
= strchr(merge_names
->buf
+ pos
, '\n');
1174 npos
= ptr
- merge_names
->buf
+ 1;
1176 npos
= merge_names
->len
;
1178 if (npos
- pos
< hexsz
+ 2 ||
1179 get_oid_hex(merge_names
->buf
+ pos
, &oid
))
1180 commit
= NULL
; /* bad */
1181 else if (memcmp(merge_names
->buf
+ pos
+ hexsz
, "\t\t", 2))
1182 continue; /* not-for-merge */
1184 char saved
= merge_names
->buf
[pos
+ hexsz
];
1185 merge_names
->buf
[pos
+ hexsz
] = '\0';
1186 commit
= get_merge_parent(merge_names
->buf
+ pos
);
1187 merge_names
->buf
[pos
+ hexsz
] = saved
;
1192 die(_("not something we can merge in %s: %s"),
1193 filename
, merge_names
->buf
+ pos
);
1195 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1198 if (merge_names
== &fetch_head_file
)
1199 strbuf_release(&fetch_head_file
);
1202 static struct commit_list
*collect_parents(struct commit
*head_commit
,
1204 int argc
, const char **argv
,
1205 struct strbuf
*merge_msg
)
1208 struct commit_list
*remoteheads
= NULL
;
1209 struct commit_list
**remotes
= &remoteheads
;
1210 struct strbuf merge_names
= STRBUF_INIT
, *autogen
= NULL
;
1212 if (merge_msg
&& (!have_message
|| shortlog_len
))
1213 autogen
= &merge_names
;
1216 remotes
= &commit_list_insert(head_commit
, remotes
)->next
;
1218 if (argc
== 1 && !strcmp(argv
[0], "FETCH_HEAD")) {
1219 handle_fetch_head(remotes
, autogen
);
1220 remoteheads
= reduce_parents(head_commit
, head_subsumed
, remoteheads
);
1222 for (i
= 0; i
< argc
; i
++) {
1223 struct commit
*commit
= get_merge_parent(argv
[i
]);
1225 help_unknown_ref(argv
[i
], "merge",
1226 _("not something we can merge"));
1227 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1229 remoteheads
= reduce_parents(head_commit
, head_subsumed
, remoteheads
);
1231 struct commit_list
*p
;
1232 for (p
= remoteheads
; p
; p
= p
->next
)
1233 merge_name(merge_remote_util(p
->item
)->name
, autogen
);
1238 prepare_merge_message(autogen
, merge_msg
);
1239 strbuf_release(autogen
);
1245 static int merging_a_throwaway_tag(struct commit
*commit
)
1248 struct object_id oid
;
1249 int is_throwaway_tag
= 0;
1251 /* Are we merging a tag? */
1252 if (!merge_remote_util(commit
) ||
1253 !merge_remote_util(commit
)->obj
||
1254 merge_remote_util(commit
)->obj
->type
!= OBJ_TAG
)
1255 return is_throwaway_tag
;
1258 * Now we know we are merging a tag object. Are we downstream
1259 * and following the tags from upstream? If so, we must have
1260 * the tag object pointed at by "refs/tags/$T" where $T is the
1261 * tagname recorded in the tag object. We want to allow such
1262 * a "just to catch up" merge to fast-forward.
1264 * Otherwise, we are playing an integrator's role, making a
1265 * merge with a throw-away tag from a contributor with
1266 * something like "git pull $contributor $signed_tag".
1267 * We want to forbid such a merge from fast-forwarding
1268 * by default; otherwise we would not keep the signature
1271 tag_ref
= xstrfmt("refs/tags/%s",
1272 ((struct tag
*)merge_remote_util(commit
)->obj
)->tag
);
1273 if (!read_ref(tag_ref
, &oid
) &&
1274 oideq(&oid
, &merge_remote_util(commit
)->obj
->oid
))
1275 is_throwaway_tag
= 0;
1277 is_throwaway_tag
= 1;
1279 return is_throwaway_tag
;
1282 int cmd_merge(int argc
, const char **argv
, const char *prefix
)
1284 struct object_id result_tree
, stash
, head_oid
;
1285 struct commit
*head_commit
;
1286 struct strbuf buf
= STRBUF_INIT
;
1287 int i
, ret
= 0, head_subsumed
;
1288 int best_cnt
= -1, merge_was_ok
= 0, automerge_was_ok
= 0;
1289 struct commit_list
*common
= NULL
;
1290 const char *best_strategy
= NULL
, *wt_strategy
= NULL
;
1291 struct commit_list
*remoteheads
= NULL
, *p
;
1292 void *branch_to_free
;
1293 int orig_argc
= argc
;
1295 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1296 usage_with_options(builtin_merge_usage
, builtin_merge_options
);
1298 prepare_repo_settings(the_repository
);
1299 the_repository
->settings
.command_requires_full_index
= 0;
1302 * Check if we are _not_ on a detached HEAD, i.e. if there is a
1305 branch
= branch_to_free
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
1307 skip_prefix(branch
, "refs/heads/", &branch
);
1309 if (!pull_twohead
) {
1310 char *default_strategy
= getenv("GIT_TEST_MERGE_ALGORITHM");
1311 if (default_strategy
&& !strcmp(default_strategy
, "ort"))
1312 pull_twohead
= "ort";
1315 init_diff_ui_defaults();
1316 git_config(git_merge_config
, NULL
);
1318 if (!branch
|| is_null_oid(&head_oid
))
1321 head_commit
= lookup_commit_or_die(&head_oid
, "HEAD");
1323 if (branch_mergeoptions
)
1324 parse_branch_merge_options(branch_mergeoptions
);
1325 argc
= parse_options(argc
, argv
, prefix
, builtin_merge_options
,
1326 builtin_merge_usage
, 0);
1327 if (shortlog_len
< 0)
1328 shortlog_len
= (merge_log_config
> 0) ? merge_log_config
: 0;
1330 if (verbosity
< 0 && show_progress
== -1)
1333 if (abort_current_merge
) {
1335 const char *nargv
[] = {"reset", "--merge", NULL
};
1336 struct strbuf stash_oid
= STRBUF_INIT
;
1339 usage_msg_opt(_("--abort expects no arguments"),
1340 builtin_merge_usage
, builtin_merge_options
);
1342 if (!file_exists(git_path_merge_head(the_repository
)))
1343 die(_("There is no merge to abort (MERGE_HEAD missing)."));
1345 if (read_oneliner(&stash_oid
, git_path_merge_autostash(the_repository
),
1346 READ_ONELINER_SKIP_IF_EMPTY
))
1347 unlink(git_path_merge_autostash(the_repository
));
1349 /* Invoke 'git reset --merge' */
1350 ret
= cmd_reset(nargc
, nargv
, prefix
);
1353 apply_autostash_oid(stash_oid
.buf
);
1355 strbuf_release(&stash_oid
);
1359 if (quit_current_merge
) {
1361 usage_msg_opt(_("--quit expects no arguments"),
1362 builtin_merge_usage
,
1363 builtin_merge_options
);
1365 remove_merge_branch_state(the_repository
);
1369 if (continue_current_merge
) {
1371 const char *nargv
[] = {"commit", NULL
};
1374 usage_msg_opt(_("--continue expects no arguments"),
1375 builtin_merge_usage
, builtin_merge_options
);
1377 if (!file_exists(git_path_merge_head(the_repository
)))
1378 die(_("There is no merge in progress (MERGE_HEAD missing)."));
1380 /* Invoke 'git commit' */
1381 ret
= cmd_commit(nargc
, nargv
, prefix
);
1385 if (repo_read_index_unmerged(the_repository
))
1386 die_resolve_conflict("merge");
1388 if (file_exists(git_path_merge_head(the_repository
))) {
1390 * There is no unmerged entry, don't advise 'git
1391 * add/rm <file>', just 'git commit'.
1393 if (advice_enabled(ADVICE_RESOLVE_CONFLICT
))
1394 die(_("You have not concluded your merge (MERGE_HEAD exists).\n"
1395 "Please, commit your changes before you merge."));
1397 die(_("You have not concluded your merge (MERGE_HEAD exists)."));
1399 if (ref_exists("CHERRY_PICK_HEAD")) {
1400 if (advice_enabled(ADVICE_RESOLVE_CONFLICT
))
1401 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
1402 "Please, commit your changes before you merge."));
1404 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists)."));
1406 resolve_undo_clear_index(&the_index
);
1408 if (option_edit
< 0)
1409 option_edit
= default_edit_option();
1411 cleanup_mode
= get_cleanup_mode(cleanup_arg
, 0 < option_edit
);
1417 if (fast_forward
== FF_NO
)
1418 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--no-ff.");
1419 if (option_commit
> 0)
1420 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--commit.");
1422 * squash can now silently disable option_commit - this is not
1423 * a problem as it is only overriding the default, not a user
1429 if (option_commit
< 0)
1433 if (default_to_upstream
)
1434 argc
= setup_with_upstream(&argv
);
1436 die(_("No commit specified and merge.defaultToUpstream not set."));
1437 } else if (argc
== 1 && !strcmp(argv
[0], "-")) {
1442 usage_with_options(builtin_merge_usage
,
1443 builtin_merge_options
);
1447 * If the merged head is a valid one there is no reason
1448 * to forbid "git merge" into a branch yet to be born.
1449 * We do the same for "git pull".
1451 struct object_id
*remote_head_oid
;
1453 die(_("Squash commit into empty head not supported yet"));
1454 if (fast_forward
== FF_NO
)
1455 die(_("Non-fast-forward commit does not make sense into "
1457 remoteheads
= collect_parents(head_commit
, &head_subsumed
,
1460 die(_("%s - not something we can merge"), argv
[0]);
1461 if (remoteheads
->next
)
1462 die(_("Can merge only exactly one commit into empty head"));
1464 if (verify_signatures
)
1465 verify_merge_signature(remoteheads
->item
, verbosity
,
1468 remote_head_oid
= &remoteheads
->item
->object
.oid
;
1469 read_empty(remote_head_oid
);
1470 update_ref("initial pull", "HEAD", remote_head_oid
, NULL
, 0,
1471 UPDATE_REFS_DIE_ON_ERR
);
1476 * All the rest are the commits being merged; prepare
1477 * the standard merge summary message to be appended
1478 * to the given message.
1480 remoteheads
= collect_parents(head_commit
, &head_subsumed
,
1481 argc
, argv
, &merge_msg
);
1483 if (!head_commit
|| !argc
)
1484 usage_with_options(builtin_merge_usage
,
1485 builtin_merge_options
);
1487 if (verify_signatures
) {
1488 for (p
= remoteheads
; p
; p
= p
->next
) {
1489 verify_merge_signature(p
->item
, verbosity
,
1494 strbuf_addstr(&buf
, "merge");
1495 for (p
= remoteheads
; p
; p
= p
->next
)
1496 strbuf_addf(&buf
, " %s", merge_remote_util(p
->item
)->name
);
1497 setenv("GIT_REFLOG_ACTION", buf
.buf
, 0);
1500 for (p
= remoteheads
; p
; p
= p
->next
) {
1501 struct commit
*commit
= p
->item
;
1502 strbuf_addf(&buf
, "GITHEAD_%s",
1503 oid_to_hex(&commit
->object
.oid
));
1504 setenv(buf
.buf
, merge_remote_util(commit
)->name
, 1);
1506 if (fast_forward
!= FF_ONLY
&& merging_a_throwaway_tag(commit
))
1507 fast_forward
= FF_NO
;
1510 if (!use_strategies
&& !pull_twohead
&&
1511 remoteheads
&& !remoteheads
->next
) {
1512 char *default_strategy
= getenv("GIT_TEST_MERGE_ALGORITHM");
1513 if (default_strategy
)
1514 append_strategy(get_strategy(default_strategy
));
1516 if (!use_strategies
) {
1518 ; /* already up-to-date */
1519 else if (!remoteheads
->next
)
1520 add_strategies(pull_twohead
, DEFAULT_TWOHEAD
);
1522 add_strategies(pull_octopus
, DEFAULT_OCTOPUS
);
1525 for (i
= 0; i
< use_strategies_nr
; i
++) {
1526 if (use_strategies
[i
]->attr
& NO_FAST_FORWARD
)
1527 fast_forward
= FF_NO
;
1528 if (use_strategies
[i
]->attr
& NO_TRIVIAL
)
1533 ; /* already up-to-date */
1534 else if (!remoteheads
->next
)
1535 common
= get_merge_bases(head_commit
, remoteheads
->item
);
1537 struct commit_list
*list
= remoteheads
;
1538 commit_list_insert(head_commit
, &list
);
1539 common
= get_octopus_merge_bases(list
);
1543 update_ref("updating ORIG_HEAD", "ORIG_HEAD",
1544 &head_commit
->object
.oid
, NULL
, 0, UPDATE_REFS_DIE_ON_ERR
);
1546 if (remoteheads
&& !common
) {
1547 /* No common ancestors found. */
1548 if (!allow_unrelated_histories
)
1549 die(_("refusing to merge unrelated histories"));
1550 /* otherwise, we need a real merge. */
1551 } else if (!remoteheads
||
1552 (!remoteheads
->next
&& !common
->next
&&
1553 common
->item
== remoteheads
->item
)) {
1555 * If head can reach all the merge then we are up to date.
1556 * but first the most common case of merging one remote.
1558 finish_up_to_date();
1560 } else if (fast_forward
!= FF_NO
&& !remoteheads
->next
&&
1562 oideq(&common
->item
->object
.oid
, &head_commit
->object
.oid
)) {
1563 /* Again the most common case of merging one remote. */
1564 const char *msg
= have_message
?
1565 "Fast-forward (no commit created; -m option ignored)" :
1567 struct commit
*commit
;
1569 if (verbosity
>= 0) {
1570 printf(_("Updating %s..%s\n"),
1571 find_unique_abbrev(&head_commit
->object
.oid
,
1573 find_unique_abbrev(&remoteheads
->item
->object
.oid
,
1576 commit
= remoteheads
->item
;
1583 create_autostash(the_repository
,
1584 git_path_merge_autostash(the_repository
));
1585 if (checkout_fast_forward(the_repository
,
1586 &head_commit
->object
.oid
,
1587 &commit
->object
.oid
,
1588 overwrite_ignore
)) {
1589 apply_autostash(git_path_merge_autostash(the_repository
));
1594 finish(head_commit
, remoteheads
, &commit
->object
.oid
, msg
);
1595 remove_merge_branch_state(the_repository
);
1597 } else if (!remoteheads
->next
&& common
->next
)
1600 * We are not doing octopus and not fast-forward. Need
1603 else if (!remoteheads
->next
&& !common
->next
&& option_commit
) {
1605 * We are not doing octopus, not fast-forward, and have
1608 refresh_index(&the_index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
1609 if (allow_trivial
&& fast_forward
!= FF_ONLY
) {
1611 * Must first ensure that index matches HEAD before
1612 * attempting a trivial merge.
1614 struct tree
*head_tree
= get_commit_tree(head_commit
);
1615 struct strbuf sb
= STRBUF_INIT
;
1617 if (repo_index_has_changes(the_repository
, head_tree
,
1619 error(_("Your local changes to the following files would be overwritten by merge:\n %s"),
1621 strbuf_release(&sb
);
1626 /* See if it is really trivial. */
1627 git_committer_info(IDENT_STRICT
);
1628 printf(_("Trying really trivial in-index merge...\n"));
1629 if (!read_tree_trivial(&common
->item
->object
.oid
,
1630 &head_commit
->object
.oid
,
1631 &remoteheads
->item
->object
.oid
)) {
1632 ret
= merge_trivial(head_commit
, remoteheads
);
1635 printf(_("Nope.\n"));
1639 * An octopus. If we can reach all the remote we are up
1643 struct commit_list
*j
;
1645 for (j
= remoteheads
; j
; j
= j
->next
) {
1646 struct commit_list
*common_one
;
1649 * Here we *have* to calculate the individual
1650 * merge_bases again, otherwise "git merge HEAD^
1651 * HEAD^^" would be missed.
1653 common_one
= get_merge_bases(head_commit
, j
->item
);
1654 if (!oideq(&common_one
->item
->object
.oid
, &j
->item
->object
.oid
)) {
1660 finish_up_to_date();
1665 if (fast_forward
== FF_ONLY
)
1666 die_ff_impossible();
1669 create_autostash(the_repository
,
1670 git_path_merge_autostash(the_repository
));
1672 /* We are going to make a new commit. */
1673 git_committer_info(IDENT_STRICT
);
1676 * At this point, we need a real merge. No matter what strategy
1677 * we use, it would operate on the index, possibly affecting the
1678 * working tree, and when resolved cleanly, have the desired
1679 * tree in the index -- this means that the index must be in
1680 * sync with the head commit. The strategies are responsible
1683 * Stash away the local changes so that we can try more than one
1684 * and/or recover from merge strategies bailing while leaving the
1685 * index and working tree polluted.
1687 if (save_state(&stash
))
1690 for (i
= 0; i
< use_strategies_nr
; i
++) {
1693 printf(_("Rewinding the tree to pristine...\n"));
1694 restore_state(&head_commit
->object
.oid
, &stash
);
1696 if (use_strategies_nr
!= 1)
1697 printf(_("Trying merge strategy %s...\n"),
1698 use_strategies
[i
]->name
);
1700 * Remember which strategy left the state in the working
1703 wt_strategy
= use_strategies
[i
]->name
;
1705 ret
= try_merge_strategy(wt_strategy
,
1706 common
, remoteheads
,
1709 * The backend exits with 1 when conflicts are
1710 * left to be resolved, with 2 when it does not
1711 * handle the given merge at all.
1716 * This strategy worked; no point in trying
1720 best_strategy
= wt_strategy
;
1723 cnt
= (use_strategies_nr
> 1) ? evaluate_result() : 0;
1724 if (best_cnt
<= 0 || cnt
<= best_cnt
) {
1725 best_strategy
= wt_strategy
;
1732 * If we have a resulting tree, that means the strategy module
1733 * auto resolved the merge cleanly.
1735 if (merge_was_ok
&& option_commit
) {
1736 automerge_was_ok
= 1;
1737 ret
= finish_automerge(head_commit
, head_subsumed
,
1738 common
, remoteheads
,
1739 &result_tree
, wt_strategy
);
1744 * Pick the result from the best strategy and have the user fix
1747 if (!best_strategy
) {
1748 restore_state(&head_commit
->object
.oid
, &stash
);
1749 if (use_strategies_nr
> 1)
1751 _("No merge strategy handled the merge.\n"));
1753 fprintf(stderr
, _("Merge with strategy %s failed.\n"),
1754 use_strategies
[0]->name
);
1755 apply_autostash(git_path_merge_autostash(the_repository
));
1758 } else if (best_strategy
== wt_strategy
)
1759 ; /* We already have its result in the working tree. */
1761 printf(_("Rewinding the tree to pristine...\n"));
1762 restore_state(&head_commit
->object
.oid
, &stash
);
1763 printf(_("Using the %s strategy to prepare resolving by hand.\n"),
1765 try_merge_strategy(best_strategy
, common
, remoteheads
,
1770 finish(head_commit
, remoteheads
, NULL
, NULL
);
1772 git_test_write_commit_graph_or_die();
1774 write_merge_state(remoteheads
);
1777 fprintf(stderr
, _("Automatic merge went well; "
1778 "stopped before committing as requested\n"));
1780 ret
= suggest_conflicts();
1782 printf(_("When finished, apply stashed changes with `git stash pop`\n"));
1785 if (!automerge_was_ok
) {
1786 free_commit_list(common
);
1787 free_commit_list(remoteheads
);
1789 strbuf_release(&buf
);
1790 free(branch_to_free
);
1791 discard_index(&the_index
);