Compare

3db2defe2ffdcde8c5a9aa1de974d79edaaa1f33..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
9 files changed, +149 −69
modified builtin/fsck.c
@@ -37,10 +37,8 @@ static int show_root;
37 37 static int show_tags;
38 38 static int show_unreachable;
39 39 static int include_reflogs = 1;
40 - static int check_full = 1;
41 40 static int connectivity_only;
42 41 static int check_strict;
43 - static int keep_cache_objects;
44 42 static struct fsck_options fsck_walk_options;
45 43 static struct fsck_options fsck_obj_options;
46 44 static int errors_found;
@@ -48,8 +46,6 @@ static int write_lost_and_found;
48 46 static int verbose;
49 47 static int show_progress = -1;
50 48 static int show_dangling = 1;
51 - static int name_objects;
52 - static int check_references = 1;
53 49 static timestamp_t now;
54 50 #define ERROR_OBJECT 01
55 51 #define ERROR_REACHABLE 02
@@ -401,14 +397,27 @@ static void check_connectivity(struct repository *repo)
401 397 }
402 398 }
403 399
404 - static int fsck_obj(struct repository *repo,
405 - struct object *obj, void *buffer, unsigned long size)
400 + static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
401 + unsigned long size, void *buffer, int *eaten, void *cb_data)
406 402 {
403 + struct repository *repo = cb_data;
404 + struct object *obj;
407 405 int err;
408 406
409 - if (obj->flags & SEEN)
410 - return 0;
411 - obj->flags |= SEEN;
407 + /*
408 + * Note, buffer may be NULL if type is OBJ_BLOB. See
409 + * verify_packfile(), data_valid variable for details.
410 + */
411 + obj = parse_object_buffer(repo, oid, type, size, buffer, eaten);
412 + if (!obj) {
413 + errors_found |= ERROR_OBJECT;
414 + err = error(_("%s: object corrupt or missing"),
415 + oid_to_hex(oid));
416 + goto out;
417 + }
418 +
419 + obj->flags &= ~REACHABLE;
420 + obj->flags |= HAS_OBJ | SEEN;
412 421
413 422 if (verbose)
414 423 fprintf_ln(stderr, _("Checking %s %s"),
@@ -417,6 +426,7 @@ static int fsck_obj(struct repository *repo,
417 426
418 427 if (fsck_walk(obj, NULL, &fsck_obj_options))
419 428 objerror(repo, obj, _("broken links"));
429 +
420 430 err = fsck_object(obj, buffer, size, &fsck_obj_options);
421 431 if (err)
422 432 goto out;
@@ -442,32 +452,11 @@ static int fsck_obj(struct repository *repo,
442 452 }
443 453
444 454 out:
445 - if (obj->type == OBJ_TREE)
455 + if (obj && obj->type == OBJ_TREE)
446 456 free_tree_buffer((struct tree *)obj);
447 457 return err;
448 458 }
449 459
450 - static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
451 - unsigned long size, void *buffer, int *eaten, void *cb_data)
452 - {
453 - struct repository *repo = cb_data;
454 - struct object *obj;
455 -
456 - /*
457 - * Note, buffer may be NULL if type is OBJ_BLOB. See
458 - * verify_packfile(), data_valid variable for details.
459 - */
460 - obj = parse_object_buffer(repo, oid, type, size, buffer, eaten);
461 - if (!obj) {
462 - errors_found |= ERROR_OBJECT;
463 - return error(_("%s: object corrupt or missing"),
464 - oid_to_hex(oid));
465 - }
466 - obj->flags &= ~(REACHABLE | SEEN);
467 - obj->flags |= HAS_OBJ;
468 - return fsck_obj(repo, obj, buffer, size);
469 - }
470 -
471 460 static int default_refs;
472 461
473 462 static void fsck_handle_reflog_oid(struct repository *repo,
@@ -722,7 +711,6 @@ static int fsck_loose(const struct object_id *oid, const char *path,
722 711 void *cb_data)
723 712 {
724 713 struct for_each_loose_cb *data = cb_data;
725 - struct object *obj;
726 714 enum object_type type = OBJ_NONE;
727 715 size_t size;
728 716 void *contents = NULL;
@@ -751,21 +739,7 @@ static int fsck_loose(const struct object_id *oid, const char *path,
751 739 if (!contents && type != OBJ_BLOB)
752 740 BUG("read_loose_object streamed a non-blob");
753 741
754 - obj = parse_object_buffer(data->repo, oid, type, size,
755 - contents, &eaten);
756 -
757 - if (!obj) {
758 - errors_found |= ERROR_OBJECT;
759 - error(_("%s: object could not be parsed: %s"),
760 - oid_to_hex(oid), path);
761 - if (!eaten)
762 - free(contents);
763 - return 0; /* keep checking other objects */
764 - }
765 -
766 - obj->flags &= ~(REACHABLE | SEEN);
767 - obj->flags |= HAS_OBJ;
768 - if (fsck_obj(data->repo, obj, contents, size))
742 + if (fsck_obj_buffer(oid, type, size, contents, &eaten, data->repo))
769 743 errors_found |= ERROR_OBJECT;
770 744
771 745 if (!eaten)
@@ -986,30 +960,36 @@ static char const * const fsck_usage[] = {
986 960 NULL
987 961 };
988 962
989 - static struct option fsck_opts[] = {
990 - OPT__VERBOSE(&verbose, N_("be verbose")),
991 - OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
992 - OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
993 - OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
994 - OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
995 - OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
996 - OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
997 - OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
998 - OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
999 - OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
1000 - OPT_BOOL(0, "lost-found", &write_lost_and_found,
1001 - N_("write dangling objects in .git/lost-found")),
1002 - OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
1003 - OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
1004 - OPT_BOOL(0, "references", &check_references, N_("check reference database consistency")),
1005 - OPT_END(),
1006 - };
1007 -
1008 963 int cmd_fsck(int argc,
1009 964 const char **argv,
1010 965 const char *prefix,
1011 966 struct repository *repo)
1012 967 {
968 + struct odb_fsck_options odb_fsck_opts = {
969 + .flags = ODB_FSCK_FULL,
970 + };
971 + int keep_cache_objects = 0;
972 + int name_objects = 0;
973 + int check_references = 1;
974 + struct option fsck_opts[] = {
975 + OPT__VERBOSE(&verbose, N_("be verbose")),
976 + OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
977 + OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
978 + OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
979 + OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
980 + OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
981 + OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
982 + OPT_BIT(0, "full", &odb_fsck_opts.flags,
983 + N_("also consider packs and alternate objects"), ODB_FSCK_FULL),
984 + OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
985 + OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
986 + OPT_BOOL(0, "lost-found", &write_lost_and_found,
987 + N_("write dangling objects in .git/lost-found")),
988 + OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
989 + OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
990 + OPT_BOOL(0, "references", &check_references, N_("check reference database consistency")),
991 + OPT_END(),
992 + };
1013 993 struct odb_source *source;
1014 994 struct snapshot snap = {
1015 995 .nr = 0,
@@ -1041,7 +1021,7 @@ int cmd_fsck(int argc,
1041 1021 show_progress = 0;
1042 1022
1043 1023 if (write_lost_and_found) {
1044 - check_full = 1;
1024 + odb_fsck_opts.flags |= ODB_FSCK_FULL;
1045 1025 include_reflogs = 0;
1046 1026 }
1047 1027
@@ -1070,9 +1050,13 @@ int cmd_fsck(int argc,
1070 1050 mark_object_for_connectivity, repo, 0);
1071 1051 } else {
1072 1052 for (source = repo->objects->sources; source; source = source->next)
1073 - fsck_source(repo, source);
1053 + if ((odb_fsck_opts.flags & ODB_FSCK_FULL) || source->local)
1054 + fsck_source(repo, source);
1055 +
1056 + if (odb_fsck(repo->objects, &odb_fsck_opts) < 0)
1057 + errors_found |= ERROR_OBJECT;
1074 1058
1075 - if (check_full) {
1059 + if (odb_fsck_opts.flags & ODB_FSCK_FULL) {
1076 1060 struct packed_git *p;
1077 1061 uint32_t total = 0, count = 0;
1078 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
modified t/t1450-fsck.sh
@@ -844,6 +844,11 @@ test_expect_success 'alternate objects are correctly blamed' '
844 844 echo "../../alt.git/objects" >.git/objects/info/alternates &&
845 845 mkdir alt.git/objects/$(dirname $path) &&
846 846 >alt.git/objects/$(dirname $path)/$(basename $path) &&
847 +
848 + # Without "--full", only the local object source is checked.
849 + git fsck --no-full >out 2>&1 &&
850 + test_must_be_empty out &&
851 +
847 852 test_must_fail git fsck >out 2>&1 &&
848 853 test_grep alt.git out
849 854 '