Skip to content

Commit 307d75b

Browse files
ttaylorrgitster
authored andcommitted
midx: implement midx_locate_pack()
The multi-pack index API exposes a `midx_contains_pack()` function that takes in a string ending in either ".idx" or ".pack" and returns whether or not the MIDX contains a given pack corresponding to that string. There is no corresponding function to locate the position of a pack within the MIDX's pack order (sorted lexically by pack filename). We could add an optional out parameter to `midx_contains_pack()` that is filled out with the pack's position when the parameter is non-NULL. To minimize the amount of fallout from this change, instead introduce a new function by renaming `midx_contains_pack()` to `midx_locate_pack()`, adding that output parameter, and then reimplementing `midx_contains_pack()` in terms of it. Future patches will make use of this new function. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5f5ccd9 commit 307d75b

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

midx.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,8 @@ static int cmp_idx_or_pack_name(const char *idx_or_pack_name,
428428
return strcmp(idx_or_pack_name, idx_name);
429429
}
430430

431-
int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name)
431+
int midx_locate_pack(struct multi_pack_index *m, const char *idx_or_pack_name,
432+
uint32_t *pos)
432433
{
433434
uint32_t first = 0, last = m->num_packs;
434435

@@ -439,8 +440,11 @@ int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name)
439440

440441
current = m->pack_names[mid];
441442
cmp = cmp_idx_or_pack_name(idx_or_pack_name, current);
442-
if (!cmp)
443+
if (!cmp) {
444+
if (pos)
445+
*pos = mid;
443446
return 1;
447+
}
444448
if (cmp > 0) {
445449
first = mid + 1;
446450
continue;
@@ -451,6 +455,11 @@ int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name)
451455
return 0;
452456
}
453457

458+
int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name)
459+
{
460+
return midx_locate_pack(m, idx_or_pack_name, NULL);
461+
}
462+
454463
int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local)
455464
{
456465
struct multi_pack_index *m;

midx.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ struct object_id *nth_midxed_object_oid(struct object_id *oid,
7070
struct multi_pack_index *m,
7171
uint32_t n);
7272
int fill_midx_entry(struct repository *r, const struct object_id *oid, struct pack_entry *e, struct multi_pack_index *m);
73-
int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name);
73+
int midx_contains_pack(struct multi_pack_index *m,
74+
const char *idx_or_pack_name);
75+
int midx_locate_pack(struct multi_pack_index *m, const char *idx_or_pack_name,
76+
uint32_t *pos);
7477
int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local);
7578

7679
/*

0 commit comments

Comments
 (0)