git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 14/18] odb/source-loose: wire up `write_object()` callback

From
Patrick Steinhardt <ps@pks.im>
Date
May 21, 2026, 08:22 UTC
Message-ID
<20260521-b4-pks-odb-source-loose-v1-14-6553b399be2d@pks.im>
In-Reply-To
<20260521-b4-pks-odb-source-loose-v1-0-6553b399be2d@pks.im>

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>
---
 object-file.c      | 58 ++++++++----------------------------------------------
 object-file.h      | 14 +++++++------
 odb/source-files.c |  5 +++--
 odb/source-loose.c | 44 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 63 insertions(+), 58 deletions(-)
diff --git a/object-file.c b/object-file.c
index fe24f00d1b..7bb5b31bca 100644
--- a/object-file.c
+++ b/object-file.c
@@ -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 @@ int odb_source_loose_write_stream(struct odb_source *source,
 	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)
 {
diff --git a/object-file.h b/object-file.h
index 1d90df9d98..2b32592de1 100644
--- a/object-file.h
+++ b/object-file.h
@@ -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);
diff --git a/odb/source-files.c b/odb/source-files.c
index ef548e6fe6..52ba04237a 100644
--- a/odb/source-files.c
+++ b/odb/source-files.c
@@ -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,
diff --git a/odb/source-loose.c b/odb/source-loose.c
index e519365d23..c91018109e 100644
--- a/odb/source-loose.c
+++ b/odb/source-loose.c
@@ -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);
-- 
2.54.0.926.g75ba10bac6.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 16 of 46 in “odb: make loose object source a proper `struct odb_source`”
  1. 00/18 odb: make loose object source a proper `struct odb_source`Patrick Steinhardt, May 21, 2026
  2. 01/18 odb/source-loose: move loose source into "odb/" subsystemPatrick Steinhardt, May 21, 2026
  3. 02/18 odb/source-loose: store pointer to "files" instead of generic sourcePatrick Steinhardt, May 21, 2026
  4. 03/18 odb/source-loose: start converting to a proper `struct odb_source`Patrick Steinhardt, May 21, 2026
  5. Junio C HamanoMay 21, 2026
  6. 04/18 odb/source-loose: wire up `reprepare()` callbackPatrick Steinhardt, May 21, 2026
  7. 05/18 odb/source-loose: wire up `close()` callbackPatrick Steinhardt, May 21, 2026
  8. 06/18 odb/source-loose: wire up `read_object_info()` callbackPatrick Steinhardt, May 21, 2026
  9. 07/18 odb/source-loose: wire up `read_object_stream()` callbackPatrick Steinhardt, May 21, 2026
  10. 08/18 odb/source-loose: wire up `for_each_object()` callbackPatrick Steinhardt, May 21, 2026
  11. 09/18 odb/source-loose: wire up `find_abbrev_len()` callbackPatrick Steinhardt, May 21, 2026
  12. 10/18 odb/source-loose: wire up `count_objects()` callbackPatrick Steinhardt, May 21, 2026
  13. 11/18 odb/source-loose: drop `odb_source_loose_has_object()`Patrick Steinhardt, May 21, 2026
  14. 12/18 odb/source-loose: wire up `freshen_object()` callbackPatrick Steinhardt, May 21, 2026
  15. 13/18 loose: refactor object map to operate on `struct odb_source_loose`Patrick Steinhardt, May 21, 2026
  16. 14/18 odb/source-loose: wire up `write_object()` callbackPatrick Steinhardt, May 21, 2026
  17. 15/18 object-file: refactor writing objects to use loose sourcePatrick Steinhardt, May 21, 2026
  18. 16/18 odb/source-loose: wire up `write_object_stream()` callbackPatrick Steinhardt, May 21, 2026
  19. Junio C HamanoMay 21, 2026
  20. Patrick SteinhardtMay 22, 2026
  21. 17/18 odb/source-loose: stub out remaining callbacksPatrick Steinhardt, May 21, 2026
  22. 18/18 odb/source-loose: drop pointer to the "files" sourcePatrick Steinhardt, May 21, 2026
  23. 00/18 odb: make loose object source a proper `struct odb_source`Patrick Steinhardt, Jun 1, 2026
  24. 01/18 odb/source-loose: move loose source into "odb/" subsystemPatrick Steinhardt, Jun 1, 2026
  25. Karthik NayakJun 3, 2026
  26. Patrick SteinhardtJun 3, 2026
  27. Karthik NayakJun 3, 2026
  28. 02/18 odb/source-loose: store pointer to "files" instead of generic sourcePatrick Steinhardt, Jun 1, 2026
  29. 03/18 odb/source-loose: start converting to a proper `struct odb_source`Patrick Steinhardt, Jun 1, 2026
  30. 04/18 odb/source-loose: wire up `reprepare()` callbackPatrick Steinhardt, Jun 1, 2026
  31. 05/18 odb/source-loose: wire up `close()` callbackPatrick Steinhardt, Jun 1, 2026
  32. 06/18 odb/source-loose: wire up `read_object_info()` callbackPatrick Steinhardt, Jun 1, 2026
  33. 07/18 odb/source-loose: wire up `read_object_stream()` callbackPatrick Steinhardt, Jun 1, 2026
  34. 08/18 odb/source-loose: wire up `for_each_object()` callbackPatrick Steinhardt, Jun 1, 2026
  35. 09/18 odb/source-loose: wire up `find_abbrev_len()` callbackPatrick Steinhardt, Jun 1, 2026
  36. 10/18 odb/source-loose: wire up `count_objects()` callbackPatrick Steinhardt, Jun 1, 2026
  37. 11/18 odb/source-loose: drop `odb_source_loose_has_object()`Patrick Steinhardt, Jun 1, 2026
  38. 12/18 odb/source-loose: wire up `freshen_object()` callbackPatrick Steinhardt, Jun 1, 2026
  39. 13/18 loose: refactor object map to operate on `struct odb_source_loose`Patrick Steinhardt, Jun 1, 2026
  40. 14/18 odb/source-loose: wire up `write_object()` callbackPatrick Steinhardt, Jun 1, 2026
  41. 15/18 object-file: refactor writing objects to use loose sourcePatrick Steinhardt, Jun 1, 2026
  42. 16/18 odb/source-loose: wire up `write_object_stream()` callbackPatrick Steinhardt, Jun 1, 2026
  43. 17/18 odb/source-loose: stub out remaining callbacksPatrick Steinhardt, Jun 1, 2026
  44. 18/18 odb/source-loose: drop pointer to the "files" sourcePatrick Steinhardt, Jun 1, 2026
  45. Junio C HamanoJun 1, 2026
  46. Junio C HamanoJun 1, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.