in builtin/merge.c [1304:1830]
int cmd_merge(int argc,
const char **argv,
const char *prefix,
struct repository *repo UNUSED)
{
struct object_id result_tree, stash, head_oid;
struct commit *head_commit;
struct strbuf buf = STRBUF_INIT;
int i, ret = 0, head_subsumed;
int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0;
struct commit_list *common = NULL;
const char *best_strategy = NULL, *wt_strategy = NULL;
struct commit_list *remoteheads = NULL, *p;
void *branch_to_free;
int orig_argc = argc;
show_usage_with_options_if_asked(argc, argv,
builtin_merge_usage, builtin_merge_options);
prepare_repo_settings(the_repository);
the_repository->settings.command_requires_full_index = 0;
/*
* Check if we are _not_ on a detached HEAD, i.e. if there is a
* current branch.
*/
branch = branch_to_free = refs_resolve_refdup(get_main_ref_store(the_repository),
"HEAD", 0, &head_oid,
NULL);
if (branch)
skip_prefix(branch, "refs/heads/", &branch);
init_diff_ui_defaults();
git_config(git_merge_config, NULL);
if (!branch || is_null_oid(&head_oid))
head_commit = NULL;
else
head_commit = lookup_commit_or_die(&head_oid, "HEAD");
if (branch_mergeoptions)
parse_branch_merge_options(branch_mergeoptions);
argc = parse_options(argc, argv, prefix, builtin_merge_options,
builtin_merge_usage, 0);
if (shortlog_len < 0)
shortlog_len = (merge_log_config > 0) ? merge_log_config : 0;
if (verbosity < 0 && show_progress == -1)
show_progress = 0;
if (abort_current_merge) {
int nargc = 2;
const char *nargv[] = {"reset", "--merge", NULL};
char stash_oid_hex[GIT_MAX_HEXSZ + 1];
struct object_id stash_oid = {0};
if (orig_argc != 2)
usage_msg_opt(_("--abort expects no arguments"),
builtin_merge_usage, builtin_merge_options);
if (!file_exists(git_path_merge_head(the_repository)))
die(_("There is no merge to abort (MERGE_HEAD missing)."));
if (!refs_read_ref(get_main_ref_store(the_repository), "MERGE_AUTOSTASH", &stash_oid))
refs_delete_ref(get_main_ref_store(the_repository),
"", "MERGE_AUTOSTASH", &stash_oid,
REF_NO_DEREF);
/* Invoke 'git reset --merge' */
ret = cmd_reset(nargc, nargv, prefix, the_repository);
if (!is_null_oid(&stash_oid)) {
oid_to_hex_r(stash_oid_hex, &stash_oid);
apply_autostash_oid(stash_oid_hex);
}
goto done;
}
if (quit_current_merge) {
if (orig_argc != 2)
usage_msg_opt(_("--quit expects no arguments"),
builtin_merge_usage,
builtin_merge_options);
remove_merge_branch_state(the_repository);
goto done;
}
if (continue_current_merge) {
int nargc = 1;
const char *nargv[] = {"commit", NULL};
if (orig_argc != 2)
usage_msg_opt(_("--continue expects no arguments"),
builtin_merge_usage, builtin_merge_options);
if (!file_exists(git_path_merge_head(the_repository)))
die(_("There is no merge in progress (MERGE_HEAD missing)."));
/* Invoke 'git commit' */
ret = cmd_commit(nargc, nargv, prefix, the_repository);
goto done;
}
if (repo_read_index_unmerged(the_repository))
die_resolve_conflict("merge");
if (file_exists(git_path_merge_head(the_repository))) {
/*
* There is no unmerged entry, don't advise 'git
* add/rm <file>', just 'git commit'.
*/
if (advice_enabled(ADVICE_RESOLVE_CONFLICT))
die(_("You have not concluded your merge (MERGE_HEAD exists).\n"
"Please, commit your changes before you merge."));
else
die(_("You have not concluded your merge (MERGE_HEAD exists)."));
}
if (refs_ref_exists(get_main_ref_store(the_repository), "CHERRY_PICK_HEAD")) {
if (advice_enabled(ADVICE_RESOLVE_CONFLICT))
die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
"Please, commit your changes before you merge."));
else
die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists)."));
}
resolve_undo_clear_index(the_repository->index);
if (option_edit < 0)
option_edit = default_edit_option();
cleanup_mode = get_cleanup_mode(cleanup_arg, 0 < option_edit);
if (verbosity < 0)
show_diffstat = 0;
if (squash) {
if (fast_forward == FF_NO)
die(_("options '%s' and '%s' cannot be used together"), "--squash", "--no-ff.");
if (option_commit > 0)
die(_("options '%s' and '%s' cannot be used together"), "--squash", "--commit.");
/*
* squash can now silently disable option_commit - this is not
* a problem as it is only overriding the default, not a user
* supplied option.
*/
option_commit = 0;
}
if (option_commit < 0)
option_commit = 1;
if (!argc) {
if (default_to_upstream)
argc = setup_with_upstream(&argv);
else
die(_("No commit specified and merge.defaultToUpstream not set."));
} else if (argc == 1 && !strcmp(argv[0], "-")) {
argv[0] = "@{-1}";
}
if (!argc)
usage_with_options(builtin_merge_usage,
builtin_merge_options);
if (!head_commit) {
/*
* If the merged head is a valid one there is no reason
* to forbid "git merge" into a branch yet to be born.
* We do the same for "git pull".
*/
struct object_id *remote_head_oid;
if (squash)
die(_("Squash commit into empty head not supported yet"));
if (fast_forward == FF_NO)
die(_("Non-fast-forward commit does not make sense into "
"an empty head"));
remoteheads = collect_parents(head_commit, &head_subsumed,
argc, argv, NULL);
if (!remoteheads)
die(_("%s - not something we can merge"), argv[0]);
if (remoteheads->next)
die(_("Can merge only exactly one commit into empty head"));
if (verify_signatures)
verify_merge_signature(remoteheads->item, verbosity,
check_trust_level);
remote_head_oid = &remoteheads->item->object.oid;
read_empty(remote_head_oid);
refs_update_ref(get_main_ref_store(the_repository),
"initial pull", "HEAD", remote_head_oid, NULL,
0,
UPDATE_REFS_DIE_ON_ERR);
goto done;
}
/*
* All the rest are the commits being merged; prepare
* the standard merge summary message to be appended
* to the given message.
*/
remoteheads = collect_parents(head_commit, &head_subsumed,
argc, argv, &merge_msg);
if (!head_commit || !argc)
usage_with_options(builtin_merge_usage,
builtin_merge_options);
if (verify_signatures) {
for (p = remoteheads; p; p = p->next) {
verify_merge_signature(p->item, verbosity,
check_trust_level);
}
}
strbuf_addstr(&buf, "merge");
for (p = remoteheads; p; p = p->next)
strbuf_addf(&buf, " %s", merge_remote_util(p->item)->name);
setenv("GIT_REFLOG_ACTION", buf.buf, 0);
strbuf_reset(&buf);
for (p = remoteheads; p; p = p->next) {
struct commit *commit = p->item;
strbuf_addf(&buf, "GITHEAD_%s",
oid_to_hex(&commit->object.oid));
setenv(buf.buf, merge_remote_util(commit)->name, 1);
strbuf_reset(&buf);
if (fast_forward != FF_ONLY && merging_a_throwaway_tag(commit))
fast_forward = FF_NO;
}
if (!use_strategies) {
if (!remoteheads)
; /* already up-to-date */
else if (!remoteheads->next)
add_strategies(pull_twohead, DEFAULT_TWOHEAD);
else
add_strategies(pull_octopus, DEFAULT_OCTOPUS);
}
for (i = 0; i < use_strategies_nr; i++) {
if (use_strategies[i]->attr & NO_FAST_FORWARD)
fast_forward = FF_NO;
if (use_strategies[i]->attr & NO_TRIVIAL)
allow_trivial = 0;
}
if (!remoteheads)
; /* already up-to-date */
else if (!remoteheads->next) {
if (repo_get_merge_bases(the_repository, head_commit,
remoteheads->item, &common) < 0) {
ret = 2;
goto done;
}
} else {
struct commit_list *list = remoteheads;
commit_list_insert(head_commit, &list);
if (get_octopus_merge_bases(list, &common) < 0) {
free(list);
ret = 2;
goto done;
}
free(list);
}
refs_update_ref(get_main_ref_store(the_repository),
"updating ORIG_HEAD", "ORIG_HEAD",
&head_commit->object.oid, NULL, 0,
UPDATE_REFS_DIE_ON_ERR);
if (remoteheads && !common) {
/* No common ancestors found. */
if (!allow_unrelated_histories)
die(_("refusing to merge unrelated histories"));
/* otherwise, we need a real merge. */
} else if (!remoteheads ||
(!remoteheads->next && !common->next &&
common->item == remoteheads->item)) {
/*
* If head can reach all the merge then we are up to date.
* but first the most common case of merging one remote.
*/
finish_up_to_date();
goto done;
} else if (fast_forward != FF_NO && !remoteheads->next &&
!common->next &&
oideq(&common->item->object.oid, &head_commit->object.oid)) {
/* Again the most common case of merging one remote. */
const char *msg = have_message ?
"Fast-forward (no commit created; -m option ignored)" :
"Fast-forward";
struct commit *commit;
if (verbosity >= 0) {
printf(_("Updating %s..%s\n"),
repo_find_unique_abbrev(the_repository, &head_commit->object.oid,
DEFAULT_ABBREV),
repo_find_unique_abbrev(the_repository, &remoteheads->item->object.oid,
DEFAULT_ABBREV));
}
commit = remoteheads->item;
if (!commit) {
ret = 1;
goto done;
}
if (autostash)
create_autostash_ref(the_repository, "MERGE_AUTOSTASH");
if (checkout_fast_forward(the_repository,
&head_commit->object.oid,
&commit->object.oid,
overwrite_ignore)) {
apply_autostash_ref(the_repository, "MERGE_AUTOSTASH");
ret = 1;
goto done;
}
finish(head_commit, remoteheads, &commit->object.oid, msg);
remove_merge_branch_state(the_repository);
goto done;
} else if (!remoteheads->next && common->next)
;
/*
* We are not doing octopus and not fast-forward. Need
* a real merge.
*/
else if (!remoteheads->next && !common->next && option_commit) {
/*
* We are not doing octopus, not fast-forward, and have
* only one common.
*/
refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL);
if (allow_trivial && fast_forward != FF_ONLY) {
/*
* Must first ensure that index matches HEAD before
* attempting a trivial merge.
*/
struct tree *head_tree = repo_get_commit_tree(the_repository,
head_commit);
struct strbuf sb = STRBUF_INIT;
if (repo_index_has_changes(the_repository, head_tree,
&sb)) {
error(_("Your local changes to the following files would be overwritten by merge:\n %s"),
sb.buf);
strbuf_release(&sb);
ret = 2;
goto done;
}
/* See if it is really trivial. */
git_committer_info(IDENT_STRICT);
printf(_("Trying really trivial in-index merge...\n"));
if (!read_tree_trivial(&common->item->object.oid,
&head_commit->object.oid,
&remoteheads->item->object.oid)) {
ret = merge_trivial(head_commit, remoteheads);
goto done;
}
printf(_("Nope.\n"));
}
} else {
/*
* An octopus. If we can reach all the remote we are up
* to date.
*/
int up_to_date = 1;
struct commit_list *j;
for (j = remoteheads; j; j = j->next) {
struct commit_list *common_one = NULL;
struct commit *common_item;
/*
* Here we *have* to calculate the individual
* merge_bases again, otherwise "git merge HEAD^
* HEAD^^" would be missed.
*/
if (repo_get_merge_bases(the_repository, head_commit,
j->item, &common_one) < 0)
exit(128);
common_item = common_one->item;
free_commit_list(common_one);
if (!oideq(&common_item->object.oid, &j->item->object.oid)) {
up_to_date = 0;
break;
}
}
if (up_to_date) {
finish_up_to_date();
goto done;
}
}
if (fast_forward == FF_ONLY)
die_ff_impossible();
if (autostash)
create_autostash_ref(the_repository, "MERGE_AUTOSTASH");
/* We are going to make a new commit. */
git_committer_info(IDENT_STRICT);
/*
* At this point, we need a real merge. No matter what strategy
* we use, it would operate on the index, possibly affecting the
* working tree, and when resolved cleanly, have the desired
* tree in the index -- this means that the index must be in
* sync with the head commit. The strategies are responsible
* to ensure this.
*
* Stash away the local changes so that we can try more than one
* and/or recover from merge strategies bailing while leaving the
* index and working tree polluted.
*/
if (save_state(&stash))
oidclr(&stash, the_repository->hash_algo);
for (i = 0; i < use_strategies_nr; i++) {
int ret, cnt;
if (i) {
printf(_("Rewinding the tree to pristine...\n"));
restore_state(&head_commit->object.oid, &stash);
}
if (use_strategies_nr != 1)
printf(_("Trying merge strategy %s...\n"),
use_strategies[i]->name);
/*
* Remember which strategy left the state in the working
* tree.
*/
wt_strategy = use_strategies[i]->name;
ret = try_merge_strategy(wt_strategy,
common, remoteheads,
head_commit);
/*
* The backend exits with 1 when conflicts are
* left to be resolved, with 2 when it does not
* handle the given merge at all.
*/
if (ret < 2) {
if (!ret) {
/*
* This strategy worked; no point in trying
* another.
*/
merge_was_ok = 1;
best_strategy = wt_strategy;
break;
}
cnt = (use_strategies_nr > 1) ? evaluate_result() : 0;
if (best_cnt <= 0 || cnt <= best_cnt) {
best_strategy = wt_strategy;
best_cnt = cnt;
}
}
}
/*
* If we have a resulting tree, that means the strategy module
* auto resolved the merge cleanly.
*/
if (merge_was_ok && option_commit) {
automerge_was_ok = 1;
ret = finish_automerge(head_commit, head_subsumed,
common, remoteheads,
&result_tree, wt_strategy);
goto done;
}
/*
* Pick the result from the best strategy and have the user fix
* it up.
*/
if (!best_strategy) {
restore_state(&head_commit->object.oid, &stash);
if (use_strategies_nr > 1)
fprintf(stderr,
_("No merge strategy handled the merge.\n"));
else
fprintf(stderr, _("Merge with strategy %s failed.\n"),
use_strategies[0]->name);
apply_autostash_ref(the_repository, "MERGE_AUTOSTASH");
ret = 2;
goto done;
} else if (best_strategy == wt_strategy)
; /* We already have its result in the working tree. */
else {
printf(_("Rewinding the tree to pristine...\n"));
restore_state(&head_commit->object.oid, &stash);
printf(_("Using the %s strategy to prepare resolving by hand.\n"),
best_strategy);
try_merge_strategy(best_strategy, common, remoteheads,
head_commit);
}
if (squash) {
finish(head_commit, remoteheads, NULL, NULL);
git_test_write_commit_graph_or_die();
} else
write_merge_state(remoteheads);
if (merge_was_ok)
fprintf(stderr, _("Automatic merge went well; "
"stopped before committing as requested\n"));
else
ret = suggest_conflicts();
if (autostash)
printf(_("When finished, apply stashed changes with `git stash pop`\n"));
done:
if (!automerge_was_ok) {
free_commit_list(common);
free_commit_list(remoteheads);
}
strbuf_release(&buf);
free(branch_to_free);
free(pull_twohead);
free(pull_octopus);
discard_index(the_repository->index);
return ret;
}