Commit

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

Browse files
1 file changed, +20 −27
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,