]> git.ipfire.org Git - thirdparty/git.git/blobdiff - pack-bitmap-write.c
Sync with maint
[thirdparty/git.git] / pack-bitmap-write.c
index a8df5ce2ab67bfdb5445e4c02e4ad65032abe9a0..a7a4964b50d1936be0d769724c1b0b5fa5420672 100644 (file)
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "object-store.h"
 #include "commit.h"
 #include "tag.h"
 #include "diff.h"
@@ -10,6 +11,7 @@
 #include "pack-bitmap.h"
 #include "sha1-lookup.h"
 #include "pack-objects.h"
+#include "commit-reach.h"
 
 struct bitmapped_commit {
        struct commit *commit;
@@ -26,8 +28,8 @@ struct bitmap_writer {
        struct ewah_bitmap *blobs;
        struct ewah_bitmap *tags;
 
-       khash_sha1 *bitmaps;
-       khash_sha1 *reused;
+       kh_oid_map_t *bitmaps;
+       kh_oid_map_t *reused;
        struct packing_data *to_pack;
 
        struct bitmapped_commit *selected;
@@ -35,7 +37,7 @@ struct bitmap_writer {
 
        struct progress *progress;
        int show_progress;
-       unsigned char pack_checksum[20];
+       unsigned char pack_checksum[GIT_MAX_RAWSZ];
 };
 
 static struct bitmap_writer writer;
@@ -48,7 +50,8 @@ void bitmap_writer_show_progress(int show)
 /**
  * Build the initial type index for the packfile
  */
-void bitmap_writer_build_type_index(struct pack_idx_entry **index,
+void bitmap_writer_build_type_index(struct packing_data *to_pack,
+                                   struct pack_idx_entry **index,
                                    uint32_t index_nr)
 {
        uint32_t i;
@@ -57,24 +60,25 @@ void bitmap_writer_build_type_index(struct pack_idx_entry **index,
        writer.trees = ewah_new();
        writer.blobs = ewah_new();
        writer.tags = ewah_new();
+       ALLOC_ARRAY(to_pack->in_pack_pos, to_pack->nr_objects);
 
        for (i = 0; i < index_nr; ++i) {
                struct object_entry *entry = (struct object_entry *)index[i];
                enum object_type real_type;
 
-               entry->in_pack_pos = i;
+               oe_set_in_pack_pos(to_pack, entry, i);
 
-               switch (entry->type) {
+               switch (oe_type(entry)) {
                case OBJ_COMMIT:
                case OBJ_TREE:
                case OBJ_BLOB:
                case OBJ_TAG:
-                       real_type = entry->type;
+                       real_type = oe_type(entry);
                        break;
 
                default:
-                       real_type = sha1_object_info(entry->idx.oid.hash,
-                                                    NULL);
+                       real_type = oid_object_info(to_pack->repo,
+                                                   &entry->idx.oid, NULL);
                        break;
                }
 
@@ -98,7 +102,7 @@ void bitmap_writer_build_type_index(struct pack_idx_entry **index,
                default:
                        die("Missing type information for %s (%d/%d)",
                            oid_to_hex(&entry->idx.oid), real_type,
-                           entry->type);
+                           oe_type(entry));
                }
        }
 }
@@ -138,22 +142,22 @@ static inline void reset_all_seen(void)
        seen_objects_nr = 0;
 }
 
-static uint32_t find_object_pos(const unsigned char *sha1)
+static uint32_t find_object_pos(const struct object_id *oid)
 {
-       struct object_entry *entry = packlist_find(writer.to_pack, sha1, NULL);
+       struct object_entry *entry = packlist_find(writer.to_pack, oid);
 
        if (!entry) {
                die("Failed to write bitmap index. Packfile doesn't have full closure "
-                       "(object %s is missing)", sha1_to_hex(sha1));
+                       "(object %s is missing)", oid_to_hex(oid));
        }
 
-       return entry->in_pack_pos;
+       return oe_in_pack_pos(writer.to_pack, entry);
 }
 
 static void show_object(struct object *object, const char *name, void *data)
 {
        struct bitmap *base = data;
-       bitmap_set(base, find_object_pos(object->oid.hash));
+       bitmap_set(base, find_object_pos(&object->oid));
        mark_as_seen(object);
 }
 
@@ -166,12 +170,12 @@ static int
 add_to_include_set(struct bitmap *base, struct commit *commit)
 {
        khiter_t hash_pos;
-       uint32_t bitmap_pos = find_object_pos(commit->object.oid.hash);
+       uint32_t bitmap_pos = find_object_pos(&commit->object.oid);
 
        if (bitmap_get(base, bitmap_pos))
                return 0;
 
-       hash_pos = kh_get_sha1(writer.bitmaps, commit->object.oid.hash);
+       hash_pos = kh_get_oid_map(writer.bitmaps, commit->object.oid);
        if (hash_pos < kh_end(writer.bitmaps)) {
                struct bitmapped_commit *bc = kh_value(writer.bitmaps, hash_pos);
                bitmap_or_ewah(base, bc->bitmap);
@@ -252,13 +256,13 @@ void bitmap_writer_build(struct packing_data *to_pack)
        struct bitmap *base = bitmap_new();
        struct rev_info revs;
 
-       writer.bitmaps = kh_init_sha1();
+       writer.bitmaps = kh_init_oid_map();
        writer.to_pack = to_pack;
 
        if (writer.show_progress)
                writer.progress = start_progress("Building bitmaps", writer.selected_nr);
 
-       init_revisions(&revs, NULL);
+       repo_init_revisions(to_pack->repo, &revs, NULL);
        revs.tag_objects = 1;
        revs.tree_objects = 1;
        revs.blob_objects = 1;
@@ -307,7 +311,7 @@ void bitmap_writer_build(struct packing_data *to_pack)
                if (i >= reuse_after)
                        stored->flags |= BITMAP_FLAG_REUSE;
 
-               hash_pos = kh_put_sha1(writer.bitmaps, object->oid.hash, &hash_ret);
+               hash_pos = kh_put_oid_map(writer.bitmaps, object->oid, &hash_ret);
                if (hash_ret == 0)
                        die("Duplicate entry when writing index: %s",
                            oid_to_hex(&object->oid));
@@ -358,21 +362,27 @@ static int date_compare(const void *_a, const void *_b)
 
 void bitmap_writer_reuse_bitmaps(struct packing_data *to_pack)
 {
-       if (prepare_bitmap_git() < 0)
+       struct bitmap_index *bitmap_git;
+       if (!(bitmap_git = prepare_bitmap_git(to_pack->repo)))
                return;
 
-       writer.reused = kh_init_sha1();
-       rebuild_existing_bitmaps(to_pack, writer.reused, writer.show_progress);
+       writer.reused = kh_init_oid_map();
+       rebuild_existing_bitmaps(bitmap_git, to_pack, writer.reused,
+                                writer.show_progress);
+       /*
+        * NEEDSWORK: rebuild_existing_bitmaps() makes writer.reused reference
+        * some bitmaps in bitmap_git, so we can't free the latter.
+        */
 }
 
-static struct ewah_bitmap *find_reused_bitmap(const unsigned char *sha1)
+static struct ewah_bitmap *find_reused_bitmap(const struct object_id *oid)
 {
        khiter_t hash_pos;
 
        if (!writer.reused)
                return NULL;
 
-       hash_pos = kh_get_sha1(writer.reused, sha1);
+       hash_pos = kh_get_oid_map(writer.reused, *oid);
        if (hash_pos >= kh_end(writer.reused))
                return NULL;
 
@@ -412,14 +422,14 @@ void bitmap_writer_select_commits(struct commit **indexed_commits,
 
                if (next == 0) {
                        chosen = indexed_commits[i];
-                       reused_bitmap = find_reused_bitmap(chosen->object.oid.hash);
+                       reused_bitmap = find_reused_bitmap(&chosen->object.oid);
                } else {
                        chosen = indexed_commits[i + next];
 
                        for (j = 0; j <= next; ++j) {
                                struct commit *cm = indexed_commits[i + j];
 
-                               reused_bitmap = find_reused_bitmap(cm->object.oid.hash);
+                               reused_bitmap = find_reused_bitmap(&cm->object.oid);
                                if (reused_bitmap || (cm->object.flags & NEEDS_BITMAP) != 0) {
                                        chosen = cm;
                                        break;
@@ -440,19 +450,19 @@ void bitmap_writer_select_commits(struct commit **indexed_commits,
 }
 
 
-static int sha1write_ewah_helper(void *f, const void *buf, size_t len)
+static int hashwrite_ewah_helper(void *f, const void *buf, size_t len)
 {
-       /* sha1write will die on error */
-       sha1write(f, buf, len);
+       /* hashwrite will die on error */
+       hashwrite(f, buf, len);
        return len;
 }
 
 /**
  * Write the bitmap index to disk
  */
-static inline void dump_bitmap(struct sha1file *f, struct ewah_bitmap *bitmap)
+static inline void dump_bitmap(struct hashfile *f, struct ewah_bitmap *bitmap)
 {
-       if (ewah_serialize_to(bitmap, sha1write_ewah_helper, f) < 0)
+       if (ewah_serialize_to(bitmap, hashwrite_ewah_helper, f) < 0)
                die("Failed to write bitmap index");
 }
 
@@ -462,7 +472,7 @@ static const unsigned char *sha1_access(size_t pos, void *table)
        return index[pos]->oid.hash;
 }
 
-static void write_selected_commits_v1(struct sha1file *f,
+static void write_selected_commits_v1(struct hashfile *f,
                                      struct pack_idx_entry **index,
                                      uint32_t index_nr)
 {
@@ -475,17 +485,17 @@ static void write_selected_commits_v1(struct sha1file *f,
                        sha1_pos(stored->commit->object.oid.hash, index, index_nr, sha1_access);
 
                if (commit_pos < 0)
-                       die("BUG: trying to write commit not in index");
+                       BUG("trying to write commit not in index");
 
-               sha1write_be32(f, commit_pos);
-               sha1write_u8(f, stored->xor_offset);
-               sha1write_u8(f, stored->flags);
+               hashwrite_be32(f, commit_pos);
+               hashwrite_u8(f, stored->xor_offset);
+               hashwrite_u8(f, stored->flags);
 
                dump_bitmap(f, stored->write_as);
        }
 }
 
-static void write_hash_cache(struct sha1file *f,
+static void write_hash_cache(struct hashfile *f,
                             struct pack_idx_entry **index,
                             uint32_t index_nr)
 {
@@ -494,7 +504,7 @@ static void write_hash_cache(struct sha1file *f,
        for (i = 0; i < index_nr; ++i) {
                struct object_entry *entry = (struct object_entry *)index[i];
                uint32_t hash_value = htonl(entry->hash);
-               sha1write(f, &hash_value, sizeof(hash_value));
+               hashwrite(f, &hash_value, sizeof(hash_value));
        }
 }
 
@@ -511,13 +521,13 @@ void bitmap_writer_finish(struct pack_idx_entry **index,
        static uint16_t default_version = 1;
        static uint16_t flags = BITMAP_OPT_FULL_DAG;
        struct strbuf tmp_file = STRBUF_INIT;
-       struct sha1file *f;
+       struct hashfile *f;
 
        struct bitmap_disk_header header;
 
        int fd = odb_mkstemp(&tmp_file, "pack/tmp_bitmap_XXXXXX");
 
-       f = sha1fd(fd, tmp_file.buf);
+       f = hashfd(fd, tmp_file.buf);
 
        memcpy(header.magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE));
        header.version = htons(default_version);
@@ -525,7 +535,7 @@ void bitmap_writer_finish(struct pack_idx_entry **index,
        header.entry_count = htonl(writer.selected_nr);
        hashcpy(header.checksum, writer.pack_checksum);
 
-       sha1write(f, &header, sizeof(header));
+       hashwrite(f, &header, sizeof(header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz);
        dump_bitmap(f, writer.commits);
        dump_bitmap(f, writer.trees);
        dump_bitmap(f, writer.blobs);
@@ -535,7 +545,7 @@ void bitmap_writer_finish(struct pack_idx_entry **index,
        if (options & BITMAP_OPT_HASH_CACHE)
                write_hash_cache(f, index, index_nr);
 
-       sha1close(f, NULL, CSUM_FSYNC);
+       finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
 
        if (adjust_shared_perm(tmp_file.buf))
                die_errno("unable to make temporary bitmap file readable");