Commit

a51b77aa1d8ed033a018849465a65fd4e1dde02b

odb: provide infrastructure for pluggable fsck checks

The on-disk consistency checks in git-fsck(1) are conceptually
backend-specific: while connectivity checks and object-level parsing
checks are generic, verifying the physical integrity of packfiles and
loose objects is meaningful only to backends that use these formats:
Having these checks live in "builtin/fsck.c" violates that layering,
because it forces the command to reach directly into format-specific
internals.

Provide new infrastructure to make these format-specific checks
pluggable and implement stubs for the different source types we already
have. In subsequent commits we'll move functionality over piece by
piece.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt <ps@pks.im> · 2026-09-11 13:27 UTC · committed by Junio C Hamano

Browse files
8 files changed, +102 −5
modified builtin/fsck.c
@@ -965,7 +965,9 @@ int cmd_fsck(int argc,
965 965 const char *prefix,
966 966 struct repository *repo)
967 967 {
968 - int check_full = 1;
968 + struct odb_fsck_options odb_fsck_opts = {
969 + .flags = ODB_FSCK_FULL,
970 + };
969 971 int keep_cache_objects = 0;
970 972 int name_objects = 0;
971 973 int check_references = 1;
@@ -977,7 +979,8 @@ int cmd_fsck(int argc,
977 979 OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
978 980 OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
979 981 OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
980 - OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
982 + OPT_BIT(0, "full", &odb_fsck_opts.flags,
983 + N_("also consider packs and alternate objects"), ODB_FSCK_FULL),
981 984 OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
982 985 OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
983 986 OPT_BOOL(0, "lost-found", &write_lost_and_found,
@@ -1018,7 +1021,7 @@ int cmd_fsck(int argc,
1018 1021 show_progress = 0;
1019 1022
1020 1023 if (write_lost_and_found) {
1021 - check_full = 1;
1024 + odb_fsck_opts.flags |= ODB_FSCK_FULL;
1022 1025 include_reflogs = 0;
1023 1026 }
1024 1027
@@ -1047,10 +1050,13 @@ int cmd_fsck(int argc,
1047 1050 mark_object_for_connectivity, repo, 0);
1048 1051 } else {
1049 1052 for (source = repo->objects->sources; source; source = source->next)
1050 - if (check_full || source->local)
1053 + if ((odb_fsck_opts.flags & ODB_FSCK_FULL) || source->local)
1051 1054 fsck_source(repo, source);
1052 1055
1053 - if (check_full) {
1056 + if (odb_fsck(repo->objects, &odb_fsck_opts) < 0)
1057 + errors_found |= ERROR_OBJECT;
1058 +
1059 + if (odb_fsck_opts.flags & ODB_FSCK_FULL) {
1054 1060 struct packed_git *p;
1055 1061 uint32_t total = 0, count = 0;
1056 1062 struct progress *progress = NULL;
modified odb.c
@@ -1177,3 +1177,11 @@ void odb_reprepare(struct object_database *o)
1177 1177 {
1178 1178 odb_prepare(o, ODB_PREPARE_FLUSH_CACHES);
1179 1179 }
1180 +
1181 + int odb_fsck(struct object_database *odb, struct odb_fsck_options *options)
1182 + {
1183 + int ret = 0;
1184 + for (struct odb_source *source = odb->sources; source; source = source->next)
1185 + ret |= odb_source_fsck(source, options);
1186 + return ret;
1187 + }
modified odb.h
@@ -206,6 +206,29 @@ void odb_prepare(struct object_database *o, enum odb_prepare_flags flags);
206 206 /* Equivalent to `odb_prepare(o, ODB_PREPARE_FLUSH_CACHES)`. */
207 207 void odb_reprepare(struct object_database *o);
208 208
209 + enum odb_fsck_flags {
210 + /*
211 + * If set, perform a full consistency check for the full object
212 + * database, including all of its sources and the contents of their
213 + * optimized formats. Otherwise, only check the local source, and
214 + * restrict checks of its optimized formats to cheap structural
215 + * verification of their metadata.
216 + */
217 + ODB_FSCK_FULL = (1 << 0),
218 + };
219 +
220 + /* Options that shall be passed to `odb_fsck()`. */
221 + struct odb_fsck_options {
222 + enum odb_fsck_flags flags;
223 + };
224 +
225 + /*
226 + * Run backend-specific integrity checks on all object sources. Each source
227 + * performs the checks appropriate to its type. Returns 0 on success, a
228 + * negative error code otherwise.
229 + */
230 + int odb_fsck(struct object_database *odb, struct odb_fsck_options *opts);
231 +
209 232 /*
210 233 * Find source by its object directory path. Returns a `NULL` pointer in case
211 234 * the source could not be found.
modified odb/source-files.c
@@ -893,6 +893,21 @@ static int odb_source_files_generate_pack(struct odb_source *source UNUSED,
893 893 return 0;
894 894 }
895 895
896 + static int odb_source_files_fsck(struct odb_source *source,
897 + struct odb_fsck_options *opts)
898 + {
899 + struct odb_source_files *files = odb_source_files_downcast(source);
900 + int ret = 0;
901 +
902 + if (!(opts->flags & ODB_FSCK_FULL) && !source->local)
903 + return 0;
904 +
905 + ret |= odb_source_fsck(&files->loose->base, opts);
906 + ret |= odb_source_fsck(&files->packed->base, opts);
907 +
908 + return ret;
909 + }
910 +
896 911 struct odb_source_files *odb_source_files_new(struct object_database *odb,
897 912 const char *path,
898 913 bool local)
@@ -908,6 +923,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
908 923 files->base.close = odb_source_files_close;
909 924 files->base.create_on_disk = odb_source_files_create_on_disk;
910 925 files->base.prepare = odb_source_files_prepare;
926 + files->base.fsck = odb_source_files_fsck;
911 927 files->base.read_object_info = odb_source_files_read_object_info;
912 928 files->base.read_object_stream = odb_source_files_read_object_stream;
913 929 files->base.for_each_object = odb_source_files_for_each_object;
modified odb/source-inmemory.c
@@ -1,6 +1,7 @@
1 1 #include "git-compat-util.h"
2 2 #include "object-file.h"
3 3 #include "odb.h"
4 + #include "fsck.h"
4 5 #include "odb/source-inmemory.h"
5 6 #include "odb/streaming.h"
6 7 #include "oidtree.h"
@@ -368,6 +369,12 @@ static void odb_source_inmemory_free(struct odb_source *source)
368 369 free(inmemory);
369 370 }
370 371
372 + static int odb_source_inmemory_fsck(struct odb_source *source UNUSED,
373 + struct odb_fsck_options *opts UNUSED)
374 + {
375 + return 0;
376 + }
377 +
371 378 struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
372 379 {
373 380 struct odb_source_inmemory *source;
@@ -378,6 +385,7 @@ struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
378 385 source->base.free = odb_source_inmemory_free;
379 386 source->base.close = odb_source_inmemory_close;
380 387 source->base.prepare = odb_source_inmemory_prepare;
388 + source->base.fsck = odb_source_inmemory_fsck;
381 389 source->base.read_object_info = odb_source_inmemory_read_object_info;
382 390 source->base.read_object_stream = odb_source_inmemory_read_object_stream;
383 391 source->base.for_each_object = odb_source_inmemory_for_each_object;
modified odb/source-loose.c
@@ -1031,6 +1031,12 @@ static void odb_source_loose_free(struct odb_source *source)
1031 1031 free(loose);
1032 1032 }
1033 1033
1034 + static int odb_source_loose_fsck(struct odb_source *source UNUSED,
1035 + struct odb_fsck_options *opts UNUSED)
1036 + {
1037 + return 0;
1038 + }
1039 +
1034 1040 struct odb_source_loose *odb_source_loose_new(struct object_database *odb,
1035 1041 const char *path,
1036 1042 bool local)
@@ -1043,6 +1049,7 @@ struct odb_source_loose *odb_source_loose_new(struct object_database *odb,
1043 1049 loose->base.free = odb_source_loose_free;
1044 1050 loose->base.close = odb_source_loose_close;
1045 1051 loose->base.prepare = odb_source_loose_prepare;
1052 + loose->base.fsck = odb_source_loose_fsck;
1046 1053 loose->base.read_object_info = odb_source_loose_read_object_info;
1047 1054 loose->base.read_object_stream = odb_source_loose_read_object_stream;
1048 1055 loose->base.for_each_object = odb_source_loose_for_each_object;
modified odb/source-packed.c
@@ -2,6 +2,7 @@
2 2 #include "abspath.h"
3 3 #include "chdir-notify.h"
4 4 #include "dir.h"
5 + #include "fsck.h"
5 6 #include "git-zlib.h"
6 7 #include "list-objects-filter-options.h"
7 8 #include "mergesort.h"
@@ -826,6 +827,12 @@ static void odb_source_packed_free(struct odb_source *source)
826 827 free(packed);
827 828 }
828 829
830 + static int odb_source_packed_fsck(struct odb_source *source UNUSED,
831 + struct odb_fsck_options *opts UNUSED)
832 + {
833 + return 0;
834 + }
835 +
829 836 struct odb_source_packed *odb_source_packed_new(struct object_database *odb,
830 837 const char *path,
831 838 bool local)
@@ -839,6 +846,7 @@ struct odb_source_packed *odb_source_packed_new(struct object_database *odb,
839 846 packed->base.free = odb_source_packed_free;
840 847 packed->base.close = odb_source_packed_close;
841 848 packed->base.prepare = odb_source_packed_prepare;
849 + packed->base.fsck = odb_source_packed_fsck;
842 850 packed->base.read_object_info = odb_source_packed_read_object_info;
843 851 packed->base.read_object_stream = odb_source_packed_read_object_stream;
844 852 packed->base.for_each_object = odb_source_packed_for_each_object;
modified odb/source.h
@@ -320,6 +320,17 @@ struct odb_source {
320 320 int (*generate_pack)(struct odb_source *source,
321 321 struct odb_pack_generator **out,
322 322 const struct odb_generate_pack_options *opts);
323 +
324 + /*
325 + * This callback is expected to check the integrity of the object source
326 + * and report any errors found via the fsck options. The checks performed
327 + * are backend-specific.
328 + *
329 + * The callback is expected to return 0 on success, a negative error
330 + * code otherwise.
331 + */
332 + int (*fsck)(struct odb_source *source,
333 + struct odb_fsck_options *options);
323 334 };
324 335
325 336 /*
@@ -588,4 +599,14 @@ static inline int odb_source_generate_pack(struct odb_source *source,
588 599 return source->generate_pack(source, out, opts);
589 600 }
590 601
602 + /*
603 + * Check the integrity of the object database source. The checks performed
604 + * are backend-specific. Returns 0 on success, a negative error code otherwise.
605 + */
606 + static inline int odb_source_fsck(struct odb_source *source,
607 + struct odb_fsck_options *opts)
608 + {
609 + return source->fsck(source, opts);
610 + }
611 +
591 612 #endif