strbuf_release(&path);
}
+static void prepare_packed_git(struct repository *r);
/*
* Give a fast, rough count of the number of objects in the repository. This
* ignores loose objects completely. If you have a lot of them, then either
list_add_tail(&p->mru, &r->objects->packed_git_mru);
}
-void prepare_packed_git_the_repository(void)
+static void prepare_packed_git(struct repository *r)
{
struct alternate_object_database *alt;
- if (the_repository->objects->packed_git_initialized)
+ if (r->objects->packed_git_initialized)
return;
- prepare_packed_git_one(the_repository, get_object_directory(), 1);
- prepare_alt_odb(the_repository);
- for (alt = the_repository->objects->alt_odb_list; alt; alt = alt->next)
- prepare_packed_git_one(the_repository, alt->path, 0);
- rearrange_packed_git(the_repository);
- prepare_packed_git_mru(the_repository);
- the_repository->objects->packed_git_initialized = 1;
+ prepare_packed_git_one(r, r->objects->objectdir, 1);
+ prepare_alt_odb(r);
+ for (alt = r->objects->alt_odb_list; alt; alt = alt->next)
+ prepare_packed_git_one(r, alt->path, 0);
+ rearrange_packed_git(r);
+ prepare_packed_git_mru(r);
+ r->objects->packed_git_initialized = 1;
}
-void reprepare_packed_git_the_repository(void)
+void reprepare_packed_git(struct repository *r)
{
- the_repository->objects->approximate_object_count_valid = 0;
- the_repository->objects->packed_git_initialized = 0;
- prepare_packed_git(the_repository);
+ r->objects->approximate_object_count_valid = 0;
+ r->objects->packed_git_initialized = 0;
+ prepare_packed_git(r);
}
struct packed_git *get_packed_git(struct repository *r)
{
+ prepare_packed_git(r);
return r->objects->packed_git;
}
struct list_head *get_packed_git_mru(struct repository *r)
{
+ prepare_packed_git(r);
return &r->objects->packed_git_mru;
}
{
int type;
struct revindex_entry *revidx;
- const unsigned char *sha1;
+ struct object_id oid;
revidx = find_pack_revindex(p, obj_offset);
if (!revidx)
return OBJ_BAD;
- sha1 = nth_packed_object_sha1(p, revidx->nr);
- mark_bad_packed_object(p, sha1);
- type = sha1_object_info(sha1, NULL);
+ nth_packed_object_oid(&oid, p, revidx->nr);
+ mark_bad_packed_object(p, oid.hash);
+ type = oid_object_info(&oid, NULL);
if (type <= OBJ_NONE)
return OBJ_BAD;
return type;
*oi->disk_sizep = revidx[1].offset - obj_offset;
}
- if (oi->typep || oi->typename) {
+ if (oi->typep || oi->type_name) {
enum object_type ptot;
ptot = packed_to_object_type(p, obj_offset, type, &w_curs,
curpos);
if (oi->typep)
*oi->typep = ptot;
- if (oi->typename) {
- const char *tn = typename(ptot);
+ if (oi->type_name) {
+ const char *tn = type_name(ptot);
if (tn)
- strbuf_addstr(oi->typename, tn);
+ strbuf_addstr(oi->type_name, tn);
}
if (ptot < 0) {
type = OBJ_BAD;
unsigned long size;
};
-static void *read_object(const unsigned char *sha1, enum object_type *type,
+static void *read_object(const struct object_id *oid, enum object_type *type,
unsigned long *size)
{
struct object_info oi = OBJECT_INFO_INIT;
oi.sizep = size;
oi.contentp = &content;
- if (sha1_object_info_extended(sha1, &oi, 0) < 0)
+ if (oid_object_info_extended(oid, &oi, 0) < 0)
return NULL;
return content;
}
struct revindex_entry *revidx = find_pack_revindex(p, obj_offset);
off_t len = revidx[1].offset - obj_offset;
if (check_pack_crc(p, &w_curs, obj_offset, len, revidx->nr)) {
- const unsigned char *sha1 =
- nth_packed_object_sha1(p, revidx->nr);
+ struct object_id oid;
+ nth_packed_object_oid(&oid, p, revidx->nr);
error("bad packed object CRC for %s",
- sha1_to_hex(sha1));
- mark_bad_packed_object(p, sha1);
+ oid_to_hex(&oid));
+ mark_bad_packed_object(p, oid.hash);
data = NULL;
goto out;
}
* of a corrupted pack, and is better than failing outright.
*/
struct revindex_entry *revidx;
- const unsigned char *base_sha1;
+ struct object_id base_oid;
revidx = find_pack_revindex(p, obj_offset);
if (revidx) {
- base_sha1 = nth_packed_object_sha1(p, revidx->nr);
+ nth_packed_object_oid(&base_oid, p, revidx->nr);
error("failed to read delta base object %s"
" at offset %"PRIuMAX" from %s",
- sha1_to_hex(base_sha1), (uintmax_t)obj_offset,
+ oid_to_hex(&base_oid), (uintmax_t)obj_offset,
p->pack_name);
- mark_bad_packed_object(p, base_sha1);
- base = read_object(base_sha1, &type, &base_size);
+ mark_bad_packed_object(p, base_oid.hash);
+ base = read_object(&base_oid, &type, &base_size);
external_base = base;
}
}
return data;
}
+int bsearch_pack(const struct object_id *oid, const struct packed_git *p, uint32_t *result)
+{
+ const unsigned char *index_fanout = p->index_data;
+ const unsigned char *index_lookup;
+ int index_lookup_width;
+
+ if (!index_fanout)
+ BUG("bsearch_pack called without a valid pack-index");
+
+ index_lookup = index_fanout + 4 * 256;
+ if (p->index_version == 1) {
+ index_lookup_width = 24;
+ index_lookup += 4;
+ } else {
+ index_lookup_width = 20;
+ index_fanout += 8;
+ index_lookup += 8;
+ }
+
+ return bsearch_hash(oid->hash, (const uint32_t*)index_fanout,
+ index_lookup, index_lookup_width, result);
+}
+
const unsigned char *nth_packed_object_sha1(struct packed_git *p,
uint32_t n)
{
off_t find_pack_entry_one(const unsigned char *sha1,
struct packed_git *p)
{
- const uint32_t *level1_ofs = p->index_data;
const unsigned char *index = p->index_data;
- unsigned hi, lo, stride;
- static int debug_lookup = -1;
-
- if (debug_lookup < 0)
- debug_lookup = !!getenv("GIT_DEBUG_LOOKUP");
+ struct object_id oid;
+ uint32_t result;
if (!index) {
if (open_pack_index(p))
return 0;
- level1_ofs = p->index_data;
- index = p->index_data;
- }
- if (p->index_version > 1) {
- level1_ofs += 2;
- index += 8;
- }
- index += 4 * 256;
- hi = ntohl(level1_ofs[*sha1]);
- lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
- if (p->index_version > 1) {
- stride = 20;
- } else {
- stride = 24;
- index += 4;
}
- if (debug_lookup)
- printf("%02x%02x%02x... lo %u hi %u nr %"PRIu32"\n",
- sha1[0], sha1[1], sha1[2], lo, hi, p->num_objects);
-
- while (lo < hi) {
- unsigned mi = lo + (hi - lo) / 2;
- int cmp = hashcmp(index + mi * stride, sha1);
-
- if (debug_lookup)
- printf("lo %u hi %u rg %u mi %u\n",
- lo, hi, hi - lo, mi);
- if (!cmp)
- return nth_packed_object_offset(p, mi);
- if (cmp > 0)
- hi = mi;
- else
- lo = mi+1;
- }
+ hashcpy(oid.hash, sha1);
+ if (bsearch_pack(&oid, p, &result))
+ return nth_packed_object_offset(p, result);
return 0;
}
return 1;
}
-/*
- * Iff a pack file contains the object named by sha1, return true and
- * store its location to e.
- */
-int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
+int find_pack_entry(struct repository *r, const unsigned char *sha1, struct pack_entry *e)
{
struct list_head *pos;
- prepare_packed_git(the_repository);
- if (!the_repository->objects->packed_git)
+ prepare_packed_git(r);
+ if (!r->objects->packed_git)
return 0;
- list_for_each(pos, &the_repository->objects->packed_git_mru) {
+ list_for_each(pos, &r->objects->packed_git_mru) {
struct packed_git *p = list_entry(pos, struct packed_git, mru);
if (fill_pack_entry(sha1, e, p)) {
- list_move(&p->mru,
- &the_repository->objects->packed_git_mru);
+ list_move(&p->mru, &r->objects->packed_git_mru);
return 1;
}
}
int has_sha1_pack(const unsigned char *sha1)
{
struct pack_entry e;
- return find_pack_entry(sha1, &e);
+ return find_pack_entry(the_repository, sha1, &e);
}
int has_pack_index(const unsigned char *sha1)