diff options
Diffstat (limited to 'builtin/merge.c')
-rw-r--r-- | builtin/merge.c | 207 |
1 files changed, 86 insertions, 121 deletions
diff --git a/builtin/merge.c b/builtin/merge.c index 848a298556..23c53a3082 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -7,6 +7,7 @@ */ #include "cache.h" +#include "config.h" #include "parse-options.h" #include "builtin.h" #include "lockfile.h" @@ -44,7 +45,6 @@ struct strategy { static const char * const builtin_merge_usage[] = { N_("git merge [<options>] [<commit>...]"), - N_("git merge [<options>] <msg> HEAD <commit>"), N_("git merge --abort"), N_("git merge --continue"), NULL @@ -244,7 +244,7 @@ static void drop_save(void) unlink(git_path_merge_mode()); } -static int save_state(unsigned char *stash) +static int save_state(struct object_id *stash) { int len; struct child_process cp = CHILD_PROCESS_INIT; @@ -265,7 +265,7 @@ static int save_state(unsigned char *stash) else if (!len) /* no changes */ return -1; strbuf_setlen(&buffer, buffer.len-1); - if (get_sha1(buffer.buf, stash)) + if (get_oid(buffer.buf, stash)) die(_("not a valid object: %s"), buffer.buf); return 0; } @@ -305,18 +305,18 @@ static void reset_hard(unsigned const char *sha1, int verbose) die(_("read-tree failed")); } -static void restore_state(const unsigned char *head, - const unsigned char *stash) +static void restore_state(const struct object_id *head, + const struct object_id *stash) { struct strbuf sb = STRBUF_INIT; const char *args[] = { "stash", "apply", NULL, NULL }; - if (is_null_sha1(stash)) + if (is_null_oid(stash)) return; - reset_hard(head, 1); + reset_hard(head->hash, 1); - args[2] = sha1_to_hex(stash); + args[2] = oid_to_hex(stash); /* * It is OK to ignore error here, for example when there was @@ -376,10 +376,10 @@ static void squash_message(struct commit *commit, struct commit_list *remotehead static void finish(struct commit *head_commit, struct commit_list *remoteheads, - const unsigned char *new_head, const char *msg) + const struct object_id *new_head, const char *msg) { struct strbuf reflog_message = STRBUF_INIT; - const unsigned char *head = head_commit->object.oid.hash; + const struct object_id *head = &head_commit->object.oid; if (!msg) strbuf_addstr(&reflog_message, getenv("GIT_REFLOG_ACTION")); @@ -397,7 +397,7 @@ static void finish(struct commit *head_commit, else { const char *argv_gc_auto[] = { "gc", "--auto", NULL }; update_ref(reflog_message.buf, "HEAD", - new_head, head, 0, + new_head->hash, head->hash, 0, UPDATE_REFS_DIE_ON_ERR); /* * We ignore errors in 'gc --auto', since the @@ -416,7 +416,7 @@ static void finish(struct commit *head_commit, DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT; opts.detect_rename = DIFF_DETECT_RENAME; diff_setup_done(&opts); - diff_tree_sha1(head, new_head, "", &opts); + diff_tree_oid(head, new_head, "", &opts); diffcore_std(&opts); diff_flush(&opts); } @@ -431,7 +431,7 @@ static void finish(struct commit *head_commit, static void merge_name(const char *remote, struct strbuf *msg) { struct commit *remote_head; - unsigned char branch_head[20]; + struct object_id branch_head; struct strbuf buf = STRBUF_INIT; struct strbuf bname = STRBUF_INIT; const char *ptr; @@ -441,25 +441,25 @@ static void merge_name(const char *remote, struct strbuf *msg) strbuf_branchname(&bname, remote, 0); remote = bname.buf; - memset(branch_head, 0, sizeof(branch_head)); + oidclr(&branch_head); remote_head = get_merge_parent(remote); if (!remote_head) die(_("'%s' does not point to a commit"), remote); - if (dwim_ref(remote, strlen(remote), branch_head, &found_ref) > 0) { + if (dwim_ref(remote, strlen(remote), branch_head.hash, &found_ref) > 0) { if (starts_with(found_ref, "refs/heads/")) { strbuf_addf(msg, "%s\t\tbranch '%s' of .\n", - sha1_to_hex(branch_head), remote); + oid_to_hex(&branch_head), remote); goto cleanup; } if (starts_with(found_ref, "refs/tags/")) { strbuf_addf(msg, "%s\t\ttag '%s' of .\n", - sha1_to_hex(branch_head), remote); + oid_to_hex(&branch_head), remote); goto cleanup; } if (starts_with(found_ref, "refs/remotes/")) { strbuf_addf(msg, "%s\t\tremote-tracking branch '%s' of .\n", - sha1_to_hex(branch_head), remote); + oid_to_hex(&branch_head), remote); goto cleanup; } } @@ -537,7 +537,7 @@ static void parse_branch_merge_options(char *bmo) die(_("Bad branch.%s.mergeoptions string: %s"), branch, split_cmdline_strerror(argc)); REALLOC_ARRAY(argv, argc + 2); - memmove(argv + 1, argv, sizeof(*argv) * (argc + 1)); + MOVE_ARRAY(argv + 1, argv, argc + 1); argc++; argv[0] = "branch.*.mergeoptions"; parse_options(argc, argv, NULL, builtin_merge_options, @@ -590,8 +590,8 @@ static int git_merge_config(const char *k, const char *v, void *cb) return git_diff_ui_config(k, v, cb); } -static int read_tree_trivial(unsigned char *common, unsigned char *head, - unsigned char *one) +static int read_tree_trivial(struct object_id *common, struct object_id *head, + struct object_id *one) { int i, nr_trees = 0; struct tree *trees[MAX_UNPACK_TREES]; @@ -626,17 +626,18 @@ static int read_tree_trivial(unsigned char *common, unsigned char *head, return 0; } -static void write_tree_trivial(unsigned char *sha1) +static void write_tree_trivial(struct object_id *oid) { - if (write_cache_as_tree(sha1, 0, NULL)) + if (write_cache_as_tree(oid->hash, 0, NULL)) die(_("git write-tree failed to write a tree")); } static int try_merge_strategy(const char *strategy, struct commit_list *common, struct commit_list *remoteheads, - struct commit *head, const char *head_arg) + struct commit *head) { static struct lock_file lock; + const char *head_arg = "HEAD"; hold_locked_index(&lock, LOCK_DIE_ON_ERROR); refresh_cache(REFRESH_QUIET); @@ -755,13 +756,17 @@ N_("Please enter a commit message to explain why this merge is necessary,\n" "Lines starting with '%c' will be ignored, and an empty message aborts\n" "the commit.\n"); +static void write_merge_heads(struct commit_list *); static void prepare_to_commit(struct commit_list *remoteheads) { struct strbuf msg = STRBUF_INIT; strbuf_addbuf(&msg, &merge_msg); strbuf_addch(&msg, '\n'); + if (squash) + BUG("the control must not reach here under --squash"); if (0 < option_edit) strbuf_commented_addf(&msg, _(merge_editor_comment), comment_line_char); + write_merge_heads(remoteheads); write_file_buf(git_path_merge_msg(), msg.buf, msg.len); if (run_commit_hook(0 < option_edit, get_index_file(), "prepare-commit-msg", git_path_merge_msg(), "merge", NULL)) @@ -781,7 +786,7 @@ static void prepare_to_commit(struct commit_list *remoteheads) static int merge_trivial(struct commit *head, struct commit_list *remoteheads) { - unsigned char result_tree[20], result_commit[20]; + struct object_id result_tree, result_commit; struct commit_list *parents, **pptr = &parents; static struct lock_file lock; @@ -792,15 +797,15 @@ static int merge_trivial(struct commit *head, struct commit_list *remoteheads) return error(_("Unable to write index.")); rollback_lock_file(&lock); - write_tree_trivial(result_tree); + write_tree_trivial(&result_tree); printf(_("Wonderful.\n")); pptr = commit_list_append(head, pptr); pptr = commit_list_append(remoteheads->item, pptr); prepare_to_commit(remoteheads); - if (commit_tree(merge_msg.buf, merge_msg.len, result_tree, parents, - result_commit, NULL, sign_commit)) + if (commit_tree(merge_msg.buf, merge_msg.len, result_tree.hash, parents, + result_commit.hash, NULL, sign_commit)) die(_("failed to write commit object")); - finish(head, remoteheads, result_commit, "In-index merge"); + finish(head, remoteheads, &result_commit, "In-index merge"); drop_save(); return 0; } @@ -809,12 +814,12 @@ static int finish_automerge(struct commit *head, int head_subsumed, struct commit_list *common, struct commit_list *remoteheads, - unsigned char *result_tree, + struct object_id *result_tree, const char *wt_strategy) { struct commit_list *parents = NULL; struct strbuf buf = STRBUF_INIT; - unsigned char result_commit[20]; + struct object_id result_commit; free_commit_list(common); parents = remoteheads; @@ -822,11 +827,11 @@ static int finish_automerge(struct commit *head, commit_list_insert(head, &parents); strbuf_addch(&merge_msg, '\n'); prepare_to_commit(remoteheads); - if (commit_tree(merge_msg.buf, merge_msg.len, result_tree, parents, - result_commit, NULL, sign_commit)) + if (commit_tree(merge_msg.buf, merge_msg.len, result_tree->hash, parents, + result_commit.hash, NULL, sign_commit)) die(_("failed to write commit object")); strbuf_addf(&buf, "Merge made by the '%s' strategy.", wt_strategy); - finish(head, remoteheads, result_commit, buf.buf); + finish(head, remoteheads, &result_commit, buf.buf); strbuf_release(&buf); drop_save(); return 0; @@ -839,9 +844,7 @@ static int suggest_conflicts(void) struct strbuf msgbuf = STRBUF_INIT; filename = git_path_merge_msg(); - fp = fopen(filename, "a"); - if (!fp) - die_errno(_("Could not open '%s' for writing"), filename); + fp = xfopen(filename, "a"); append_conflicts_hint(&msgbuf); fputs(msgbuf.buf, fp); @@ -853,24 +856,6 @@ static int suggest_conflicts(void) return 1; } -static struct commit *is_old_style_invocation(int argc, const char **argv, - const unsigned char *head) -{ - struct commit *second_token = NULL; - if (argc > 2) { - unsigned char second_sha1[20]; - - if (get_sha1(argv[1], second_sha1)) - return NULL; - second_token = lookup_commit_reference_gently(second_sha1, 0); - if (!second_token) - die(_("'%s' is not a commit"), argv[1]); - if (hashcmp(second_token->object.oid.hash, head)) - return NULL; - } - return second_token; -} - static int evaluate_result(void) { int cnt = 0; @@ -923,7 +908,7 @@ static int setup_with_upstream(const char ***argv) return i; } -static void write_merge_state(struct commit_list *remoteheads) +static void write_merge_heads(struct commit_list *remoteheads) { struct commit_list *j; struct strbuf buf = STRBUF_INIT; @@ -939,8 +924,6 @@ static void write_merge_state(struct commit_list *remoteheads) strbuf_addf(&buf, "%s\n", oid_to_hex(oid)); } write_file_buf(git_path_merge_head(), buf.buf, buf.len); - strbuf_addch(&merge_msg, '\n'); - write_file_buf(git_path_merge_msg(), merge_msg.buf, merge_msg.len); strbuf_reset(&buf); if (fast_forward == FF_NO) @@ -948,6 +931,13 @@ static void write_merge_state(struct commit_list *remoteheads) write_file_buf(git_path_merge_mode(), buf.buf, buf.len); } +static void write_merge_state(struct commit_list *remoteheads) +{ + write_merge_heads(remoteheads); + strbuf_addch(&merge_msg, '\n'); + write_file_buf(git_path_merge_msg(), merge_msg.buf, merge_msg.len); +} + static int default_edit_option(void) { static const char name[] = "GIT_MERGE_AUTOEDIT"; @@ -1038,7 +1028,7 @@ static void handle_fetch_head(struct commit_list **remotes, struct strbuf *merge die_errno(_("could not close '%s'"), filename); for (pos = 0; pos < merge_names->len; pos = npos) { - unsigned char sha1[20]; + struct object_id oid; char *ptr; struct commit *commit; @@ -1048,16 +1038,16 @@ static void handle_fetch_head(struct commit_list **remotes, struct strbuf *merge else npos = merge_names->len; - if (npos - pos < 40 + 2 || - get_sha1_hex(merge_names->buf + pos, sha1)) + if (npos - pos < GIT_SHA1_HEXSZ + 2 || + get_oid_hex(merge_names->buf + pos, &oid)) commit = NULL; /* bad */ - else if (memcmp(merge_names->buf + pos + 40, "\t\t", 2)) + else if (memcmp(merge_names->buf + pos + GIT_SHA1_HEXSZ, "\t\t", 2)) continue; /* not-for-merge */ else { - char saved = merge_names->buf[pos + 40]; - merge_names->buf[pos + 40] = '\0'; + char saved = merge_names->buf[pos + GIT_SHA1_HEXSZ]; + merge_names->buf[pos + GIT_SHA1_HEXSZ] = '\0'; commit = get_merge_parent(merge_names->buf + pos); - merge_names->buf[pos + 40] = saved; + merge_names->buf[pos + GIT_SHA1_HEXSZ] = saved; } if (!commit) { if (ptr) @@ -1117,12 +1107,9 @@ static struct commit_list *collect_parents(struct commit *head_commit, int cmd_merge(int argc, const char **argv, const char *prefix) { - unsigned char result_tree[20]; - unsigned char stash[20]; - unsigned char head_sha1[20]; + struct object_id result_tree, stash, head_oid; struct commit *head_commit; struct strbuf buf = STRBUF_INIT; - const char *head_arg; int i, ret = 0, head_subsumed; int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0; struct commit_list *common = NULL; @@ -1138,13 +1125,13 @@ int cmd_merge(int argc, const char **argv, const char *prefix) * Check if we are _not_ on a detached HEAD, i.e. if there is a * current branch. */ - branch = branch_to_free = resolve_refdup("HEAD", 0, head_sha1, NULL); - if (branch && starts_with(branch, "refs/heads/")) - branch += 11; - if (!branch || is_null_sha1(head_sha1)) + branch = branch_to_free = resolve_refdup("HEAD", 0, head_oid.hash, NULL); + if (branch) + skip_prefix(branch, "refs/heads/", &branch); + if (!branch || is_null_oid(&head_oid)) head_commit = NULL; else - head_commit = lookup_commit_or_die(head_sha1, "HEAD"); + head_commit = lookup_commit_or_die(&head_oid, "HEAD"); init_diff_ui_defaults(); git_config(git_merge_config, NULL); @@ -1242,7 +1229,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix) * to forbid "git merge" into a branch yet to be born. * We do the same for "git pull". */ - unsigned char *remote_head_sha1; + struct object_id *remote_head_oid; if (squash) die(_("Squash commit into empty head not supported yet")); if (fast_forward == FF_NO) @@ -1254,42 +1241,20 @@ int cmd_merge(int argc, const char **argv, const char *prefix) die(_("%s - not something we can merge"), argv[0]); if (remoteheads->next) die(_("Can merge only exactly one commit into empty head")); - remote_head_sha1 = remoteheads->item->object.oid.hash; - read_empty(remote_head_sha1, 0); - update_ref("initial pull", "HEAD", remote_head_sha1, + remote_head_oid = &remoteheads->item->object.oid; + read_empty(remote_head_oid->hash, 0); + update_ref("initial pull", "HEAD", remote_head_oid->hash, NULL, 0, UPDATE_REFS_DIE_ON_ERR); goto done; } /* - * This could be traditional "merge <msg> HEAD <commit>..." and - * the way we can tell it is to see if the second token is HEAD, - * but some people might have misused the interface and used a - * commit-ish that is the same as HEAD there instead. - * Traditional format never would have "-m" so it is an - * additional safety measure to check for it. + * All the rest are the commits being merged; prepare + * the standard merge summary message to be appended + * to the given message. */ - if (!have_message && - is_old_style_invocation(argc, argv, head_commit->object.oid.hash)) { - warning("old-style 'git merge <msg> HEAD <commit>' is deprecated."); - strbuf_addstr(&merge_msg, argv[0]); - head_arg = argv[1]; - argv += 2; - argc -= 2; - remoteheads = collect_parents(head_commit, &head_subsumed, - argc, argv, NULL); - } else { - /* We are invoked directly as the first-class UI. */ - head_arg = "HEAD"; - - /* - * 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); - } + remoteheads = collect_parents(head_commit, &head_subsumed, + argc, argv, &merge_msg); if (!head_commit || !argc) usage_with_options(builtin_merge_usage, @@ -1298,7 +1263,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix) if (verify_signatures) { for (p = remoteheads; p; p = p->next) { struct commit *commit = p->item; - char hex[GIT_SHA1_HEXSZ + 1]; + char hex[GIT_MAX_HEXSZ + 1]; struct signature_check signature_check; memset(&signature_check, 0, sizeof(signature_check)); @@ -1415,14 +1380,14 @@ int cmd_merge(int argc, const char **argv, const char *prefix) goto done; } - if (checkout_fast_forward(head_commit->object.oid.hash, - commit->object.oid.hash, + if (checkout_fast_forward(&head_commit->object.oid, + &commit->object.oid, overwrite_ignore)) { ret = 1; goto done; } - finish(head_commit, remoteheads, commit->object.oid.hash, msg.buf); + finish(head_commit, remoteheads, &commit->object.oid, msg.buf); drop_save(); goto done; } else if (!remoteheads->next && common->next) @@ -1441,9 +1406,9 @@ int cmd_merge(int argc, const char **argv, const char *prefix) /* 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.hash, - head_commit->object.oid.hash, - remoteheads->item->object.oid.hash)) { + if (!read_tree_trivial(&common->item->object.oid, + &head_commit->object.oid, + &remoteheads->item->object.oid)) { ret = merge_trivial(head_commit, remoteheads); goto done; } @@ -1495,14 +1460,14 @@ int cmd_merge(int argc, const char **argv, const char *prefix) /* * Stash away the local changes so that we can try more than one. */ - save_state(stash)) - hashclr(stash); + save_state(&stash)) + oidclr(&stash); for (i = 0; i < use_strategies_nr; i++) { int ret; if (i) { printf(_("Rewinding the tree to pristine...\n")); - restore_state(head_commit->object.oid.hash, stash); + restore_state(&head_commit->object.oid, &stash); } if (use_strategies_nr != 1) printf(_("Trying merge strategy %s...\n"), @@ -1515,7 +1480,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix) ret = try_merge_strategy(use_strategies[i]->name, common, remoteheads, - head_commit, head_arg); + head_commit); if (!option_commit && !ret) { merge_was_ok = 1; /* @@ -1547,7 +1512,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix) } /* Automerge succeeded. */ - write_tree_trivial(result_tree); + write_tree_trivial(&result_tree); automerge_was_ok = 1; break; } @@ -1559,7 +1524,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix) if (automerge_was_ok) { ret = finish_automerge(head_commit, head_subsumed, common, remoteheads, - result_tree, wt_strategy); + &result_tree, wt_strategy); goto done; } @@ -1568,7 +1533,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix) * it up. */ if (!best_strategy) { - restore_state(head_commit->object.oid.hash, stash); + restore_state(&head_commit->object.oid, &stash); if (use_strategies_nr > 1) fprintf(stderr, _("No merge strategy handled the merge.\n")); @@ -1581,11 +1546,11 @@ int cmd_merge(int argc, const char **argv, const char *prefix) ; /* We already have its result in the working tree. */ else { printf(_("Rewinding the tree to pristine...\n")); - restore_state(head_commit->object.oid.hash, stash); + restore_state(&head_commit->object.oid, &stash); printf(_("Using the %s to prepare resolving by hand.\n"), best_strategy); try_merge_strategy(best_strategy, common, remoteheads, - head_commit, head_arg); + head_commit); } if (squash) |