Skip to content

Commit 4a58a56

Browse files
committed
Merge branch 'tb/prepare-midx-pack-cleanup' into seen
* tb/prepare-midx-pack-cleanup: midx: return a `packed_git` pointer from `prepare_midx_pack()` midx-write.c: extract inner loop from fill_packs_from_midx() midx-write.c: simplify fill_packs_from_midx() midx-write.c: guard against incremental MIDXs in want_included_pack() pack-bitmap.c: fix broken warning() when missing MIDX'd pack
2 parents f74c2ba + dbef682 commit 4a58a56

File tree

4 files changed

+118
-91
lines changed

4 files changed

+118
-91
lines changed

midx-write.c

Lines changed: 70 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -938,44 +938,65 @@ static struct multi_pack_index *lookup_multi_pack_index(struct repository *r,
938938
return result;
939939
}
940940

941-
static int fill_packs_from_midx(struct write_midx_context *ctx,
942-
const char *preferred_pack_name, uint32_t flags)
941+
static int fill_packs_from_midx_1(struct write_midx_context *ctx,
942+
struct multi_pack_index *m,
943+
uint32_t flags)
943944
{
944-
struct multi_pack_index *m;
945-
946-
for (m = ctx->m; m; m = m->base_midx) {
947-
uint32_t i;
945+
for (uint32_t i = 0; i < m->num_packs; i++) {
946+
struct packed_git *p = NULL;
948947

949-
for (i = 0; i < m->num_packs; i++) {
950-
ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
948+
ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
951949

952-
/*
953-
* If generating a reverse index, need to have
954-
* packed_git's loaded to compare their
955-
* mtimes and object count.
956-
*
957-
* If a preferred pack is specified, need to
958-
* have packed_git's loaded to ensure the chosen
959-
* preferred pack has a non-zero object count.
960-
*/
961-
if (flags & MIDX_WRITE_REV_INDEX ||
962-
preferred_pack_name) {
963-
if (prepare_midx_pack(ctx->repo, m,
964-
m->num_packs_in_base + i)) {
965-
error(_("could not load pack"));
966-
return 1;
967-
}
968-
969-
if (open_pack_index(m->packs[i]))
970-
die(_("could not open index for %s"),
971-
m->packs[i]->pack_name);
950+
/*
951+
* If generating a reverse index, need to have
952+
* packed_git's loaded to compare their
953+
* mtimes and object count.
954+
*/
955+
if (flags & MIDX_WRITE_REV_INDEX) {
956+
p = prepare_midx_pack(ctx->repo, m,
957+
m->num_packs_in_base + i);
958+
if (!p) {
959+
error(_("could not load pack"));
960+
return 1;
972961
}
973962

974-
fill_pack_info(&ctx->info[ctx->nr++], m->packs[i],
975-
m->pack_names[i],
976-
m->num_packs_in_base + i);
963+
if (open_pack_index(p))
964+
die(_("could not open index for %s"),
965+
p->pack_name);
977966
}
967+
968+
fill_pack_info(&ctx->info[ctx->nr++], p,
969+
m->pack_names[i],
970+
m->num_packs_in_base + i);
971+
}
972+
973+
return 0;
974+
}
975+
976+
static int fill_packs_from_midx(struct write_midx_context *ctx,
977+
const char *preferred_pack_name, uint32_t flags)
978+
{
979+
struct multi_pack_index *m;
980+
981+
if (preferred_pack_name) {
982+
/*
983+
* If a preferred pack is specified, need to have
984+
* packed_git's loaded to ensure the chosen preferred
985+
* pack has a non-zero object count.
986+
*
987+
* Trick ourselves into thinking that we're writing a
988+
* reverse index in this case in order to open up the
989+
* pack index file.
990+
*/
991+
flags |= MIDX_WRITE_REV_INDEX;
978992
}
993+
994+
for (m = ctx->m; m; m = m->base_midx) {
995+
int ret = fill_packs_from_midx_1(ctx, m, flags);
996+
if (ret)
997+
return ret;
998+
}
999+
9791000
return 0;
9801001
}
9811002

@@ -1578,20 +1599,19 @@ int expire_midx_packs(struct repository *r, const char *object_dir, unsigned fla
15781599
_("Finding and deleting unreferenced packfiles"),
15791600
m->num_packs);
15801601
for (i = 0; i < m->num_packs; i++) {
1602+
struct packed_git *p;
15811603
char *pack_name;
15821604
display_progress(progress, i + 1);
15831605

15841606
if (count[i])
15851607
continue;
15861608

1587-
if (prepare_midx_pack(r, m, i))
1609+
p = prepare_midx_pack(r, m, i);
1610+
if (!p || p->pack_keep || p->is_cruft)
15881611
continue;
15891612

1590-
if (m->packs[i]->pack_keep || m->packs[i]->is_cruft)
1591-
continue;
1592-
1593-
pack_name = xstrdup(m->packs[i]->pack_name);
1594-
close_pack(m->packs[i]);
1613+
pack_name = xstrdup(p->pack_name);
1614+
close_pack(p);
15951615

15961616
string_list_insert(&packs_to_drop, m->pack_names[i]);
15971617
unlink_pack_path(pack_name, 0);
@@ -1636,9 +1656,12 @@ static int want_included_pack(struct repository *r,
16361656
uint32_t pack_int_id)
16371657
{
16381658
struct packed_git *p;
1639-
if (prepare_midx_pack(r, m, pack_int_id))
1659+
1660+
ASSERT(m && !m->base_midx);
1661+
1662+
p = prepare_midx_pack(r, m, pack_int_id);
1663+
if (!p)
16401664
return 0;
1641-
p = m->packs[pack_int_id];
16421665
if (!pack_kept_objects && p->pack_keep)
16431666
return 0;
16441667
if (p->is_cruft)
@@ -1655,6 +1678,8 @@ static void fill_included_packs_all(struct repository *r,
16551678
uint32_t i;
16561679
int pack_kept_objects = 0;
16571680

1681+
ASSERT(m && !m->base_midx);
1682+
16581683
repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects);
16591684

16601685
for (i = 0; i < m->num_packs; i++) {
@@ -1675,17 +1700,18 @@ static void fill_included_packs_batch(struct repository *r,
16751700
struct repack_info *pack_info;
16761701
int pack_kept_objects = 0;
16771702

1703+
ASSERT(m && !m->base_midx);
1704+
16781705
CALLOC_ARRAY(pack_info, m->num_packs);
16791706

16801707
repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects);
16811708

16821709
for (i = 0; i < m->num_packs; i++) {
1683-
pack_info[i].pack_int_id = i;
1684-
1685-
if (prepare_midx_pack(r, m, i))
1686-
continue;
1710+
struct packed_git *p = prepare_midx_pack(r, m, i);
16871711

1688-
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;
16891715
}
16901716

16911717
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,9 +488,9 @@ 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"),
493-
bitmap_git->midx->pack_names[i]);
493+
bitmap_git->midx->pack_names[i - bitmap_git->midx->num_packs_in_base]);
494494
goto cleanup;
495495
}
496496
}

0 commit comments

Comments
 (0)