Skip to content

Commit 71049b4

Browse files
committed
midx: use raw oid data
A multi-pack index uses raw oid data, use a byte array to index into them.
1 parent 41d4ac5 commit 71049b4

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

src/libgit2/midx.c

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ static int midx_parse_oid_lookup(
115115
struct git_midx_chunk *chunk_oid_lookup)
116116
{
117117
uint32_t i;
118-
git_oid *oid, *prev_oid, zero_oid = {{0}};
118+
unsigned char *oid, *prev_oid, zero_oid[GIT_OID_RAWSZ] = {0};
119119

120120
if (chunk_oid_lookup->offset == 0)
121121
return midx_error("missing OID Lookup chunk");
@@ -124,10 +124,10 @@ static int midx_parse_oid_lookup(
124124
if (chunk_oid_lookup->length != idx->num_objects * GIT_OID_RAWSZ)
125125
return midx_error("OID Lookup chunk has wrong length");
126126

127-
idx->oid_lookup = oid = (git_oid *)(data + chunk_oid_lookup->offset);
128-
prev_oid = &zero_oid;
129-
for (i = 0; i < idx->num_objects; ++i, ++oid) {
130-
if (git_oid_cmp(prev_oid, oid) >= 0)
127+
idx->oid_lookup = oid = (unsigned char *)(data + chunk_oid_lookup->offset);
128+
prev_oid = zero_oid;
129+
for (i = 0; i < idx->num_objects; ++i, oid += GIT_OID_RAWSZ) {
130+
if (git_oid_raw_cmp(prev_oid, oid) >= 0)
131131
return midx_error("OID Lookup index is non-monotonic");
132132
prev_oid = oid;
133133
}
@@ -389,7 +389,7 @@ int git_midx_entry_find(
389389
int pos, found = 0;
390390
size_t pack_index;
391391
uint32_t hi, lo;
392-
const git_oid *current = NULL;
392+
unsigned char *current = NULL;
393393
const unsigned char *object_offset;
394394
off64_t offset;
395395

@@ -403,26 +403,25 @@ int git_midx_entry_find(
403403
if (pos >= 0) {
404404
/* An object matching exactly the oid was found */
405405
found = 1;
406-
current = idx->oid_lookup + pos;
406+
current = idx->oid_lookup + (pos * GIT_OID_RAWSZ);
407407
} else {
408408
/* No object was found */
409409
/* pos refers to the object with the "closest" oid to short_oid */
410410
pos = -1 - pos;
411411
if (pos < (int)idx->num_objects) {
412-
current = idx->oid_lookup + pos;
412+
current = idx->oid_lookup + (pos * GIT_OID_RAWSZ);
413413

414-
if (!git_oid_ncmp(short_oid, current, len))
414+
if (!git_oid_raw_ncmp(short_oid->id, current, len))
415415
found = 1;
416416
}
417417
}
418418

419419
if (found && len != GIT_OID_HEXSZ && pos + 1 < (int)idx->num_objects) {
420420
/* Check for ambiguousity */
421-
const git_oid *next = current + 1;
421+
const unsigned char *next = current + GIT_OID_RAWSZ;
422422

423-
if (!git_oid_ncmp(short_oid, next, len)) {
423+
if (!git_oid_raw_ncmp(short_oid->id, next, len))
424424
found = 2;
425-
}
426425
}
427426

428427
if (!found)
@@ -450,7 +449,7 @@ int git_midx_entry_find(
450449
return midx_error("invalid index into the packfile names table");
451450
e->pack_index = pack_index;
452451
e->offset = offset;
453-
git_oid_cpy(&e->sha1, current);
452+
git_oid_fromraw(&e->sha1, current);
454453
return 0;
455454
}
456455

@@ -459,13 +458,17 @@ int git_midx_foreach_entry(
459458
git_odb_foreach_cb cb,
460459
void *data)
461460
{
461+
git_oid oid;
462462
size_t i;
463463
int error;
464464

465465
GIT_ASSERT_ARG(idx);
466466

467467
for (i = 0; i < idx->num_objects; ++i) {
468-
if ((error = cb(&idx->oid_lookup[i], data)) != 0)
468+
if ((error = git_oid_fromraw(&oid, &idx->oid_lookup[i * GIT_OID_RAWSZ])) < 0)
469+
return error;
470+
471+
if ((error = cb(&oid, data)) != 0)
469472
return git_error_set_after_callback(error);
470473
}
471474

@@ -751,7 +754,7 @@ static int midx_write(
751754

752755
/* Fill the OID Lookup table. */
753756
git_vector_foreach (&object_entries, i, entry) {
754-
error = git_str_put(&oid_lookup, (const char *)&entry->sha1, sizeof(entry->sha1));
757+
error = git_str_put(&oid_lookup, (char *)&entry->sha1.id, GIT_OID_RAWSZ);
755758
if (error < 0)
756759
goto cleanup;
757760
}

src/libgit2/midx.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "map.h"
1818
#include "mwindow.h"
1919
#include "odb.h"
20+
#include "oid.h"
2021

2122
/*
2223
* A multi-pack-index file.
@@ -40,7 +41,7 @@ typedef struct git_midx_file {
4041
uint32_t num_objects;
4142

4243
/* The OID Lookup table. */
43-
git_oid *oid_lookup;
44+
unsigned char *oid_lookup;
4445

4546
/* The Object Offsets table. Each entry has two 4-byte fields with the pack index and the offset. */
4647
const unsigned char *object_offsets;

0 commit comments

Comments
 (0)