commit-graph: avoid repeated mixed generation number warnings

When validating that a commit-graph has either all zero, or all non-zero generation numbers, we emit a warning on both the rising and falling edge of transitioning between the two. So if we are unfortunate enough to see a commit-graph which has a repeating sequence of zero, then non-zero generation numbers, we'll generate many warnings that contain more or less the same information. Avoid this by keeping track of a single example for a commit with zero- and non-zero generation, and emit a single warning at the end of verification if both are non-NULL. Co-authored-by: Jeff King <peff@peff.net> Signed-off-by: Jeff King <peff@peff.net> 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 db6044d76261a996c03ce8f1e08240f326a42e15
2 files changed +15 -18
commit-graph.c
+13 -16
@@ -2560,9 +2560,6 @@ static void graph_report(const char *fmt, ...)
2560 va_end(ap);
2561 }
2562
2563 -#define GENERATION_ZERO_EXISTS 1
2564 -#define GENERATION_NUMBER_EXISTS 2
2565 -
2563 static int commit_graph_checksum_valid(struct commit_graph *g)
2564 {
2565 return hashfile_checksum_valid(g->data, g->data_len);
@@ -2575,7 +2572,8 @@ static int verify_one_commit_graph(struct repository *r,
2572 {
2573 uint32_t i, cur_fanout_pos = 0;
2574 struct object_id prev_oid, cur_oid;
2578 - int generation_zero = 0;
2575 + struct commit *seen_gen_zero = NULL;
2576 + struct commit *seen_gen_non_zero = NULL;
2577
2578 verify_commit_graph_error = verify_commit_graph_lite(g);
2579 if (verify_commit_graph_error)
@@ -2681,19 +2679,12 @@ static int verify_one_commit_graph(struct repository *r,
2679 graph_report(_("commit-graph parent list for commit %s terminates early"),
2680 oid_to_hex(&cur_oid));
2681
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));
2688 - generation_zero = GENERATION_ZERO_EXISTS;
2689 - } else {
2690 - if (generation_zero == GENERATION_ZERO_EXISTS)
2691 - graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2692 - oid_to_hex(&cur_oid));
2693 - generation_zero = GENERATION_NUMBER_EXISTS;
2694 - }
2682 + if (commit_graph_generation_from_graph(graph_commit))
2683 + seen_gen_non_zero = graph_commit;
2684 + else
2685 + seen_gen_zero = graph_commit;
2686
2696 - if (generation_zero == GENERATION_ZERO_EXISTS)
2687 + if (seen_gen_zero)
2688 continue;
2689
2690 /*
@@ -2719,6 +2710,12 @@ static int verify_one_commit_graph(struct repository *r,
2710 odb_commit->date);
2711 }
2712
2713 + if (seen_gen_zero && seen_gen_non_zero)
2714 + graph_report(_("commit-graph has both zero and non-zero "
2715 + "generations (e.g., commits '%s' and '%s')"),
2716 + oid_to_hex(&seen_gen_zero->object.oid),
2717 + oid_to_hex(&seen_gen_non_zero->object.oid));
2718 +
2719 return verify_commit_graph_error;
2720 }
2721
t/t5318-commit-graph.sh
+2 -2
@@ -620,12 +620,12 @@ test_expect_success 'detect incorrect chunk count' '
620
621 test_expect_success 'detect mixed generation numbers (non-zero to zero)' '
622 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION_LAST "\0\0\0\0" \
623 - "but non-zero elsewhere"
623 + "both zero and non-zero generations"
624 '
625
626 test_expect_success 'detect mixed generation numbers (zero to non-zero)' '
627 corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\0\0\0\0" \
628 - "but zero elsewhere"
628 + "both zero and non-zero generations"
629 '
630
631 test_expect_success 'git fsck (checks commit-graph when config set to true)' '