]> git.ipfire.org Git - thirdparty/git.git/commitdiff
object-file: force objects loose via generic interface
authorPatrick Steinhardt <ps@pks.im>
Fri, 17 Jul 2026 09:32:15 +0000 (11:32 +0200)
committerJunio C Hamano <gitster@pobox.com>
Sun, 19 Jul 2026 02:03:48 +0000 (19:03 -0700)
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>
object-file.c

index 067a63a4f1e13b949465f3a8e04b39c9e3979018..89825feed0c1b1998066dfb3e604357e980372c8 100644 (file)
@@ -898,13 +898,11 @@ int force_object_loose(struct odb_source *source,
 {
        struct odb_source_files *files = odb_source_files_downcast(source);
        const struct git_hash_algo *compat = source->odb->repo->compat_hash_algo;
-       void *buf = NULL;
-       size_t len;
        struct object_info oi = OBJECT_INFO_INIT;
-       struct object_id compat_oid;
+       struct object_id compat_oid, *compat_oid_p = NULL;
        enum object_type type;
-       char hdr[MAX_HEADER_LEN];
-       int hdrlen;
+       void *buf = NULL;
+       size_t len;
        int ret;
 
        for (struct odb_source *s = source->odb->sources; s; s = s->next) {
@@ -927,15 +925,12 @@ int force_object_loose(struct odb_source *source,
                                    oid_to_hex(oid), compat->name);
                        goto out;
                }
-       }
 
-       hdrlen = format_object_header(hdr, sizeof(hdr), type, len);
-       ret = write_loose_object(files->loose, oid, hdr, hdrlen, buf, len, mtime, 0);
-       if (ret)
-               goto out;
+               compat_oid_p = &compat_oid;
+       }
 
-       if (compat)
-               ret = repo_add_loose_object_map(files->loose, oid, &compat_oid);
+       ret = odb_source_write_object(&files->loose->base, buf, len, type, oid,
+                                     compat_oid_p, mtime, 0);
 
 out:
        free(buf);