Compare
a51b77aa1d8ed033a018849465a65fd4e1dde02b..426d291b6095a51ee249b70a088f99cd67d4bb27
builtin/fsck: move reverse index verification into the packed source
The checks for reverse indexes live in `check_pack_rev_indexes()`, which
is hosted in "builtin/fsck.c". These checks are obviously specific to
the "packed" backend.
Move the logic into `odb_source_packed_fsck()`. As in the preceding
commit, drop the dedicated `ERROR_PACK_REV_INDEX` bit and instead use
the generic `ERROR_OBJECT` bit.
Note that this changes behaviour in two ways:
- The checks are now skipped when "--connectivity-only" was passed.
This is because we don't even run `odb_fsck()` at all when that
flag has been passed by the user, and not verifying data structures
of the object database matches the documented intent of that flag,
which is to only check the connectivity of reachable objects.
- The checks are now skipped for non-local sources when "--no-full"
was passed. This is, again, in line with the documented intent of
that flag.
Add a test to cast these semantics into stone.
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
4 files changed,
+101
−69
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" | |
| @@ -24,7 +23,6 @@ | |||
| 24 | 23 | #include "run-command.h" | |
| 25 | 24 | #include "sparse-index.h" | |
| 26 | 25 | #include "worktree.h" | |
| 27 | - | #include "pack-revindex.h" | |
| 28 | 26 | #include "pack-bitmap.h" | |
| 29 | 27 | ||
| 30 | 28 | #define REACHABLE 0x0001 | |
| @@ -49,11 +47,9 @@ static int show_dangling = 1; | |||
| 49 | 47 | static timestamp_t now; | |
| 50 | 48 | #define ERROR_OBJECT 01 | |
| 51 | 49 | #define ERROR_REACHABLE 02 | |
| 52 | - | #define ERROR_PACK 04 | |
| 53 | 50 | #define ERROR_REFS 010 | |
| 54 | 51 | #define ERROR_COMMIT_GRAPH 020 | |
| 55 | 52 | #define ERROR_MULTI_PACK_INDEX 040 | |
| 56 | - | #define ERROR_PACK_REV_INDEX 0100 | |
| 57 | 53 | #define ERROR_BITMAP 0200 | |
| 58 | 54 | ||
| 59 | 55 | static const char *describe_object(const struct object_id *oid) | |
| @@ -892,40 +888,6 @@ static int mark_object_for_connectivity(const struct object_id *oid, | |||
| 892 | 888 | return 0; | |
| 893 | 889 | } | |
| 894 | 890 | ||
| 895 | - | static int check_pack_rev_indexes(struct repository *r, int show_progress) | |
| 896 | - | { | |
| 897 | - | struct progress *progress = NULL; | |
| 898 | - | struct packed_git *p; | |
| 899 | - | uint32_t pack_count = 0; | |
| 900 | - | int res = 0; | |
| 901 | - | ||
| 902 | - | if (show_progress) { | |
| 903 | - | repo_for_each_pack(r, p) | |
| 904 | - | pack_count++; | |
| 905 | - | progress = start_delayed_progress(r, | |
| 906 | - | "Verifying reverse pack-indexes", pack_count); | |
| 907 | - | pack_count = 0; | |
| 908 | - | } | |
| 909 | - | ||
| 910 | - | repo_for_each_pack(r, p) { | |
| 911 | - | int load_error = load_pack_revindex_from_disk(p); | |
| 912 | - | ||
| 913 | - | if (load_error < 0) { | |
| 914 | - | error(_("unable to load rev-index for pack '%s'"), p->pack_name); | |
| 915 | - | res = ERROR_PACK_REV_INDEX; | |
| 916 | - | } else if (!load_error && | |
| 917 | - | !load_pack_revindex(r, p) && | |
| 918 | - | verify_pack_revindex(p)) { | |
| 919 | - | error(_("invalid rev-index for pack '%s'"), p->pack_name); | |
| 920 | - | res = ERROR_PACK_REV_INDEX; | |
| 921 | - | } | |
| 922 | - | display_progress(progress, ++pack_count); | |
| 923 | - | } | |
| 924 | - | stop_progress(&progress); | |
| 925 | - | ||
| 926 | - | return res; | |
| 927 | - | } | |
| 928 | - | ||
| 929 | 891 | static void fsck_refs(struct repository *r) | |
| 930 | 892 | { | |
| 931 | 893 | struct child_process refs_verify = CHILD_PROCESS_INIT; | |
| @@ -967,6 +929,8 @@ int cmd_fsck(int argc, | |||
| 967 | 929 | { | |
| 968 | 930 | struct odb_fsck_options odb_fsck_opts = { | |
| 969 | 931 | .flags = ODB_FSCK_FULL, | |
| 932 | + | .object_cb = fsck_obj_buffer, | |
| 933 | + | .object_payload = repo, | |
| 970 | 934 | }; | |
| 971 | 935 | int keep_cache_objects = 0; | |
| 972 | 936 | int name_objects = 0; | |
| @@ -1019,6 +983,8 @@ int cmd_fsck(int argc, | |||
| 1019 | 983 | show_progress = isatty(2); | |
| 1020 | 984 | if (verbose) | |
| 1021 | 985 | show_progress = 0; | |
| 986 | + | if (show_progress) | |
| 987 | + | odb_fsck_opts.flags |= ODB_FSCK_PROGRESS; | |
| 1022 | 988 | ||
| 1023 | 989 | if (write_lost_and_found) { | |
| 1024 | 990 | odb_fsck_opts.flags |= ODB_FSCK_FULL; | |
| @@ -1056,33 +1022,6 @@ int cmd_fsck(int argc, | |||
| 1056 | 1022 | if (odb_fsck(repo->objects, &odb_fsck_opts) < 0) | |
| 1057 | 1023 | errors_found |= ERROR_OBJECT; | |
| 1058 | 1024 | ||
| 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 | 1025 | if (fsck_finish(&fsck_obj_options)) | |
| 1087 | 1026 | errors_found |= ERROR_OBJECT; | |
| 1088 | 1027 | } | |
| @@ -1129,7 +1068,6 @@ int cmd_fsck(int argc, | |||
| 1129 | 1068 | free_worktrees(worktrees); | |
| 1130 | 1069 | } | |
| 1131 | 1070 | ||
| 1132 | - | errors_found |= check_pack_rev_indexes(repo, show_progress); | |
| 1133 | 1071 | if (verify_bitmap_files(repo)) | |
| 1134 | 1072 | errors_found |= ERROR_BITMAP; | |
| 1135 | 1073 | ||
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,11 @@ | |||
| 9 | 9 | #include "midx.h" | |
| 10 | 10 | #include "odb/source-packed.h" | |
| 11 | 11 | #include "odb/streaming.h" | |
| 12 | + | #include "pack.h" | |
| 13 | + | #include "pack-revindex.h" | |
| 12 | 14 | #include "packfile.h" | |
| 13 | 15 | #include "pack-bitmap.h" | |
| 16 | + | #include "progress.h" | |
| 14 | 17 | ||
| 15 | 18 | static int find_pack_entry(struct odb_source_packed *store, | |
| 16 | 19 | const struct object_id *oid, | |
| @@ -827,10 +830,86 @@ static void odb_source_packed_free(struct odb_source *source) | |||
| 827 | 830 | free(packed); | |
| 828 | 831 | } | |
| 829 | 832 | ||
| 830 | - | static int odb_source_packed_fsck(struct odb_source *source UNUSED, | |
| 831 | - | struct odb_fsck_options *opts UNUSED) | |
| 833 | + | static int verify_packs(struct odb_source_packed *source, | |
| 834 | + | struct odb_fsck_options *opts) | |
| 832 | 835 | { | |
| 833 | - | return 0; | |
| 836 | + | struct progress *progress = NULL; | |
| 837 | + | struct packfile_list_entry *e; | |
| 838 | + | uint32_t total = 0, count = 0; | |
| 839 | + | int ret = 0; | |
| 840 | + | ||
| 841 | + | if (opts->flags & ODB_FSCK_PROGRESS) { | |
| 842 | + | for (e = packfile_store_get_packs(source); e; e = e->next) { | |
| 843 | + | if (open_pack_index(e->pack)) | |
| 844 | + | continue; | |
| 845 | + | total += e->pack->num_objects; | |
| 846 | + | } | |
| 847 | + | ||
| 848 | + | progress = start_progress(source->base.odb->repo, | |
| 849 | + | _("Checking objects"), total); | |
| 850 | + | } | |
| 851 | + | ||
| 852 | + | for (e = packfile_store_get_packs(source); e; e = e->next) { | |
| 853 | + | /* verify gives error messages itself */ | |
| 854 | + | if (verify_pack(source->base.odb->repo, e->pack, | |
| 855 | + | opts->object_cb, opts->object_payload, | |
| 856 | + | progress, count)) | |
| 857 | + | ret = -1; | |
| 858 | + | count += e->pack->num_objects; | |
| 859 | + | } | |
| 860 | + | stop_progress(&progress); | |
| 861 | + | ||
| 862 | + | return ret; | |
| 863 | + | } | |
| 864 | + | ||
| 865 | + | static int verify_reverse_indices(struct odb_source_packed *source, | |
| 866 | + | struct odb_fsck_options *opts) | |
| 867 | + | { | |
| 868 | + | struct progress *progress = NULL; | |
| 869 | + | struct packfile_list_entry *e; | |
| 870 | + | uint32_t pack_count = 0; | |
| 871 | + | int res = 0; | |
| 872 | + | ||
| 873 | + | if (opts->flags & ODB_FSCK_PROGRESS) { | |
| 874 | + | for (e = packfile_store_get_packs(source); e; e = e->next) | |
| 875 | + | pack_count++; | |
| 876 | + | progress = start_delayed_progress(source->base.odb->repo, | |
| 877 | + | "Verifying reverse pack-indexes", pack_count); | |
| 878 | + | pack_count = 0; | |
| 879 | + | } | |
| 880 | + | ||
| 881 | + | for (e = packfile_store_get_packs(source); e; e = e->next) { | |
| 882 | + | int load_error = load_pack_revindex_from_disk(e->pack); | |
| 883 | + | ||
| 884 | + | if (load_error < 0) { | |
| 885 | + | error(_("unable to load rev-index for pack '%s'"), e->pack->pack_name); | |
| 886 | + | res = -1; | |
| 887 | + | } else if (!load_error && | |
| 888 | + | !load_pack_revindex(source->base.odb->repo, e->pack) && | |
| 889 | + | verify_pack_revindex(e->pack)) { | |
| 890 | + | error(_("invalid rev-index for pack '%s'"), e->pack->pack_name); | |
| 891 | + | res = -1; | |
| 892 | + | } | |
| 893 | + | display_progress(progress, ++pack_count); | |
| 894 | + | } | |
| 895 | + | stop_progress(&progress); | |
| 896 | + | ||
| 897 | + | return res; | |
| 898 | + | } | |
| 899 | + | ||
| 900 | + | static int odb_source_packed_fsck(struct odb_source *source, | |
| 901 | + | struct odb_fsck_options *opts) | |
| 902 | + | { | |
| 903 | + | struct odb_source_packed *packed = odb_source_packed_downcast(source); | |
| 904 | + | int ret = 0; | |
| 905 | + | ||
| 906 | + | if ((opts->flags & ODB_FSCK_FULL) && verify_packs(packed, opts) < 0) | |
| 907 | + | ret = -1; | |
| 908 | + | ||
| 909 | + | if (verify_reverse_indices(packed, opts) < 0) | |
| 910 | + | ret = -1; | |
| 911 | + | ||
| 912 | + | return ret; | |
| 834 | 913 | } | |
| 835 | 914 | ||
| 836 | 915 | struct odb_source_packed *odb_source_packed_new(struct object_database *odb, | |
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 | |