Compare
3db2defe2ffdcde8c5a9aa1de974d79edaaa1f33..dbdea7a91889cf4bfc2e64f59e9889f71f3a258e
builtin/fsck: merge `fsck_obj_buffer()` and `fsck_obj()`
The interfaces of the functions `fsck_obj()` and `fsck_obj_buffer()` are somewhat similar to one another. The only difference between those two is that `fsck_obj()` takes an already-parsed object as input, whereas `fsck_obj_buffer()` parses the buffer and then calls `fsck_obj()`. Furthermore, `fsck_obj()` has no callers other than `fsck_obj_buffer()`. Refactor the code by merging those two functions. This makes it obvious which function does what, and it allows us to get rid of the early return in `fsck_obj()` in case `SEEN` is set as the only caller unconditionally clears that bit before calling it anyway. 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
1 file changed,
+21
−43
modified builtin/fsck.c
| @@ -401,14 +401,27 @@ static void check_connectivity(struct repository *repo) | |||
| 401 | 401 | } | |
| 402 | 402 | } | |
| 403 | 403 | ||
| 404 | - | static int fsck_obj(struct repository *repo, | |
| 405 | - | struct object *obj, void *buffer, unsigned long size) | |
| 404 | + | static int fsck_obj_buffer(const struct object_id *oid, enum object_type type, | |
| 405 | + | unsigned long size, void *buffer, int *eaten, void *cb_data) | |
| 406 | 406 | { | |
| 407 | + | struct repository *repo = cb_data; | |
| 408 | + | struct object *obj; | |
| 407 | 409 | int err; | |
| 408 | 410 | ||
| 409 | - | if (obj->flags & SEEN) | |
| 410 | - | return 0; | |
| 411 | - | obj->flags |= SEEN; | |
| 411 | + | /* | |
| 412 | + | * Note, buffer may be NULL if type is OBJ_BLOB. See | |
| 413 | + | * verify_packfile(), data_valid variable for details. | |
| 414 | + | */ | |
| 415 | + | obj = parse_object_buffer(repo, oid, type, size, buffer, eaten); | |
| 416 | + | if (!obj) { | |
| 417 | + | errors_found |= ERROR_OBJECT; | |
| 418 | + | err = error(_("%s: object corrupt or missing"), | |
| 419 | + | oid_to_hex(oid)); | |
| 420 | + | goto out; | |
| 421 | + | } | |
| 422 | + | ||
| 423 | + | obj->flags &= ~REACHABLE; | |
| 424 | + | obj->flags |= HAS_OBJ | SEEN; | |
| 412 | 425 | ||
| 413 | 426 | if (verbose) | |
| 414 | 427 | fprintf_ln(stderr, _("Checking %s %s"), | |
| @@ -417,6 +430,7 @@ static int fsck_obj(struct repository *repo, | |||
| 417 | 430 | ||
| 418 | 431 | if (fsck_walk(obj, NULL, &fsck_obj_options)) | |
| 419 | 432 | objerror(repo, obj, _("broken links")); | |
| 433 | + | ||
| 420 | 434 | err = fsck_object(obj, buffer, size, &fsck_obj_options); | |
| 421 | 435 | if (err) | |
| 422 | 436 | goto out; | |
| @@ -442,32 +456,11 @@ static int fsck_obj(struct repository *repo, | |||
| 442 | 456 | } | |
| 443 | 457 | ||
| 444 | 458 | out: | |
| 445 | - | if (obj->type == OBJ_TREE) | |
| 459 | + | if (obj && obj->type == OBJ_TREE) | |
| 446 | 460 | free_tree_buffer((struct tree *)obj); | |
| 447 | 461 | return err; | |
| 448 | 462 | } | |
| 449 | 463 | ||
| 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 | 464 | static int default_refs; | |
| 472 | 465 | ||
| 473 | 466 | static void fsck_handle_reflog_oid(struct repository *repo, | |
| @@ -722,7 +715,6 @@ static int fsck_loose(const struct object_id *oid, const char *path, | |||
| 722 | 715 | void *cb_data) | |
| 723 | 716 | { | |
| 724 | 717 | struct for_each_loose_cb *data = cb_data; | |
| 725 | - | struct object *obj; | |
| 726 | 718 | enum object_type type = OBJ_NONE; | |
| 727 | 719 | size_t size; | |
| 728 | 720 | void *contents = NULL; | |
| @@ -751,21 +743,7 @@ static int fsck_loose(const struct object_id *oid, const char *path, | |||
| 751 | 743 | if (!contents && type != OBJ_BLOB) | |
| 752 | 744 | BUG("read_loose_object streamed a non-blob"); | |
| 753 | 745 | ||
| 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)) | |
| 746 | + | if (fsck_obj_buffer(oid, type, size, contents, &eaten, data->repo)) | |
| 769 | 747 | errors_found |= ERROR_OBJECT; | |
| 770 | 748 | ||
| 771 | 749 | if (!eaten) | |