object-file: force objects loose via generic interface

When repacking objects we may end up "loosening" objects via `force_objects_loose()`. The implementation of this logic still sits with "object-file.c" even though it is ultimately an implementation detail of the "files" backend. Moving this logic around is non-trivial though as we depend on `write_loose_object()`, which is an internal implementation detail of how we write loose objects. Until now it wasn't possible to use the generic function `odb_source_write_object()` though, because the "loose" implementation thereof would skip writing the object in case it already exists in any other source. This restriction was lifted over the preceding commits though, where this object existence check is now handled on the object database level and not on the individual source level anymore. Consequently, it is now possible to use generic interfaces. Refactor the code accordingly so that we can move the logic around in a subsequent commit. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jul 17, 2026 at 11:32 UTC 627d3b7ab5dc76f0d4e4ef7a42184524df8f2ab1
1 file changed +7 -12
object-file.c
+7 -12
@@ -898,13 +898,11 @@ int force_object_loose(struct odb_source *source,
898 {
899 struct odb_source_files *files = odb_source_files_downcast(source);
900 const struct git_hash_algo *compat = source->odb->repo->compat_hash_algo;
901 - void *buf = NULL;
902 - size_t len;
901 struct object_info oi = OBJECT_INFO_INIT;
904 - struct object_id compat_oid;
902 + struct object_id compat_oid, *compat_oid_p = NULL;
903 enum object_type type;
906 - char hdr[MAX_HEADER_LEN];
907 - int hdrlen;
904 + void *buf = NULL;
905 + size_t len;
906 int ret;
907
908 for (struct odb_source *s = source->odb->sources; s; s = s->next) {
@@ -927,15 +925,12 @@ int force_object_loose(struct odb_source *source,
925 oid_to_hex(oid), compat->name);
926 goto out;
927 }
930 - }
928
932 - hdrlen = format_object_header(hdr, sizeof(hdr), type, len);
933 - ret = write_loose_object(files->loose, oid, hdr, hdrlen, buf, len, mtime, 0);
934 - if (ret)
935 - goto out;
929 + compat_oid_p = &compat_oid;
930 + }
931
937 - if (compat)
938 - ret = repo_add_loose_object_map(files->loose, oid, &compat_oid);
932 + ret = odb_source_write_object(&files->loose->base, buf, len, type, oid,
933 + compat_oid_p, mtime, 0);
934
935 out:
936 free(buf);