summaryrefslogtreecommitdiff
path: root/pack-bitmap.c
diff options
context:
space:
mode:
Diffstat (limited to 'pack-bitmap.c')
-rw-r--r--pack-bitmap.c833
1 files changed, 637 insertions, 196 deletions
diff --git a/pack-bitmap.c b/pack-bitmap.c
index 6069b2fe55..d90e1d9d8c 100644
--- a/pack-bitmap.c
+++ b/pack-bitmap.c
@@ -12,6 +12,8 @@
#include "packfile.h"
#include "repository.h"
#include "object-store.h"
+#include "list-objects-filter-options.h"
+#include "config.h"
/*
* An entry on the bitmap index, representing the bitmap for a given
@@ -137,9 +139,10 @@ static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)
static int load_bitmap_header(struct bitmap_index *index)
{
struct bitmap_disk_header *header = (void *)index->map;
+ size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz;
- if (index->map_size < sizeof(*header) + the_hash_algo->rawsz)
- return error("Corrupted bitmap index (missing header data)");
+ if (index->map_size < header_size + the_hash_algo->rawsz)
+ return error("Corrupted bitmap index (too small)");
if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
return error("Corrupted bitmap index file (wrong header)");
@@ -151,25 +154,29 @@ static int load_bitmap_header(struct bitmap_index *index)
/* Parse known bitmap format options */
{
uint32_t flags = ntohs(header->options);
+ size_t cache_size = st_mult(index->pack->num_objects, sizeof(uint32_t));
+ unsigned char *index_end = index->map + index->map_size - the_hash_algo->rawsz;
if ((flags & BITMAP_OPT_FULL_DAG) == 0)
return error("Unsupported options for bitmap index file "
"(Git requires BITMAP_OPT_FULL_DAG)");
if (flags & BITMAP_OPT_HASH_CACHE) {
- unsigned char *end = index->map + index->map_size - the_hash_algo->rawsz;
- index->hashes = ((uint32_t *)end) - index->pack->num_objects;
+ if (cache_size > index_end - index->map - header_size)
+ return error("corrupted bitmap index file (too short to fit hash cache)");
+ index->hashes = (void *)(index_end - cache_size);
+ index_end -= cache_size;
}
}
index->entry_count = ntohl(header->entry_count);
- index->map_pos += sizeof(*header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz;
+ index->map_pos += header_size;
return 0;
}
static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
struct ewah_bitmap *root,
- const unsigned char *hash,
+ const struct object_id *oid,
struct stored_bitmap *xor_with,
int flags)
{
@@ -181,7 +188,7 @@ static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
stored->root = root;
stored->xor = xor_with;
stored->flags = flags;
- oidread(&stored->oid, hash);
+ oidcpy(&stored->oid, oid);
hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret);
@@ -189,7 +196,7 @@ static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
* because the SHA1 already existed on the map. this is bad, there
* shouldn't be duplicated commits in the index */
if (ret == 0) {
- error("Duplicate entry in bitmap index: %s", hash_to_hex(hash));
+ error("Duplicate entry in bitmap index: %s", oid_to_hex(oid));
return NULL;
}
@@ -221,13 +228,18 @@ static int load_bitmap_entries_v1(struct bitmap_index *index)
struct ewah_bitmap *bitmap = NULL;
struct stored_bitmap *xor_bitmap = NULL;
uint32_t commit_idx_pos;
- const unsigned char *sha1;
+ struct object_id oid;
+
+ if (index->map_size - index->map_pos < 6)
+ return error("corrupt ewah bitmap: truncated header for entry %d", i);
commit_idx_pos = read_be32(index->map, &index->map_pos);
xor_offset = read_u8(index->map, &index->map_pos);
flags = read_u8(index->map, &index->map_pos);
- sha1 = nth_packed_object_sha1(index->pack, commit_idx_pos);
+ if (nth_packed_object_id(&oid, index->pack, commit_idx_pos) < 0)
+ return error("corrupt ewah bitmap: commit index %u out of range",
+ (unsigned)commit_idx_pos);
bitmap = read_bitmap_1(index);
if (!bitmap)
@@ -244,7 +256,7 @@ static int load_bitmap_entries_v1(struct bitmap_index *index)
}
recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
- index, bitmap, sha1, xor_bitmap, flags);
+ index, bitmap, &oid, xor_bitmap, flags);
}
return 0;
@@ -326,6 +338,13 @@ failed:
munmap(bitmap_git->map, bitmap_git->map_size);
bitmap_git->map = NULL;
bitmap_git->map_size = 0;
+
+ kh_destroy_oid_map(bitmap_git->bitmaps);
+ bitmap_git->bitmaps = NULL;
+
+ kh_destroy_oid_pos(bitmap_git->ext_index.positions);
+ bitmap_git->ext_index.positions = NULL;
+
return -1;
}
@@ -362,10 +381,20 @@ struct include_data {
struct bitmap *seen;
};
+struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
+ struct commit *commit)
+{
+ khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
+ commit->object.oid);
+ if (hash_pos >= kh_end(bitmap_git->bitmaps))
+ return NULL;
+ return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
+}
+
static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
const struct object_id *oid)
{
- khash_oid_pos *positions = bitmap_git->ext_index.positions;
+ kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
khiter_t pos = kh_get_oid_pos(positions, *oid);
if (pos < kh_end(positions)) {
@@ -379,11 +408,14 @@ static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
const struct object_id *oid)
{
+ uint32_t pos;
off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
if (!offset)
return -1;
- return find_revindex_position(bitmap_git->pack, offset);
+ if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
+ return -1;
+ return pos;
}
static int bitmap_position(struct bitmap_index *bitmap_git,
@@ -447,10 +479,10 @@ static void show_commit(struct commit *commit, void *data)
static int add_to_include_set(struct bitmap_index *bitmap_git,
struct include_data *data,
- const struct object_id *oid,
+ struct commit *commit,
int bitmap_pos)
{
- khiter_t hash_pos;
+ struct ewah_bitmap *partial;
if (data->seen && bitmap_get(data->seen, bitmap_pos))
return 0;
@@ -458,10 +490,9 @@ static int add_to_include_set(struct bitmap_index *bitmap_git,
if (bitmap_get(data->base, bitmap_pos))
return 0;
- hash_pos = kh_get_oid_map(bitmap_git->bitmaps, *oid);
- if (hash_pos < kh_end(bitmap_git->bitmaps)) {
- struct stored_bitmap *st = kh_value(bitmap_git->bitmaps, hash_pos);
- bitmap_or_ewah(data->base, lookup_stored_bitmap(st));
+ partial = bitmap_for_commit(bitmap_git, commit);
+ if (partial) {
+ bitmap_or_ewah(data->base, partial);
return 0;
}
@@ -480,8 +511,7 @@ static int should_include(struct commit *commit, void *_data)
(struct object *)commit,
NULL);
- if (!add_to_include_set(data->bitmap_git, data, &commit->object.oid,
- bitmap_pos)) {
+ if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
struct commit_list *parent = commit->parents;
while (parent) {
@@ -495,10 +525,28 @@ static int should_include(struct commit *commit, void *_data)
return 1;
}
+static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
+ struct bitmap **base,
+ struct commit *commit)
+{
+ struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
+
+ if (!or_with)
+ return 0;
+
+ if (*base == NULL)
+ *base = ewah_to_bitmap(or_with);
+ else
+ bitmap_or_ewah(*base, or_with);
+
+ return 1;
+}
+
static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
struct rev_info *revs,
struct object_list *roots,
- struct bitmap *seen)
+ struct bitmap *seen,
+ struct list_objects_filter_options *filter)
{
struct bitmap *base = NULL;
int needs_walk = 0;
@@ -517,21 +565,10 @@ static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
struct object *object = roots->item;
roots = roots->next;
- if (object->type == OBJ_COMMIT) {
- khiter_t pos = kh_get_oid_map(bitmap_git->bitmaps, object->oid);
-
- if (pos < kh_end(bitmap_git->bitmaps)) {
- struct stored_bitmap *st = kh_value(bitmap_git->bitmaps, pos);
- struct ewah_bitmap *or_with = lookup_stored_bitmap(st);
-
- if (base == NULL)
- base = ewah_to_bitmap(or_with);
- else
- bitmap_or_ewah(base, or_with);
-
- object->flags |= SEEN;
- continue;
- }
+ if (object->type == OBJ_COMMIT &&
+ add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
+ object->flags |= SEEN;
+ continue;
}
object_list_insert(object, &not_mapped);
@@ -591,14 +628,19 @@ static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
show_data.bitmap_git = bitmap_git;
show_data.base = base;
- traverse_commit_list(revs, show_commit, show_object,
- &show_data);
+ traverse_commit_list_filtered(filter, revs,
+ show_commit, show_object,
+ &show_data, NULL);
+
+ revs->include_check = NULL;
+ revs->include_check_data = NULL;
}
return base;
}
static void show_extended_objects(struct bitmap_index *bitmap_git,
+ struct rev_info *revs,
show_reachable_fn show_reach)
{
struct bitmap *objects = bitmap_git->result;
@@ -612,17 +654,48 @@ static void show_extended_objects(struct bitmap_index *bitmap_git,
continue;
obj = eindex->objects[i];
+ if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
+ (obj->type == OBJ_TREE && !revs->tree_objects) ||
+ (obj->type == OBJ_TAG && !revs->tag_objects))
+ continue;
+
show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
}
}
+static void init_type_iterator(struct ewah_iterator *it,
+ struct bitmap_index *bitmap_git,
+ enum object_type type)
+{
+ switch (type) {
+ case OBJ_COMMIT:
+ ewah_iterator_init(it, bitmap_git->commits);
+ break;
+
+ case OBJ_TREE:
+ ewah_iterator_init(it, bitmap_git->trees);
+ break;
+
+ case OBJ_BLOB:
+ ewah_iterator_init(it, bitmap_git->blobs);
+ break;
+
+ case OBJ_TAG:
+ ewah_iterator_init(it, bitmap_git->tags);
+ break;
+
+ default:
+ BUG("object type %d not stored by bitmap type index", type);
+ break;
+ }
+}
+
static void show_objects_for_type(
struct bitmap_index *bitmap_git,
- struct ewah_bitmap *type_filter,
enum object_type object_type,
show_reachable_fn show_reach)
{
- size_t pos = 0, i = 0;
+ size_t i = 0;
uint32_t offset;
struct ewah_iterator it;
@@ -630,38 +703,35 @@ static void show_objects_for_type(
struct bitmap *objects = bitmap_git->result;
- if (bitmap_git->reuse_objects == bitmap_git->pack->num_objects)
- return;
-
- ewah_iterator_init(&it, type_filter);
+ init_type_iterator(&it, bitmap_git, object_type);
- while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
+ for (i = 0; i < objects->word_alloc &&
+ ewah_iterator_next(&filter, &it); i++) {
eword_t word = objects->words[i] & filter;
+ size_t pos = (i * BITS_IN_EWORD);
+
+ if (!word)
+ continue;
for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
struct object_id oid;
- struct revindex_entry *entry;
- uint32_t hash = 0;
+ uint32_t hash = 0, index_pos;
+ off_t ofs;
if ((word >> offset) == 0)
break;
offset += ewah_bit_ctz64(word >> offset);
- if (pos + offset < bitmap_git->reuse_objects)
- continue;
-
- entry = &bitmap_git->pack->revindex[pos + offset];
- nth_packed_object_oid(&oid, bitmap_git->pack, entry->nr);
+ index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
+ ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
+ nth_packed_object_id(&oid, bitmap_git->pack, index_pos);
if (bitmap_git->hashes)
- hash = get_be32(bitmap_git->hashes + entry->nr);
+ hash = get_be32(bitmap_git->hashes + index_pos);
- show_reach(&oid, object_type, 0, hash, bitmap_git->pack, entry->offset);
+ show_reach(&oid, object_type, 0, hash, bitmap_git->pack, ofs);
}
-
- pos += BITS_IN_EWORD;
- i++;
}
}
@@ -679,7 +749,249 @@ static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
return 0;
}
-struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs)
+static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
+ struct object_list *tip_objects,
+ enum object_type type)
+{
+ struct bitmap *result = bitmap_new();
+ struct object_list *p;
+
+ for (p = tip_objects; p; p = p->next) {
+ int pos;
+
+ if (p->item->type != type)
+ continue;
+
+ pos = bitmap_position(bitmap_git, &p->item->oid);
+ if (pos < 0)
+ continue;
+
+ bitmap_set(result, pos);
+ }
+
+ return result;
+}
+
+static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
+ struct object_list *tip_objects,
+ struct bitmap *to_filter,
+ enum object_type type)
+{
+ struct eindex *eindex = &bitmap_git->ext_index;
+ struct bitmap *tips;
+ struct ewah_iterator it;
+ eword_t mask;
+ uint32_t i;
+
+ /*
+ * The non-bitmap version of this filter never removes
+ * objects which the other side specifically asked for,
+ * so we must match that behavior.
+ */
+ tips = find_tip_objects(bitmap_git, tip_objects, type);
+
+ /*
+ * We can use the blob type-bitmap to work in whole words
+ * for the objects that are actually in the bitmapped packfile.
+ */
+ for (i = 0, init_type_iterator(&it, bitmap_git, type);
+ i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
+ i++) {
+ if (i < tips->word_alloc)
+ mask &= ~tips->words[i];
+ to_filter->words[i] &= ~mask;
+ }
+
+ /*
+ * Clear any blobs that weren't in the packfile (and so would not have
+ * been caught by the loop above. We'll have to check them
+ * individually.
+ */
+ for (i = 0; i < eindex->count; i++) {
+ uint32_t pos = i + bitmap_git->pack->num_objects;
+ if (eindex->objects[i]->type == type &&
+ bitmap_get(to_filter, pos) &&
+ !bitmap_get(tips, pos))
+ bitmap_unset(to_filter, pos);
+ }
+
+ bitmap_free(tips);
+}
+
+static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
+ struct object_list *tip_objects,
+ struct bitmap *to_filter)
+{
+ filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
+ OBJ_BLOB);
+}
+
+static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
+ uint32_t pos)
+{
+ struct packed_git *pack = bitmap_git->pack;
+ unsigned long size;
+ struct object_info oi = OBJECT_INFO_INIT;
+
+ oi.sizep = &size;
+
+ if (pos < pack->num_objects) {
+ off_t ofs = pack_pos_to_offset(pack, pos);
+ if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
+ struct object_id oid;
+ nth_packed_object_id(&oid, pack,
+ pack_pos_to_index(pack, pos));
+ die(_("unable to get size of %s"), oid_to_hex(&oid));
+ }
+ } else {
+ struct eindex *eindex = &bitmap_git->ext_index;
+ struct object *obj = eindex->objects[pos - pack->num_objects];
+ if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
+ die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
+ }
+
+ return size;
+}
+
+static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
+ struct object_list *tip_objects,
+ struct bitmap *to_filter,
+ unsigned long limit)
+{
+ struct eindex *eindex = &bitmap_git->ext_index;
+ struct bitmap *tips;
+ struct ewah_iterator it;
+ eword_t mask;
+ uint32_t i;
+
+ tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
+
+ for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
+ i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
+ i++) {
+ eword_t word = to_filter->words[i] & mask;
+ unsigned offset;
+
+ for (offset = 0; offset < BITS_IN_EWORD; offset++) {
+ uint32_t pos;
+
+ if ((word >> offset) == 0)
+ break;
+ offset += ewah_bit_ctz64(word >> offset);
+ pos = i * BITS_IN_EWORD + offset;
+
+ if (!bitmap_get(tips, pos) &&
+ get_size_by_pos(bitmap_git, pos) >= limit)
+ bitmap_unset(to_filter, pos);
+ }
+ }
+
+ for (i = 0; i < eindex->count; i++) {
+ uint32_t pos = i + bitmap_git->pack->num_objects;
+ if (eindex->objects[i]->type == OBJ_BLOB &&
+ bitmap_get(to_filter, pos) &&
+ !bitmap_get(tips, pos) &&
+ get_size_by_pos(bitmap_git, pos) >= limit)
+ bitmap_unset(to_filter, pos);
+ }
+
+ bitmap_free(tips);
+}
+
+static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
+ struct object_list *tip_objects,
+ struct bitmap *to_filter,
+ unsigned long limit)
+{
+ if (limit)
+ BUG("filter_bitmap_tree_depth given non-zero limit");
+
+ filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
+ OBJ_TREE);
+ filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
+ OBJ_BLOB);
+}
+
+static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
+ struct object_list *tip_objects,
+ struct bitmap *to_filter,
+ enum object_type object_type)
+{
+ if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
+ BUG("filter_bitmap_object_type given invalid object");
+
+ if (object_type != OBJ_TAG)
+ filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
+ if (object_type != OBJ_COMMIT)
+ filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
+ if (object_type != OBJ_TREE)
+ filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
+ if (object_type != OBJ_BLOB)
+ filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
+}
+
+static int filter_bitmap(struct bitmap_index *bitmap_git,
+ struct object_list *tip_objects,
+ struct bitmap *to_filter,
+ struct list_objects_filter_options *filter)
+{
+ if (!filter || filter->choice == LOFC_DISABLED)
+ return 0;
+
+ if (filter->choice == LOFC_BLOB_NONE) {
+ if (bitmap_git)
+ filter_bitmap_blob_none(bitmap_git, tip_objects,
+ to_filter);
+ return 0;
+ }
+
+ if (filter->choice == LOFC_BLOB_LIMIT) {
+ if (bitmap_git)
+ filter_bitmap_blob_limit(bitmap_git, tip_objects,
+ to_filter,
+ filter->blob_limit_value);
+ return 0;
+ }
+
+ if (filter->choice == LOFC_TREE_DEPTH &&
+ filter->tree_exclude_depth == 0) {
+ if (bitmap_git)
+ filter_bitmap_tree_depth(bitmap_git, tip_objects,
+ to_filter,
+ filter->tree_exclude_depth);
+ return 0;
+ }
+
+ if (filter->choice == LOFC_OBJECT_TYPE) {
+ if (bitmap_git)
+ filter_bitmap_object_type(bitmap_git, tip_objects,
+ to_filter,
+ filter->object_type);
+ return 0;
+ }
+
+ if (filter->choice == LOFC_COMBINE) {
+ int i;
+ for (i = 0; i < filter->sub_nr; i++) {
+ if (filter_bitmap(bitmap_git, tip_objects, to_filter,
+ &filter->sub[i]) < 0)
+ return -1;
+ }
+ return 0;
+ }
+
+ /* filter choice not handled */
+ return -1;
+}
+
+static int can_filter_bitmap(struct list_objects_filter_options *filter)
+{
+ return !filter_bitmap(NULL, NULL, NULL, filter);
+}
+
+struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
+ struct list_objects_filter_options *filter,
+ int filter_provided_objects)
{
unsigned int i;
@@ -689,9 +1001,22 @@ struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs)
struct bitmap *wants_bitmap = NULL;
struct bitmap *haves_bitmap = NULL;
- struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
+ struct bitmap_index *bitmap_git;
+
+ /*
+ * We can't do pathspec limiting with bitmaps, because we don't know
+ * which commits are associated with which object changes (let alone
+ * even which objects are associated with which paths).
+ */
+ if (revs->prune)
+ return NULL;
+
+ if (!can_filter_bitmap(filter))
+ return NULL;
+
/* try to open a bitmapped pack, but don't parse it yet
* because we may not need to use it */
+ CALLOC_ARRAY(bitmap_git, 1);
if (open_pack_bitmap(revs->repo, bitmap_git) < 0)
goto cleanup;
@@ -709,9 +1034,8 @@ struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs)
else
object_list_insert(object, &wants);
- if (!tag->tagged)
- die("bad tag");
- object = parse_object_or_die(&tag->tagged->oid, NULL);
+ object = parse_object_or_die(get_tagged_oid(tag), NULL);
+ object->flags |= (tag->object.flags & UNINTERESTING);
}
if (object->flags & UNINTERESTING)
@@ -744,7 +1068,8 @@ struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs)
if (haves) {
revs->ignore_missing_links = 1;
- haves_bitmap = find_objects(bitmap_git, revs, haves, NULL);
+ haves_bitmap = find_objects(bitmap_git, revs, haves, NULL,
+ filter);
reset_revision_walk();
revs->ignore_missing_links = 0;
@@ -752,7 +1077,8 @@ struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs)
BUG("failed to perform bitmap walk");
}
- wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap);
+ wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap,
+ filter);
if (!wants_bitmap)
BUG("failed to perform bitmap walk");
@@ -760,93 +1086,171 @@ struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs)
if (haves_bitmap)
bitmap_and_not(wants_bitmap, haves_bitmap);
+ filter_bitmap(bitmap_git, (filter && filter_provided_objects) ? NULL : wants,
+ wants_bitmap, filter);
+
bitmap_git->result = wants_bitmap;
bitmap_git->haves = haves_bitmap;
+ object_list_free(&wants);
+ object_list_free(&haves);
+
return bitmap_git;
cleanup:
free_bitmap_index(bitmap_git);
+ object_list_free(&wants);
+ object_list_free(&haves);
return NULL;
}
-int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
- struct packed_git **packfile,
- uint32_t *entries,
- off_t *up_to)
+static void try_partial_reuse(struct bitmap_index *bitmap_git,
+ size_t pos,
+ struct bitmap *reuse,
+ struct pack_window **w_curs)
{
+ off_t offset, header;
+ enum object_type type;
+ unsigned long size;
+
+ if (pos >= bitmap_git->pack->num_objects)
+ return; /* not actually in the pack */
+
+ offset = header = pack_pos_to_offset(bitmap_git->pack, pos);
+ type = unpack_object_header(bitmap_git->pack, w_curs, &offset, &size);
+ if (type < 0)
+ return; /* broken packfile, punt */
+
+ if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
+ off_t base_offset;
+ uint32_t base_pos;
+
+ /*
+ * Find the position of the base object so we can look it up
+ * in our bitmaps. If we can't come up with an offset, or if
+ * that offset is not in the revidx, the pack is corrupt.
+ * There's nothing we can do, so just punt on this object,
+ * and the normal slow path will complain about it in
+ * more detail.
+ */
+ base_offset = get_delta_base(bitmap_git->pack, w_curs,
+ &offset, type, header);
+ if (!base_offset)
+ return;
+ if (offset_to_pack_pos(bitmap_git->pack, base_offset, &base_pos) < 0)
+ return;
+
+ /*
+ * We assume delta dependencies always point backwards. This
+ * lets us do a single pass, and is basically always true
+ * due to the way OFS_DELTAs work. You would not typically
+ * find REF_DELTA in a bitmapped pack, since we only bitmap
+ * packs we write fresh, and OFS_DELTA is the default). But
+ * let's double check to make sure the pack wasn't written with
+ * odd parameters.
+ */
+ if (base_pos >= pos)
+ return;
+
+ /*
+ * And finally, if we're not sending the base as part of our
+ * reuse chunk, then don't send this object either. The base
+ * would come after us, along with other objects not
+ * necessarily in the pack, which means we'd need to convert
+ * to REF_DELTA on the fly. Better to just let the normal
+ * object_entry code path handle it.
+ */
+ if (!bitmap_get(reuse, base_pos))
+ return;
+ }
+
/*
- * Reuse the packfile content if we need more than
- * 90% of its objects
+ * If we got here, then the object is OK to reuse. Mark it.
*/
- static const double REUSE_PERCENT = 0.9;
+ bitmap_set(reuse, pos);
+}
+int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
+ struct packed_git **packfile_out,
+ uint32_t *entries,
+ struct bitmap **reuse_out)
+{
struct bitmap *result = bitmap_git->result;
- uint32_t reuse_threshold;
- uint32_t i, reuse_objects = 0;
+ struct bitmap *reuse;
+ struct pack_window *w_curs = NULL;
+ size_t i = 0;
+ uint32_t offset;
assert(result);
- for (i = 0; i < result->word_alloc; ++i) {
- if (result->words[i] != (eword_t)~0) {
- reuse_objects += ewah_bit_ctz64(~result->words[i]);
- break;
- }
+ while (i < result->word_alloc && result->words[i] == (eword_t)~0)
+ i++;
- reuse_objects += BITS_IN_EWORD;
- }
+ /* Don't mark objects not in the packfile */
+ if (i > bitmap_git->pack->num_objects / BITS_IN_EWORD)
+ i = bitmap_git->pack->num_objects / BITS_IN_EWORD;
-#ifdef GIT_BITMAP_DEBUG
- {
- const unsigned char *sha1;
- struct revindex_entry *entry;
+ reuse = bitmap_word_alloc(i);
+ memset(reuse->words, 0xFF, i * sizeof(eword_t));
- entry = &bitmap_git->reverse_index->revindex[reuse_objects];
- sha1 = nth_packed_object_sha1(bitmap_git->pack, entry->nr);
+ for (; i < result->word_alloc; ++i) {
+ eword_t word = result->words[i];
+ size_t pos = (i * BITS_IN_EWORD);
- fprintf(stderr, "Failed to reuse at %d (%016llx)\n",
- reuse_objects, result->words[i]);
- fprintf(stderr, " %s\n", hash_to_hex(sha1));
+ for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
+ if ((word >> offset) == 0)
+ break;
+
+ offset += ewah_bit_ctz64(word >> offset);
+ try_partial_reuse(bitmap_git, pos + offset, reuse, &w_curs);
+ }
}
-#endif
- if (!reuse_objects)
- return -1;
+ unuse_pack(&w_curs);
- if (reuse_objects >= bitmap_git->pack->num_objects) {
- bitmap_git->reuse_objects = *entries = bitmap_git->pack->num_objects;
- *up_to = -1; /* reuse the full pack */
- *packfile = bitmap_git->pack;
- return 0;
+ *entries = bitmap_popcount(reuse);
+ if (!*entries) {
+ bitmap_free(reuse);
+ return -1;
}
- reuse_threshold = bitmap_popcount(bitmap_git->result) * REUSE_PERCENT;
+ /*
+ * Drop any reused objects from the result, since they will not
+ * need to be handled separately.
+ */
+ bitmap_and_not(result, reuse);
+ *packfile_out = bitmap_git->pack;
+ *reuse_out = reuse;
+ return 0;
+}
- if (reuse_objects < reuse_threshold)
- return -1;
+int bitmap_walk_contains(struct bitmap_index *bitmap_git,
+ struct bitmap *bitmap, const struct object_id *oid)
+{
+ int idx;
- bitmap_git->reuse_objects = *entries = reuse_objects;
- *up_to = bitmap_git->pack->revindex[reuse_objects].offset;
- *packfile = bitmap_git->pack;
+ if (!bitmap)
+ return 0;
- return 0;
+ idx = bitmap_position(bitmap_git, oid);
+ return idx >= 0 && bitmap_get(bitmap, idx);
}
void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
+ struct rev_info *revs,
show_reachable_fn show_reachable)
{
assert(bitmap_git->result);
- show_objects_for_type(bitmap_git, bitmap_git->commits,
- OBJ_COMMIT, show_reachable);
- show_objects_for_type(bitmap_git, bitmap_git->trees,
- OBJ_TREE, show_reachable);
- show_objects_for_type(bitmap_git, bitmap_git->blobs,
- OBJ_BLOB, show_reachable);
- show_objects_for_type(bitmap_git, bitmap_git->tags,
- OBJ_TAG, show_reachable);
+ show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
+ if (revs->tree_objects)
+ show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
+ if (revs->blob_objects)
+ show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
+ if (revs->tag_objects)
+ show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
- show_extended_objects(bitmap_git, show_reachable);
+ show_extended_objects(bitmap_git, revs, show_reachable);
}
static uint32_t count_object_type(struct bitmap_index *bitmap_git,
@@ -859,26 +1263,7 @@ static uint32_t count_object_type(struct bitmap_index *bitmap_git,
struct ewah_iterator it;
eword_t filter;
- switch (type) {
- case OBJ_COMMIT:
- ewah_iterator_init(&it, bitmap_git->commits);
- break;
-
- case OBJ_TREE:
- ewah_iterator_init(&it, bitmap_git->trees);
- break;
-
- case OBJ_BLOB:
- ewah_iterator_init(&it, bitmap_git->blobs);
- break;
-
- case OBJ_TAG:
- ewah_iterator_init(&it, bitmap_git->tags);
- break;
-
- default:
- return 0;
- }
+ init_type_iterator(&it, bitmap_git, type);
while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
eword_t word = objects->words[i++] & filter;
@@ -952,10 +1337,10 @@ void test_bitmap_walk(struct rev_info *revs)
{
struct object *root;
struct bitmap *result = NULL;
- khiter_t pos;
size_t result_popcnt;
struct bitmap_test_data tdata;
struct bitmap_index *bitmap_git;
+ struct ewah_bitmap *bm;
if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
die("failed to load bitmap indexes");
@@ -967,12 +1352,9 @@ void test_bitmap_walk(struct rev_info *revs)
bitmap_git->version, bitmap_git->entry_count);
root = revs->pending.objects[0].item;
- pos = kh_get_oid_map(bitmap_git->bitmaps, root->oid);
-
- if (pos < kh_end(bitmap_git->bitmaps)) {
- struct stored_bitmap *st = kh_value(bitmap_git->bitmaps, pos);
- struct ewah_bitmap *bm = lookup_stored_bitmap(st);
+ bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
+ if (bm) {
fprintf(stderr, "Found bitmap for %s. %d bits / %08x checksum\n",
oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
@@ -1003,14 +1385,32 @@ void test_bitmap_walk(struct rev_info *revs)
if (bitmap_equals(result, tdata.base))
fprintf(stderr, "OK!\n");
else
- fprintf(stderr, "Mismatch!\n");
+ die("mismatch in bitmap results");
+
+ free_bitmap_index(bitmap_git);
+}
+
+int test_bitmap_commits(struct repository *r)
+{
+ struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
+ struct object_id oid;
+ MAYBE_UNUSED void *value;
+
+ if (!bitmap_git)
+ die("failed to load bitmap indexes");
+
+ kh_foreach(bitmap_git->bitmaps, oid, value, {
+ printf("%s\n", oid_to_hex(&oid));
+ });
free_bitmap_index(bitmap_git);
+
+ return 0;
}
-static int rebuild_bitmap(uint32_t *reposition,
- struct ewah_bitmap *source,
- struct bitmap *dest)
+int rebuild_bitmap(const uint32_t *reposition,
+ struct ewah_bitmap *source,
+ struct bitmap *dest)
{
uint32_t pos = 0;
struct ewah_iterator it;
@@ -1039,63 +1439,28 @@ static int rebuild_bitmap(uint32_t *reposition,
return 0;
}
-int rebuild_existing_bitmaps(struct bitmap_index *bitmap_git,
- struct packing_data *mapping,
- khash_sha1 *reused_bitmaps,
- int show_progress)
+uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
+ struct packing_data *mapping)
{
uint32_t i, num_objects;
uint32_t *reposition;
- struct bitmap *rebuild;
- struct stored_bitmap *stored;
- struct progress *progress = NULL;
-
- khiter_t hash_pos;
- int hash_ret;
num_objects = bitmap_git->pack->num_objects;
- reposition = xcalloc(num_objects, sizeof(uint32_t));
+ CALLOC_ARRAY(reposition, num_objects);
for (i = 0; i < num_objects; ++i) {
- const unsigned char *sha1;
- struct revindex_entry *entry;
+ struct object_id oid;
struct object_entry *oe;
- entry = &bitmap_git->pack->revindex[i];
- sha1 = nth_packed_object_sha1(bitmap_git->pack, entry->nr);
- oe = packlist_find(mapping, sha1, NULL);
+ nth_packed_object_id(&oid, bitmap_git->pack,
+ pack_pos_to_index(bitmap_git->pack, i));
+ oe = packlist_find(mapping, &oid);
if (oe)
reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
}
- rebuild = bitmap_new();
- i = 0;
-
- if (show_progress)
- progress = start_progress("Reusing bitmaps", 0);
-
- kh_foreach_value(bitmap_git->bitmaps, stored, {
- if (stored->flags & BITMAP_FLAG_REUSE) {
- if (!rebuild_bitmap(reposition,
- lookup_stored_bitmap(stored),
- rebuild)) {
- hash_pos = kh_put_sha1(reused_bitmaps,
- stored->oid.hash,
- &hash_ret);
- kh_value(reused_bitmaps, hash_pos) =
- bitmap_to_ewah(rebuild);
- }
- bitmap_reset(rebuild);
- display_progress(progress, ++i);
- }
- });
-
- stop_progress(&progress);
-
- free(reposition);
- bitmap_free(rebuild);
- return 0;
+ return reposition;
}
void free_bitmap_index(struct bitmap_index *b)
@@ -1120,16 +1485,92 @@ void free_bitmap_index(struct bitmap_index *b)
int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
const struct object_id *oid)
{
- int pos;
+ return bitmap_git &&
+ bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
+}
- if (!bitmap_git)
- return 0; /* no bitmap loaded */
- if (!bitmap_git->haves)
- return 0; /* walk had no "haves" */
+static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
+ enum object_type object_type)
+{
+ struct bitmap *result = bitmap_git->result;
+ struct packed_git *pack = bitmap_git->pack;
+ off_t total = 0;
+ struct ewah_iterator it;
+ eword_t filter;
+ size_t i;
- pos = bitmap_position_packfile(bitmap_git, oid);
- if (pos < 0)
- return 0;
+ init_type_iterator(&it, bitmap_git, object_type);
+ for (i = 0; i < result->word_alloc &&
+ ewah_iterator_next(&filter, &it); i++) {
+ eword_t word = result->words[i] & filter;
+ size_t base = (i * BITS_IN_EWORD);
+ unsigned offset;
+
+ if (!word)
+ continue;
+
+ for (offset = 0; offset < BITS_IN_EWORD; offset++) {
+ size_t pos;
- return bitmap_get(bitmap_git->haves, pos);
+ if ((word >> offset) == 0)
+ break;
+
+ offset += ewah_bit_ctz64(word >> offset);
+ pos = base + offset;
+ total += pack_pos_to_offset(pack, pos + 1) -
+ pack_pos_to_offset(pack, pos);
+ }
+ }
+
+ return total;
+}
+
+static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
+{
+ struct bitmap *result = bitmap_git->result;
+ struct packed_git *pack = bitmap_git->pack;
+ struct eindex *eindex = &bitmap_git->ext_index;
+ off_t total = 0;
+ struct object_info oi = OBJECT_INFO_INIT;
+ off_t object_size;
+ size_t i;
+
+ oi.disk_sizep = &object_size;
+
+ for (i = 0; i < eindex->count; i++) {
+ struct object *obj = eindex->objects[i];
+
+ if (!bitmap_get(result, pack->num_objects + i))
+ continue;
+
+ if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
+ die(_("unable to get disk usage of %s"),
+ oid_to_hex(&obj->oid));
+
+ total += object_size;
+ }
+ return total;
+}
+
+off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
+ struct rev_info *revs)
+{
+ off_t total = 0;
+
+ total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
+ if (revs->tree_objects)
+ total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
+ if (revs->blob_objects)
+ total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
+ if (revs->tag_objects)
+ total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
+
+ total += get_disk_usage_for_extended(bitmap_git);
+
+ return total;
+}
+
+const struct string_list *bitmap_preferred_tips(struct repository *r)
+{
+ return repo_config_get_value_multi(r, "pack.preferbitmaptips");
}