Compare

286f72ca82975741c861f23a7ee67d73cfb221d4..a51b77aa1d8ed033a018849465a65fd4e1dde02b

odb: provide infrastructure for pluggable fsck checks

The on-disk consistency checks in git-fsck(1) are conceptually
backend-specific: while connectivity checks and object-level parsing
checks are generic, verifying the physical integrity of packfiles and
loose objects is meaningful only to backends that use these formats:
Having these checks live in "builtin/fsck.c" violates that layering,
because it forces the command to reach directly into format-specific
internals.

Provide new infrastructure to make these format-specific checks
pluggable and implement stubs for the different source types we already
have. In subsequent commits we'll move functionality over piece by
piece.

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
18 files changed, +240 −161
modified builtin/fsck.c
@@ -37,10 +37,8 @@ static int show_root;
37 37 static int show_tags;
38 38 static int show_unreachable;
39 39 static int include_reflogs = 1;
40 - static int check_full = 1;
41 40 static int connectivity_only;
42 41 static int check_strict;
43 - static int keep_cache_objects;
44 42 static struct fsck_options fsck_walk_options;
45 43 static struct fsck_options fsck_obj_options;
46 44 static int errors_found;
@@ -48,8 +46,6 @@ static int write_lost_and_found;
48 46 static int verbose;
49 47 static int show_progress = -1;
50 48 static int show_dangling = 1;
51 - static int name_objects;
52 - static int check_references = 1;
53 49 static timestamp_t now;
54 50 #define ERROR_OBJECT 01
55 51 #define ERROR_REACHABLE 02
@@ -401,14 +397,27 @@ static void check_connectivity(struct repository *repo)
401 397 }
402 398 }
403 399
404 - static int fsck_obj(struct repository *repo,
405 - struct object *obj, void *buffer, unsigned long size)
400 + static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
401 + unsigned long size, void *buffer, int *eaten, void *cb_data)
406 402 {
403 + struct repository *repo = cb_data;
404 + struct object *obj;
407 405 int err;
408 406
409 - if (obj->flags & SEEN)
410 - return 0;
411 - obj->flags |= SEEN;
407 + /*
408 + * Note, buffer may be NULL if type is OBJ_BLOB. See
409 + * verify_packfile(), data_valid variable for details.
410 + */
411 + obj = parse_object_buffer(repo, oid, type, size, buffer, eaten);
412 + if (!obj) {
413 + errors_found |= ERROR_OBJECT;
414 + err = error(_("%s: object corrupt or missing"),
415 + oid_to_hex(oid));
416 + goto out;
417 + }
418 +
419 + obj->flags &= ~REACHABLE;
420 + obj->flags |= HAS_OBJ | SEEN;
412 421
413 422 if (verbose)
414 423 fprintf_ln(stderr, _("Checking %s %s"),
@@ -417,6 +426,7 @@ static int fsck_obj(struct repository *repo,
417 426
418 427 if (fsck_walk(obj, NULL, &fsck_obj_options))
419 428 objerror(repo, obj, _("broken links"));
429 +
420 430 err = fsck_object(obj, buffer, size, &fsck_obj_options);
421 431 if (err)
422 432 goto out;
@@ -442,32 +452,11 @@ static int fsck_obj(struct repository *repo,
442 452 }
443 453
444 454 out:
445 - if (obj->type == OBJ_TREE)
455 + if (obj && obj->type == OBJ_TREE)
446 456 free_tree_buffer((struct tree *)obj);
447 457 return err;
448 458 }
449 459
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 460 static int default_refs;
472 461
473 462 static void fsck_handle_reflog_oid(struct repository *repo,
@@ -722,7 +711,6 @@ static int fsck_loose(const struct object_id *oid, const char *path,
722 711 void *cb_data)
723 712 {
724 713 struct for_each_loose_cb *data = cb_data;
725 - struct object *obj;
726 714 enum object_type type = OBJ_NONE;
727 715 size_t size;
728 716 void *contents = NULL;
@@ -751,21 +739,7 @@ static int fsck_loose(const struct object_id *oid, const char *path,
751 739 if (!contents && type != OBJ_BLOB)
752 740 BUG("read_loose_object streamed a non-blob");
753 741
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))
742 + if (fsck_obj_buffer(oid, type, size, contents, &eaten, data->repo))
769 743 errors_found |= ERROR_OBJECT;
770 744
771 745 if (!eaten)
@@ -986,30 +960,36 @@ static char const * const fsck_usage[] = {
986 960 NULL
987 961 };
988 962
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 963 int cmd_fsck(int argc,
1009 964 const char **argv,
1010 965 const char *prefix,
1011 966 struct repository *repo)
1012 967 {
968 + struct odb_fsck_options odb_fsck_opts = {
969 + .flags = ODB_FSCK_FULL,
970 + };
971 + int keep_cache_objects = 0;
972 + int name_objects = 0;
973 + int check_references = 1;
974 + struct option fsck_opts[] = {
975 + OPT__VERBOSE(&verbose, N_("be verbose")),
976 + OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
977 + OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
978 + OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
979 + OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
980 + OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
981 + OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
982 + OPT_BIT(0, "full", &odb_fsck_opts.flags,
983 + N_("also consider packs and alternate objects"), ODB_FSCK_FULL),
984 + OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
985 + OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
986 + OPT_BOOL(0, "lost-found", &write_lost_and_found,
987 + N_("write dangling objects in .git/lost-found")),
988 + OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
989 + OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
990 + OPT_BOOL(0, "references", &check_references, N_("check reference database consistency")),
991 + OPT_END(),
992 + };
1013 993 struct odb_source *source;
1014 994 struct snapshot snap = {
1015 995 .nr = 0,
@@ -1041,7 +1021,7 @@ int cmd_fsck(int argc,
1041 1021 show_progress = 0;
1042 1022
1043 1023 if (write_lost_and_found) {
1044 - check_full = 1;
1024 + odb_fsck_opts.flags |= ODB_FSCK_FULL;
1045 1025 include_reflogs = 0;
1046 1026 }
1047 1027
@@ -1069,11 +1049,14 @@ int cmd_fsck(int argc,
1069 1049 odb_for_each_object(repo->objects, NULL,
1070 1050 mark_object_for_connectivity, repo, 0);
1071 1051 } else {
1072 - odb_prepare_alternates(repo->objects);
1073 1052 for (source = repo->objects->sources; source; source = source->next)
1074 - fsck_source(repo, source);
1053 + if ((odb_fsck_opts.flags & ODB_FSCK_FULL) || source->local)
1054 + fsck_source(repo, source);
1055 +
1056 + if (odb_fsck(repo->objects, &odb_fsck_opts) < 0)
1057 + errors_found |= ERROR_OBJECT;
1075 1058
1076 - if (check_full) {
1059 + if (odb_fsck_opts.flags & ODB_FSCK_FULL) {
1077 1060 struct packed_git *p;
1078 1061 uint32_t total = 0, count = 0;
1079 1062 struct progress *progress = NULL;
@@ -1155,7 +1138,6 @@ int cmd_fsck(int argc,
1155 1138 if (repo->settings.core_commit_graph) {
1156 1139 struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
1157 1140
1158 - odb_prepare_alternates(repo->objects);
1159 1141 for (source = repo->objects->sources; source; source = source->next) {
1160 1142 child_process_init(&commit_graph_verify);
1161 1143 commit_graph_verify.git_cmd = 1;
@@ -1173,7 +1155,6 @@ int cmd_fsck(int argc,
1173 1155 if (repo->settings.core_multi_pack_index) {
1174 1156 struct child_process midx_verify = CHILD_PROCESS_INIT;
1175 1157
1176 - odb_prepare_alternates(repo->objects);
1177 1158 for (source = repo->objects->sources; source; source = source->next) {
1178 1159 child_process_init(&midx_verify);
1179 1160 midx_verify.git_cmd = 1;
modified builtin/pack-objects.c
@@ -1780,8 +1780,6 @@ static int want_object_in_pack_mtime(const struct object_id *oid,
1780 1780 *found_offset = 0;
1781 1781 }
1782 1782
1783 - odb_prepare_alternates(the_repository->objects);
1784 -
1785 1783 for (source = the_repository->objects->sources; source; source = source->next) {
1786 1784 struct odb_source_files *files = odb_source_files_downcast(source);
1787 1785 struct multi_pack_index *m = get_multi_pack_index(files->packed);
@@ -4520,7 +4518,6 @@ static void add_objects_in_unpacked_packs(void)
4520 4518 .source_infop = &source_info,
4521 4519 };
4522 4520
4523 - odb_prepare_alternates(to_pack.repo->objects);
4524 4521 for (source = to_pack.repo->objects->sources; source; source = source->next) {
4525 4522 struct odb_source_files *files = odb_source_files_downcast(source);
4526 4523
modified commit-graph.c
@@ -651,8 +651,6 @@ struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb,
651 651 count = st->st_size / (odb->repo->hash_algo->hexsz + 1);
652 652 CALLOC_ARRAY(oids, count);
653 653
654 - odb_prepare_alternates(odb);
655 -
656 654 for (i = 0; i < count; i++) {
657 655 struct odb_source *source;
658 656
@@ -768,7 +766,6 @@ static struct commit_graph *prepare_commit_graph(struct repository *r)
768 766 if (!commit_graph_compatible(r))
769 767 return NULL;
770 768
771 - odb_prepare_alternates(r->objects);
772 769 for (source = r->objects->sources; source; source = source->next) {
773 770 r->objects->commit_graph = read_commit_graph_one(source);
774 771 if (r->objects->commit_graph)
@@ -2018,7 +2015,6 @@ static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
2018 2015 _("Finding commits for commit graph among packed objects"),
2019 2016 ctx->approx_nr_objects);
2020 2017
2021 - odb_prepare_alternates(ctx->r->objects);
2022 2018 for (source = ctx->r->objects->sources; source; source = source->next) {
2023 2019 struct odb_source_files *files = odb_source_files_downcast(source);
2024 2020 odb_source_for_each_object(&files->packed->base, &oi, add_packed_commits_oi,
modified loose.c
@@ -115,7 +115,6 @@ int repo_read_loose_object_map(struct repository *repo)
115 115 {
116 116 struct odb_source *source;
117 117
118 - odb_prepare_alternates(repo->objects);
119 118 for (source = repo->objects->sources; source; source = source->next) {
120 119 struct odb_source_files *files = odb_source_files_downcast(source);
121 120 if (loose_object_map_load(files->loose) < 0)
modified object-name.c
@@ -280,7 +280,6 @@ static int init_object_disambiguation(struct repository *r,
280 280
281 281 ds->len = len;
282 282 ds->repo = r;
283 - odb_prepare_alternates(r->objects);
284 283 return 0;
285 284 }
286 285
modified odb.c
@@ -2,11 +2,10 @@
2 2 #include "abspath.h"
3 3 #include "commit-graph.h"
4 4 #include "config.h"
5 - #include "dir.h"
6 5 #include "environment.h"
7 6 #include "gettext.h"
7 + #include "hashmap.h"
8 8 #include "hex.h"
9 - #include "khash.h"
10 9 #include "lockfile.h"
11 10 #include "loose.h"
12 11 #include "midx.h"
@@ -29,8 +28,47 @@
29 28 #include "trace2.h"
30 29 #include "write-or-die.h"
31 30
32 - KHASH_INIT(odb_path_map, const char * /* key: odb_path */,
33 - struct odb_source *, 1, fspathhash, fspatheq)
31 + /*
32 + * NEEDSWORK: we're using "core.ignoreCase" to deduplicate alternates that
33 + * _may_ be the same. This requires quite a bit of boilerplate for dubious
34 + * benefit:
35 + *
36 + * - Duplicating alternates should really only lead to regressed performance.
37 + *
38 + * - We don't properly resolve symlinks or mointpoints, so we may still end
39 + * up duplicating alternates.
40 + *
41 + * - The value may be lying, in which case we might deduplicate alternates
42 + * that are in fact not mapping to the same directory.
43 + *
44 + * We should investigate whether we can remove this whole mechanism outright.
45 + */
46 + static int odb_source_paths_cmp(struct object_database *o,
47 + const char *a, const char *b)
48 + {
49 + if (o->source_paths_icase < 0) {
50 + int icase = 0;
51 + repo_config_get_bool(o->repo, "core.ignorecase", &icase);
52 + o->source_paths_icase = icase;
53 + }
54 +
55 + return o->source_paths_icase ? strcasecmp(a, b) : strcmp(a, b);
56 + }
57 +
58 + static int odb_source_by_path_cmp(const void *cb_data,
59 + const struct hashmap_entry *entry,
60 + const struct hashmap_entry *entry_or_key,
61 + const void *keydata)
62 + {
63 + struct object_database *o = (struct object_database *)cb_data;
64 + const struct odb_source *source = container_of(entry, const struct odb_source, by_path_entry);
65 + const char *path = keydata;
66 +
67 + if (!path)
68 + path = container_of(entry_or_key, const struct odb_source, by_path_entry)->path;
69 +
70 + return odb_source_paths_cmp(o, source->path, path);
71 + }
34 72
35 73 int odb_mkstemp(struct object_database *odb,
36 74 struct strbuf *temp_filename, const char *pattern)
@@ -58,8 +96,8 @@ int odb_mkstemp(struct object_database *odb,
58 96 */
59 97 static bool odb_is_source_usable(struct object_database *o, const char *path)
60 98 {
61 - int r;
62 99 struct strbuf normalized_objdir = STRBUF_INIT;
100 + struct hashmap_entry key;
63 101 bool usable = false;
64 102
65 103 strbuf_realpath(&normalized_objdir, o->sources->path, 1);
@@ -76,20 +114,18 @@ static bool odb_is_source_usable(struct object_database *o, const char *path)
76 114 * Prevent the common mistake of listing the same
77 115 * thing twice, or object directory itself.
78 116 */
79 - if (!o->source_by_path) {
80 - khiter_t p;
81 -
82 - o->source_by_path = kh_init_odb_path_map();
117 + if (!hashmap_get_size(&o->source_by_path)) {
83 118 assert(!o->sources->next);
84 - p = kh_put_odb_path_map(o->source_by_path, o->sources->path, &r);
85 - assert(r == 1); /* never used */
86 - kh_value(o->source_by_path, p) = o->sources;
119 + hashmap_entry_init(&o->sources->by_path_entry,
120 + strihash(o->sources->path));
121 + hashmap_add(&o->source_by_path, &o->sources->by_path_entry);
87 122 }
88 123
89 - if (fspatheq(path, normalized_objdir.buf))
124 + if (!odb_source_paths_cmp(o, path, normalized_objdir.buf))
90 125 goto out;
91 126
92 - if (kh_get_odb_path_map(o->source_by_path, path) < kh_end(o->source_by_path))
127 + hashmap_entry_init(&key, strihash(path));
128 + if (hashmap_get(&o->source_by_path, &key, path))
93 129 goto out;
94 130
95 131 usable = true;
@@ -172,8 +208,6 @@ static struct odb_source *odb_add_alternate_recursively(struct object_database *
172 208 {
173 209 struct odb_source *alternate = NULL;
174 210 struct strvec sources = STRVEC_INIT;
175 - khiter_t pos;
176 - int ret;
177 211
178 212 if (!odb_is_source_usable(odb, source))
179 213 goto error;
@@ -184,10 +218,11 @@ static struct odb_source *odb_add_alternate_recursively(struct object_database *
184 218 *odb->sources_tail = alternate;
185 219 odb->sources_tail = &(alternate->next);
186 220
187 - pos = kh_put_odb_path_map(odb->source_by_path, alternate->path, &ret);
188 - if (!ret)
221 + hashmap_entry_init(&alternate->by_path_entry, strihash(alternate->path));
222 + if (hashmap_get(&odb->source_by_path, &alternate->by_path_entry,
223 + alternate->path))
189 224 BUG("source must not yet exist");
190 - kh_value(odb->source_by_path, pos) = alternate;
225 + hashmap_add(&odb->source_by_path, &alternate->by_path_entry);
191 226
192 227 /* recursively add alternates */
193 228 odb_source_read_alternates(alternate, &sources);
@@ -210,18 +245,12 @@ void odb_add_to_alternates_file(struct object_database *odb,
210 245 int ret = odb_source_write_alternate(odb->sources, dir);
211 246 if (ret < 0)
212 247 die(NULL);
213 - if (odb->loaded_alternates)
214 - odb_add_alternate_recursively(odb, dir, 0);
248 + odb_add_alternate_recursively(odb, dir, 0);
215 249 }
216 250
217 251 struct odb_source *odb_add_to_alternates_memory(struct object_database *odb,
218 252 const char *dir)
219 253 {
220 - /*
221 - * Make sure alternates are initialized, or else our entry may be
222 - * overwritten when they are.
223 - */
224 - odb_prepare_alternates(odb);
225 254 return odb_add_alternate_recursively(odb, dir, 0);
226 255 }
227 256
@@ -230,12 +259,6 @@ struct odb_source *odb_set_temporary_primary_source(struct object_database *odb,
230 259 {
231 260 struct odb_source *source;
232 261
233 - /*
234 - * Make sure alternates are initialized, or else our entry may be
235 - * overwritten when they are.
236 - */
237 - odb_prepare_alternates(odb);
238 -
239 262 /*
240 263 * Make a new primary odb and link the old primary ODB in as an
241 264 * alternate
@@ -341,7 +364,6 @@ struct odb_source *odb_find_source(struct object_database *odb, const char *obj_
341 364 char *obj_dir_real = real_pathdup(obj_dir, 1);
342 365 struct strbuf odb_path_real = STRBUF_INIT;
343 366
344 - odb_prepare_alternates(odb);
345 367 for (source = odb->sources; source; source = source->next) {
346 368 strbuf_realpath(&odb_path_real, source->path, 1);
347 369 if (!strcmp(obj_dir_real, odb_path_real.buf))
@@ -475,7 +497,6 @@ int odb_for_each_alternate(struct object_database *odb,
475 497 struct odb_source *alternate;
476 498 int r = 0;
477 499
478 - odb_prepare_alternates(odb);
479 500 for (alternate = odb->sources->next; alternate; alternate = alternate->next) {
480 501 r = cb(alternate, payload);
481 502 if (r)
@@ -484,26 +505,22 @@ int odb_for_each_alternate(struct object_database *odb,
484 505 return r;
485 506 }
486 507
487 - void odb_prepare_alternates(struct object_database *odb)
508 + static void odb_prepare_alternates(struct object_database *odb,
509 + const char *alternate_db)
488 510 {
489 511 struct strvec sources = STRVEC_INIT;
490 512
491 - if (odb->loaded_alternates)
492 - return;
493 -
494 - parse_alternates(odb->alternate_db, PATH_SEP, NULL, &sources);
513 + parse_alternates(alternate_db, PATH_SEP, NULL, &sources);
495 514 odb_source_read_alternates(odb->sources, &sources);
515 +
496 516 for (size_t i = 0; i < sources.nr; i++)
497 517 odb_add_alternate_recursively(odb, sources.v[i], 0);
498 518
499 - odb->loaded_alternates = 1;
500 -
501 519 strvec_clear(&sources);
502 520 }
503 521
504 522 int odb_has_alternates(struct object_database *odb)
505 523 {
506 - odb_prepare_alternates(odb);
507 524 return !!odb->sources->next;
508 525 }
509 526
@@ -563,8 +580,6 @@ static int do_oid_object_info_extended(struct object_database *odb,
563 580 if (!odb_source_read_object_info(odb->inmemory_objects, oid, oi, flags))
564 581 return 0;
565 582
566 - odb_prepare_alternates(odb);
567 -
568 583 while (1) {
569 584 struct odb_source *source;
570 585
@@ -827,7 +842,6 @@ int odb_freshen_object(struct object_database *odb,
827 842 const struct object_id *oid)
828 843 {
829 844 struct odb_source *source;
830 - odb_prepare_alternates(odb);
831 845 for (source = odb->sources; source; source = source->next)
832 846 if (odb_source_freshen_object(source, oid, NULL))
833 847 return 1;
@@ -842,7 +856,6 @@ int odb_for_each_object_ext(struct object_database *odb,
842 856 {
843 857 int ret;
844 858
845 - odb_prepare_alternates(odb);
846 859 for (struct odb_source *source = odb->sources; source; source = source->next) {
847 860 if (opts->flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY && !source->local)
848 861 continue;
@@ -880,7 +893,6 @@ int odb_count_objects(struct object_database *odb,
880 893 return 0;
881 894 }
882 895
883 - odb_prepare_alternates(odb);
884 896 for (source = odb->sources; source; source = source->next) {
885 897 unsigned long c;
886 898
@@ -960,7 +972,6 @@ int odb_find_abbrev_len(struct object_database *odb,
960 972 goto out;
961 973 }
962 974
963 - odb_prepare_alternates(odb);
964 975 for (struct odb_source *source = odb->sources; source; source = source->next) {
965 976 ret = odb_source_find_abbrev_len(source, oid, len, &len);
966 977 if (ret)
@@ -1077,6 +1088,8 @@ struct object_database *odb_new(struct repository *repo,
1077 1088 o->repo = repo;
1078 1089 pthread_mutex_init(&o->replace_mutex, NULL);
1079 1090 string_list_init_dup(&o->submodule_source_paths);
1091 + hashmap_init(&o->source_by_path, odb_source_by_path_cmp, o, 0);
1092 + o->source_paths_icase = -1;
1080 1093
1081 1094 if (flags & ODB_NEW_HONOR_ENV) {
1082 1095 primary_source = xstrdup_or_null(getenv(DB_ENVIRONMENT));
@@ -1087,9 +1100,11 @@ struct object_database *odb_new(struct repository *repo,
1087 1100
1088 1101 o->sources = odb_source_new(o, primary_source, true);
1089 1102 o->sources_tail = &o->sources->next;
1090 - o->alternate_db = secondary_sources;
1091 1103 o->inmemory_objects = &odb_source_inmemory_new(o)->base;
1092 1104
1105 + odb_prepare_alternates(o, secondary_sources);
1106 +
1107 + free(secondary_sources);
1093 1108 free(primary_source);
1094 1109 return o;
1095 1110 }
@@ -1115,8 +1130,7 @@ static void odb_free_sources(struct object_database *o)
1115 1130 odb_source_free(o->inmemory_objects);
1116 1131 o->inmemory_objects = NULL;
1117 1132
1118 - kh_destroy_odb_path_map(o->source_by_path);
1119 - o->source_by_path = NULL;
1133 + hashmap_clear(&o->source_by_path);
1120 1134 }
1121 1135
1122 1136 void odb_free(struct object_database *o)
@@ -1124,8 +1138,6 @@ void odb_free(struct object_database *o)
1124 1138 if (!o)
1125 1139 return;
1126 1140
1127 - free(o->alternate_db);
1128 -
1129 1141 oidmap_clear(&o->replace_map, 1);
1130 1142 pthread_mutex_destroy(&o->replace_mutex);
1131 1143
@@ -1147,14 +1159,14 @@ void odb_prepare(struct object_database *o, enum odb_prepare_flags flags)
1147 1159 * Reprepare alt odbs, in case the alternates file was modified
1148 1160 * during the course of this process. This only _adds_ odbs to
1149 1161 * the linked list, so existing odbs will continue to exist for
1150 - * the lifetime of the process.
1162 + * the lifetime of the process. Consequently, we don't have to
1163 + * reprocess GIT_ALTERNATE_OBJECT_DIRECTORIES here.
1151 1164 */
1152 1165 if (flags & ODB_PREPARE_FLUSH_CACHES) {
1153 - o->loaded_alternates = 0;
1166 + odb_prepare_alternates(o, NULL);
1154 1167 o->object_count_valid = 0;
1155 1168 }
1156 1169
1157 - odb_prepare_alternates(o);
1158 1170 for (source = o->sources; source; source = source->next)
1159 1171 odb_source_prepare(source, flags);
1160 1172
@@ -1165,3 +1177,11 @@ void odb_reprepare(struct object_database *o)
1165 1177 {
1166 1178 odb_prepare(o, ODB_PREPARE_FLUSH_CACHES);
1167 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
@@ -1,6 +1,7 @@
1 1 #ifndef ODB_H
2 2 #define ODB_H
3 3
4 + #include "hashmap.h"
4 5 #include "object.h"
5 6 #include "oid-array.h"
6 7 #include "oidset.h"
@@ -55,16 +56,19 @@ struct object_database {
55 56 */
56 57 struct odb_source *sources;
57 58 struct odb_source **sources_tail;
58 - struct kh_odb_path_map *source_by_path;
59 59
60 - int loaded_alternates;
60 + /*
61 + * Map of object database sources, keyed by their respective paths.
62 + * This map is used to detect the case where the same source is
63 + * registered multiple times.
64 + */
65 + struct hashmap source_by_path;
61 66
62 67 /*
63 - * A list of alternate object directories loaded from the environment;
64 - * this should not generally need to be accessed directly, but will
65 - * populate the "sources" list when odb_prepare_alternates() is run.
68 + * Whether source paths shall be compared case-insensitively, as
69 + * determined by "core.ignoreCase".
66 70 */
67 - char *alternate_db;
71 + int source_paths_icase;
68 72
69 73 /*
70 74 * Objects that should be substituted by other objects
@@ -202,6 +206,29 @@ void odb_prepare(struct object_database *o, enum odb_prepare_flags flags);
202 206 /* Equivalent to `odb_prepare(o, ODB_PREPARE_FLUSH_CACHES)`. */
203 207 void odb_reprepare(struct object_database *o);
204 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 +
220 + /* Options that shall be passed to `odb_fsck()`. */
221 + struct odb_fsck_options {
222 + enum odb_fsck_flags flags;
223 + };
224 +
225 + /*
226 + * Run backend-specific integrity checks on all object sources. Each source
227 + * performs the checks appropriate to its type. Returns 0 on success, a
228 + * negative error code otherwise.
229 + */
230 + int odb_fsck(struct object_database *odb, struct odb_fsck_options *opts);
231 +
205 232 /*
206 233 * Find source by its object directory path. Returns a `NULL` pointer in case
207 234 * the source could not be found.
@@ -261,12 +288,6 @@ void odb_for_each_alternate_ref(struct object_database *odb,
261 288 int odb_mkstemp(struct object_database *odb,
262 289 struct strbuf *temp_filename, const char *pattern);
263 290
264 - /*
265 - * Prepare alternate object sources for the given database by reading
266 - * "objects/info/alternates" and opening the respective sources.
267 - */
268 - void odb_prepare_alternates(struct object_database *odb);
269 -
270 291 /*
271 292 * Check whether the object database has any alternates. The primary object
272 293 * source does not count as alternate.
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
@@ -1031,6 +1031,12 @@ static void odb_source_loose_free(struct odb_source *source)
1031 1031 free(loose);
1032 1032 }
1033 1033
1034 + static int odb_source_loose_fsck(struct odb_source *source UNUSED,
1035 + struct odb_fsck_options *opts UNUSED)
1036 + {
1037 + return 0;
1038 + }
1039 +
1034 1040 struct odb_source_loose *odb_source_loose_new(struct object_database *odb,
1035 1041 const char *path,
1036 1042 bool local)
@@ -1043,6 +1049,7 @@ struct odb_source_loose *odb_source_loose_new(struct object_database *odb,
1043 1049 loose->base.free = odb_source_loose_free;
1044 1050 loose->base.close = odb_source_loose_close;
1045 1051 loose->base.prepare = odb_source_loose_prepare;
1052 + loose->base.fsck = odb_source_loose_fsck;
1046 1053 loose->base.read_object_info = odb_source_loose_read_object_info;
1047 1054 loose->base.read_object_stream = odb_source_loose_read_object_stream;
1048 1055 loose->base.for_each_object = odb_source_loose_for_each_object;
modified odb/source-packed.c
@@ -2,6 +2,7 @@
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"
@@ -826,6 +827,12 @@ static void odb_source_packed_free(struct odb_source *source)
826 827 free(packed);
827 828 }
828 829
830 + static int odb_source_packed_fsck(struct odb_source *source UNUSED,
831 + struct odb_fsck_options *opts UNUSED)
832 + {
833 + return 0;
834 + }
835 +
829 836 struct odb_source_packed *odb_source_packed_new(struct object_database *odb,
830 837 const char *path,
831 838 bool local)
@@ -839,6 +846,7 @@ struct odb_source_packed *odb_source_packed_new(struct object_database *odb,
839 846 packed->base.free = odb_source_packed_free;
840 847 packed->base.close = odb_source_packed_close;
841 848 packed->base.prepare = odb_source_packed_prepare;
849 + packed->base.fsck = odb_source_packed_fsck;
842 850 packed->base.read_object_info = odb_source_packed_read_object_info;
843 851 packed->base.read_object_stream = odb_source_packed_read_object_stream;
844 852 packed->base.for_each_object = odb_source_packed_for_each_object;
modified odb/source.h
@@ -1,6 +1,7 @@
1 1 #ifndef ODB_SOURCE_H
2 2 #define ODB_SOURCE_H
3 3
4 + #include "hashmap.h"
4 5 #include "object.h"
5 6 #include "odb.h"
6 7 #include "odb/transaction.h"
@@ -50,6 +51,12 @@ struct strvec;
50 51 struct odb_source {
51 52 struct odb_source *next;
52 53
54 + /*
55 + * Entry in the object database's map of sources, keyed by this
56 + * source's path.
57 + */
58 + struct hashmap_entry by_path_entry;
59 +
53 60 /* Object database that owns this object source. */
54 61 struct object_database *odb;
55 62
@@ -313,6 +320,17 @@ struct odb_source {
313 320 int (*generate_pack)(struct odb_source *source,
314 321 struct odb_pack_generator **out,
315 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);
316 334 };
317 335
318 336 /*
@@ -581,4 +599,14 @@ static inline int odb_source_generate_pack(struct odb_source *source,
581 599 return source->generate_pack(source, out, opts);
582 600 }
583 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 +
584 612 #endif
modified odb/streaming.c
@@ -184,7 +184,6 @@ static int istream_source(struct odb_stream **out,
184 184 {
185 185 struct odb_source *source;
186 186
187 - odb_prepare_alternates(odb);
188 187 for (source = odb->sources; source; source = source->next)
189 188 if (!odb_source_read_object_stream(out, source, oid))
190 189 return 0;
modified pack-bitmap.c
@@ -717,7 +717,6 @@ static int open_bitmap(struct repository *r,
717 717
718 718 assert(!bitmap_git->map);
719 719
720 - odb_prepare_alternates(r->objects);
721 720 for (source = r->objects->sources; source; source = source->next) {
722 721 struct odb_source_files *files = odb_source_files_downcast(source);
723 722
@@ -3417,7 +3416,6 @@ int verify_bitmap_files(struct repository *r)
3417 3416 struct packed_git *p;
3418 3417 int res = 0;
3419 3418
3420 - odb_prepare_alternates(r->objects);
3421 3419 for (source = r->objects->sources; source; source = source->next) {
3422 3420 struct odb_source_files *files = odb_source_files_downcast(source);
3423 3421 struct multi_pack_index *m = get_multi_pack_index(files->packed);
modified packfile.c
@@ -1936,7 +1936,6 @@ int has_object_pack(struct repository *r, const struct object_id *oid)
1936 1936 {
1937 1937 struct odb_source *source;
1938 1938
1939 - odb_prepare_alternates(r->objects);
1940 1939 for (source = r->objects->sources; source; source = source->next) {
1941 1940 struct odb_source_files *files = odb_source_files_downcast(source);
1942 1941 if (!odb_source_read_object_info(&files->packed->base, oid, NULL, 0))
modified packfile.h
@@ -77,8 +77,6 @@ static inline struct repo_for_each_pack_data repo_for_eack_pack_data_init(struct
77 77 {
78 78 struct repo_for_each_pack_data data = { 0 };
79 79
80 - odb_prepare_alternates(repo->objects);
81 -
82 80 for (struct odb_source *source = repo->objects->sources; source; source = source->next) {
83 81 struct odb_source_files *files = odb_source_files_downcast(source);
84 82 struct packfile_list_entry *entry = packfile_store_get_packs(files->packed);
modified setup.c
@@ -2880,12 +2880,6 @@ int init_db(struct repository *repo,
2880 2880 reinit = create_default_files(repo, template_dir, original_git_dir,
2881 2881 &repo_fmt, init_shared_repository);
2882 2882
2883 - if (!(flags & INIT_DB_SKIP_REFDB))
2884 - create_reference_database(repo, initial_branch, flags & INIT_DB_QUIET);
2885 - create_object_database(repo);
2886 -
2887 - startup_info->have_repository = 1;
2888 -
2889 2883 if (repo_settings_get_shared_repository(repo)) {
2890 2884 char buf[10];
2891 2885 /* We do not spell "group" and such, so that
@@ -2907,6 +2901,12 @@ int init_db(struct repository *repo,
2907 2901 repo_config_set(repo, "receive.denyNonFastforwards", "true");
2908 2902 }
2909 2903
2904 + if (!(flags & INIT_DB_SKIP_REFDB))
2905 + create_reference_database(repo, initial_branch, flags & INIT_DB_QUIET);
2906 + create_object_database(repo);
2907 +
2908 + startup_info->have_repository = 1;
2909 +
2910 2910 if (!(flags & INIT_DB_QUIET)) {
2911 2911 int len = strlen(git_dir);
2912 2912
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 '