commit-graph: rewrite to use checksum_valid()

Rewrite an existing caller in `git commit-graph verify` to take advantage of checksum_valid(). Note that the replacement isn't a verbatim cut-and-paste, since the new function avoids using hashfile at all and instead talks to the_hash_algo directly, but it is functionally equivalent. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Jun 23, 2021 at 14:39 UTC 15316a4732eeb0dab27ba406cb80e8704cb9b46d
1 file changed +6 -8
commit-graph.c
+6 -8
@@ -2422,14 +2422,16 @@ static void graph_report(const char *fmt, ...)
2422 #define GENERATION_ZERO_EXISTS 1
2423 #define GENERATION_NUMBER_EXISTS 2
2424
2425 +static int commit_graph_checksum_valid(struct commit_graph *g)
2426 +{
2427 + return hashfile_checksum_valid(g->data, g->data_len);
2428 +}
2429 +
2430 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2431 {
2432 uint32_t i, cur_fanout_pos = 0;
2433 struct object_id prev_oid, cur_oid;
2429 - unsigned char checksum[GIT_MAX_HEXSZ];
2434 int generation_zero = 0;
2431 - struct hashfile *f;
2432 - int devnull;
2435 struct progress *progress = NULL;
2436 int local_error = 0;
2437
@@ -2442,11 +2444,7 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2444 if (verify_commit_graph_error)
2445 return verify_commit_graph_error;
2446
2445 - devnull = open("/dev/null", O_WRONLY);
2446 - f = hashfd(devnull, NULL);
2447 - hashwrite(f, g->data, g->data_len - g->hash_len);
2448 - finalize_hashfile(f, checksum, CSUM_CLOSE);
2449 - if (!hasheq(checksum, g->data + g->data_len - g->hash_len)) {
2447 + if (!commit_graph_checksum_valid(g)) {
2448 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2449 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2450 }