commit-graph: introduce `ensure_generations_valid()`

Use the just-introduced compute_reachable_generation_numbers_1() to implement a function which dynamically computes topological levels (or corrected commit dates) for out-of-graph commits. This will be useful for the ahead-behind algorithm we are about to introduce, which needs accurate topological levels on _all_ commits reachable from the tips in order to avoid over-counting. Co-authored-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Mar 20, 2023 at 11:26 UTC c08645b353514fe14dbd62cf52afd49d0e88146b
2 files changed +37
commit-graph.c
+29
@@ -1604,6 +1604,35 @@ static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1604 stop_progress(&ctx->progress);
1605 }
1606
1607 +static void set_generation_in_graph_data(struct commit *c, timestamp_t t,
1608 + void *data)
1609 +{
1610 + commit_graph_data_at(c)->generation = t;
1611 +}
1612 +
1613 +/*
1614 + * After this method, all commits reachable from those in the given
1615 + * list will have non-zero, non-infinite generation numbers.
1616 + */
1617 +void ensure_generations_valid(struct repository *r,
1618 + struct commit **commits, size_t nr)
1619 +{
1620 + int generation_version = get_configured_generation_version(r);
1621 + struct packed_commit_list list = {
1622 + .list = commits,
1623 + .alloc = nr,
1624 + .nr = nr,
1625 + };
1626 + struct compute_generation_info info = {
1627 + .r = r,
1628 + .commits = &list,
1629 + .get_generation = get_generation_from_graph_data,
1630 + .set_generation = set_generation_in_graph_data,
1631 + };
1632 +
1633 + compute_reachable_generation_numbers(&info, generation_version);
1634 +}
1635 +
1636 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1637 {
1638 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
commit-graph.h
+8
@@ -190,4 +190,12 @@ struct commit_graph_data {
190 */
191 timestamp_t commit_graph_generation(const struct commit *);
192 uint32_t commit_graph_position(const struct commit *);
193 +
194 +/*
195 + * After this method, all commits reachable from those in the given
196 + * list will have non-zero, non-infinite generation numbers.
197 + */
198 +void ensure_generations_valid(struct repository *r,
199 + struct commit **commits, size_t nr);
200 +
201 #endif