sequencer: refactor transform_todos() to work on a todo_list
[gitweb.git] / pack-bitmap.c
index 70465b8bf5a5e2716ad5c878b56746a32cf7c0a3..4695aaf6b4eda41ec2479a326ddbe7a0bed065ab 100644 (file)
@@ -66,7 +66,7 @@ struct bitmap_index {
        /* Number of bitmapped commits */
        uint32_t entry_count;
 
-       /* Name-hash cache (or NULL if not present). */
+       /* If not NULL, this is a name-hash cache pointing into map. */
        uint32_t *hashes;
 
        /*
@@ -86,10 +86,11 @@ struct bitmap_index {
        /* Bitmap result of the last performed walk */
        struct bitmap *result;
 
+       /* "have" bitmap from the last performed walk */
+       struct bitmap *haves;
+
        /* Version of the bitmap index */
        unsigned int version;
-
-       unsigned loaded : 1;
 };
 
 static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)
@@ -303,7 +304,7 @@ static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git
 
 static int load_pack_bitmap(struct bitmap_index *bitmap_git)
 {
-       assert(bitmap_git->map && !bitmap_git->loaded);
+       assert(bitmap_git->map);
 
        bitmap_git->bitmaps = kh_init_sha1();
        bitmap_git->ext_index.positions = kh_init_sha1_pos();
@@ -318,7 +319,6 @@ static int load_pack_bitmap(struct bitmap_index *bitmap_git)
        if (load_bitmap_entries_v1(bitmap_git) < 0)
                goto failed;
 
-       bitmap_git->loaded = 1;
        return 0;
 
 failed:
@@ -328,14 +328,15 @@ static int load_pack_bitmap(struct bitmap_index *bitmap_git)
        return -1;
 }
 
-static int open_pack_bitmap(struct bitmap_index *bitmap_git)
+static int open_pack_bitmap(struct repository *r,
+                           struct bitmap_index *bitmap_git)
 {
        struct packed_git *p;
        int ret = -1;
 
-       assert(!bitmap_git->map && !bitmap_git->loaded);
+       assert(!bitmap_git->map);
 
-       for (p = get_packed_git(the_repository); p; p = p->next) {
+       for (p = get_all_packs(r); p; p = p->next) {
                if (open_pack_bitmap_1(bitmap_git, p) == 0)
                        ret = 0;
        }
@@ -343,13 +344,14 @@ static int open_pack_bitmap(struct bitmap_index *bitmap_git)
        return ret;
 }
 
-struct bitmap_index *prepare_bitmap_git(void)
+struct bitmap_index *prepare_bitmap_git(struct repository *r)
 {
        struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
 
-       if (!open_pack_bitmap(bitmap_git) && !load_pack_bitmap(bitmap_git))
+       if (!open_pack_bitmap(r, bitmap_git) && !load_pack_bitmap(bitmap_git))
                return bitmap_git;
 
+       free_bitmap_index(bitmap_git);
        return NULL;
 }
 
@@ -689,8 +691,8 @@ struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs)
        struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
        /* try to open a bitmapped pack, but don't parse it yet
         * because we may not need to use it */
-       if (open_pack_bitmap(bitmap_git) < 0)
-               return NULL;
+       if (open_pack_bitmap(revs->repo, bitmap_git) < 0)
+               goto cleanup;
 
        for (i = 0; i < revs->pending.nr; ++i) {
                struct object *object = revs->pending.objects[i].item;
@@ -723,19 +725,19 @@ struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs)
         * optimize here
         */
        if (haves && !in_bitmapped_pack(bitmap_git, haves))
-               return NULL;
+               goto cleanup;
 
        /* if we don't want anything, we're done here */
        if (!wants)
-               return NULL;
+               goto cleanup;
 
        /*
         * now we're going to use bitmaps, so load the actual bitmap entries
         * from disk. this is the point of no return; after this the rev_list
         * becomes invalidated and we must perform the revwalk through bitmaps
         */
-       if (!bitmap_git->loaded && load_pack_bitmap(bitmap_git) < 0)
-               return NULL;
+       if (load_pack_bitmap(bitmap_git) < 0)
+               goto cleanup;
 
        object_array_clear(&revs->pending);
 
@@ -758,9 +760,13 @@ struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs)
                bitmap_and_not(wants_bitmap, haves_bitmap);
 
        bitmap_git->result = wants_bitmap;
+       bitmap_git->haves = haves_bitmap;
 
-       bitmap_free(haves_bitmap);
        return bitmap_git;
+
+cleanup:
+       free_bitmap_index(bitmap_git);
+       return NULL;
 }
 
 int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
@@ -840,9 +846,6 @@ void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
                OBJ_TAG, show_reachable);
 
        show_extended_objects(bitmap_git, show_reachable);
-
-       bitmap_free(bitmap_git->result);
-       bitmap_git->result = NULL;
 }
 
 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
@@ -953,7 +956,7 @@ void test_bitmap_walk(struct rev_info *revs)
        struct bitmap_test_data tdata;
        struct bitmap_index *bitmap_git;
 
-       if (!(bitmap_git = prepare_bitmap_git()))
+       if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
                die("failed to load bitmap indexes");
 
        if (revs->pending.nr != 1)
@@ -1001,7 +1004,7 @@ void test_bitmap_walk(struct rev_info *revs)
        else
                fprintf(stderr, "Mismatch!\n");
 
-       bitmap_free(result);
+       free_bitmap_index(bitmap_git);
 }
 
 static int rebuild_bitmap(uint32_t *reposition,
@@ -1093,3 +1096,39 @@ int rebuild_existing_bitmaps(struct bitmap_index *bitmap_git,
        bitmap_free(rebuild);
        return 0;
 }
+
+void free_bitmap_index(struct bitmap_index *b)
+{
+       if (!b)
+               return;
+
+       if (b->map)
+               munmap(b->map, b->map_size);
+       ewah_pool_free(b->commits);
+       ewah_pool_free(b->trees);
+       ewah_pool_free(b->blobs);
+       ewah_pool_free(b->tags);
+       kh_destroy_sha1(b->bitmaps);
+       free(b->ext_index.objects);
+       free(b->ext_index.hashes);
+       bitmap_free(b->result);
+       bitmap_free(b->haves);
+       free(b);
+}
+
+int bitmap_has_sha1_in_uninteresting(struct bitmap_index *bitmap_git,
+                                    const unsigned char *sha1)
+{
+       int pos;
+
+       if (!bitmap_git)
+               return 0; /* no bitmap loaded */
+       if (!bitmap_git->haves)
+               return 0; /* walk had no "haves" */
+
+       pos = bitmap_position_packfile(bitmap_git, sha1);
+       if (pos < 0)
+               return 0;
+
+       return bitmap_get(bitmap_git->haves, pos);
+}