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

[PATCH 4/9] odb: lift object existence check out of the "loose" backend

From
Patrick Steinhardt <ps@pks.im>
Date
Jul 17, 2026, 09:32 UTC
Message-ID
<20260717-pks-odb-move-loose-object-writing-v1-4-46446a3cb5b7@pks.im>
In-Reply-To
<20260717-pks-odb-move-loose-object-writing-v1-0-46446a3cb5b7@pks.im>

Before writing a new loose object we first check whether the object already exists in any of the sources attached to the object database. This results in a couple of issues:

  - We have a layering violation, where the source needs to be aware of
    objects stored in any of the other sources.
  - Every backend would have to reimplement this check, which feels
    somewhat pointless.
  - It is not possible to easily write an object into a source in case
    the same object already exists in another source.

Refactor the code and lift up the object existence check from the "loose" backend into the generic ODB layer. No callers need adjustment as none of them write via a specific source, but via the ODB layer.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 odb.c              | 7 +++++++
 odb/source-loose.c | 8 ++------
 2 files changed, 9 insertions(+), 6 deletions(-)
diff --git a/odb.c b/odb.c
index 4adbdf8a64..bfeca76f4e 100644
--- a/odb.c
+++ b/odb.c
@@ -997,6 +997,13 @@ int odb_write_object_ext(struct object_database *odb,
 
 	hash_object_file(odb->repo->hash_algo, buf, len, type, oid);
 
+	/*
+	 * We can skip the write in case we already have the object available.
+	 * In that case, we only freshen its mtime.
+	 */
+	if (odb_freshen_object(odb, oid))
+		return 0;
+
 	if (compat) {
 		const struct git_hash_algo *algo = odb->repo->hash_algo;
 
diff --git a/odb/source-loose.c b/odb/source-loose.c
index d4715da6d1..04af1a54a3 100644
--- a/odb/source-loose.c
+++ b/odb/source-loose.c
@@ -595,16 +595,12 @@ static int odb_source_loose_write_object(struct odb_source *source,
 
 	hdrlen = format_object_header(hdr, sizeof(hdr), type, len);
 
-	/*
-	 * Normally if we have it in the pack then we do not bother writing
-	 * it out into .git/objects/??/?{38} file.
-	 */
-	if (odb_freshen_object(source->odb, oid))
-		return 0;
 	if (write_loose_object(loose, oid, hdr, hdrlen, buf, len, 0, flags))
 		return -1;
+
 	if (compat_oid)
 		return repo_add_loose_object_map(loose, oid, compat_oid);
+
 	return 0;
 }
 
-- 
2.55.0.407.g700c83d4f3.dirty
Previous: Patrick SteinhardtNext: Toon Claes
Message 7 of 17 in “object-file: move writing of loose objects into "loose" source”
  1. 0/9 object-file: move writing of loose objects into "loose" sourcePatrick Steinhardt, Jul 17, 2026
  2. 1/9 odb: compute compat object ID in `odb_write_object_ext()`Patrick Steinhardt, Jul 17, 2026
  3. Justin ToblerJul 28, 2026
  4. 2/9 t/u-odb-inmemory: implement wrapper for writing objectsPatrick Steinhardt, Jul 17, 2026
  5. Justin ToblerJul 28, 2026
  6. 3/9 odb: compute object hash in `odb_write_object_ext()`Patrick Steinhardt, Jul 17, 2026
  7. 4/9 odb: lift object existence check out of the "loose" backendPatrick Steinhardt, Jul 17, 2026
  8. Toon ClaesJul 22, 2026
  9. 5/9 odb: support setting mtime when writing objectsPatrick Steinhardt, Jul 17, 2026
  10. 6/9 object-file: fix memory leak in `force_object_loose()`Patrick Steinhardt, Jul 17, 2026
  11. 7/9 object-file: force objects loose via generic interfacePatrick Steinhardt, Jul 17, 2026
  12. 8/9 object-file: move `force_object_loose()`Patrick Steinhardt, Jul 17, 2026
  13. 9/9 object-file: move logic to write loose objectsPatrick Steinhardt, Jul 17, 2026
  14. Toon ClaesJul 22, 2026
  15. SZEDER GáborJul 18, 2026
  16. Junio C HamanoJul 19, 2026
  17. Junio C HamanoJul 19, 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.