builtin/fsck: fix trivial dependence on `the_repository`
We have a bunch of sites in "builtin/fsck.c" that depend on `the_repository` even though we already have a repository available, or in cases where we can trivially make it available. Refactor such sites to use the context-provided repository instead. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Mar 23, 2026 at 16:02 UTC
da3ead3ee3a27df391932379b0b7283f2b17729f
1 file changed
+49
-49
builtin/fsck.c
+49
-49
@@ -195,13 +195,13 @@ static int traverse_one_object(struct object *obj)
195
return result;
196
}
197
198
-static int traverse_reachable(void)
198
+static int traverse_reachable(struct repository *repo)
199
{
200
struct progress *progress = NULL;
201
unsigned int nr = 0;
202
int result = 0;
203
if (show_progress)
204
- progress = start_delayed_progress(the_repository,
204
+ progress = start_delayed_progress(repo,
205
_("Checking connectivity"), 0);
206
while (pending.nr) {
207
result |= traverse_one_object(object_array_pop(&pending));
@@ -255,7 +255,7 @@ static int mark_unreachable_referents(const struct object_id *oid,
255
/*
256
* Check a single reachable object
257
*/
258
-static void check_reachable_object(struct object *obj)
258
+static void check_reachable_object(struct repository *repo, struct object *obj)
259
{
260
/*
261
* We obviously want the object to be parsed,
@@ -263,9 +263,9 @@ static void check_reachable_object(struct object *obj)
263
* do a full fsck
264
*/
265
if (!(obj->flags & HAS_OBJ)) {
266
- if (is_promisor_object(the_repository, &obj->oid))
266
+ if (is_promisor_object(repo, &obj->oid))
267
return;
268
- if (has_object_pack(the_repository, &obj->oid))
268
+ if (has_object_pack(repo, &obj->oid))
269
return; /* it is in pack - forget about it */
270
printf_ln(_("missing %s %s"),
271
printable_type(&obj->oid, obj->type),
@@ -278,7 +278,7 @@ static void check_reachable_object(struct object *obj)
278
/*
279
* Check a single unreachable object
280
*/
281
-static void check_unreachable_object(struct object *obj)
281
+static void check_unreachable_object(struct repository *repo, struct object *obj)
282
{
283
/*
284
* Missing unreachable object? Ignore it. It's not like
@@ -318,19 +318,19 @@ static void check_unreachable_object(struct object *obj)
318
printable_type(&obj->oid, obj->type),
319
describe_object(&obj->oid));
320
if (write_lost_and_found) {
321
- char *filename = repo_git_path(the_repository, "lost-found/%s/%s",
321
+ char *filename = repo_git_path(repo, "lost-found/%s/%s",
322
obj->type == OBJ_COMMIT ? "commit" : "other",
323
describe_object(&obj->oid));
324
FILE *f;
325
326
- if (safe_create_leading_directories_const(the_repository, filename)) {
326
+ if (safe_create_leading_directories_const(repo, filename)) {
327
error(_("could not create lost-found"));
328
free(filename);
329
return;
330
}
331
f = xfopen(filename, "w");
332
if (obj->type == OBJ_BLOB) {
333
- if (odb_stream_blob_to_fd(the_repository->objects, fileno(f),
333
+ if (odb_stream_blob_to_fd(repo->objects, fileno(f),
334
&obj->oid, NULL, 1))
335
die_errno(_("could not write '%s'"), filename);
336
} else
@@ -350,23 +350,23 @@ static void check_unreachable_object(struct object *obj)
350
*/
351
}
352
353
-static void check_object(struct object *obj)
353
+static void check_object(struct repository *repo, struct object *obj)
354
{
355
if (verbose)
356
fprintf_ln(stderr, _("Checking %s"), describe_object(&obj->oid));
357
358
if (obj->flags & REACHABLE)
359
- check_reachable_object(obj);
359
+ check_reachable_object(repo, obj);
360
else
361
- check_unreachable_object(obj);
361
+ check_unreachable_object(repo, obj);
362
}
363
364
-static void check_connectivity(void)
364
+static void check_connectivity(struct repository *repo)
365
{
366
int i, max;
367
368
/* Traverse the pending reachable objects */
369
- traverse_reachable();
369
+ traverse_reachable(repo);
370
371
/*
372
* With --connectivity-only, we won't have actually opened and marked
@@ -384,20 +384,20 @@ static void check_connectivity(void)
384
* and ignore any that weren't present in our earlier
385
* traversal.
386
*/
387
- odb_for_each_object(the_repository->objects, NULL,
387
+ odb_for_each_object(repo->objects, NULL,
388
mark_unreachable_referents, NULL, 0);
389
}
390
391
/* Look up all the requirements, warn about missing objects.. */
392
- max = get_max_object_index(the_repository);
392
+ max = get_max_object_index(repo);
393
if (verbose)
394
fprintf_ln(stderr, _("Checking connectivity (%d objects)"), max);
395
396
for (i = 0; i < max; i++) {
397
- struct object *obj = get_indexed_object(the_repository, i);
397
+ struct object *obj = get_indexed_object(repo, i);
398
399
if (obj)
400
- check_object(obj);
400
+ check_object(repo, obj);
401
}
402
}
403
@@ -770,7 +770,7 @@ static int fsck_subdir(unsigned int nr, const char *path UNUSED, void *data)
770
return 0;
771
}
772
773
-static void fsck_source(struct odb_source *source)
773
+static void fsck_source(struct repository *repo, struct odb_source *source)
774
{
775
struct progress *progress = NULL;
776
struct for_each_loose_cb cb_data = {
@@ -781,7 +781,7 @@ static void fsck_source(struct odb_source *source)
781
fprintf_ln(stderr, _("Checking object directory"));
782
783
if (show_progress)
784
- progress = start_progress(the_repository,
784
+ progress = start_progress(repo,
785
_("Checking object directories"), 256);
786
787
for_each_loose_file_in_source(source, fsck_loose,
@@ -790,7 +790,7 @@ static void fsck_source(struct odb_source *source)
790
stop_progress(&progress);
791
}
792
793
-static int fsck_cache_tree(struct cache_tree *it, const char *index_path)
793
+static int fsck_cache_tree(struct repository *repo, struct cache_tree *it, const char *index_path)
794
{
795
int i;
796
int err = 0;
@@ -799,7 +799,7 @@ static int fsck_cache_tree(struct cache_tree *it, const char *index_path)
799
fprintf_ln(stderr, _("Checking cache tree of %s"), index_path);
800
801
if (0 <= it->entry_count) {
802
- struct object *obj = parse_object(the_repository, &it->oid);
802
+ struct object *obj = parse_object(repo, &it->oid);
803
if (!obj) {
804
error(_("%s: invalid sha1 pointer in cache-tree of %s"),
805
oid_to_hex(&it->oid), index_path);
@@ -813,7 +813,7 @@ static int fsck_cache_tree(struct cache_tree *it, const char *index_path)
813
err |= objerror(obj, _("non-tree in cache-tree"));
814
}
815
for (i = 0; i < it->subtree_nr; i++)
816
- err |= fsck_cache_tree(it->down[i]->cache_tree, index_path);
816
+ err |= fsck_cache_tree(repo, it->down[i]->cache_tree, index_path);
817
return err;
818
}
819
@@ -839,7 +839,7 @@ static int fsck_resolve_undo(struct index_state *istate,
839
if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
840
continue;
841
842
- obj = parse_object(the_repository, &ru->oid[i]);
842
+ obj = parse_object(istate->repo, &ru->oid[i]);
843
if (!obj) {
844
error(_("%s: invalid sha1 pointer in resolve-undo of %s"),
845
oid_to_hex(&ru->oid[i]),
@@ -871,7 +871,7 @@ static void fsck_index(struct index_state *istate, const char *index_path,
871
mode = istate->cache[i]->ce_mode;
872
if (S_ISGITLINK(mode))
873
continue;
874
- blob = lookup_blob(the_repository,
874
+ blob = lookup_blob(istate->repo,
875
&istate->cache[i]->oid);
876
if (!blob)
877
continue;
@@ -884,7 +884,7 @@ static void fsck_index(struct index_state *istate, const char *index_path,
884
mark_object_reachable(obj);
885
}
886
if (istate->cache_tree)
887
- fsck_cache_tree(istate->cache_tree, index_path);
887
+ fsck_cache_tree(istate->repo, istate->cache_tree, index_path);
888
fsck_resolve_undo(istate, index_path);
889
}
890
@@ -907,7 +907,7 @@ static int check_pack_rev_indexes(struct repository *r, int show_progress)
907
if (show_progress) {
908
repo_for_each_pack(r, p)
909
pack_count++;
910
- progress = start_delayed_progress(the_repository,
910
+ progress = start_delayed_progress(r,
911
"Verifying reverse pack-indexes", pack_count);
912
pack_count = 0;
913
}
@@ -1027,11 +1027,11 @@ int cmd_fsck(int argc,
1027
if (name_objects)
1028
fsck_enable_object_names(&fsck_walk_options);
1029
1030
- repo_config(the_repository, git_fsck_config, &fsck_obj_options);
1031
- prepare_repo_settings(the_repository);
1030
+ repo_config(repo, git_fsck_config, &fsck_obj_options);
1031
+ prepare_repo_settings(repo);
1032
1033
if (check_references)
1034
- fsck_refs(the_repository);
1034
+ fsck_refs(repo);
1035
1036
/*
1037
* Take a snapshot of the refs before walking objects to avoid looking
@@ -1042,15 +1042,15 @@ int cmd_fsck(int argc,
1042
snapshot_refs(&snap, argc, argv);
1043
1044
/* Ensure we get a "fresh" view of the odb */
1045
- odb_reprepare(the_repository->objects);
1045
+ odb_reprepare(repo->objects);
1046
1047
if (connectivity_only) {
1048
- odb_for_each_object(the_repository->objects, NULL,
1048
+ odb_for_each_object(repo->objects, NULL,
1049
mark_object_for_connectivity, NULL, 0);
1050
} else {
1051
- odb_prepare_alternates(the_repository->objects);
1052
- for (source = the_repository->objects->sources; source; source = source->next)
1053
- fsck_source(source);
1051
+ odb_prepare_alternates(repo->objects);
1052
+ for (source = repo->objects->sources; source; source = source->next)
1053
+ fsck_source(repo, source);
1054
1055
if (check_full) {
1056
struct packed_git *p;
@@ -1058,19 +1058,19 @@ int cmd_fsck(int argc,
1058
struct progress *progress = NULL;
1059
1060
if (show_progress) {
1061
- repo_for_each_pack(the_repository, p) {
1061
+ repo_for_each_pack(repo, p) {
1062
if (open_pack_index(p))
1063
continue;
1064
total += p->num_objects;
1065
}
1066
1067
- progress = start_progress(the_repository,
1067
+ progress = start_progress(repo,
1068
_("Checking objects"), total);
1069
}
1070
1071
- repo_for_each_pack(the_repository, p) {
1071
+ repo_for_each_pack(repo, p) {
1072
/* verify gives error messages itself */
1073
- if (verify_pack(the_repository,
1073
+ if (verify_pack(repo,
1074
p, fsck_obj_buffer,
1075
progress, count))
1076
errors_found |= ERROR_PACK;
@@ -1104,7 +1104,7 @@ int cmd_fsck(int argc,
1104
for (p = worktrees; *p; p++) {
1105
struct worktree *wt = *p;
1106
struct index_state istate =
1107
- INDEX_STATE_INIT(the_repository);
1107
+ INDEX_STATE_INIT(repo);
1108
char *path, *wt_gitdir;
1109
1110
/*
@@ -1125,17 +1125,17 @@ int cmd_fsck(int argc,
1125
free_worktrees(worktrees);
1126
}
1127
1128
- errors_found |= check_pack_rev_indexes(the_repository, show_progress);
1129
- if (verify_bitmap_files(the_repository))
1128
+ errors_found |= check_pack_rev_indexes(repo, show_progress);
1129
+ if (verify_bitmap_files(repo))
1130
errors_found |= ERROR_BITMAP;
1131
1132
- check_connectivity();
1132
+ check_connectivity(repo);
1133
1134
- if (the_repository->settings.core_commit_graph) {
1134
+ if (repo->settings.core_commit_graph) {
1135
struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
1136
1137
- odb_prepare_alternates(the_repository->objects);
1138
- for (source = the_repository->objects->sources; source; source = source->next) {
1137
+ odb_prepare_alternates(repo->objects);
1138
+ for (source = repo->objects->sources; source; source = source->next) {
1139
child_process_init(&commit_graph_verify);
1140
commit_graph_verify.git_cmd = 1;
1141
strvec_pushl(&commit_graph_verify.args, "commit-graph",
@@ -1149,11 +1149,11 @@ int cmd_fsck(int argc,
1149
}
1150
}
1151
1152
- if (the_repository->settings.core_multi_pack_index) {
1152
+ if (repo->settings.core_multi_pack_index) {
1153
struct child_process midx_verify = CHILD_PROCESS_INIT;
1154
1155
- odb_prepare_alternates(the_repository->objects);
1156
- for (source = the_repository->objects->sources; source; source = source->next) {
1155
+ odb_prepare_alternates(repo->objects);
1156
+ for (source = repo->objects->sources; source; source = source->next) {
1157
child_process_init(&midx_verify);
1158
midx_verify.git_cmd = 1;
1159
strvec_pushl(&midx_verify.args, "multi-pack-index",