[PATCH 05/14] fsck: stop relying on global state via `parse_oid_hex()`
- From
Patrick Steinhardt <ps@pks.im>
- Date
- Mar 20, 2026, 11:47 UTC
- Message-ID
- <20260320-b4-pks-fsck-without-the-repository-v1-5-6594f997926b@pks.im>
- In-Reply-To
- <20260320-b4-pks-fsck-without-the-repository-v1-0-6594f997926b@pks.im>
The function `parse_oid_hex()` implicitly relies on global state, as it uses the hash algorithm of `the_repository` to parse the input string. Refactor "fsck.c" to use `parse_oid_hex_algop()` instead and make the repository available via parameters as required.
This was the last dependency on `the_repository` in "fsck.c", so we can now drop `USE_THE_REPOSITORY_VARIABLE`.
Signed-off-by: Patrick Steinhardt <ps@pks.im> --- builtin/fsck.c | 2 +- builtin/index-pack.c | 5 +++-- builtin/mktag.c | 4 ++-- builtin/unpack-objects.c | 2 +- fsck.c | 33 ++++++++++++++++++--------------- fsck.h | 11 +++++++---- object-file.c | 3 ++- 7 files changed, 34 insertions(+), 26 deletions(-)
diff --git a/builtin/fsck.c b/builtin/fsck.c index fd689a1e14..ff9951c10c 100644 --- a/builtin/fsck.c +++ b/builtin/fsck.c @@ -415,7 +415,7 @@ static int fsck_obj(struct object *obj, void *buffer, unsigned long size) if (fsck_walk(the_repository, obj, NULL, &fsck_obj_options)) objerror(obj, _("broken links")); - err = fsck_object(obj, buffer, size, &fsck_obj_options); + err = fsck_object(the_repository, obj, buffer, size, &fsck_obj_options); if (err) goto out; diff --git a/builtin/index-pack.c b/builtin/index-pack.c index c4117e5970..031e4697d9 100644 --- a/builtin/index-pack.c +++ b/builtin/index-pack.c @@ -933,7 +933,8 @@ static void sha1_object(const void *data, struct object_entry *obj_entry, else die(_("invalid blob object %s"), oid_to_hex(oid)); if (do_fsck_object && - fsck_object(&blob->object, (void *)data, size, &fsck_options)) + fsck_object(the_repository, &blob->object, (void *)data, + size, &fsck_options)) die(_("fsck error in packed object")); } else { struct object *obj; @@ -952,7 +953,7 @@ static void sha1_object(const void *data, struct object_entry *obj_entry, if (!obj) die(_("invalid %s"), type_name(type)); if (do_fsck_object && - fsck_object(obj, buf, size, &fsck_options)) + fsck_object(the_repository, obj, buf, size, &fsck_options)) die(_("fsck error in packed object")); if (strict && fsck_walk(the_repository, obj, NULL, &fsck_options)) die(_("Not all child objects of %s are reachable"), oid_to_hex(&obj->oid)); diff --git a/builtin/mktag.c b/builtin/mktag.c index 2cf843c195..1925f04321 100644 --- a/builtin/mktag.c +++ b/builtin/mktag.c @@ -100,8 +100,8 @@ int cmd_mktag(int argc, /* config might set fsck.extraHeaderEntry=* again */ fsck_options_parse_config(&fsck_options, the_repository); - if (fsck_tag_standalone(NULL, buf.buf, buf.len, &fsck_options, - &tagged_oid, &tagged_type)) + if (fsck_tag_standalone(the_repository, NULL, buf.buf, buf.len, + &fsck_options, &tagged_oid, &tagged_type)) die(_("tag on stdin did not pass our strict fsck check")); if (verify_object_in_tag(&tagged_oid, &tagged_type) < 0) diff --git a/builtin/unpack-objects.c b/builtin/unpack-objects.c index 3e748a55d9..dadc2ba65f 100644 --- a/builtin/unpack-objects.c +++ b/builtin/unpack-objects.c @@ -241,7 +241,7 @@ static int check_object(struct object *obj, enum object_type type, obj_buf = lookup_object_buffer(obj); if (!obj_buf) die("Whoops! Cannot find object '%s'", oid_to_hex(&obj->oid)); - if (fsck_object(obj, obj_buf->buffer, obj_buf->size, &fsck_options)) + if (fsck_object(the_repository, obj, obj_buf->buffer, obj_buf->size, &fsck_options)) die("fsck error in packed object"); fsck_options.walk = check_object; if (fsck_walk(the_repository, obj, NULL, &fsck_options)) diff --git a/fsck.c b/fsck.c index efd987dd2b..edf7a0618f 100644 --- a/fsck.c +++ b/fsck.c @@ -1,5 +1,3 @@ -#define USE_THE_REPOSITORY_VARIABLE - #include "git-compat-util.h" #include "date.h" #include "dir.h" @@ -962,7 +960,8 @@ static int fsck_ident(const char **ident, const char *ident_end, return 0; } -static int fsck_commit(const struct object_id *oid, +static int fsck_commit(struct repository *repo, + const struct object_id *oid, const char *buffer, unsigned long size, struct fsck_options *options) { @@ -983,14 +982,14 @@ static int fsck_commit(const struct object_id *oid, if (buffer >= buffer_end || !skip_prefix(buffer, "tree ", &buffer)) return report(options, oid, OBJ_COMMIT, FSCK_MSG_MISSING_TREE, "invalid format - expected 'tree' line"); - if (parse_oid_hex(buffer, &tree_oid, &p) || *p != '\n') { + if (parse_oid_hex_algop(buffer, &tree_oid, &p, repo->hash_algo) || *p != '\n') { err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_TREE_SHA1, "invalid 'tree' line format - bad sha1"); if (err) return err; } buffer = p + 1; while (buffer < buffer_end && skip_prefix(buffer, "parent ", &buffer)) { - if (parse_oid_hex(buffer, &parent_oid, &p) || *p != '\n') { + if (parse_oid_hex_algop(buffer, &parent_oid, &p, repo->hash_algo) || *p != '\n') { err = report(options, oid, OBJ_COMMIT, FSCK_MSG_BAD_PARENT_SHA1, "invalid 'parent' line format - bad sha1"); if (err) return err; @@ -1024,16 +1023,18 @@ static int fsck_commit(const struct object_id *oid, return 0; } -static int fsck_tag(const struct object_id *oid, const char *buffer, +static int fsck_tag(struct repository *repo, + const struct object_id *oid, const char *buffer, unsigned long size, struct fsck_options *options) { struct object_id tagged_oid; int tagged_type; - return fsck_tag_standalone(oid, buffer, size, options, &tagged_oid, - &tagged_type); + return fsck_tag_standalone(repo, oid, buffer, size, options, + &tagged_oid, &tagged_type); } -int fsck_tag_standalone(const struct object_id *oid, const char *buffer, +int fsck_tag_standalone(struct repository *repo, + const struct object_id *oid, const char *buffer, unsigned long size, struct fsck_options *options, struct object_id *tagged_oid, int *tagged_type) @@ -1057,7 +1058,7 @@ int fsck_tag_standalone(const struct object_id *oid, const char *buffer, ret = report(options, oid, OBJ_TAG, FSCK_MSG_MISSING_OBJECT, "invalid format - expected 'object' line"); goto done; } - if (parse_oid_hex(buffer, tagged_oid, &p) || *p != '\n') { + if (parse_oid_hex_algop(buffer, tagged_oid, &p, repo->hash_algo) || *p != '\n') { ret = report(options, oid, OBJ_TAG, FSCK_MSG_BAD_OBJECT_SHA1, "invalid 'object' line format - bad sha1"); if (ret) goto done; @@ -1266,16 +1267,18 @@ static int fsck_blob(const struct object_id *oid, const char *buf, return ret; } -int fsck_object(struct object *obj, void *data, unsigned long size, +int fsck_object(struct repository *repo, + struct object *obj, void *data, unsigned long size, struct fsck_options *options) { if (!obj) return report(options, NULL, OBJ_NONE, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck"); - return fsck_buffer(&obj->oid, obj->type, data, size, options); + return fsck_buffer(repo, &obj->oid, obj->type, data, size, options); } -int fsck_buffer(const struct object_id *oid, enum object_type type, +int fsck_buffer(struct repository *repo, + const struct object_id *oid, enum object_type type, const void *data, unsigned long size, struct fsck_options *options) { @@ -1284,9 +1287,9 @@ int fsck_buffer(const struct object_id *oid, enum object_type type, if (type == OBJ_TREE) return fsck_tree(oid, data, size, options); if (type == OBJ_COMMIT) - return fsck_commit(oid, data, size, options); + return fsck_commit(repo, oid, data, size, options); if (type == OBJ_TAG) - return fsck_tag(oid, data, size, options); + return fsck_tag(repo, oid, data, size, options); return report(options, oid, type, FSCK_MSG_UNKNOWN_TYPE, diff --git a/fsck.h b/fsck.h index 3ca1683f74..62b7c3c93a 100644 --- a/fsck.h +++ b/fsck.h @@ -231,14 +231,16 @@ int fsck_walk(struct repository *repo, * Blob objects my pass a NULL data pointer, which indicates they are too large * to fit in memory. All other types must pass a real buffer. */ -int fsck_object(struct object *obj, void *data, unsigned long size, - struct fsck_options *options); +int fsck_object(struct repository *repo, + struct object *obj, void *data, unsigned long size, + struct fsck_options *options); /* * Same as fsck_object(), but for when the caller doesn't have an object * struct. */ -int fsck_buffer(const struct object_id *oid, enum object_type, +int fsck_buffer(struct repository *repo, + const struct object_id *oid, enum object_type, const void *data, unsigned long size, struct fsck_options *options); @@ -246,7 +248,8 @@ int fsck_buffer(const struct object_id *oid, enum object_type, * fsck a tag, and pass info about it back to the caller. This is * exposed fsck_object() internals for git-mktag(1). */ -int fsck_tag_standalone(const struct object_id *oid, const char *buffer, +int fsck_tag_standalone(struct repository *repo, + const struct object_id *oid, const char *buffer, unsigned long size, struct fsck_options *options, struct object_id *tagged_oid, int *tag_type); diff --git a/object-file.c b/object-file.c index ecbcc501dd..f58acaa939 100644 --- a/object-file.c +++ b/object-file.c @@ -1283,7 +1283,8 @@ static int index_mem(struct index_state *istate, opts.strict = 1; opts.error_func = hash_format_check_report; - if (fsck_buffer(null_oid(istate->repo->hash_algo), type, buf, size, &opts)) + if (fsck_buffer(the_repository, null_oid(istate->repo->hash_algo), + type, buf, size, &opts)) die(_("refusing to create malformed object")); fsck_finish(the_repository, &opts); }
-- 2.53.0.1055.ga2ffed1127.dirty