Skip to content

Commit 7b6d0cd

Browse files
committed
Merge branch 'ps/fsck-wo-the-repository'
Internals of "git fsck" have been refactored to not depend on the global `the_repository` variable. * ps/fsck-wo-the-repository: builtin/fsck: stop using `the_repository` in error reporting builtin/fsck: stop using `the_repository` when marking objects builtin/fsck: stop using `the_repository` when checking packed objects builtin/fsck: stop using `the_repository` with loose objects builtin/fsck: stop using `the_repository` when checking reflogs builtin/fsck: stop using `the_repository` when checking refs builtin/fsck: stop using `the_repository` when snapshotting refs builtin/fsck: fix trivial dependence on `the_repository` fsck: drop USE_THE_REPOSITORY fsck: store repository in fsck options fsck: initialize fsck options via a function fetch-pack: move fsck options into function scope
2 parents 59063fe + 6fea405 commit 7b6d0cd

File tree

11 files changed

+259
-181
lines changed

11 files changed

+259
-181
lines changed

builtin/fsck.c

Lines changed: 149 additions & 124 deletions
Large diffs are not rendered by default.

builtin/index-pack.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static int nr_threads;
136136
static int from_stdin;
137137
static int strict;
138138
static int do_fsck_object;
139-
static struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES;
139+
static struct fsck_options fsck_options;
140140
static int verbose;
141141
static const char *progress_title;
142142
static int show_resolving_progress;
@@ -1908,6 +1908,8 @@ int cmd_index_pack(int argc,
19081908
show_usage_if_asked(argc, argv, index_pack_usage);
19091909

19101910
disable_replace_refs();
1911+
1912+
fsck_options_init(&fsck_options, the_repository, FSCK_OPTIONS_MISSING_GITMODULES);
19111913
fsck_options.walk = mark_link;
19121914

19131915
reset_pack_idx_option(&opts);

builtin/mktag.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static char const * const builtin_mktag_usage[] = {
1616
};
1717
static int option_strict = 1;
1818

19-
static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
19+
static struct fsck_options fsck_options;
2020

2121
static int mktag_fsck_error_func(struct fsck_options *o UNUSED,
2222
void *fsck_report UNUSED,
@@ -75,7 +75,7 @@ static int verify_object_in_tag(struct object_id *tagged_oid, int *tagged_type)
7575
int cmd_mktag(int argc,
7676
const char **argv,
7777
const char *prefix,
78-
struct repository *repo UNUSED)
78+
struct repository *repo)
7979
{
8080
static struct option builtin_mktag_options[] = {
8181
OPT_BOOL(0, "strict", &option_strict,
@@ -94,6 +94,7 @@ int cmd_mktag(int argc,
9494
if (strbuf_read(&buf, 0, 0) < 0)
9595
die_errno(_("could not read from stdin"));
9696

97+
fsck_options_init(&fsck_options, repo, FSCK_OPTIONS_STRICT);
9798
fsck_options.error_func = mktag_fsck_error_func;
9899
fsck_set_msg_type_from_ids(&fsck_options, FSCK_MSG_EXTRA_HEADER_ENTRY,
99100
FSCK_WARN);

builtin/refs.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ static int cmd_refs_migrate(int argc, const char **argv, const char *prefix,
7878
}
7979

8080
static int cmd_refs_verify(int argc, const char **argv, const char *prefix,
81-
struct repository *repo UNUSED)
81+
struct repository *repo)
8282
{
83-
struct fsck_options fsck_refs_options = FSCK_REFS_OPTIONS_DEFAULT;
83+
struct fsck_options fsck_refs_options;
8484
struct worktree **worktrees;
8585
const char * const verify_usage[] = {
8686
REFS_VERIFY_USAGE,
@@ -93,6 +93,8 @@ static int cmd_refs_verify(int argc, const char **argv, const char *prefix,
9393
};
9494
int ret = 0;
9595

96+
fsck_options_init(&fsck_refs_options, repo, FSCK_OPTIONS_REFS);
97+
9698
argc = parse_options(argc, argv, prefix, options, verify_usage, 0);
9799
if (argc)
98100
usage(_("'git refs verify' takes no arguments"));

builtin/unpack-objects.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static unsigned int offset, len;
2929
static off_t consumed_bytes;
3030
static off_t max_input_size;
3131
static struct git_hash_ctx ctx;
32-
static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
32+
static struct fsck_options fsck_options;
3333
static struct progress *progress;
3434

3535
/*
@@ -613,7 +613,7 @@ static void unpack_all(void)
613613
int cmd_unpack_objects(int argc,
614614
const char **argv,
615615
const char *prefix UNUSED,
616-
struct repository *repo UNUSED)
616+
struct repository *repo)
617617
{
618618
int i;
619619
struct object_id oid;
@@ -627,6 +627,8 @@ int cmd_unpack_objects(int argc,
627627

628628
show_usage_if_asked(argc, argv, unpack_usage);
629629

630+
fsck_options_init(&fsck_options, repo, FSCK_OPTIONS_STRICT);
631+
630632
for (i = 1 ; i < argc; i++) {
631633
const char *arg = argv[i];
632634

fetch-pack.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ static int server_supports_filtering;
5151
static int advertise_sid;
5252
static struct shallow_lock shallow_lock;
5353
static const char *alternate_shallow_file;
54-
static struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES;
5554
static struct strbuf fsck_msg_types = STRBUF_INIT;
5655
static struct string_list uri_protocols = STRING_LIST_INIT_DUP;
5756

@@ -1096,6 +1095,7 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
10961095
struct shallow_info *si,
10971096
struct string_list *pack_lockfiles)
10981097
{
1098+
struct fsck_options fsck_options = { 0 };
10991099
struct repository *r = the_repository;
11001100
struct ref *ref = copy_ref_list(orig_ref);
11011101
struct object_id oid;
@@ -1224,13 +1224,16 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
12241224
alternate_shallow_file = setup_temporary_shallow(si->shallow);
12251225
} else
12261226
alternate_shallow_file = NULL;
1227+
1228+
fsck_options_init(&fsck_options, the_repository, FSCK_OPTIONS_MISSING_GITMODULES);
12271229
if (get_pack(args, fd, pack_lockfiles, NULL, sought, nr_sought,
12281230
&fsck_options.gitmodules_found))
12291231
die(_("git fetch-pack: fetch failed."));
12301232
if (fsck_finish(&fsck_options))
12311233
die("fsck failed");
12321234

12331235
all_done:
1236+
fsck_options_clear(&fsck_options);
12341237
if (negotiator)
12351238
negotiator->release(negotiator);
12361239
return ref;
@@ -1650,6 +1653,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
16501653
struct string_list *pack_lockfiles)
16511654
{
16521655
struct repository *r = the_repository;
1656+
struct fsck_options fsck_options;
16531657
struct ref *ref = copy_ref_list(orig_ref);
16541658
enum fetch_state state = FETCH_CHECK_LOCAL;
16551659
struct oidset common = OIDSET_INIT;
@@ -1667,6 +1671,8 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
16671671
struct strvec index_pack_args = STRVEC_INIT;
16681672
const char *promisor_remote_config;
16691673

1674+
fsck_options_init(&fsck_options, the_repository, FSCK_OPTIONS_MISSING_GITMODULES);
1675+
16701676
if (server_feature_v2("promisor-remote", &promisor_remote_config))
16711677
promisor_remote_reply(promisor_remote_config, NULL);
16721678

@@ -1878,6 +1884,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
18781884
if (negotiator)
18791885
negotiator->release(negotiator);
18801886

1887+
fsck_options_clear(&fsck_options);
18811888
oidset_clear(&common);
18821889
return ref;
18831890
}

fsck.c

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#define USE_THE_REPOSITORY_VARIABLE
2-
31
#include "git-compat-util.h"
42
#include "date.h"
53
#include "dir.h"
@@ -207,7 +205,7 @@ void fsck_set_msg_types(struct fsck_options *options, const char *values)
207205
if (equal == len)
208206
die("skiplist requires a path");
209207
oidset_parse_file(&options->skip_oids, buf + equal + 1,
210-
the_repository->hash_algo);
208+
options->repo->hash_algo);
211209
buf += len + 1;
212210
continue;
213211
}
@@ -360,7 +358,7 @@ static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *op
360358
int res = 0;
361359
const char *name;
362360

363-
if (repo_parse_tree(the_repository, tree))
361+
if (repo_parse_tree(options->repo, tree))
364362
return -1;
365363

366364
name = fsck_get_object_name(options, &tree->object.oid);
@@ -375,14 +373,14 @@ static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *op
375373
continue;
376374

377375
if (S_ISDIR(entry.mode)) {
378-
obj = (struct object *)lookup_tree(the_repository, &entry.oid);
376+
obj = (struct object *)lookup_tree(options->repo, &entry.oid);
379377
if (name && obj)
380378
fsck_put_object_name(options, &entry.oid, "%s%s/",
381379
name, entry.path);
382380
result = options->walk(obj, OBJ_TREE, data, options);
383381
}
384382
else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode)) {
385-
obj = (struct object *)lookup_blob(the_repository, &entry.oid);
383+
obj = (struct object *)lookup_blob(options->repo, &entry.oid);
386384
if (name && obj)
387385
fsck_put_object_name(options, &entry.oid, "%s%s",
388386
name, entry.path);
@@ -409,15 +407,15 @@ static int fsck_walk_commit(struct commit *commit, void *data, struct fsck_optio
409407
int result;
410408
const char *name;
411409

412-
if (repo_parse_commit(the_repository, commit))
410+
if (repo_parse_commit(options->repo, commit))
413411
return -1;
414412

415413
name = fsck_get_object_name(options, &commit->object.oid);
416414
if (name)
417415
fsck_put_object_name(options, get_commit_tree_oid(commit),
418416
"%s:", name);
419417

420-
result = options->walk((struct object *) repo_get_commit_tree(the_repository, commit),
418+
result = options->walk((struct object *) repo_get_commit_tree(options->repo, commit),
421419
OBJ_TREE, data, options);
422420
if (result < 0)
423421
return result;
@@ -474,7 +472,7 @@ static int fsck_walk_tag(struct tag *tag, void *data, struct fsck_options *optio
474472
{
475473
const char *name = fsck_get_object_name(options, &tag->object.oid);
476474

477-
if (parse_tag(the_repository, tag))
475+
if (parse_tag(options->repo, tag))
478476
return -1;
479477
if (name)
480478
fsck_put_object_name(options, &tag->tagged->oid, "%s", name);
@@ -487,7 +485,7 @@ int fsck_walk(struct object *obj, void *data, struct fsck_options *options)
487485
return -1;
488486

489487
if (obj->type == OBJ_NONE)
490-
parse_object(the_repository, &obj->oid);
488+
parse_object(options->repo, &obj->oid);
491489

492490
switch (obj->type) {
493491
case OBJ_BLOB:
@@ -970,14 +968,14 @@ static int fsck_commit(const struct object_id *oid,
970968

971969
if (buffer >= buffer_end || !skip_prefix(buffer, "tree ", &buffer))
972970
return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line");
973-
if (parse_oid_hex(buffer, &tree_oid, &p) || *p != '\n') {
971+
if (parse_oid_hex_algop(buffer, &tree_oid, &p, options->repo->hash_algo) || *p != '\n') {
974972
err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1");
975973
if (err)
976974
return err;
977975
}
978976
buffer = p + 1;
979977
while (buffer < buffer_end && skip_prefix(buffer, "parent ", &buffer)) {
980-
if (parse_oid_hex(buffer, &parent_oid, &p) || *p != '\n') {
978+
if (parse_oid_hex_algop(buffer, &parent_oid, &p, options->repo->hash_algo) || *p != '\n') {
981979
err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1");
982980
if (err)
983981
return err;
@@ -1044,7 +1042,7 @@ int fsck_tag_standalone(const struct object_id *oid, const char *buffer,
10441042
ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line");
10451043
goto done;
10461044
}
1047-
if (parse_oid_hex(buffer, tagged_oid, &p) || *p != '\n') {
1045+
if (parse_oid_hex_algop(buffer, tagged_oid, &p, options->repo->hash_algo) || *p != '\n') {
10481046
ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1");
10491047
if (ret)
10501048
goto done;
@@ -1336,9 +1334,9 @@ static int fsck_blobs(struct oidset *blobs_found, struct oidset *blobs_done,
13361334
if (oidset_contains(blobs_done, oid))
13371335
continue;
13381336

1339-
buf = odb_read_object(the_repository->objects, oid, &type, &size);
1337+
buf = odb_read_object(options->repo->objects, oid, &type, &size);
13401338
if (!buf) {
1341-
if (is_promisor_object(the_repository, oid))
1339+
if (is_promisor_object(options->repo, oid))
13421340
continue;
13431341
ret |= report(options,
13441342
oid, OBJ_BLOB, msg_missing,
@@ -1380,6 +1378,54 @@ bool fsck_has_queued_checks(struct fsck_options *options)
13801378
!oidset_equal(&options->gitattributes_found, &options->gitattributes_done);
13811379
}
13821380

1381+
void fsck_options_init(struct fsck_options *options,
1382+
struct repository *repo,
1383+
enum fsck_options_type type)
1384+
{
1385+
static const struct fsck_options defaults[] = {
1386+
[FSCK_OPTIONS_DEFAULT] = {
1387+
.skip_oids = OIDSET_INIT,
1388+
.gitmodules_found = OIDSET_INIT,
1389+
.gitmodules_done = OIDSET_INIT,
1390+
.gitattributes_found = OIDSET_INIT,
1391+
.gitattributes_done = OIDSET_INIT,
1392+
.error_func = fsck_objects_error_function
1393+
},
1394+
[FSCK_OPTIONS_STRICT] = {
1395+
.strict = 1,
1396+
.gitmodules_found = OIDSET_INIT,
1397+
.gitmodules_done = OIDSET_INIT,
1398+
.gitattributes_found = OIDSET_INIT,
1399+
.gitattributes_done = OIDSET_INIT,
1400+
.error_func = fsck_objects_error_function,
1401+
},
1402+
[FSCK_OPTIONS_MISSING_GITMODULES] = {
1403+
.strict = 1,
1404+
.gitmodules_found = OIDSET_INIT,
1405+
.gitmodules_done = OIDSET_INIT,
1406+
.gitattributes_found = OIDSET_INIT,
1407+
.gitattributes_done = OIDSET_INIT,
1408+
.error_func = fsck_objects_error_cb_print_missing_gitmodules,
1409+
},
1410+
[FSCK_OPTIONS_REFS] = {
1411+
.error_func = fsck_refs_error_function,
1412+
},
1413+
};
1414+
1415+
switch (type) {
1416+
case FSCK_OPTIONS_DEFAULT:
1417+
case FSCK_OPTIONS_STRICT:
1418+
case FSCK_OPTIONS_MISSING_GITMODULES:
1419+
case FSCK_OPTIONS_REFS:
1420+
memcpy(options, &defaults[type], sizeof(*options));
1421+
break;
1422+
default:
1423+
BUG("unknown fsck options type %d", type);
1424+
}
1425+
1426+
options->repo = repo;
1427+
}
1428+
13831429
void fsck_options_clear(struct fsck_options *options)
13841430
{
13851431
free(options->msg_type);

fsck.h

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ struct fsck_ref_report {
166166
const char *path;
167167
};
168168

169+
struct repository;
170+
169171
struct fsck_options {
172+
struct repository *repo;
170173
fsck_walk_func walk;
171174
fsck_error error_func;
172175
unsigned strict;
@@ -180,34 +183,6 @@ struct fsck_options {
180183
kh_oid_map_t *object_names;
181184
};
182185

183-
#define FSCK_OPTIONS_DEFAULT { \
184-
.skip_oids = OIDSET_INIT, \
185-
.gitmodules_found = OIDSET_INIT, \
186-
.gitmodules_done = OIDSET_INIT, \
187-
.gitattributes_found = OIDSET_INIT, \
188-
.gitattributes_done = OIDSET_INIT, \
189-
.error_func = fsck_objects_error_function \
190-
}
191-
#define FSCK_OPTIONS_STRICT { \
192-
.strict = 1, \
193-
.gitmodules_found = OIDSET_INIT, \
194-
.gitmodules_done = OIDSET_INIT, \
195-
.gitattributes_found = OIDSET_INIT, \
196-
.gitattributes_done = OIDSET_INIT, \
197-
.error_func = fsck_objects_error_function, \
198-
}
199-
#define FSCK_OPTIONS_MISSING_GITMODULES { \
200-
.strict = 1, \
201-
.gitmodules_found = OIDSET_INIT, \
202-
.gitmodules_done = OIDSET_INIT, \
203-
.gitattributes_found = OIDSET_INIT, \
204-
.gitattributes_done = OIDSET_INIT, \
205-
.error_func = fsck_objects_error_cb_print_missing_gitmodules, \
206-
}
207-
#define FSCK_REFS_OPTIONS_DEFAULT { \
208-
.error_func = fsck_refs_error_function, \
209-
}
210-
211186
/* descend in all linked child objects
212187
* the return value is:
213188
* -1 error in processing the object
@@ -255,6 +230,17 @@ int fsck_finish(struct fsck_options *options);
255230
*/
256231
bool fsck_has_queued_checks(struct fsck_options *options);
257232

233+
enum fsck_options_type {
234+
FSCK_OPTIONS_DEFAULT,
235+
FSCK_OPTIONS_STRICT,
236+
FSCK_OPTIONS_MISSING_GITMODULES,
237+
FSCK_OPTIONS_REFS,
238+
};
239+
240+
void fsck_options_init(struct fsck_options *options,
241+
struct repository *repo,
242+
enum fsck_options_type type);
243+
258244
/*
259245
* Clear the fsck_options struct, freeing any allocated memory.
260246
*/

object-file.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1282,8 +1282,9 @@ static int index_mem(struct index_state *istate,
12821282
}
12831283
}
12841284
if (flags & INDEX_FORMAT_CHECK) {
1285-
struct fsck_options opts = FSCK_OPTIONS_DEFAULT;
1285+
struct fsck_options opts;
12861286

1287+
fsck_options_init(&opts, the_repository, FSCK_OPTIONS_DEFAULT);
12871288
opts.strict = 1;
12881289
opts.error_func = hash_format_check_report;
12891290
if (fsck_buffer(null_oid(istate->repo->hash_algo), type, buf, size, &opts))

0 commit comments

Comments
 (0)