Commit

6bc77804047f3aba512b9f559b6c713fc7775954

builtin/fsck: move packfile verification into the packed source

Move the packfile verification out of `cmd_fsck()` and into the "packed"
source. While doing so, thread the progress meter and object callback
through the newly introduced `struct odb_fsck_options` so that the
caller's preferences are honoured without exposing those details at the
"builtin/fsck.c" level.

Note that the old code reported failures when verifying packfiles with
the `ERROR_PACK` bit, which gets returned to the caller via the exit
code. This bit is neither exercised in our test suite nor is it
documented anywhere in our codebase. Furthermore, this bit is highly
specific to the object storage backend, which makes it a bad fit for the
new pluggable infrastructure. So instead of retaining these semantics,
we drop them and return the generic `ERROR_OBJECT` bit.

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, +54 −32
modified builtin/fsck.c
@@ -7,7 +7,6 @@
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"
@@ -49,7 +48,6 @@ static int show_dangling = 1;
49 48 static timestamp_t now;
50 49 #define ERROR_OBJECT 01
51 50 #define ERROR_REACHABLE 02
52 - #define ERROR_PACK 04
53 51 #define ERROR_REFS 010
54 52 #define ERROR_COMMIT_GRAPH 020
55 53 #define ERROR_MULTI_PACK_INDEX 040
@@ -967,6 +965,8 @@ int cmd_fsck(int argc,
967 965 {
968 966 struct odb_fsck_options odb_fsck_opts = {
969 967 .flags = ODB_FSCK_FULL,
968 + .object_cb = fsck_obj_buffer,
969 + .object_payload = repo,
970 970 };
971 971 int keep_cache_objects = 0;
972 972 int name_objects = 0;
@@ -1019,6 +1019,8 @@ int cmd_fsck(int argc,
1019 1019 show_progress = isatty(2);
1020 1020 if (verbose)
1021 1021 show_progress = 0;
1022 + if (show_progress)
1023 + odb_fsck_opts.flags |= ODB_FSCK_PROGRESS;
1022 1024
1023 1025 if (write_lost_and_found) {
1024 1026 odb_fsck_opts.flags |= ODB_FSCK_FULL;
@@ -1056,33 +1058,6 @@ int cmd_fsck(int argc,
1056 1058 if (odb_fsck(repo->objects, &odb_fsck_opts) < 0)
1057 1059 errors_found |= ERROR_OBJECT;
1058 1060
1059 - if (odb_fsck_opts.flags & ODB_FSCK_FULL) {
1060 - struct packed_git *p;
1061 - uint32_t total = 0, count = 0;
1062 - struct progress *progress = NULL;
1063 -
1064 - if (show_progress) {
1065 - repo_for_each_pack(repo, p) {
1066 - if (open_pack_index(p))
1067 - continue;
1068 - total += p->num_objects;
1069 - }
1070 -
1071 - progress = start_progress(repo,
1072 - _("Checking objects"), total);
1073 - }
1074 -
1075 - repo_for_each_pack(repo, p) {
1076 - /* verify gives error messages itself */
1077 - if (verify_pack(repo,
1078 - p, fsck_obj_buffer, repo,
1079 - progress, count))
1080 - errors_found |= ERROR_PACK;
1081 - count += p->num_objects;
1082 - }
1083 - stop_progress(&progress);
1084 - }
1085 -
1086 1061 if (fsck_finish(&fsck_obj_options))
1087 1062 errors_found |= ERROR_OBJECT;
1088 1063 }
modified odb.h
@@ -215,11 +215,18 @@ enum odb_fsck_flags {
215 215 * verification of their metadata.
216 216 */
217 217 ODB_FSCK_FULL = (1 << 0),
218 +
219 + /* Display a progress meter, if sensible. */
220 + ODB_FSCK_PROGRESS = (1 << 1),
218 221 };
219 222
220 223 /* Options that shall be passed to `odb_fsck()`. */
221 224 struct odb_fsck_options {
222 225 enum odb_fsck_flags flags;
226 +
227 + int (*object_cb)(const struct object_id *oid, enum object_type type,
228 + unsigned long size, void *buffer, int *eaten, void *cb_data);
229 + void *object_payload;
223 230 };
224 231
225 232 /*
modified odb/source-packed.c
@@ -9,8 +9,10 @@
9 9 #include "midx.h"
10 10 #include "odb/source-packed.h"
11 11 #include "odb/streaming.h"
12 + #include "pack.h"
12 13 #include "packfile.h"
13 14 #include "pack-bitmap.h"
15 + #include "progress.h"
14 16
15 17 static int find_pack_entry(struct odb_source_packed *store,
16 18 const struct object_id *oid,
@@ -827,10 +829,48 @@ static void odb_source_packed_free(struct odb_source *source)
827 829 free(packed);
828 830 }
829 831
830 - static int odb_source_packed_fsck(struct odb_source *source UNUSED,
831 - struct odb_fsck_options *opts UNUSED)
832 + static int verify_packs(struct odb_source_packed *source,
833 + struct odb_fsck_options *opts)
832 834 {
833 - return 0;
835 + struct progress *progress = NULL;
836 + struct packfile_list_entry *e;
837 + uint32_t total = 0, count = 0;
838 + int ret = 0;
839 +
840 + if (opts->flags & ODB_FSCK_PROGRESS) {
841 + for (e = packfile_store_get_packs(source); e; e = e->next) {
842 + if (open_pack_index(e->pack))
843 + continue;
844 + total += e->pack->num_objects;
845 + }
846 +
847 + progress = start_progress(source->base.odb->repo,
848 + _("Checking objects"), total);
849 + }
850 +
851 + for (e = packfile_store_get_packs(source); e; e = e->next) {
852 + /* verify gives error messages itself */
853 + if (verify_pack(source->base.odb->repo, e->pack,
854 + opts->object_cb, opts->object_payload,
855 + progress, count))
856 + ret = -1;
857 + count += e->pack->num_objects;
858 + }
859 + stop_progress(&progress);
860 +
861 + return ret;
862 + }
863 +
864 + static int odb_source_packed_fsck(struct odb_source *source,
865 + struct odb_fsck_options *opts)
866 + {
867 + struct odb_source_packed *packed = odb_source_packed_downcast(source);
868 + int ret = 0;
869 +
870 + if ((opts->flags & ODB_FSCK_FULL) && verify_packs(packed, opts) < 0)
871 + ret = -1;
872 +
873 + return ret;
834 874 }
835 875
836 876 struct odb_source_packed *odb_source_packed_new(struct object_database *odb,