Commit

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
3 files changed, +93 −90
modified builtin/fsck.c
@@ -12,7 +12,6 @@
12 12 #include "parse-options.h"
13 13 #include "progress.h"
14 14 #include "packfile.h"
15 - #include "object-file.h"
16 15 #include "object-name.h"
17 16 #include "odb.h"
18 17 #include "odb/streaming.h"
@@ -695,88 +694,6 @@ static void process_refs(struct repository *repo, struct snapshot *snap)
695 694 }
696 695 }
697 696
698 - struct for_each_loose_cb {
699 - struct repository *repo;
700 - struct progress *progress;
701 - };
702 -
703 - static int fsck_loose(const struct object_id *oid, const char *path,
704 - void *cb_data)
705 - {
706 - struct for_each_loose_cb *data = cb_data;
707 - enum object_type type = OBJ_NONE;
708 - size_t size;
709 - void *contents = NULL;
710 - int eaten;
711 - struct object_info oi = OBJECT_INFO_INIT;
712 - struct object_id real_oid = *null_oid(data->repo->hash_algo);
713 - int err = 0;
714 -
715 - oi.sizep = &size;
716 - oi.typep = &type;
717 -
718 - if (read_loose_object(data->repo, path, oid, &real_oid, &contents, &oi) < 0) {
719 - if (contents && !oideq(&real_oid, oid))
720 - err = error(_("%s: hash-path mismatch, found at: %s"),
721 - oid_to_hex(&real_oid), path);
722 - else
723 - err = error(_("%s: object corrupt or missing: %s"),
724 - oid_to_hex(oid), path);
725 - }
726 - if (err < 0) {
727 - errors_found |= ERROR_OBJECT;
728 - free(contents);
729 - return 0; /* keep checking other objects */
730 - }
731 -
732 - if (!contents && type != OBJ_BLOB)
733 - BUG("read_loose_object streamed a non-blob");
734 -
735 - if (fsck_obj_buffer(oid, type, size, contents, &eaten, data->repo))
736 - errors_found |= ERROR_OBJECT;
737 -
738 - if (!eaten)
739 - free(contents);
740 - return 0; /* keep checking other objects, even if we saw an error */
741 - }
742 -
743 - static int fsck_cruft(const char *basename, const char *path,
744 - void *data UNUSED)
745 - {
746 - if (!starts_with(basename, "tmp_obj_"))
747 - fprintf_ln(stderr, _("bad sha1 file: %s"), path);
748 - return 0;
749 - }
750 -
751 - static int fsck_subdir(unsigned int nr, const char *path UNUSED, void *data)
752 - {
753 - struct for_each_loose_cb *cb_data = data;
754 - struct progress *progress = cb_data->progress;
755 - display_progress(progress, nr + 1);
756 - return 0;
757 - }
758 -
759 - static void fsck_source(struct repository *repo, struct odb_source *source)
760 - {
761 - struct progress *progress = NULL;
762 - struct for_each_loose_cb cb_data = {
763 - .repo = source->odb->repo,
764 - .progress = progress,
765 - };
766 -
767 - if (verbose)
768 - fprintf_ln(stderr, _("Checking object directory"));
769 -
770 - if (show_progress)
771 - progress = start_progress(repo,
772 - _("Checking object directories"), 256);
773 -
774 - for_each_loose_file_in_source(source, fsck_loose,
775 - fsck_cruft, fsck_subdir, &cb_data);
776 - display_progress(progress, 256);
777 - stop_progress(&progress);
778 - }
779 -
780 697 static int fsck_cache_tree(struct repository *repo, struct cache_tree *it, const char *index_path)
781 698 {
782 699 int i;
@@ -978,8 +895,10 @@ int cmd_fsck(int argc,
978 895
979 896 if (show_progress == -1)
980 897 show_progress = isatty(2);
981 - if (verbose)
898 + if (verbose) {
982 899 show_progress = 0;
900 + odb_fsck_opts.flags |= ODB_FSCK_VERBOSE;
901 + }
983 902 if (show_progress)
984 903 odb_fsck_opts.flags |= ODB_FSCK_PROGRESS;
985 904
@@ -1012,10 +931,6 @@ int cmd_fsck(int argc,
1012 931 odb_for_each_object(repo->objects, NULL,
1013 932 mark_object_for_connectivity, repo, 0);
1014 933 } else {
1015 - for (source = repo->objects->sources; source; source = source->next)
1016 - if ((odb_fsck_opts.flags & ODB_FSCK_FULL) || source->local)
1017 - fsck_source(repo, source);
1018 -
1019 934 if (odb_fsck(repo->objects, &odb_fsck_opts) < 0)
1020 935 errors_found |= ERROR_OBJECT;
1021 936
modified odb.h
@@ -218,6 +218,9 @@ enum odb_fsck_flags {
218 218
219 219 /* Display a progress meter, if sensible. */
220 220 ODB_FSCK_PROGRESS = (1 << 1),
221 +
222 + /* Be extra verbose when checking the database. */
223 + ODB_FSCK_VERBOSE = (1 << 2),
221 224 };
222 225
223 226 /* Options that shall be passed to `odb_fsck()`. */
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,12 +1032,96 @@ static void odb_source_loose_free(struct odb_source *source)
1031 1032 free(loose);
1032 1033 }
1033 1034
1034 - static int odb_source_loose_fsck(struct odb_source *source UNUSED,
1035 - struct odb_fsck_options *opts UNUSED)
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)
1036 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);
1037 1098 return 0;
1038 1099 }
1039 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 +
1040 1125 struct odb_source_loose *odb_source_loose_new(struct object_database *odb,
1041 1126 const char *path,
1042 1127 bool local)