commit-graph: introduce `commit_graph_generation_from_graph()`

In 2ee11f7261 (commit-graph: return generation from memory, 2023-03-20), the `commit_graph_generation()` function stopped returning zeros when asked to locate the generation number of a given commit. This was done at the time to prepare for a later change which set generation values in memory, meaning that we could no longer rely on `graph_pos` alone to tell us whether or not to trust the generation number returned by this function. In 2ee11f7261, it was noted that this change only impacted very old commit-graphs, which were written with all commits having generation number 0. Indeed, zero is not a valid generation number, so we should never expect to see that value outside of the aforementioned case. The test fallout in 2ee11f7261 indicated that we were no longer able to fsck a specific old case of commit-graph corruption, where we see a non-zero generation number after having seen a generation number of 0 earlier. Introduce a variant of `commit_graph_generation()` which behaves like that function did prior to 2ee11f7261, known as `commit_graph_generation_from_graph()`. Then use this function in the context of `verify_one_commit_graph()`, where we only want to trust the values from the graph. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Aug 21, 2023 at 17:34 UTC 868c991155b3392e0b1a6aab8daab89bb93400f4
2 files changed +13 -3
commit-graph.c
+12 -2
@@ -128,6 +128,16 @@ timestamp_t commit_graph_generation(const struct commit *c)
128 return GENERATION_NUMBER_INFINITY;
129 }
130
131 +static timestamp_t commit_graph_generation_from_graph(const struct commit *c)
132 +{
133 + struct commit_graph_data *data =
134 + commit_graph_data_slab_peek(&commit_graph_data_slab, c);
135 +
136 + if (!data || data->graph_pos == COMMIT_NOT_FROM_GRAPH)
137 + return GENERATION_NUMBER_INFINITY;
138 + return data->generation;
139 +}
140 +
141 static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
142 {
143 unsigned int i, nth_slab;
@@ -2659,7 +2669,7 @@ static int verify_one_commit_graph(struct repository *r,
2669 oid_to_hex(&graph_parents->item->object.oid),
2670 oid_to_hex(&odb_parents->item->object.oid));
2671
2662 - generation = commit_graph_generation(graph_parents->item);
2672 + generation = commit_graph_generation_from_graph(graph_parents->item);
2673 if (generation > max_generation)
2674 max_generation = generation;
2675
@@ -2671,7 +2681,7 @@ static int verify_one_commit_graph(struct repository *r,
2681 graph_report(_("commit-graph parent list for commit %s terminates early"),
2682 oid_to_hex(&cur_oid));
2683
2674 - if (!commit_graph_generation(graph_commit)) {
2684 + if (!commit_graph_generation_from_graph(graph_commit)) {
2685 if (generation_zero == GENERATION_NUMBER_EXISTS)
2686 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2687 oid_to_hex(&cur_oid));
t/t5318-commit-graph.sh
+1 -1
@@ -598,7 +598,7 @@ test_expect_success 'detect incorrect generation number' '
598
599 test_expect_success 'detect incorrect generation number' '
600 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\01" \
601 - "commit-graph generation for commit"
601 + "but zero elsewhere"
602 '
603
604 test_expect_success 'detect incorrect commit date' '