Skip to content

Commit dbef682

Browse files
ttaylorrgitster
authored andcommitted
midx: return a packed_git pointer from prepare_midx_pack()
The prepare_midx_pack() function is designed to convert a MIDX-specific pack_int_id for a given pack into a pointer into an actual `packed_git` structure. In general, these calls look something like: struct packed_git *p; if (prepare_midx_pack(the_repository, some_midx, some_pack_int_id)) die("could not load pack xyz"); p = some_midx->packs[some_pack_int_id]; , and in a pre-incremental MIDX world, this pattern works well. However, in a post-incremental MIDX world, this pattern is a little more prone to errors. These errors can happen when the given 'pack_int_id' is not usable as an index into the 'm->packs' array. And this happens in all layers but the bottom-most one in an incremental MIDX chain. Each layer stores only the packs that are local to that layer of the chain, and offsets them by the total number of packs in the base MIDX(s). But there is other awkwardness here. Thinking back to the above snippet, suppose that the pack with ID 'some_pack_int_id' is in a layer in the middle of the MIDX chain. Then it is still invalid to do: p = some_midx->packs[some_pack_int_id - some_midx->num_packs_in_base]; , becuase the top-most layer (here 'some_midx') may not even have that pack! So we would have to chase down the '->base_midx' pointer in order to get the correct result. midx.c has a helper to do this (called 'midx_for_pack()'), but it is meant only for internal use. That means that a full, working version of the above adjusted to handle incremental MIDXs would look something like: struct packed_git *p; if (prepare_midx_pack(the_repository, some_midx, some_pack_int_id)) die("could not load pack xyz"); while (m && pack_int_id < m->num_packs_in_base) m = m->base_midx; if (!m) BUG("broken midx?"); if (pack_int_id >= m->num_packs + m->num_packs_in_base) BUG("broken pack_int_id?"); p = m->packs[pack_int_id - m->num_packs_in_base]; , which is far too verbose to access a single pack by its pack_int_id in a MIDX chain. Let's instead have prepare_midx_pack() return a pointer to the packed_git structure itself, hiding the above as an implementation detail of prepare_midx_pack(). This patch turns the above snippet into: struct packed_git *p = prepare_midx_pack(the_repository, some_midx, some_pack_int_id); if (!p) die("could not load pack xyz"); making it far easier and less error-prone to access packs by their pack_int_id in a MIDX chain. (In the future, we may want to consider similar treatment for, e.g., the pack_names array. Likewise, it might make sense to rename the "packs" member of the MIDX structure to suggest that it shouldn't be accessed directly outside of midx.c.) Suggested-by: Jeff King <[email protected]> Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d222530 commit dbef682

File tree

4 files changed

+66
-64
lines changed

4 files changed

+66
-64
lines changed

midx-write.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,8 @@ static int fill_packs_from_midx_1(struct write_midx_context *ctx,
943943
uint32_t flags)
944944
{
945945
for (uint32_t i = 0; i < m->num_packs; i++) {
946+
struct packed_git *p = NULL;
947+
946948
ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
947949

948950
/*
@@ -951,18 +953,19 @@ static int fill_packs_from_midx_1(struct write_midx_context *ctx,
951953
* mtimes and object count.
952954
*/
953955
if (flags & MIDX_WRITE_REV_INDEX) {
954-
if (prepare_midx_pack(ctx->repo, m,
955-
m->num_packs_in_base + i)) {
956+
p = prepare_midx_pack(ctx->repo, m,
957+
m->num_packs_in_base + i);
958+
if (!p) {
956959
error(_("could not load pack"));
957960
return 1;
958961
}
959962

960-
if (open_pack_index(m->packs[i]))
963+
if (open_pack_index(p))
961964
die(_("could not open index for %s"),
962-
m->packs[i]->pack_name);
965+
p->pack_name);
963966
}
964967

965-
fill_pack_info(&ctx->info[ctx->nr++], m->packs[i],
968+
fill_pack_info(&ctx->info[ctx->nr++], p,
966969
m->pack_names[i],
967970
m->num_packs_in_base + i);
968971
}
@@ -1596,20 +1599,19 @@ int expire_midx_packs(struct repository *r, const char *object_dir, unsigned fla
15961599
_("Finding and deleting unreferenced packfiles"),
15971600
m->num_packs);
15981601
for (i = 0; i < m->num_packs; i++) {
1602+
struct packed_git *p;
15991603
char *pack_name;
16001604
display_progress(progress, i + 1);
16011605

16021606
if (count[i])
16031607
continue;
16041608

1605-
if (prepare_midx_pack(r, m, i))
1606-
continue;
1607-
1608-
if (m->packs[i]->pack_keep || m->packs[i]->is_cruft)
1609+
p = prepare_midx_pack(r, m, i);
1610+
if (!p || p->pack_keep || p->is_cruft)
16091611
continue;
16101612

1611-
pack_name = xstrdup(m->packs[i]->pack_name);
1612-
close_pack(m->packs[i]);
1613+
pack_name = xstrdup(p->pack_name);
1614+
close_pack(p);
16131615

16141616
string_list_insert(&packs_to_drop, m->pack_names[i]);
16151617
unlink_pack_path(pack_name, 0);
@@ -1657,9 +1659,9 @@ static int want_included_pack(struct repository *r,
16571659

16581660
ASSERT(m && !m->base_midx);
16591661

1660-
if (prepare_midx_pack(r, m, pack_int_id))
1662+
p = prepare_midx_pack(r, m, pack_int_id);
1663+
if (!p)
16611664
return 0;
1662-
p = m->packs[pack_int_id];
16631665
if (!pack_kept_objects && p->pack_keep)
16641666
return 0;
16651667
if (p->is_cruft)
@@ -1705,12 +1707,11 @@ static void fill_included_packs_batch(struct repository *r,
17051707
repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects);
17061708

17071709
for (i = 0; i < m->num_packs; i++) {
1708-
pack_info[i].pack_int_id = i;
1710+
struct packed_git *p = prepare_midx_pack(r, m, i);
17091711

1710-
if (prepare_midx_pack(r, m, i))
1711-
continue;
1712-
1713-
pack_info[i].mtime = m->packs[i]->mtime;
1712+
pack_info[i].pack_int_id = i;
1713+
if (p)
1714+
pack_info[i].mtime = p->mtime;
17141715
}
17151716

17161717
for (i = 0; i < m->num_objects; i++) {

midx.c

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -449,50 +449,48 @@ static uint32_t midx_for_pack(struct multi_pack_index **_m,
449449
return pack_int_id - m->num_packs_in_base;
450450
}
451451

452-
int prepare_midx_pack(struct repository *r, struct multi_pack_index *m,
453-
uint32_t pack_int_id)
452+
struct packed_git *prepare_midx_pack(struct repository *r,
453+
struct multi_pack_index *m,
454+
uint32_t pack_int_id)
454455
{
455-
struct strbuf pack_name = STRBUF_INIT;
456-
struct strbuf key = STRBUF_INIT;
457-
struct packed_git *p;
458-
459-
pack_int_id = midx_for_pack(&m, pack_int_id);
460-
461-
if (m->packs[pack_int_id] == (void *)(intptr_t)-1)
462-
return 1;
463-
if (m->packs[pack_int_id])
464-
return 0;
465-
466-
strbuf_addf(&pack_name, "%s/pack/%s", m->object_dir,
467-
m->pack_names[pack_int_id]);
468-
469-
/* pack_map holds the ".pack" name, but we have the .idx */
470-
strbuf_addbuf(&key, &pack_name);
471-
strbuf_strip_suffix(&key, ".idx");
472-
strbuf_addstr(&key, ".pack");
473-
p = hashmap_get_entry_from_hash(&r->objects->pack_map,
474-
strhash(key.buf), key.buf,
475-
struct packed_git, packmap_ent);
476-
if (!p) {
477-
p = add_packed_git(r, pack_name.buf, pack_name.len, m->local);
478-
if (p) {
479-
install_packed_git(r, p);
480-
list_add_tail(&p->mru, &r->objects->packed_git_mru);
456+
uint32_t pack_pos = midx_for_pack(&m, pack_int_id);
457+
458+
if (!m->packs[pack_pos]) {
459+
struct strbuf pack_name = STRBUF_INIT;
460+
struct strbuf key = STRBUF_INIT;
461+
struct packed_git *p;
462+
463+
strbuf_addf(&pack_name, "%s/pack/%s", m->object_dir,
464+
m->pack_names[pack_pos]);
465+
466+
/* pack_map holds the ".pack" name, but we have the .idx */
467+
strbuf_addbuf(&key, &pack_name);
468+
strbuf_strip_suffix(&key, ".idx");
469+
strbuf_addstr(&key, ".pack");
470+
p = hashmap_get_entry_from_hash(&r->objects->pack_map,
471+
strhash(key.buf), key.buf,
472+
struct packed_git, packmap_ent);
473+
if (!p) {
474+
p = add_packed_git(r, pack_name.buf, pack_name.len,
475+
m->local);
476+
if (p) {
477+
install_packed_git(r, p);
478+
list_add_tail(&p->mru,
479+
&r->objects->packed_git_mru);
480+
}
481481
}
482-
}
483482

484-
strbuf_release(&pack_name);
485-
strbuf_release(&key);
483+
strbuf_release(&pack_name);
484+
strbuf_release(&key);
486485

487-
if (!p) {
488-
m->packs[pack_int_id] = (void *)(intptr_t)-1;
489-
return 1;
486+
m->packs[pack_pos] = p ? p : (void *)(intptr_t)-1;
487+
if (p)
488+
p->multi_pack_index = 1;
490489
}
491490

492-
p->multi_pack_index = 1;
493-
m->packs[pack_int_id] = p;
494-
495-
return 0;
491+
if (m->packs[pack_pos] == (void *)(intptr_t)-1)
492+
return NULL;
493+
return m->packs[pack_pos];
496494
}
497495

498496
struct packed_git *nth_midxed_pack(struct multi_pack_index *m,
@@ -514,10 +512,11 @@ int nth_bitmapped_pack(struct repository *r, struct multi_pack_index *m,
514512
if (!m->chunk_bitmapped_packs)
515513
return error(_("MIDX does not contain the BTMP chunk"));
516514

517-
if (prepare_midx_pack(r, m, pack_int_id))
518-
return error(_("could not load bitmapped pack %"PRIu32), pack_int_id);
515+
bp->p = prepare_midx_pack(r, m, pack_int_id);
516+
if (!bp->p)
517+
return error(_("could not load bitmapped pack %"PRIu32),
518+
pack_int_id);
519519

520-
bp->p = m->packs[local_pack_int_id];
521520
bp->bitmap_pos = get_be32((char *)m->chunk_bitmapped_packs +
522521
MIDX_CHUNK_BITMAPPED_PACKS_WIDTH * local_pack_int_id);
523522
bp->bitmap_nr = get_be32((char *)m->chunk_bitmapped_packs +
@@ -614,9 +613,9 @@ int fill_midx_entry(struct repository *r,
614613
midx_for_object(&m, pos);
615614
pack_int_id = nth_midxed_pack_int_id(m, pos);
616615

617-
if (prepare_midx_pack(r, m, pack_int_id))
616+
p = prepare_midx_pack(r, m, pack_int_id);
617+
if (!p)
618618
return 0;
619-
p = m->packs[pack_int_id - m->num_packs_in_base];
620619

621620
/*
622621
* We are about to tell the caller where they can locate the
@@ -917,7 +916,7 @@ int verify_midx_file(struct repository *r, const char *object_dir, unsigned flag
917916
_("Looking for referenced packfiles"),
918917
m->num_packs + m->num_packs_in_base);
919918
for (i = 0; i < m->num_packs + m->num_packs_in_base; i++) {
920-
if (prepare_midx_pack(r, m, i))
919+
if (!prepare_midx_pack(r, m, i))
921920
midx_report("failed to load pack in position %d", i);
922921

923922
display_progress(progress, i + 1);

midx.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ void get_split_midx_filename_ext(const struct git_hash_algo *hash_algo,
104104
struct multi_pack_index *load_multi_pack_index(struct repository *r,
105105
const char *object_dir,
106106
int local);
107-
int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id);
107+
struct packed_git *prepare_midx_pack(struct repository *r,
108+
struct multi_pack_index *m,
109+
uint32_t pack_int_id);
108110
struct packed_git *nth_midxed_pack(struct multi_pack_index *m,
109111
uint32_t pack_int_id);
110112
int nth_bitmapped_pack(struct repository *r, struct multi_pack_index *m,

pack-bitmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
488488
}
489489

490490
for (i = 0; i < bitmap_git->midx->num_packs + bitmap_git->midx->num_packs_in_base; i++) {
491-
if (prepare_midx_pack(bitmap_repo(bitmap_git), bitmap_git->midx, i)) {
491+
if (!prepare_midx_pack(bitmap_repo(bitmap_git), bitmap_git->midx, i)) {
492492
warning(_("could not open pack %s"),
493493
bitmap_git->midx->pack_names[i - bitmap_git->midx->num_packs_in_base]);
494494
goto cleanup;

0 commit comments

Comments
 (0)