]> git.ipfire.org Git - thirdparty/git.git/commitdiff
odb/source-loose: wire up `write_object()` callback
authorPatrick Steinhardt <ps@pks.im>
Thu, 21 May 2026 08:22:34 +0000 (10:22 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 21 May 2026 13:35:20 +0000 (22:35 +0900)
Move `odb_source_loose_write_object()` from "object-file.c" into
"odb/source-loose.c" and wire it up as the `write_object()` callback of
the loose source.

As in preceding commits, this requires us to expose a couple of generic
functions from "object-file.c" as they are used in both subsystems now.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
object-file.c
object-file.h
odb/source-files.c
odb/source-loose.c

index fe24f00d1b79bf3287462797d91949c55ede73e7..7bb5b31bcad88b5d2f5ca6c03216923f4d656baa 100644 (file)
@@ -326,10 +326,10 @@ static void hash_object_body(const struct git_hash_algo *algo, struct git_hash_c
        git_hash_final_oid(oid, c);
 }
 
-static void write_object_file_prepare(const struct git_hash_algo *algo,
-                                     const void *buf, unsigned long len,
-                                     enum object_type type, struct object_id *oid,
-                                     char *hdr, int *hdrlen)
+void write_object_file_prepare(const struct git_hash_algo *algo,
+                              const void *buf, unsigned long len,
+                              enum object_type type, struct object_id *oid,
+                              char *hdr, int *hdrlen)
 {
        struct git_hash_ctx c;
 
@@ -746,10 +746,10 @@ static int end_loose_object_common(struct odb_source *source,
        return Z_OK;
 }
 
-static int write_loose_object(struct odb_source *source,
-                             const struct object_id *oid, char *hdr,
-                             int hdrlen, const void *buf, unsigned long len,
-                             time_t mtime, unsigned flags)
+int write_loose_object(struct odb_source *source,
+                      const struct object_id *oid, char *hdr,
+                      int hdrlen, const void *buf, unsigned long len,
+                      time_t mtime, unsigned flags)
 {
        int fd, ret;
        unsigned char compressed[4096];
@@ -926,48 +926,6 @@ cleanup:
        return err;
 }
 
-int odb_source_loose_write_object(struct odb_source *source,
-                                 const void *buf, unsigned long len,
-                                 enum object_type type, struct object_id *oid,
-                                 struct object_id *compat_oid_in,
-                                 enum odb_write_object_flags flags)
-{
-       struct odb_source_files *files = odb_source_files_downcast(source);
-       const struct git_hash_algo *algo = source->odb->repo->hash_algo;
-       const struct git_hash_algo *compat = source->odb->repo->compat_hash_algo;
-       struct object_id compat_oid;
-       char hdr[MAX_HEADER_LEN];
-       int hdrlen = sizeof(hdr);
-
-       /* Generate compat_oid */
-       if (compat) {
-               if (compat_oid_in)
-                       oidcpy(&compat_oid, compat_oid_in);
-               else if (type == OBJ_BLOB)
-                       hash_object_file(compat, buf, len, type, &compat_oid);
-               else {
-                       struct strbuf converted = STRBUF_INIT;
-                       convert_object_file(source->odb->repo, &converted, algo, compat,
-                                           buf, len, type, 0);
-                       hash_object_file(compat, converted.buf, converted.len,
-                                        type, &compat_oid);
-                       strbuf_release(&converted);
-               }
-       }
-
-       /* Normally if we have it in the pack then we do not bother writing
-        * it out into .git/objects/??/?{38} file.
-        */
-       write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen);
-       if (odb_freshen_object(source->odb, oid))
-               return 0;
-       if (write_loose_object(source, oid, hdr, hdrlen, buf, len, 0, flags))
-               return -1;
-       if (compat)
-               return repo_add_loose_object_map(files->loose, oid, &compat_oid);
-       return 0;
-}
-
 int force_object_loose(struct odb_source *source,
                       const struct object_id *oid, time_t mtime)
 {
index 1d90df9d98b78e3a5b7dbb9b97db2a23d2244707..2b32592de1135b0afdbed1b9c85381236ed99d42 100644 (file)
@@ -23,12 +23,6 @@ int index_path(struct index_state *istate, struct object_id *oid, const char *pa
 struct object_info;
 struct odb_source;
 
-int odb_source_loose_write_object(struct odb_source *source,
-                                 const void *buf, unsigned long len,
-                                 enum object_type type, struct object_id *oid,
-                                 struct object_id *compat_oid_in,
-                                 enum odb_write_object_flags flags);
-
 int odb_source_loose_write_stream(struct odb_source *source,
                                  struct odb_write_stream *stream, size_t len,
                                  struct object_id *oid);
@@ -129,6 +123,14 @@ int finalize_object_file_flags(struct repository *repo,
 void hash_object_file(const struct git_hash_algo *algo, const void *buf,
                      unsigned long len, enum object_type type,
                      struct object_id *oid);
+void write_object_file_prepare(const struct git_hash_algo *algo,
+                              const void *buf, unsigned long len,
+                              enum object_type type, struct object_id *oid,
+                              char *hdr, int *hdrlen);
+int write_loose_object(struct odb_source *source,
+                      const struct object_id *oid, char *hdr,
+                      int hdrlen, const void *buf, unsigned long len,
+                      time_t mtime, unsigned flags);
 
 /* Helper to check and "touch" a file */
 int check_and_freshen_file(const char *fn, int freshen);
index ef548e6fe69cd0c7d9f2b155821589c118f60eb8..52ba04237acfd715697f36a761ab1490006a2cbf 100644 (file)
@@ -164,8 +164,9 @@ static int odb_source_files_write_object(struct odb_source *source,
                                         struct object_id *compat_oid,
                                         enum odb_write_object_flags flags)
 {
-       return odb_source_loose_write_object(source, buf, len, type,
-                                            oid, compat_oid, flags);
+       struct odb_source_files *files = odb_source_files_downcast(source);
+       return odb_source_write_object(&files->loose->base, buf, len, type,
+                                      oid, compat_oid, flags);
 }
 
 static int odb_source_files_write_object_stream(struct odb_source *source,
index e519365d23f6809828dadf313e7672957a787798..c91018109e5b6836f7312b0f4e48b58d2e54921a 100644 (file)
@@ -5,6 +5,7 @@
 #include "hex.h"
 #include "loose.h"
 #include "object-file.h"
+#include "object-file-convert.h"
 #include "odb.h"
 #include "odb/source-files.h"
 #include "odb/source-loose.h"
@@ -588,6 +589,48 @@ static int odb_source_loose_freshen_object(struct odb_source *source,
        return !!check_and_freshen_file(path.buf, 1);
 }
 
+static int odb_source_loose_write_object(struct odb_source *source,
+                                        const void *buf, unsigned long len,
+                                        enum object_type type, struct object_id *oid,
+                                        struct object_id *compat_oid_in,
+                                        enum odb_write_object_flags flags)
+{
+       struct odb_source_loose *loose = odb_source_loose_downcast(source);
+       const struct git_hash_algo *algo = source->odb->repo->hash_algo;
+       const struct git_hash_algo *compat = source->odb->repo->compat_hash_algo;
+       struct object_id compat_oid;
+       char hdr[MAX_HEADER_LEN];
+       int hdrlen = sizeof(hdr);
+
+       /* Generate compat_oid */
+       if (compat) {
+               if (compat_oid_in)
+                       oidcpy(&compat_oid, compat_oid_in);
+               else if (type == OBJ_BLOB)
+                       hash_object_file(compat, buf, len, type, &compat_oid);
+               else {
+                       struct strbuf converted = STRBUF_INIT;
+                       convert_object_file(source->odb->repo, &converted, algo, compat,
+                                           buf, len, type, 0);
+                       hash_object_file(compat, converted.buf, converted.len,
+                                        type, &compat_oid);
+                       strbuf_release(&converted);
+               }
+       }
+
+       /* Normally if we have it in the pack then we do not bother writing
+        * it out into .git/objects/??/?{38} file.
+        */
+       write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen);
+       if (odb_freshen_object(source->odb, oid))
+               return 0;
+       if (write_loose_object(source, oid, hdr, hdrlen, buf, len, 0, flags))
+               return -1;
+       if (compat)
+               return repo_add_loose_object_map(loose, oid, &compat_oid);
+       return 0;
+}
+
 static void odb_source_loose_clear_cache(struct odb_source_loose *loose)
 {
        oidtree_clear(loose->cache);
@@ -647,6 +690,7 @@ struct odb_source_loose *odb_source_loose_new(struct odb_source_files *files)
        loose->base.find_abbrev_len = odb_source_loose_find_abbrev_len;
        loose->base.count_objects = odb_source_loose_count_objects;
        loose->base.freshen_object = odb_source_loose_freshen_object;
+       loose->base.write_object = odb_source_loose_write_object;
 
        if (!is_absolute_path(loose->base.path))
                chdir_notify_register(NULL, odb_source_loose_reparent, loose);