Merge branch 'ks/fix-rebase-doc-picture' into maint
[gitweb.git] / notes-merge.c
index b3d1dab51fb9e7aea4af05211326a1a91b1279d7..32caaaff7477d3640c81a19c54c1c92ffb8ca8b1 100644 (file)
@@ -12,7 +12,7 @@
 #include "notes-utils.h"
 
 struct notes_merge_pair {
-       unsigned char obj[20], base[20], local[20], remote[20];
+       struct object_id obj, base, local, remote;
 };
 
 void init_notes_merge_options(struct notes_merge_options *o)
@@ -41,14 +41,14 @@ static int verify_notes_filepair(struct diff_filepair *p, unsigned char *sha1)
        switch (p->status) {
        case DIFF_STATUS_MODIFIED:
                assert(p->one->mode == p->two->mode);
-               assert(!is_null_sha1(p->one->sha1));
-               assert(!is_null_sha1(p->two->sha1));
+               assert(!is_null_oid(&p->one->oid));
+               assert(!is_null_oid(&p->two->oid));
                break;
        case DIFF_STATUS_ADDED:
-               assert(is_null_sha1(p->one->sha1));
+               assert(is_null_oid(&p->one->oid));
                break;
        case DIFF_STATUS_DELETED:
-               assert(is_null_sha1(p->two->sha1));
+               assert(is_null_oid(&p->two->oid));
                break;
        default:
                return -1;
@@ -75,7 +75,7 @@ static struct notes_merge_pair *find_notes_merge_pair_pos(
        int i = last_index < len ? last_index : len - 1;
        int prev_cmp = 0, cmp = -1;
        while (i >= 0 && i < len) {
-               cmp = hashcmp(obj, list[i].obj);
+               cmp = hashcmp(obj, list[i].obj.hash);
                if (!cmp) /* obj belongs @ i */
                        break;
                else if (cmp < 0 && prev_cmp <= 0) /* obj belongs < i */
@@ -108,9 +108,10 @@ static struct notes_merge_pair *find_notes_merge_pair_pos(
        return list + i;
 }
 
-static unsigned char uninitialized[20] =
+static struct object_id uninitialized = {
        "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" \
-       "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
+       "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
+};
 
 static struct notes_merge_pair *diff_tree_remote(struct notes_merge_options *o,
                                                 const unsigned char *base,
@@ -142,35 +143,35 @@ static struct notes_merge_pair *diff_tree_remote(struct notes_merge_options *o,
                if (verify_notes_filepair(p, obj)) {
                        trace_printf("\t\tCannot merge entry '%s' (%c): "
                               "%.7s -> %.7s. Skipping!\n", p->one->path,
-                              p->status, sha1_to_hex(p->one->sha1),
-                              sha1_to_hex(p->two->sha1));
+                              p->status, oid_to_hex(&p->one->oid),
+                              oid_to_hex(&p->two->oid));
                        continue;
                }
                mp = find_notes_merge_pair_pos(changes, len, obj, 1, &occupied);
                if (occupied) {
                        /* We've found an addition/deletion pair */
-                       assert(!hashcmp(mp->obj, obj));
-                       if (is_null_sha1(p->one->sha1)) { /* addition */
-                               assert(is_null_sha1(mp->remote));
-                               hashcpy(mp->remote, p->two->sha1);
-                       } else if (is_null_sha1(p->two->sha1)) { /* deletion */
-                               assert(is_null_sha1(mp->base));
-                               hashcpy(mp->base, p->one->sha1);
+                       assert(!hashcmp(mp->obj.hash, obj));
+                       if (is_null_oid(&p->one->oid)) { /* addition */
+                               assert(is_null_oid(&mp->remote));
+                               oidcpy(&mp->remote, &p->two->oid);
+                       } else if (is_null_oid(&p->two->oid)) { /* deletion */
+                               assert(is_null_oid(&mp->base));
+                               oidcpy(&mp->base, &p->one->oid);
                        } else
                                assert(!"Invalid existing change recorded");
                } else {
-                       hashcpy(mp->obj, obj);
-                       hashcpy(mp->base, p->one->sha1);
-                       hashcpy(mp->local, uninitialized);
-                       hashcpy(mp->remote, p->two->sha1);
+                       hashcpy(mp->obj.hash, obj);
+                       oidcpy(&mp->base, &p->one->oid);
+                       oidcpy(&mp->local, &uninitialized);
+                       oidcpy(&mp->remote, &p->two->oid);
                        len++;
                }
                trace_printf("\t\tStored remote change for %s: %.7s -> %.7s\n",
-                      sha1_to_hex(mp->obj), sha1_to_hex(mp->base),
-                      sha1_to_hex(mp->remote));
+                      oid_to_hex(&mp->obj), oid_to_hex(&mp->base),
+                      oid_to_hex(&mp->remote));
        }
        diff_flush(&opt);
-       free_pathspec(&opt.pathspec);
+       clear_pathspec(&opt.pathspec);
 
        *num_changes = len;
        return changes;
@@ -203,21 +204,21 @@ static void diff_tree_local(struct notes_merge_options *o,
                if (verify_notes_filepair(p, obj)) {
                        trace_printf("\t\tCannot merge entry '%s' (%c): "
                               "%.7s -> %.7s. Skipping!\n", p->one->path,
-                              p->status, sha1_to_hex(p->one->sha1),
-                              sha1_to_hex(p->two->sha1));
+                              p->status, oid_to_hex(&p->one->oid),
+                              oid_to_hex(&p->two->oid));
                        continue;
                }
                mp = find_notes_merge_pair_pos(changes, len, obj, 0, &match);
                if (!match) {
                        trace_printf("\t\tIgnoring local-only change for %s: "
                               "%.7s -> %.7s\n", sha1_to_hex(obj),
-                              sha1_to_hex(p->one->sha1),
-                              sha1_to_hex(p->two->sha1));
+                              oid_to_hex(&p->one->oid),
+                              oid_to_hex(&p->two->oid));
                        continue;
                }
 
-               assert(!hashcmp(mp->obj, obj));
-               if (is_null_sha1(p->two->sha1)) { /* deletion */
+               assert(!hashcmp(mp->obj.hash, obj));
+               if (is_null_oid(&p->two->oid)) { /* deletion */
                        /*
                         * Either this is a true deletion (1), or it is part
                         * of an A/D pair (2), or D/A pair (3):
@@ -227,9 +228,9 @@ static void diff_tree_local(struct notes_merge_options *o,
                         * (3) mp->local is uninitialized; set it to null_sha1
                         *     (will be overwritten by following addition)
                         */
-                       if (!hashcmp(mp->local, uninitialized))
-                               hashclr(mp->local);
-               } else if (is_null_sha1(p->one->sha1)) { /* addition */
+                       if (!oidcmp(&mp->local, &uninitialized))
+                               oidclr(&mp->local);
+               } else if (is_null_oid(&p->one->oid)) { /* addition */
                        /*
                         * Either this is a true addition (1), or it is part
                         * of an A/D pair (2), or D/A pair (3):
@@ -238,25 +239,25 @@ static void diff_tree_local(struct notes_merge_options *o,
                         * (2) mp->local is uninitialized; set to p->two->sha1
                         * (3) mp->local is null_sha1;     set to p->two->sha1
                         */
-                       assert(is_null_sha1(mp->local) ||
-                              !hashcmp(mp->local, uninitialized));
-                       hashcpy(mp->local, p->two->sha1);
+                       assert(is_null_oid(&mp->local) ||
+                              !oidcmp(&mp->local, &uninitialized));
+                       oidcpy(&mp->local, &p->two->oid);
                } else { /* modification */
                        /*
                         * This is a true modification. p->one->sha1 shall
                         * match mp->base, and mp->local shall be uninitialized.
                         * Set mp->local to p->two->sha1.
                         */
-                       assert(!hashcmp(p->one->sha1, mp->base));
-                       assert(!hashcmp(mp->local, uninitialized));
-                       hashcpy(mp->local, p->two->sha1);
+                       assert(!oidcmp(&p->one->oid, &mp->base));
+                       assert(!oidcmp(&mp->local, &uninitialized));
+                       oidcpy(&mp->local, &p->two->oid);
                }
                trace_printf("\t\tStored local change for %s: %.7s -> %.7s\n",
-                      sha1_to_hex(mp->obj), sha1_to_hex(mp->base),
-                      sha1_to_hex(mp->local));
+                      oid_to_hex(&mp->obj), oid_to_hex(&mp->base),
+                      oid_to_hex(&mp->local));
        }
        diff_flush(&opt);
-       free_pathspec(&opt.pathspec);
+       clear_pathspec(&opt.pathspec);
 }
 
 static void check_notes_merge_worktree(struct notes_merge_options *o)
@@ -269,15 +270,15 @@ static void check_notes_merge_worktree(struct notes_merge_options *o)
                if (file_exists(git_path(NOTES_MERGE_WORKTREE)) &&
                    !is_empty_dir(git_path(NOTES_MERGE_WORKTREE))) {
                        if (advice_resolve_conflict)
-                               die("You have not concluded your previous "
+                               die(_("You have not concluded your previous "
                                    "notes merge (%s exists).\nPlease, use "
                                    "'git notes merge --commit' or 'git notes "
                                    "merge --abort' to commit/abort the "
                                    "previous merge before you start a new "
-                                   "notes merge.", git_path("NOTES_MERGE_*"));
+                                   "notes merge."), git_path("NOTES_MERGE_*"));
                        else
-                               die("You have not concluded your notes merge "
-                                   "(%s exists).", git_path("NOTES_MERGE_*"));
+                               die(_("You have not concluded your notes merge "
+                                   "(%s exists)."), git_path("NOTES_MERGE_*"));
                }
 
                if (safe_create_leading_directories_const(git_path(
@@ -298,12 +299,8 @@ static void write_buf_to_worktree(const unsigned char *obj,
        char *path = git_pathdup(NOTES_MERGE_WORKTREE "/%s", sha1_to_hex(obj));
        if (safe_create_leading_directories_const(path))
                die_errno("unable to create directory for '%s'", path);
-       if (file_exists(path))
-               die("found existing file at '%s'", path);
 
-       fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0666);
-       if (fd < 0)
-               die_errno("failed to open '%s'", path);
+       fd = xopen(path, O_WRONLY | O_EXCL | O_CREAT, 0666);
 
        while (size > 0) {
                long ret = write_in_full(fd, buf, size);
@@ -347,11 +344,11 @@ static int ll_merge_in_worktree(struct notes_merge_options *o,
        mmfile_t base, local, remote;
        int status;
 
-       read_mmblob(&base, p->base);
-       read_mmblob(&local, p->local);
-       read_mmblob(&remote, p->remote);
+       read_mmblob(&base, &p->base);
+       read_mmblob(&local, &p->local);
+       read_mmblob(&remote, &p->remote);
 
-       status = ll_merge(&result_buf, sha1_to_hex(p->obj), &base, NULL,
+       status = ll_merge(&result_buf, oid_to_hex(&p->obj), &base, NULL,
                          &local, o->local_ref, &remote, o->remote_ref, NULL);
 
        free(base.ptr);
@@ -361,7 +358,7 @@ static int ll_merge_in_worktree(struct notes_merge_options *o,
        if ((status < 0) || !result_buf.ptr)
                die("Failed to execute internal merge");
 
-       write_buf_to_worktree(p->obj, result_buf.ptr, result_buf.size);
+       write_buf_to_worktree(p->obj.hash, result_buf.ptr, result_buf.size);
        free(result_buf.ptr);
 
        return status;
@@ -376,51 +373,52 @@ static int merge_one_change_manual(struct notes_merge_options *o,
 
        trace_printf("\t\t\tmerge_one_change_manual(obj = %.7s, base = %.7s, "
               "local = %.7s, remote = %.7s)\n",
-              sha1_to_hex(p->obj), sha1_to_hex(p->base),
-              sha1_to_hex(p->local), sha1_to_hex(p->remote));
+              oid_to_hex(&p->obj), oid_to_hex(&p->base),
+              oid_to_hex(&p->local), oid_to_hex(&p->remote));
 
        /* add "Conflicts:" section to commit message first time through */
        if (!o->has_worktree)
                strbuf_addstr(&(o->commit_msg), "\n\nConflicts:\n");
 
-       strbuf_addf(&(o->commit_msg), "\t%s\n", sha1_to_hex(p->obj));
+       strbuf_addf(&(o->commit_msg), "\t%s\n", oid_to_hex(&p->obj));
 
        if (o->verbosity >= 2)
-               printf("Auto-merging notes for %s\n", sha1_to_hex(p->obj));
+               printf("Auto-merging notes for %s\n", oid_to_hex(&p->obj));
        check_notes_merge_worktree(o);
-       if (is_null_sha1(p->local)) {
+       if (is_null_oid(&p->local)) {
                /* D/F conflict, checkout p->remote */
-               assert(!is_null_sha1(p->remote));
+               assert(!is_null_oid(&p->remote));
                if (o->verbosity >= 1)
                        printf("CONFLICT (delete/modify): Notes for object %s "
                                "deleted in %s and modified in %s. Version from %s "
                                "left in tree.\n",
-                               sha1_to_hex(p->obj), lref, rref, rref);
-               write_note_to_worktree(p->obj, p->remote);
-       } else if (is_null_sha1(p->remote)) {
+                               oid_to_hex(&p->obj), lref, rref, rref);
+               write_note_to_worktree(p->obj.hash, p->remote.hash);
+       } else if (is_null_oid(&p->remote)) {
                /* D/F conflict, checkout p->local */
-               assert(!is_null_sha1(p->local));
+               assert(!is_null_oid(&p->local));
                if (o->verbosity >= 1)
                        printf("CONFLICT (delete/modify): Notes for object %s "
                                "deleted in %s and modified in %s. Version from %s "
                                "left in tree.\n",
-                               sha1_to_hex(p->obj), rref, lref, lref);
-               write_note_to_worktree(p->obj, p->local);
+                               oid_to_hex(&p->obj), rref, lref, lref);
+               write_note_to_worktree(p->obj.hash, p->local.hash);
        } else {
                /* "regular" conflict, checkout result of ll_merge() */
                const char *reason = "content";
-               if (is_null_sha1(p->base))
+               if (is_null_oid(&p->base))
                        reason = "add/add";
-               assert(!is_null_sha1(p->local));
-               assert(!is_null_sha1(p->remote));
+               assert(!is_null_oid(&p->local));
+               assert(!is_null_oid(&p->remote));
                if (o->verbosity >= 1)
                        printf("CONFLICT (%s): Merge conflict in notes for "
-                               "object %s\n", reason, sha1_to_hex(p->obj));
+                               "object %s\n", reason,
+                               oid_to_hex(&p->obj));
                ll_merge_in_worktree(o, p);
        }
 
        trace_printf("\t\t\tremoving from partial merge result\n");
-       remove_note(t, p->obj);
+       remove_note(t, p->obj.hash);
 
        return 1;
 }
@@ -439,29 +437,29 @@ static int merge_one_change(struct notes_merge_options *o,
        case NOTES_MERGE_RESOLVE_OURS:
                if (o->verbosity >= 2)
                        printf("Using local notes for %s\n",
-                                               sha1_to_hex(p->obj));
+                                               oid_to_hex(&p->obj));
                /* nothing to do */
                return 0;
        case NOTES_MERGE_RESOLVE_THEIRS:
                if (o->verbosity >= 2)
                        printf("Using remote notes for %s\n",
-                                               sha1_to_hex(p->obj));
-               if (add_note(t, p->obj, p->remote, combine_notes_overwrite))
+                                               oid_to_hex(&p->obj));
+               if (add_note(t, p->obj.hash, p->remote.hash, combine_notes_overwrite))
                        die("BUG: combine_notes_overwrite failed");
                return 0;
        case NOTES_MERGE_RESOLVE_UNION:
                if (o->verbosity >= 2)
                        printf("Concatenating local and remote notes for %s\n",
-                                                       sha1_to_hex(p->obj));
-               if (add_note(t, p->obj, p->remote, combine_notes_concatenate))
+                                                       oid_to_hex(&p->obj));
+               if (add_note(t, p->obj.hash, p->remote.hash, combine_notes_concatenate))
                        die("failed to concatenate notes "
                            "(combine_notes_concatenate)");
                return 0;
        case NOTES_MERGE_RESOLVE_CAT_SORT_UNIQ:
                if (o->verbosity >= 2)
                        printf("Concatenating unique lines in local and remote "
-                               "notes for %s\n", sha1_to_hex(p->obj));
-               if (add_note(t, p->obj, p->remote, combine_notes_cat_sort_uniq))
+                               "notes for %s\n", oid_to_hex(&p->obj));
+               if (add_note(t, p->obj.hash, p->remote.hash, combine_notes_cat_sort_uniq))
                        die("failed to concatenate notes "
                            "(combine_notes_cat_sort_uniq)");
                return 0;
@@ -479,20 +477,21 @@ static int merge_changes(struct notes_merge_options *o,
        for (i = 0; i < *num_changes; i++) {
                struct notes_merge_pair *p = changes + i;
                trace_printf("\t\t%.7s: %.7s -> %.7s/%.7s\n",
-                      sha1_to_hex(p->obj), sha1_to_hex(p->base),
-                      sha1_to_hex(p->local), sha1_to_hex(p->remote));
+                      oid_to_hex(&p->obj), oid_to_hex(&p->base),
+                      oid_to_hex(&p->local),
+                      oid_to_hex(&p->remote));
 
-               if (!hashcmp(p->base, p->remote)) {
+               if (!oidcmp(&p->base, &p->remote)) {
                        /* no remote change; nothing to do */
                        trace_printf("\t\t\tskipping (no remote change)\n");
-               } else if (!hashcmp(p->local, p->remote)) {
+               } else if (!oidcmp(&p->local, &p->remote)) {
                        /* same change in local and remote; nothing to do */
                        trace_printf("\t\t\tskipping (local == remote)\n");
-               } else if (!hashcmp(p->local, uninitialized) ||
-                          !hashcmp(p->local, p->base)) {
+               } else if (!oidcmp(&p->local, &uninitialized) ||
+                          !oidcmp(&p->local, &p->base)) {
                        /* no local change; adopt remote change */
                        trace_printf("\t\t\tno local change, adopted remote\n");
-                       if (add_note(t, p->obj, p->remote,
+                       if (add_note(t, p->obj.hash, p->remote.hash,
                                     combine_notes_overwrite))
                                die("BUG: combine_notes_overwrite failed");
                } else {
@@ -602,15 +601,15 @@ int notes_merge(struct notes_merge_options *o,
                if (o->verbosity >= 4)
                        printf("No merge base found; doing history-less merge\n");
        } else if (!bases->next) {
-               base_sha1 = bases->item->object.sha1;
-               base_tree_sha1 = bases->item->tree->object.sha1;
+               base_sha1 = bases->item->object.oid.hash;
+               base_tree_sha1 = bases->item->tree->object.oid.hash;
                if (o->verbosity >= 4)
                        printf("One merge base found (%.7s)\n",
                                sha1_to_hex(base_sha1));
        } else {
                /* TODO: How to handle multiple merge-bases? */
-               base_sha1 = bases->item->object.sha1;
-               base_tree_sha1 = bases->item->tree->object.sha1;
+               base_sha1 = bases->item->object.oid.hash;
+               base_tree_sha1 = bases->item->tree->object.oid.hash;
                if (o->verbosity >= 3)
                        printf("Multiple merge bases found. Using the first "
                                "(%.7s)\n", sha1_to_hex(base_sha1));
@@ -618,27 +617,27 @@ int notes_merge(struct notes_merge_options *o,
 
        if (o->verbosity >= 4)
                printf("Merging remote commit %.7s into local commit %.7s with "
-                       "merge-base %.7s\n", sha1_to_hex(remote->object.sha1),
-                       sha1_to_hex(local->object.sha1),
+                       "merge-base %.7s\n", oid_to_hex(&remote->object.oid),
+                       oid_to_hex(&local->object.oid),
                        sha1_to_hex(base_sha1));
 
-       if (!hashcmp(remote->object.sha1, base_sha1)) {
+       if (!hashcmp(remote->object.oid.hash, base_sha1)) {
                /* Already merged; result == local commit */
                if (o->verbosity >= 2)
                        printf("Already up-to-date!\n");
-               hashcpy(result_sha1, local->object.sha1);
+               hashcpy(result_sha1, local->object.oid.hash);
                goto found_result;
        }
-       if (!hashcmp(local->object.sha1, base_sha1)) {
+       if (!hashcmp(local->object.oid.hash, base_sha1)) {
                /* Fast-forward; result == remote commit */
                if (o->verbosity >= 2)
                        printf("Fast-forward\n");
-               hashcpy(result_sha1, remote->object.sha1);
+               hashcpy(result_sha1, remote->object.oid.hash);
                goto found_result;
        }
 
-       result = merge_from_diffs(o, base_tree_sha1, local->tree->object.sha1,
-                                 remote->tree->object.sha1, local_tree);
+       result = merge_from_diffs(o, base_tree_sha1, local->tree->object.oid.hash,
+                                 remote->tree->object.oid.hash, local_tree);
 
        if (result != 0) { /* non-trivial merge (with or without conflicts) */
                /* Commit (partial) result */
@@ -677,7 +676,7 @@ int notes_merge_commit(struct notes_merge_options *o,
        const char *msg = strstr(buffer, "\n\n");
        int baselen;
 
-       strbuf_addstr(&path, git_path(NOTES_MERGE_WORKTREE));
+       git_path_buf(&path, NOTES_MERGE_WORKTREE);
        if (o->verbosity >= 3)
                printf("Committing notes in notes merge worktree at %s\n",
                        path.buf);
@@ -742,7 +741,7 @@ int notes_merge_abort(struct notes_merge_options *o)
        struct strbuf buf = STRBUF_INIT;
        int ret;
 
-       strbuf_addstr(&buf, git_path(NOTES_MERGE_WORKTREE));
+       git_path_buf(&buf, NOTES_MERGE_WORKTREE);
        if (o->verbosity >= 3)
                printf("Removing notes merge worktree at %s/*\n", buf.buf);
        ret = remove_dir_recursively(&buf, REMOVE_DIR_KEEP_TOPLEVEL);