reftable: add code to facilitate consistency checks

The `git refs verify` command is used to run consistency checks on the reference backends. This command is also invoked when users run 'git fsck'. While the files-backend has some fsck checks added, the reftable backend lacks such checks. Let's add the required infrastructure and a check to test for the files present in the reftable directory. Since the reftable library is treated as an independent library we should ensure that the library code works independently without knowledge about Git's internals. To do this, add both 'reftable/fsck.c' and 'reftable/reftable-fsck.h'. Which provide an entry point 'reftable_fsck_check' for running fsck checks over a provided reftable stack. The callee provides the function with callbacks to handle issue and information reporting. The added check, goes over all tables in the reftable stack validates that they have a valid name. It not, it raises an error. While here, move 'reftable/error.o' in the Makefile to retain lexicographic ordering. Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Oct 7, 2025 at 14:11 UTC 9051638519e7f9d52ce87d1baa88b35141f073aa
4 files changed +143 -1
Makefile
+2 -1
@@ -2729,9 +2729,10 @@ XDIFF_OBJS += xdiff/xutils.o
2729 xdiff-objs: $(XDIFF_OBJS)
2730
2731 REFTABLE_OBJS += reftable/basics.o
2732 -REFTABLE_OBJS += reftable/error.o
2732 REFTABLE_OBJS += reftable/block.o
2733 REFTABLE_OBJS += reftable/blocksource.o
2734 +REFTABLE_OBJS += reftable/error.o
2735 +REFTABLE_OBJS += reftable/fsck.o
2736 REFTABLE_OBJS += reftable/iter.o
2737 REFTABLE_OBJS += reftable/merged.o
2738 REFTABLE_OBJS += reftable/pq.o
meson.build
+1
@@ -452,6 +452,7 @@ libgit_sources = [
452 'reftable/error.c',
453 'reftable/block.c',
454 'reftable/blocksource.c',
455 + 'reftable/fsck.c',
456 'reftable/iter.c',
457 'reftable/merged.c',
458 'reftable/pq.c',
reftable/fsck.c new
+100
@@ -0,0 +1,100 @@
1 +#include "basics.h"
2 +#include "reftable-fsck.h"
3 +#include "reftable-table.h"
4 +#include "stack.h"
5 +
6 +static bool table_has_valid_name(const char *name)
7 +{
8 + const char *ptr = name;
9 + char *endptr;
10 +
11 + /* strtoull doesn't set errno on success */
12 + errno = 0;
13 +
14 + strtoull(ptr, &endptr, 16);
15 + if (errno)
16 + return false;
17 + ptr = endptr;
18 +
19 + if (*ptr != '-')
20 + return false;
21 + ptr++;
22 +
23 + strtoull(ptr, &endptr, 16);
24 + if (errno)
25 + return false;
26 + ptr = endptr;
27 +
28 + if (*ptr != '-')
29 + return false;
30 + ptr++;
31 +
32 + strtoul(ptr, &endptr, 16);
33 + if (errno)
34 + return false;
35 + ptr = endptr;
36 +
37 + if (strcmp(ptr, ".ref") && strcmp(ptr, ".log"))
38 + return false;
39 +
40 + return true;
41 +}
42 +
43 +typedef int (*table_check_fn)(struct reftable_table *table,
44 + reftable_fsck_report_fn report_fn,
45 + void *cb_data);
46 +
47 +static int table_check_name(struct reftable_table *table,
48 + reftable_fsck_report_fn report_fn,
49 + void *cb_data)
50 +{
51 + if (!table_has_valid_name(table->name)) {
52 + struct reftable_fsck_info info;
53 +
54 + info.error = REFTABLE_FSCK_ERROR_TABLE_NAME;
55 + info.msg = "invalid reftable table name";
56 + info.path = table->name;
57 +
58 + return report_fn(&info, cb_data);
59 + }
60 +
61 + return 0;
62 +}
63 +
64 +static int table_checks(struct reftable_table *table,
65 + reftable_fsck_report_fn report_fn,
66 + reftable_fsck_verbose_fn verbose_fn UNUSED,
67 + void *cb_data)
68 +{
69 + table_check_fn table_check_fns[] = {
70 + table_check_name,
71 + NULL,
72 + };
73 + int err = 0;
74 +
75 + for (size_t i = 0; table_check_fns[i]; i++)
76 + err |= table_check_fns[i](table, report_fn, cb_data);
77 +
78 + return err;
79 +}
80 +
81 +int reftable_fsck_check(struct reftable_stack *stack,
82 + reftable_fsck_report_fn report_fn,
83 + reftable_fsck_verbose_fn verbose_fn,
84 + void *cb_data)
85 +{
86 + struct reftable_buf msg = REFTABLE_BUF_INIT;
87 + int err = 0;
88 +
89 + for (size_t i = 0; i < stack->tables_len; i++) {
90 + reftable_buf_reset(&msg);
91 + reftable_buf_addstr(&msg, "Checking table: ");
92 + reftable_buf_addstr(&msg, stack->tables[i]->name);
93 + verbose_fn(msg.buf, cb_data);
94 +
95 + err |= table_checks(stack->tables[i], report_fn, verbose_fn, cb_data);
96 + }
97 +
98 + reftable_buf_release(&msg);
99 + return err;
100 +}
reftable/reftable-fsck.h new
+40
@@ -0,0 +1,40 @@
1 +#ifndef REFTABLE_FSCK_H
2 +#define REFTABLE_FSCK_H
3 +
4 +#include "reftable-stack.h"
5 +
6 +enum reftable_fsck_error {
7 + /* Invalid table name */
8 + REFTABLE_FSCK_ERROR_TABLE_NAME = 0,
9 + /* Used for bounds checking, must be last */
10 + REFTABLE_FSCK_MAX_VALUE,
11 +};
12 +
13 +/* Represents an individual error encountered during the FSCK checks. */
14 +struct reftable_fsck_info {
15 + enum reftable_fsck_error error;
16 + const char *msg;
17 + const char *path;
18 +};
19 +
20 +typedef int reftable_fsck_report_fn(struct reftable_fsck_info *info,
21 + void *cb_data);
22 +typedef void reftable_fsck_verbose_fn(const char *msg, void *cb_data);
23 +
24 +/*
25 + * Given a reftable stack, perform consistency checks on the stack.
26 + *
27 + * If an issue is encountered, the issue is reported to the callee via the
28 + * provided 'report_fn'. If the issue is non-recoverable the flow will not
29 + * continue. If it is recoverable, the flow will continue and further issues
30 + * will be reported as identified.
31 + *
32 + * The 'verbose_fn' will be invoked to provide verbose information about
33 + * the progress and state of the consistency checks.
34 + */
35 +int reftable_fsck_check(struct reftable_stack *stack,
36 + reftable_fsck_report_fn report_fn,
37 + reftable_fsck_verbose_fn verbose_fn,
38 + void *cb_data);
39 +
40 +#endif /* REFTABLE_FSCK_H */