Compare

3db2defe2ffdcde8c5a9aa1de974d79edaaa1f33..0d5ebb323b19c92ea6d5e31f932c040267760466

builtin/fsck: move loose object verification into the loose source

The consistency checks for loose objects are hosted by "builtin/fsck.c".
These checks are obviously specific to the "loose" backend.

Move the logic into `odb_source_loose_fsck()`. Introduce a new "verbose"
flag so that we can properly retain semantics around whether or not we
want to print some status messages.

Note that this fixes a bug as a side effect: the progress meter was
captured in the callback data before `start_progress()` was even called,
so the per-subdirectory progress updates always operated on a NULL
pointer and the meter jumped straight from 0 to 256 upon completion. The
new code only sets up the callback data's progress meter after it has
been created, so the progress display now advances incrementally again.

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
14 files changed, +396 −259
modified builtin/fsck.c
@@ -7,13 +7,11 @@
7 7 #include "blob.h"
8 8 #include "tag.h"
9 9 #include "refs.h"
10 - #include "pack.h"
11 10 #include "cache-tree.h"
12 11 #include "fsck.h"
13 12 #include "parse-options.h"
14 13 #include "progress.h"
15 14 #include "packfile.h"
16 - #include "object-file.h"
17 15 #include "object-name.h"
18 16 #include "odb.h"
19 17 #include "odb/streaming.h"
@@ -24,8 +22,6 @@
24 22 #include "run-command.h"
25 23 #include "sparse-index.h"
26 24 #include "worktree.h"
27 - #include "pack-revindex.h"
28 - #include "pack-bitmap.h"
29 25
30 26 #define REACHABLE 0x0001
31 27 #define SEEN 0x0002
@@ -37,10 +33,8 @@ static int show_root;
37 33 static int show_tags;
38 34 static int show_unreachable;
39 35 static int include_reflogs = 1;
40 - static int check_full = 1;
41 36 static int connectivity_only;
42 37 static int check_strict;
43 - static int keep_cache_objects;
44 38 static struct fsck_options fsck_walk_options;
45 39 static struct fsck_options fsck_obj_options;
46 40 static int errors_found;
@@ -48,17 +42,11 @@ static int write_lost_and_found;
48 42 static int verbose;
49 43 static int show_progress = -1;
50 44 static int show_dangling = 1;
51 - static int name_objects;
52 - static int check_references = 1;
53 45 static timestamp_t now;
54 46 #define ERROR_OBJECT 01
55 47 #define ERROR_REACHABLE 02
56 - #define ERROR_PACK 04
57 48 #define ERROR_REFS 010
58 49 #define ERROR_COMMIT_GRAPH 020
59 - #define ERROR_MULTI_PACK_INDEX 040
60 - #define ERROR_PACK_REV_INDEX 0100
61 - #define ERROR_BITMAP 0200
62 50
63 51 static const char *describe_object(const struct object_id *oid)
64 52 {
@@ -401,14 +389,27 @@ static void check_connectivity(struct repository *repo)
401 389 }
402 390 }
403 391
404 - static int fsck_obj(struct repository *repo,
405 - struct object *obj, void *buffer, unsigned long size)
392 + static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
393 + unsigned long size, void *buffer, int *eaten, void *cb_data)
406 394 {
395 + struct repository *repo = cb_data;
396 + struct object *obj;
407 397 int err;
408 398
409 - if (obj->flags & SEEN)
410 - return 0;
411 - obj->flags |= SEEN;
399 + /*
400 + * Note, buffer may be NULL if type is OBJ_BLOB. See
401 + * verify_packfile(), data_valid variable for details.
402 + */
403 + obj = parse_object_buffer(repo, oid, type, size, buffer, eaten);
404 + if (!obj) {
405 + errors_found |= ERROR_OBJECT;
406 + err = error(_("%s: object corrupt or missing"),
407 + oid_to_hex(oid));
408 + goto out;
409 + }
410 +
411 + obj->flags &= ~REACHABLE;
412 + obj->flags |= HAS_OBJ | SEEN;
412 413
413 414 if (verbose)
414 415 fprintf_ln(stderr, _("Checking %s %s"),
@@ -417,6 +418,7 @@ static int fsck_obj(struct repository *repo,
417 418
418 419 if (fsck_walk(obj, NULL, &fsck_obj_options))
419 420 objerror(repo, obj, _("broken links"));
421 +
420 422 err = fsck_object(obj, buffer, size, &fsck_obj_options);
421 423 if (err)
422 424 goto out;
@@ -442,32 +444,11 @@ static int fsck_obj(struct repository *repo,
442 444 }
443 445
444 446 out:
445 - if (obj->type == OBJ_TREE)
447 + if (obj && obj->type == OBJ_TREE)
446 448 free_tree_buffer((struct tree *)obj);
447 449 return err;
448 450 }
449 451
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 452 static int default_refs;
472 453
473 454 static void fsck_handle_reflog_oid(struct repository *repo,
@@ -713,103 +694,6 @@ static void process_refs(struct repository *repo, struct snapshot *snap)
713 694 }
714 695 }
715 696
716 - struct for_each_loose_cb {
717 - struct repository *repo;
718 - struct progress *progress;
719 - };
720 -
721 - static int fsck_loose(const struct object_id *oid, const char *path,
722 - void *cb_data)
723 - {
724 - struct for_each_loose_cb *data = cb_data;
725 - struct object *obj;
726 - enum object_type type = OBJ_NONE;
727 - size_t size;
728 - void *contents = NULL;
729 - int eaten;
730 - struct object_info oi = OBJECT_INFO_INIT;
731 - struct object_id real_oid = *null_oid(data->repo->hash_algo);
732 - int err = 0;
733 -
734 - oi.sizep = &size;
735 - oi.typep = &type;
736 -
737 - if (read_loose_object(data->repo, path, oid, &real_oid, &contents, &oi) < 0) {
738 - if (contents && !oideq(&real_oid, oid))
739 - err = error(_("%s: hash-path mismatch, found at: %s"),
740 - oid_to_hex(&real_oid), path);
741 - else
742 - err = error(_("%s: object corrupt or missing: %s"),
743 - oid_to_hex(oid), path);
744 - }
745 - if (err < 0) {
746 - errors_found |= ERROR_OBJECT;
747 - free(contents);
748 - return 0; /* keep checking other objects */
749 - }
750 -
751 - if (!contents && type != OBJ_BLOB)
752 - BUG("read_loose_object streamed a non-blob");
753 -
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))
769 - errors_found |= ERROR_OBJECT;
770 -
771 - if (!eaten)
772 - free(contents);
773 - return 0; /* keep checking other objects, even if we saw an error */
774 - }
775 -
776 - static int fsck_cruft(const char *basename, const char *path,
777 - void *data UNUSED)
778 - {
779 - if (!starts_with(basename, "tmp_obj_"))
780 - fprintf_ln(stderr, _("bad sha1 file: %s"), path);
781 - return 0;
782 - }
783 -
784 - static int fsck_subdir(unsigned int nr, const char *path UNUSED, void *data)
785 - {
786 - struct for_each_loose_cb *cb_data = data;
787 - struct progress *progress = cb_data->progress;
788 - display_progress(progress, nr + 1);
789 - return 0;
790 - }
791 -
792 - static void fsck_source(struct repository *repo, struct odb_source *source)
793 - {
794 - struct progress *progress = NULL;
795 - struct for_each_loose_cb cb_data = {
796 - .repo = source->odb->repo,
797 - .progress = progress,
798 - };
799 -
800 - if (verbose)
801 - fprintf_ln(stderr, _("Checking object directory"));
802 -
803 - if (show_progress)
804 - progress = start_progress(repo,
805 - _("Checking object directories"), 256);
806 -
807 - for_each_loose_file_in_source(source, fsck_loose,
808 - fsck_cruft, fsck_subdir, &cb_data);
809 - display_progress(progress, 256);
810 - stop_progress(&progress);
811 - }
812 -
813 697 static int fsck_cache_tree(struct repository *repo, struct cache_tree *it, const char *index_path)
814 698 {
815 699 int i;
@@ -918,40 +802,6 @@ static int mark_object_for_connectivity(const struct object_id *oid,
918 802 return 0;
919 803 }
920 804
921 - static int check_pack_rev_indexes(struct repository *r, int show_progress)
922 - {
923 - struct progress *progress = NULL;
924 - struct packed_git *p;
925 - uint32_t pack_count = 0;
926 - int res = 0;
927 -
928 - if (show_progress) {
929 - repo_for_each_pack(r, p)
930 - pack_count++;
931 - progress = start_delayed_progress(r,
932 - "Verifying reverse pack-indexes", pack_count);
933 - pack_count = 0;
934 - }
935 -
936 - repo_for_each_pack(r, p) {
937 - int load_error = load_pack_revindex_from_disk(p);
938 -
939 - if (load_error < 0) {
940 - error(_("unable to load rev-index for pack '%s'"), p->pack_name);
941 - res = ERROR_PACK_REV_INDEX;
942 - } else if (!load_error &&
943 - !load_pack_revindex(r, p) &&
944 - verify_pack_revindex(p)) {
945 - error(_("invalid rev-index for pack '%s'"), p->pack_name);
946 - res = ERROR_PACK_REV_INDEX;
947 - }
948 - display_progress(progress, ++pack_count);
949 - }
950 - stop_progress(&progress);
951 -
952 - return res;
953 - }
954 -
955 805 static void fsck_refs(struct repository *r)
956 806 {
957 807 struct child_process refs_verify = CHILD_PROCESS_INIT;
@@ -986,30 +836,38 @@ static char const * const fsck_usage[] = {
986 836 NULL
987 837 };
988 838
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 839 int cmd_fsck(int argc,
1009 840 const char **argv,
1010 841 const char *prefix,
1011 842 struct repository *repo)
1012 843 {
844 + struct odb_fsck_options odb_fsck_opts = {
845 + .flags = ODB_FSCK_FULL,
846 + .object_cb = fsck_obj_buffer,
847 + .object_payload = repo,
848 + };
849 + int keep_cache_objects = 0;
850 + int name_objects = 0;
851 + int check_references = 1;
852 + struct option fsck_opts[] = {
853 + OPT__VERBOSE(&verbose, N_("be verbose")),
854 + OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
855 + OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
856 + OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
857 + OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
858 + OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
859 + OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
860 + OPT_BIT(0, "full", &odb_fsck_opts.flags,
861 + N_("also consider packs and alternate objects"), ODB_FSCK_FULL),
862 + OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
863 + OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
864 + OPT_BOOL(0, "lost-found", &write_lost_and_found,
865 + N_("write dangling objects in .git/lost-found")),
866 + OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
867 + OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
868 + OPT_BOOL(0, "references", &check_references, N_("check reference database consistency")),
869 + OPT_END(),
870 + };
1013 871 struct odb_source *source;
1014 872 struct snapshot snap = {
1015 873 .nr = 0,
@@ -1037,11 +895,15 @@ int cmd_fsck(int argc,
1037 895
1038 896 if (show_progress == -1)
1039 897 show_progress = isatty(2);
1040 - if (verbose)
898 + if (verbose) {
1041 899 show_progress = 0;
900 + odb_fsck_opts.flags |= ODB_FSCK_VERBOSE;
901 + }
902 + if (show_progress)
903 + odb_fsck_opts.flags |= ODB_FSCK_PROGRESS;
1042 904
1043 905 if (write_lost_and_found) {
1044 - check_full = 1;
906 + odb_fsck_opts.flags |= ODB_FSCK_FULL;
1045 907 include_reflogs = 0;
1046 908 }
1047 909
@@ -1069,35 +931,8 @@ int cmd_fsck(int argc,
1069 931 odb_for_each_object(repo->objects, NULL,
1070 932 mark_object_for_connectivity, repo, 0);
1071 933 } else {
1072 - for (source = repo->objects->sources; source; source = source->next)
1073 - fsck_source(repo, source);
1074 -
1075 - if (check_full) {
1076 - struct packed_git *p;
1077 - uint32_t total = 0, count = 0;
1078 - struct progress *progress = NULL;
1079 -
1080 - if (show_progress) {
1081 - repo_for_each_pack(repo, p) {
1082 - if (open_pack_index(p))
1083 - continue;
1084 - total += p->num_objects;
1085 - }
1086 -
1087 - progress = start_progress(repo,
1088 - _("Checking objects"), total);
1089 - }
1090 -
1091 - repo_for_each_pack(repo, p) {
1092 - /* verify gives error messages itself */
1093 - if (verify_pack(repo,
1094 - p, fsck_obj_buffer, repo,
1095 - progress, count))
1096 - errors_found |= ERROR_PACK;
1097 - count += p->num_objects;
1098 - }
1099 - stop_progress(&progress);
1100 - }
934 + if (odb_fsck(repo->objects, &odb_fsck_opts) < 0)
935 + errors_found |= ERROR_OBJECT;
1101 936
1102 937 if (fsck_finish(&fsck_obj_options))
1103 938 errors_found |= ERROR_OBJECT;
@@ -1145,10 +980,6 @@ int cmd_fsck(int argc,
1145 980 free_worktrees(worktrees);
1146 981 }
1147 982
1148 - errors_found |= check_pack_rev_indexes(repo, show_progress);
1149 - if (verify_bitmap_files(repo))
1150 - errors_found |= ERROR_BITMAP;
1151 -
1152 983 check_connectivity(repo);
1153 984
1154 985 if (repo->settings.core_commit_graph) {
@@ -1168,23 +999,6 @@ int cmd_fsck(int argc,
1168 999 }
1169 1000 }
1170 1001
1171 - if (repo->settings.core_multi_pack_index) {
1172 - struct child_process midx_verify = CHILD_PROCESS_INIT;
1173 -
1174 - for (source = repo->objects->sources; source; source = source->next) {
1175 - child_process_init(&midx_verify);
1176 - midx_verify.git_cmd = 1;
1177 - strvec_pushl(&midx_verify.args, "multi-pack-index",
1178 - "verify", "--object-dir", source->path, NULL);
1179 - if (show_progress)
1180 - strvec_push(&midx_verify.args, "--progress");
1181 - else
1182 - strvec_push(&midx_verify.args, "--no-progress");
1183 - if (run_command(&midx_verify))
1184 - errors_found |= ERROR_MULTI_PACK_INDEX;
1185 - }
1186 - }
1187 -
1188 1002 free_snapshot_refs(&snap);
1189 1003 return errors_found;
1190 1004 }
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,39 @@ 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 + /* Display a progress meter, if sensible. */
220 + ODB_FSCK_PROGRESS = (1 << 1),
221 +
222 + /* Be extra verbose when checking the database. */
223 + ODB_FSCK_VERBOSE = (1 << 2),
224 + };
225 +
226 + /* Options that shall be passed to `odb_fsck()`. */
227 + struct odb_fsck_options {
228 + enum odb_fsck_flags flags;
229 +
230 + int (*object_cb)(const struct object_id *oid, enum object_type type,
231 + unsigned long size, void *buffer, int *eaten, void *cb_data);
232 + void *object_payload;
233 + };
234 +
235 + /*
236 + * Run backend-specific integrity checks on all object sources. Each source
237 + * performs the checks appropriate to its type. Returns 0 on success, a
238 + * negative error code otherwise.
239 + */
240 + int odb_fsck(struct object_database *odb, struct odb_fsck_options *opts);
241 +
209 242 /*
210 243 * Find source by its object directory path. Returns a `NULL` pointer in case
211 244 * 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
@@ -12,6 +12,7 @@
12 12 #include "odb/streaming.h"
13 13 #include "oidtree.h"
14 14 #include "path.h"
15 + #include "progress.h"
15 16 #include "repository.h"
16 17 #include "strbuf.h"
17 18 #include "tempfile.h"
@@ -1031,6 +1032,96 @@ static void odb_source_loose_free(struct odb_source *source)
1031 1032 free(loose);
1032 1033 }
1033 1034
1035 + struct fsck_loose_data {
1036 + struct odb_source_loose *source;
1037 + struct odb_fsck_options *opts;
1038 + struct progress *progress;
1039 + bool error_found;
1040 + };
1041 +
1042 + static int fsck_loose(const struct object_id *oid, const char *path,
1043 + void *cb_data)
1044 + {
1045 + struct fsck_loose_data *data = cb_data;
1046 + enum object_type type = OBJ_NONE;
1047 + size_t size;
1048 + void *contents = NULL;
1049 + int eaten = 0;
1050 + struct object_info oi = OBJECT_INFO_INIT;
1051 + struct object_id real_oid = *null_oid(data->source->base.odb->repo->hash_algo);
1052 + int err = 0;
1053 +
1054 + oi.sizep = &size;
1055 + oi.typep = &type;
1056 +
1057 + if (read_loose_object(data->source->base.odb->repo,
1058 + path, oid, &real_oid, &contents, &oi) < 0) {
1059 + if (contents && !oideq(&real_oid, oid))
1060 + err = error(_("%s: hash-path mismatch, found at: %s"),
1061 + oid_to_hex(&real_oid), path);
1062 + else
1063 + err = error(_("%s: object corrupt or missing: %s"),
1064 + oid_to_hex(oid), path);
1065 + }
1066 + if (err < 0)
1067 + goto out;
1068 +
1069 + if (!contents && type != OBJ_BLOB)
1070 + BUG("read_loose_object streamed a non-blob");
1071 +
1072 + if (data->opts->object_cb(oid, type, size, contents, &eaten,
1073 + data->opts->object_payload)) {
1074 + err = -1;
1075 + goto out;
1076 + }
1077 +
1078 + out:
1079 + if (err)
1080 + data->error_found = true;
1081 + if (!eaten)
1082 + free(contents);
1083 + return 0; /* keep checking other objects, even if we saw an error */
1084 + }
1085 +
1086 + static int fsck_cruft(const char *basename, const char *path,
1087 + void *data UNUSED)
1088 + {
1089 + if (!starts_with(basename, "tmp_obj_"))
1090 + fprintf_ln(stderr, _("bad sha1 file: %s"), path);
1091 + return 0;
1092 + }
1093 +
1094 + static int fsck_subdir(unsigned int nr, const char *path UNUSED, void *cb_data)
1095 + {
1096 + struct fsck_loose_data *data = cb_data;
1097 + display_progress(data->progress, nr + 1);
1098 + return 0;
1099 + }
1100 +
1101 + static int odb_source_loose_fsck(struct odb_source *source,
1102 + struct odb_fsck_options *opts)
1103 + {
1104 + struct odb_source_loose *loose = odb_source_loose_downcast(source);
1105 + struct fsck_loose_data data = {
1106 + .source = loose,
1107 + .opts = opts,
1108 + };
1109 +
1110 + if (opts->flags & ODB_FSCK_VERBOSE)
1111 + fprintf_ln(stderr, _("Checking object directory"));
1112 +
1113 + if (opts->flags & ODB_FSCK_PROGRESS)
1114 + data.progress = start_progress(source->odb->repo,
1115 + _("Checking object directories"), 256);
1116 +
1117 + for_each_loose_file_in_source(source, fsck_loose,
1118 + fsck_cruft, fsck_subdir, &data);
1119 + display_progress(data.progress, 256);
1120 + stop_progress(&data.progress);
1121 +
1122 + return data.error_found ? -1 : 0;
1123 + }
1124 +
1034 1125 struct odb_source_loose *odb_source_loose_new(struct object_database *odb,
1035 1126 const char *path,
1036 1127 bool local)
@@ -1043,6 +1134,7 @@ struct odb_source_loose *odb_source_loose_new(struct object_database *odb,
1043 1134 loose->base.free = odb_source_loose_free;
1044 1135 loose->base.close = odb_source_loose_close;
1045 1136 loose->base.prepare = odb_source_loose_prepare;
1137 + loose->base.fsck = odb_source_loose_fsck;
1046 1138 loose->base.read_object_info = odb_source_loose_read_object_info;
1047 1139 loose->base.read_object_stream = odb_source_loose_read_object_stream;
1048 1140 loose->base.for_each_object = odb_source_loose_for_each_object;
modified odb/source-packed.c
@@ -2,14 +2,19 @@
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"
8 9 #include "midx.h"
9 10 #include "odb/source-packed.h"
10 11 #include "odb/streaming.h"
12 + #include "pack.h"
13 + #include "pack-revindex.h"
11 14 #include "packfile.h"
12 15 #include "pack-bitmap.h"
16 + #include "progress.h"
17 + #include "run-command.h"
13 18
14 19 static int find_pack_entry(struct odb_source_packed *store,
15 20 const struct object_id *oid,
@@ -826,6 +831,117 @@ static void odb_source_packed_free(struct odb_source *source)
826 831 free(packed);
827 832 }
828 833
834 + static int verify_packs(struct odb_source_packed *source,
835 + struct odb_fsck_options *opts)
836 + {
837 + struct progress *progress = NULL;
838 + struct packfile_list_entry *e;
839 + uint32_t total = 0, count = 0;
840 + int ret = 0;
841 +
842 + if (opts->flags & ODB_FSCK_PROGRESS) {
843 + for (e = packfile_store_get_packs(source); e; e = e->next) {
844 + if (open_pack_index(e->pack))
845 + continue;
846 + total += e->pack->num_objects;
847 + }
848 +
849 + progress = start_progress(source->base.odb->repo,
850 + _("Checking objects"), total);
851 + }
852 +
853 + for (e = packfile_store_get_packs(source); e; e = e->next) {
854 + /* verify gives error messages itself */
855 + if (verify_pack(source->base.odb->repo, e->pack,
856 + opts->object_cb, opts->object_payload,
857 + progress, count))
858 + ret = -1;
859 + count += e->pack->num_objects;
860 + }
861 + stop_progress(&progress);
862 +
863 + return ret;
864 + }
865 +
866 + static int verify_reverse_indices(struct odb_source_packed *source,
867 + struct odb_fsck_options *opts)
868 + {
869 + struct progress *progress = NULL;
870 + struct packfile_list_entry *e;
871 + uint32_t pack_count = 0;
872 + int res = 0;
873 +
874 + if (opts->flags & ODB_FSCK_PROGRESS) {
875 + for (e = packfile_store_get_packs(source); e; e = e->next)
876 + pack_count++;
877 + progress = start_delayed_progress(source->base.odb->repo,
878 + "Verifying reverse pack-indexes", pack_count);
879 + pack_count = 0;
880 + }
881 +
882 + for (e = packfile_store_get_packs(source); e; e = e->next) {
883 + int load_error = load_pack_revindex_from_disk(e->pack);
884 +
885 + if (load_error < 0) {
886 + error(_("unable to load rev-index for pack '%s'"), e->pack->pack_name);
887 + res = -1;
888 + } else if (!load_error &&
889 + !load_pack_revindex(source->base.odb->repo, e->pack) &&
890 + verify_pack_revindex(e->pack)) {
891 + error(_("invalid rev-index for pack '%s'"), e->pack->pack_name);
892 + res = -1;
893 + }
894 + display_progress(progress, ++pack_count);
895 + }
896 + stop_progress(&progress);
897 +
898 + return res;
899 + }
900 +
901 + static int verify_midx(struct odb_source_packed *source,
902 + struct odb_fsck_options *opts)
903 + {
904 + struct child_process midx_verify = CHILD_PROCESS_INIT;
905 +
906 + prepare_repo_settings(source->base.odb->repo);
907 + if (!source->base.odb->repo->settings.core_multi_pack_index)
908 + return 0;
909 +
910 + child_process_init(&midx_verify);
911 + midx_verify.git_cmd = 1;
912 + strvec_pushl(&midx_verify.args, "multi-pack-index",
913 + "verify", "--object-dir", source->base.path, NULL);
914 + if (opts->flags & ODB_FSCK_PROGRESS)
915 + strvec_push(&midx_verify.args, "--progress");
916 + else
917 + strvec_push(&midx_verify.args, "--no-progress");
918 + if (run_command(&midx_verify))
919 + return -1;
920 +
921 + return 0;
922 + }
923 +
924 + static int odb_source_packed_fsck(struct odb_source *source,
925 + struct odb_fsck_options *opts)
926 + {
927 + struct odb_source_packed *packed = odb_source_packed_downcast(source);
928 + int ret = 0;
929 +
930 + if ((opts->flags & ODB_FSCK_FULL) && verify_packs(packed, opts) < 0)
931 + ret = -1;
932 +
933 + if (verify_reverse_indices(packed, opts) < 0)
934 + ret = -1;
935 +
936 + if (verify_bitmap_files(packed))
937 + ret = -1;
938 +
939 + if (verify_midx(packed, opts) < 0)
940 + ret = -1;
941 +
942 + return ret;
943 + }
944 +
829 945 struct odb_source_packed *odb_source_packed_new(struct object_database *odb,
830 946 const char *path,
831 947 bool local)
@@ -839,6 +955,7 @@ struct odb_source_packed *odb_source_packed_new(struct object_database *odb,
839 955 packed->base.free = odb_source_packed_free;
840 956 packed->base.close = odb_source_packed_close;
841 957 packed->base.prepare = odb_source_packed_prepare;
958 + packed->base.fsck = odb_source_packed_fsck;
842 959 packed->base.read_object_info = odb_source_packed_read_object_info;
843 960 packed->base.read_object_stream = odb_source_packed_read_object_stream;
844 961 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 pack-bitmap.c
@@ -3410,28 +3410,22 @@ static int verify_bitmap_file(const struct git_hash_algo *algop,
3410 3410 return res;
3411 3411 }
3412 3412
3413 - int verify_bitmap_files(struct repository *r)
3413 + int verify_bitmap_files(struct odb_source_packed *source)
3414 3414 {
3415 - struct odb_source *source;
3416 - struct packed_git *p;
3415 + struct packfile_list_entry *e;
3416 + struct multi_pack_index *m;
3417 3417 int res = 0;
3418 3418
3419 - for (source = r->objects->sources; source; source = source->next) {
3420 - struct odb_source_files *files = odb_source_files_downcast(source);
3421 - struct multi_pack_index *m = get_multi_pack_index(files->packed);
3422 - char *midx_bitmap_name;
3423 -
3424 - if (!m)
3425 - continue;
3426 -
3427 - midx_bitmap_name = midx_bitmap_filename(m);
3428 - res |= verify_bitmap_file(r->hash_algo, midx_bitmap_name);
3419 + m = get_multi_pack_index(source);
3420 + if (m) {
3421 + char *midx_bitmap_name = midx_bitmap_filename(m);
3422 + res |= verify_bitmap_file(source->base.odb->repo->hash_algo, midx_bitmap_name);
3429 3423 free(midx_bitmap_name);
3430 3424 }
3431 3425
3432 - repo_for_each_pack(r, p) {
3433 - char *pack_bitmap_name = pack_bitmap_filename(p);
3434 - res |= verify_bitmap_file(r->hash_algo, pack_bitmap_name);
3426 + for (e = packfile_store_get_packs(source); e; e = e->next) {
3427 + char *pack_bitmap_name = pack_bitmap_filename(e->pack);
3428 + res |= verify_bitmap_file(source->base.odb->repo->hash_algo, pack_bitmap_name);
3435 3429 free(pack_bitmap_name);
3436 3430 }
3437 3431
modified pack-bitmap.h
@@ -205,7 +205,7 @@ int bitmap_is_midx(struct bitmap_index *bitmap_git);
205 205
206 206 int bitmap_is_preferred_refname(struct repository *r, const char *refname);
207 207
208 - int verify_bitmap_files(struct repository *r);
208 + int verify_bitmap_files(struct odb_source_packed *source);
209 209
210 210 struct ewah_bitmap *read_bitmap(const unsigned char *map,
211 211 size_t map_size, size_t *map_pos);
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 '
modified t/t5319-multi-pack-index.sh
@@ -573,6 +573,19 @@ test_expect_success 'verify incorrect checksum' '
573 573 $objdir "incorrect checksum"
574 574 '
575 575
576 + test_expect_success 'git fsck --no-full checks multi-pack-index, --connectivity-only does not' '
577 + pos=$(($(wc -c <$objdir/pack/multi-pack-index) - 10)) &&
578 + corrupt_midx_and_verify $pos \
579 + "\377\377\377\377\377\377\377\377\377\377" \
580 + $objdir "incorrect checksum" &&
581 +
582 + test_must_fail git fsck --no-full 2>err &&
583 + test_grep "incorrect checksum" err &&
584 +
585 + git fsck --connectivity-only 2>err &&
586 + test_grep ! "incorrect checksum" err
587 + '
588 +
576 589 test_expect_success 'setup for v1-specific fsck tests' '
577 590 git -c midx.version=1 multi-pack-index write
578 591 '
modified t/t5325-reverse-index.sh
@@ -204,4 +204,12 @@ test_expect_success 'fsck catches invalid header: hash function' '
204 204 "reverse-index file .* has unsupported hash id"
205 205 '
206 206
207 + test_expect_success 'fsck --no-full checks rev-index, --connectivity-only does not' '
208 + test_must_fail git -C corrupt fsck --no-full 2>err &&
209 + test_grep "has unsupported hash id" err &&
210 +
211 + git -C corrupt fsck --connectivity-only 2>err &&
212 + test_grep ! "has unsupported hash id" err
213 + '
214 +
207 215 test_done
modified t/t5326-multi-pack-bitmaps.sh
@@ -498,7 +498,15 @@ test_expect_success 'git fsck correctly identifies good and bad bitmaps' '
498 498 corrupt_file "$packbitmap" &&
499 499 test_must_fail git fsck 2>err &&
500 500 test_grep "bitmap file '\''$midxbitmap'\'' has invalid checksum" err &&
501 - test_grep "bitmap file '\''$packbitmap'\'' has invalid checksum" err
501 + test_grep "bitmap file '\''$packbitmap'\'' has invalid checksum" err &&
502 +
503 + # The bitmap checks are performed with "--no-full", but not with
504 + # "--connectivity-only".
505 + test_must_fail git fsck --no-full 2>err &&
506 + test_grep "bitmap file '\''$midxbitmap'\'' has invalid checksum" err &&
507 + test_grep "bitmap file '\''$packbitmap'\'' has invalid checksum" err &&
508 + git fsck --connectivity-only 2>err &&
509 + test_grep ! "invalid checksum" err
502 510 '
503 511
504 512 test_expect_success 'corrupt MIDX with bitmap causes fallback' '