Skip to content

Commit

Permalink
fsck: provide a function to fsck buffer without object struct
Browse files Browse the repository at this point in the history
The fsck code has been slowly moving away from requiring an object
struct in commits like 103fb6d (fsck: accept an oid instead of a
"struct tag" for fsck_tag(), 2019-10-18), c5b4269 (fsck: accept an
oid instead of a "struct commit" for fsck_commit(), 2019-10-18), etc.

However, the only external interface that fsck.c provides is
fsck_object(), which requires an object struct, then promptly discards
everything except its oid and type. Let's factor out the post-discard
part of that function as fsck_buffer(), leaving fsck_object() as a thin
wrapper around it. That will provide more flexibility for callers which
may not have a struct.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
peff authored and gitster committed Jan 18, 2023
1 parent 34959d8 commit 35ff327
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
29 changes: 18 additions & 11 deletions fsck.c
Original file line number Diff line number Diff line change
Expand Up @@ -1237,19 +1237,26 @@ int fsck_object(struct object *obj, void *data, unsigned long size,
if (!obj)
return report(options, NULL, OBJ_NONE, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck");

if (obj->type == OBJ_BLOB)
return fsck_blob(&obj->oid, data, size, options);
if (obj->type == OBJ_TREE)
return fsck_tree(&obj->oid, data, size, options);
if (obj->type == OBJ_COMMIT)
return fsck_commit(&obj->oid, data, size, options);
if (obj->type == OBJ_TAG)
return fsck_tag(&obj->oid, data, size, options);

return report(options, &obj->oid, obj->type,
return fsck_buffer(&obj->oid, obj->type, data, size, options);
}

int fsck_buffer(const struct object_id *oid, enum object_type type,
void *data, unsigned long size,
struct fsck_options *options)
{
if (type == OBJ_BLOB)
return fsck_blob(oid, data, size, options);
if (type == OBJ_TREE)
return fsck_tree(oid, data, size, options);
if (type == OBJ_COMMIT)
return fsck_commit(oid, data, size, options);
if (type == OBJ_TAG)
return fsck_tag(oid, data, size, options);

return report(options, oid, type,
FSCK_MSG_UNKNOWN_TYPE,
"unknown type '%d' (internal fsck error)",
obj->type);
type);
}

int fsck_error_function(struct fsck_options *o,
Expand Down
8 changes: 8 additions & 0 deletions fsck.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ int fsck_walk(struct object *obj, void *data, struct fsck_options *options);
int fsck_object(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,
void *data, unsigned long size,
struct fsck_options *options);

/*
* fsck a tag, and pass info about it back to the caller. This is
* exposed fsck_object() internals for git-mktag(1).
Expand Down

0 comments on commit 35ff327

Please sign in to comment.