midx: use commit_stack
Simplify collection commits in a callback function by passing it a commit_stack pointer all the way from the caller, instead of using separate variables for array and item count and a bunch of intermediate members in struct bitmap_commit_cb. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Dec 24, 2025 at 18:03 UTC
041c557171f160174cc40b0583adf411cef9e316
1 file changed
+12
-23
midx-write.c
+12
-23
@@ -723,9 +723,7 @@ static int add_ref_to_pending(const struct reference *ref, void *cb_data)
723
}
724
725
struct bitmap_commit_cb {
726
- struct commit **commits;
727
- size_t commits_nr, commits_alloc;
728
-
726
+ struct commit_stack *commits;
727
struct write_midx_context *ctx;
728
};
729
@@ -745,8 +743,7 @@ static void bitmap_show_commit(struct commit *commit, void *_data)
743
if (pos < 0)
744
return;
745
748
- ALLOC_GROW(data->commits, data->commits_nr + 1, data->commits_alloc);
749
- data->commits[data->commits_nr++] = commit;
746
+ commit_stack_push(data->commits, commit);
747
}
748
749
static int read_refs_snapshot(const char *refs_snapshot,
@@ -784,17 +781,15 @@ static int read_refs_snapshot(const char *refs_snapshot,
781
return 0;
782
}
783
787
-static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr_p,
788
- const char *refs_snapshot,
789
- struct write_midx_context *ctx)
784
+static void find_commits_for_midx_bitmap(struct commit_stack *commits,
785
+ const char *refs_snapshot,
786
+ struct write_midx_context *ctx)
787
{
788
struct rev_info revs;
792
- struct bitmap_commit_cb cb = {0};
789
+ struct bitmap_commit_cb cb = { .commits = commits, .ctx = ctx };
790
791
trace2_region_enter("midx", "find_commits_for_midx_bitmap", ctx->repo);
792
796
- cb.ctx = ctx;
797
-
793
repo_init_revisions(ctx->repo, &revs, NULL);
794
if (refs_snapshot) {
795
read_refs_snapshot(refs_snapshot, &revs);
@@ -823,14 +818,10 @@ static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr
818
die(_("revision walk setup failed"));
819
820
traverse_commit_list(&revs, bitmap_show_commit, NULL, &cb);
826
- if (indexed_commits_nr_p)
827
- *indexed_commits_nr_p = cb.commits_nr;
821
822
release_revisions(&revs);
823
824
trace2_region_leave("midx", "find_commits_for_midx_bitmap", ctx->repo);
832
-
833
- return cb.commits;
825
}
826
827
static int write_midx_bitmap(struct write_midx_context *ctx,
@@ -1375,15 +1366,14 @@ static int write_midx_internal(struct odb_source *source,
1366
1367
if (flags & MIDX_WRITE_BITMAP) {
1368
struct packing_data pdata;
1378
- struct commit **commits;
1379
- uint32_t commits_nr;
1369
+ struct commit_stack commits = COMMIT_STACK_INIT;
1370
1371
if (!ctx.entries_nr)
1372
BUG("cannot write a bitmap without any objects");
1373
1374
prepare_midx_packing_data(&pdata, &ctx);
1375
1386
- commits = find_commits_for_midx_bitmap(&commits_nr, refs_snapshot, &ctx);
1376
+ find_commits_for_midx_bitmap(&commits, refs_snapshot, &ctx);
1377
1378
/*
1379
* The previous steps translated the information from
@@ -1394,17 +1384,16 @@ static int write_midx_internal(struct odb_source *source,
1384
FREE_AND_NULL(ctx.entries);
1385
ctx.entries_nr = 0;
1386
1397
- if (write_midx_bitmap(&ctx,
1398
- midx_hash, &pdata, commits, commits_nr,
1399
- flags) < 0) {
1387
+ if (write_midx_bitmap(&ctx, midx_hash, &pdata,
1388
+ commits.items, commits.nr, flags) < 0) {
1389
error(_("could not write multi-pack bitmap"));
1390
clear_packing_data(&pdata);
1402
- free(commits);
1391
+ commit_stack_clear(&commits);
1392
goto cleanup;
1393
}
1394
1395
clear_packing_data(&pdata);
1407
- free(commits);
1396
+ commit_stack_clear(&commits);
1397
}
1398
/*
1399
* NOTE: Do not use ctx.entries beyond this point, since it might