Skip to content

Commit fba6818

Browse files
ttaylorrgitster
authored andcommitted
midx: factor out fill_pack_info()
When selecting which packfiles will be written while generating a MIDX, the MIDX internals fill out a 'struct pack_info' with various pieces of book-keeping. Instead of filling out each field of the `pack_info` structure individually in each of the two spots that modify the array of such structures (`ctx->info`), extract a common routine that does this for us. This reduces the code duplication by a modest amount. But more importantly, it zero-initializes the structure before assigning values into it. This hardens us for a future change which will add additional fields to this structure which (until this patch) was not zero-initialized. As a result, any new fields added to the `pack_info` structure need only be updated in a single location, instead of at each spot within midx.c. There are no functional changes in this patch. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a96015a commit fba6818

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

midx.c

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,17 @@ struct pack_info {
475475
unsigned expired : 1;
476476
};
477477

478+
static void fill_pack_info(struct pack_info *info,
479+
struct packed_git *p, const char *pack_name,
480+
uint32_t orig_pack_int_id)
481+
{
482+
memset(info, 0, sizeof(struct pack_info));
483+
484+
info->orig_pack_int_id = orig_pack_int_id;
485+
info->pack_name = xstrdup(pack_name);
486+
info->p = p;
487+
}
488+
478489
static int pack_info_compare(const void *_a, const void *_b)
479490
{
480491
struct pack_info *a = (struct pack_info *)_a;
@@ -515,6 +526,7 @@ static void add_pack_to_midx(const char *full_path, size_t full_path_len,
515526
const char *file_name, void *data)
516527
{
517528
struct write_midx_context *ctx = data;
529+
struct packed_git *p;
518530

519531
if (ends_with(file_name, ".idx")) {
520532
display_progress(ctx->progress, ++ctx->pack_paths_checked);
@@ -541,27 +553,22 @@ static void add_pack_to_midx(const char *full_path, size_t full_path_len,
541553

542554
ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
543555

544-
ctx->info[ctx->nr].p = add_packed_git(full_path,
545-
full_path_len,
546-
0);
547-
548-
if (!ctx->info[ctx->nr].p) {
556+
p = add_packed_git(full_path, full_path_len, 0);
557+
if (!p) {
549558
warning(_("failed to add packfile '%s'"),
550559
full_path);
551560
return;
552561
}
553562

554-
if (open_pack_index(ctx->info[ctx->nr].p)) {
563+
if (open_pack_index(p)) {
555564
warning(_("failed to open pack-index '%s'"),
556565
full_path);
557-
close_pack(ctx->info[ctx->nr].p);
558-
FREE_AND_NULL(ctx->info[ctx->nr].p);
566+
close_pack(p);
567+
free(p);
559568
return;
560569
}
561570

562-
ctx->info[ctx->nr].pack_name = xstrdup(file_name);
563-
ctx->info[ctx->nr].orig_pack_int_id = ctx->nr;
564-
ctx->info[ctx->nr].expired = 0;
571+
fill_pack_info(&ctx->info[ctx->nr], p, file_name, ctx->nr);
565572
ctx->nr++;
566573
}
567574
}
@@ -1321,11 +1328,6 @@ static int write_midx_internal(const char *object_dir,
13211328
for (i = 0; i < ctx.m->num_packs; i++) {
13221329
ALLOC_GROW(ctx.info, ctx.nr + 1, ctx.alloc);
13231330

1324-
ctx.info[ctx.nr].orig_pack_int_id = i;
1325-
ctx.info[ctx.nr].pack_name = xstrdup(ctx.m->pack_names[i]);
1326-
ctx.info[ctx.nr].p = ctx.m->packs[i];
1327-
ctx.info[ctx.nr].expired = 0;
1328-
13291331
if (flags & MIDX_WRITE_REV_INDEX) {
13301332
/*
13311333
* If generating a reverse index, need to have
@@ -1341,10 +1343,10 @@ static int write_midx_internal(const char *object_dir,
13411343
if (open_pack_index(ctx.m->packs[i]))
13421344
die(_("could not open index for %s"),
13431345
ctx.m->packs[i]->pack_name);
1344-
ctx.info[ctx.nr].p = ctx.m->packs[i];
13451346
}
13461347

1347-
ctx.nr++;
1348+
fill_pack_info(&ctx.info[ctx.nr++], ctx.m->packs[i],
1349+
ctx.m->pack_names[i], i);
13481350
}
13491351
}
13501352

0 commit comments

Comments
 (0)