Compare

dbdea7a91889cf4bfc2e64f59e9889f71f3a258e..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, +376 −217
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 {
@@ -706,88 +694,6 @@ static void process_refs(struct repository *repo, struct snapshot *snap)
706 694 }
707 695 }
708 696
709 - struct for_each_loose_cb {
710 - struct repository *repo;
711 - struct progress *progress;
712 - };
713 -
714 - static int fsck_loose(const struct object_id *oid, const char *path,
715 - void *cb_data)
716 - {
717 - struct for_each_loose_cb *data = cb_data;
718 - enum object_type type = OBJ_NONE;
719 - size_t size;
720 - void *contents = NULL;
721 - int eaten;
722 - struct object_info oi = OBJECT_INFO_INIT;
723 - struct object_id real_oid = *null_oid(data->repo->hash_algo);
724 - int err = 0;
725 -
726 - oi.sizep = &size;
727 - oi.typep = &type;
728 -
729 - if (read_loose_object(data->repo, path, oid, &real_oid, &contents, &oi) < 0) {
730 - if (contents && !oideq(&real_oid, oid))
731 - err = error(_("%s: hash-path mismatch, found at: %s"),
732 - oid_to_hex(&real_oid), path);
733 - else
734 - err = error(_("%s: object corrupt or missing: %s"),
735 - oid_to_hex(oid), path);
736 - }
737 - if (err < 0) {
738 - errors_found |= ERROR_OBJECT;
739 - free(contents);
740 - return 0; /* keep checking other objects */
741 - }
742 -
743 - if (!contents && type != OBJ_BLOB)
744 - BUG("read_loose_object streamed a non-blob");
745 -
746 - if (fsck_obj_buffer(oid, type, size, contents, &eaten, data->repo))
747 - errors_found |= ERROR_OBJECT;
748 -
749 - if (!eaten)
750 - free(contents);
751 - return 0; /* keep checking other objects, even if we saw an error */
752 - }
753 -
754 - static int fsck_cruft(const char *basename, const char *path,
755 - void *data UNUSED)
756 - {
757 - if (!starts_with(basename, "tmp_obj_"))
758 - fprintf_ln(stderr, _("bad sha1 file: %s"), path);
759 - return 0;
760 - }
761 -
762 - static int fsck_subdir(unsigned int nr, const char *path UNUSED, void *data)
763 - {
764 - struct for_each_loose_cb *cb_data = data;
765 - struct progress *progress = cb_data->progress;
766 - display_progress(progress, nr + 1);
767 - return 0;
768 - }
769 -
770 - static void fsck_source(struct repository *repo, struct odb_source *source)
771 - {
772 - struct progress *progress = NULL;
773 - struct for_each_loose_cb cb_data = {
774 - .repo = source->odb->repo,
775 - .progress = progress,
776 - };
777 -
778 - if (verbose)
779 - fprintf_ln(stderr, _("Checking object directory"));
780 -
781 - if (show_progress)
782 - progress = start_progress(repo,
783 - _("Checking object directories"), 256);
784 -
785 - for_each_loose_file_in_source(source, fsck_loose,
786 - fsck_cruft, fsck_subdir, &cb_data);
787 - display_progress(progress, 256);
788 - stop_progress(&progress);
789 - }
790 -
791 697 static int fsck_cache_tree(struct repository *repo, struct cache_tree *it, const char *index_path)
792 698 {
793 699 int i;
@@ -896,40 +802,6 @@ static int mark_object_for_connectivity(const struct object_id *oid,
896 802 return 0;
897 803 }
898 804
899 - static int check_pack_rev_indexes(struct repository *r, int show_progress)
900 - {
901 - struct progress *progress = NULL;
902 - struct packed_git *p;
903 - uint32_t pack_count = 0;
904 - int res = 0;
905 -
906 - if (show_progress) {
907 - repo_for_each_pack(r, p)
908 - pack_count++;
909 - progress = start_delayed_progress(r,
910 - "Verifying reverse pack-indexes", pack_count);
911 - pack_count = 0;
912 - }
913 -
914 - repo_for_each_pack(r, p) {
915 - int load_error = load_pack_revindex_from_disk(p);
916 -
917 - if (load_error < 0) {
918 - error(_("unable to load rev-index for pack '%s'"), p->pack_name);
919 - res = ERROR_PACK_REV_INDEX;
920 - } else if (!load_error &&
921 - !load_pack_revindex(r, p) &&
922 - verify_pack_revindex(p)) {
923 - error(_("invalid rev-index for pack '%s'"), p->pack_name);
924 - res = ERROR_PACK_REV_INDEX;
925 - }
926 - display_progress(progress, ++pack_count);
927 - }
928 - stop_progress(&progress);
929 -
930 - return res;
931 - }
932 -
933 805 static void fsck_refs(struct repository *r)
934 806 {
935 807 struct child_process refs_verify = CHILD_PROCESS_INIT;
@@ -964,30 +836,38 @@ static char const * const fsck_usage[] = {
964 836 NULL
965 837 };
966 838
967 - static struct option fsck_opts[] = {
968 - OPT__VERBOSE(&verbose, N_("be verbose")),
969 - OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
970 - OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
971 - OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
972 - OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
973 - OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
974 - OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
975 - OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
976 - OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
977 - OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
978 - OPT_BOOL(0, "lost-found", &write_lost_and_found,
979 - N_("write dangling objects in .git/lost-found")),
980 - OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
981 - OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
982 - OPT_BOOL(0, "references", &check_references, N_("check reference database consistency")),
983 - OPT_END(),
984 - };
985 -
986 839 int cmd_fsck(int argc,
987 840 const char **argv,
988 841 const char *prefix,
989 842 struct repository *repo)
990 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 + };
991 871 struct odb_source *source;
992 872 struct snapshot snap = {
993 873 .nr = 0,
@@ -1015,11 +895,15 @@ int cmd_fsck(int argc,
1015 895
1016 896 if (show_progress == -1)
1017 897 show_progress = isatty(2);
1018 - if (verbose)
898 + if (verbose) {
1019 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;
1020 904
1021 905 if (write_lost_and_found) {
1022 - check_full = 1;
906 + odb_fsck_opts.flags |= ODB_FSCK_FULL;
1023 907 include_reflogs = 0;
1024 908 }
1025 909
@@ -1047,35 +931,8 @@ int cmd_fsck(int argc,
1047 931 odb_for_each_object(repo->objects, NULL,
1048 932 mark_object_for_connectivity, repo, 0);
1049 933 } else {
1050 - for (source = repo->objects->sources; source; source = source->next)
1051 - fsck_source(repo, source);
1052 -
1053 - if (check_full) {
1054 - struct packed_git *p;
1055 - uint32_t total = 0, count = 0;
1056 - struct progress *progress = NULL;
1057 -
1058 - if (show_progress) {
1059 - repo_for_each_pack(repo, p) {
1060 - if (open_pack_index(p))
1061 - continue;
1062 - total += p->num_objects;
1063 - }
1064 -
1065 - progress = start_progress(repo,
1066 - _("Checking objects"), total);
1067 - }
1068 -
1069 - repo_for_each_pack(repo, p) {
1070 - /* verify gives error messages itself */
1071 - if (verify_pack(repo,
1072 - p, fsck_obj_buffer, repo,
1073 - progress, count))
1074 - errors_found |= ERROR_PACK;
1075 - count += p->num_objects;
1076 - }
1077 - stop_progress(&progress);
1078 - }
934 + if (odb_fsck(repo->objects, &odb_fsck_opts) < 0)
935 + errors_found |= ERROR_OBJECT;
1079 936
1080 937 if (fsck_finish(&fsck_obj_options))
1081 938 errors_found |= ERROR_OBJECT;
@@ -1123,10 +980,6 @@ int cmd_fsck(int argc,
1123 980 free_worktrees(worktrees);
1124 981 }
1125 982
1126 - errors_found |= check_pack_rev_indexes(repo, show_progress);
1127 - if (verify_bitmap_files(repo))
1128 - errors_found |= ERROR_BITMAP;
1129 -
1130 983 check_connectivity(repo);
1131 984
1132 985 if (repo->settings.core_commit_graph) {
@@ -1146,23 +999,6 @@ int cmd_fsck(int argc,
1146 999 }
1147 1000 }
1148 1001
1149 - if (repo->settings.core_multi_pack_index) {
1150 - struct child_process midx_verify = CHILD_PROCESS_INIT;
1151 -
1152 - for (source = repo->objects->sources; source; source = source->next) {
1153 - child_process_init(&midx_verify);
1154 - midx_verify.git_cmd = 1;
1155 - strvec_pushl(&midx_verify.args, "multi-pack-index",
1156 - "verify", "--object-dir", source->path, NULL);
1157 - if (show_progress)
1158 - strvec_push(&midx_verify.args, "--progress");
1159 - else
1160 - strvec_push(&midx_verify.args, "--no-progress");
1161 - if (run_command(&midx_verify))
1162 - errors_found |= ERROR_MULTI_PACK_INDEX;
1163 - }
1164 - }
1165 -
1166 1002 free_snapshot_refs(&snap);
1167 1003 return errors_found;
1168 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' '