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

[PATCH v2 6/7] object-file: generalize packfile writes to use odb_write_stream

From
Justin Tobler <jltobler@gmail.com>
Date
Apr 1, 2026, 03:03 UTC
Message-ID
<20260401030316.1847362-7-jltobler@gmail.com>
In-Reply-To
<20260401030316.1847362-1-jltobler@gmail.com>

The `index_blob_packfile_transaction()` function streams blob data directly from an fd. This makes it difficult to reuse as part of a generic transactional object writing interface.

Refactor the packfile write path to operate on a `struct odb_write_stream`, allowing callers to supply data from arbitrary sources.

Signed-off-by: Justin Tobler <jltobler@gmail.com>
---
 object-file.c | 55 +++++++++++++++++++++++++++------------------------
 1 file changed, 29 insertions(+), 26 deletions(-)
diff --git a/object-file.c b/object-file.c
index 23229fbd95..f7e830c4ec 100644
--- a/object-file.c
+++ b/object-file.c
@@ -1444,18 +1444,19 @@ static int hash_blob_stream(struct odb_write_stream *stream,
 }
 
 /*
- * Read the contents from fd for size bytes, streaming it to the
+ * Read the contents from the stream provided, streaming it to the
  * packfile in state while updating the hash in ctx.
  */
 static void stream_blob_to_pack(struct transaction_packfile *state,
-				struct git_hash_ctx *ctx, int fd, size_t size,
-				const char *path)
+				struct git_hash_ctx *ctx, size_t size,
+				struct odb_write_stream *stream)
 {
 	git_zstream s;
 	unsigned char ibuf[16384];
 	unsigned char obuf[16384];
 	unsigned hdrlen;
 	int status = Z_OK;
+	size_t total = 0;
 
 	git_deflate_init(&s, pack_compression_level);
 
@@ -1464,23 +1465,20 @@ static void stream_blob_to_pack(struct transaction_packfile *state,
 	s.avail_out = sizeof(obuf) - hdrlen;
 
 	while (status != Z_STREAM_END) {
-		if (size && !s.avail_in) {
-			size_t rsize = size < sizeof(ibuf) ? size : sizeof(ibuf);
-			ssize_t read_result = read_in_full(fd, ibuf, rsize);
-			if (read_result < 0)
-				die_errno("failed to read from '%s'", path);
-			if ((size_t)read_result != rsize)
-				die("failed to read %u bytes from '%s'",
-				    (unsigned)rsize, path);
+		if (!stream->is_finished && !s.avail_in) {
+			ssize_t rsize = stream->read(stream, ibuf, sizeof(ibuf));
+
+			if (rsize < 0)
+				die("failed to read blob data");
 
 			git_hash_update(ctx, ibuf, rsize);
 
 			s.next_in = ibuf;
 			s.avail_in = rsize;
-			size -= rsize;
+			total += rsize;
 		}
 
-		status = git_deflate(&s, size ? 0 : Z_FINISH);
+		status = git_deflate(&s, stream->is_finished ? Z_FINISH : 0);
 
 		if (!s.avail_out || status == Z_STREAM_END) {
 			size_t written = s.next_out - obuf;
@@ -1500,6 +1498,11 @@ static void stream_blob_to_pack(struct transaction_packfile *state,
 			die("unexpected deflate failure: %d", status);
 		}
 	}
+
+	if (total != size)
+		die("read %" PRIuMAX " bytes of blob data, but expected %" PRIuMAX " bytes",
+		    (uintmax_t)total, (uintmax_t)size);
+
 	git_deflate_end(&s);
 }
 
@@ -1571,10 +1574,13 @@ static void flush_packfile_transaction(struct odb_transaction_files *transaction
  * binary blobs, they generally do not want to get any conversion, and
  * callers should avoid this code path when filters are requested.
  */
-static int index_blob_packfile_transaction(struct odb_transaction_files *transaction,
-					   struct object_id *result_oid, int fd,
-					   size_t size, const char *path)
+static int index_blob_packfile_transaction(struct odb_transaction *base,
+					   struct odb_write_stream *stream,
+					   size_t size, struct object_id *result_oid)
 {
+	struct odb_transaction_files *transaction = container_of(base,
+								 struct odb_transaction_files,
+								 base);
 	struct transaction_packfile *state = &transaction->packfile;
 	struct git_hash_ctx ctx;
 	unsigned char obuf[16384];
@@ -1608,7 +1614,7 @@ static int index_blob_packfile_transaction(struct odb_transaction_files *transac
 	hashfile_checkpoint(state->f, &checkpoint);
 	idx->offset = state->offset;
 	crc32_begin(state->f);
-	stream_blob_to_pack(state, &ctx, fd, size, path);
+	stream_blob_to_pack(state, &ctx, size, stream);
 	git_hash_final_oid(result_oid, &ctx);
 
 	idx->crc32 = crc32_end(state->f);
@@ -1652,15 +1658,12 @@ int index_fd(struct index_state *istate, struct object_id *oid,
 
 		if (flags & INDEX_WRITE_OBJECT) {
 			struct object_database *odb = the_repository->objects;
-			struct odb_transaction_files *files_transaction;
-			struct odb_transaction *transaction;
-
-			transaction = odb_transaction_begin(odb);
-			files_transaction = container_of(odb->transaction,
-							 struct odb_transaction_files,
-							 base);
-			ret = index_blob_packfile_transaction(files_transaction, oid, fd,
-						      xsize_t(st->st_size), path);
+			struct odb_transaction *transaction = odb_transaction_begin(odb);
+
+			ret = index_blob_packfile_transaction(odb->transaction,
+							      &stream,
+							      xsize_t(st->st_size),
+							      oid);
 			odb_transaction_commit(transaction);
 		} else {
 			ret = hash_blob_stream(&stream,
-- 
2.53.0.381.g628a66ccf6
Previous: Justin ToblerNext: Justin Tobler
Message 33 of 57 in “odb: add write operation to ODB transaction interface”
  1. 0/6 odb: add write operation to ODB transaction interfaceJustin Tobler, Mar 31, 2026
  2. 1/6 odb: split `struct odb_transaction` into separate headerJustin Tobler, Mar 31, 2026
  3. Patrick SteinhardtMar 31, 2026
  4. Justin ToblerMar 31, 2026
  5. Junio C HamanoMar 31, 2026
  6. Justin ToblerMar 31, 2026
  7. 2/6 odb/transaction: use pluggable `begin_transaction()`Justin Tobler, Mar 31, 2026
  8. Patrick SteinhardtMar 31, 2026
  9. 3/6 object-file: remove flags from transaction packfile writesJustin Tobler, Mar 31, 2026
  10. Patrick SteinhardtMar 31, 2026
  11. Justin ToblerMar 31, 2026
  12. 4/6 object-file: avoid fd seekback by checking object size upfrontJustin Tobler, Mar 31, 2026
  13. Patrick SteinhardtMar 31, 2026
  14. Justin ToblerMar 31, 2026
  15. 5/6 object-file: generalize packfile writes to use odb_write_streamJustin Tobler, Mar 31, 2026
  16. Patrick SteinhardtMar 31, 2026
  17. Justin ToblerMar 31, 2026
  18. Patrick SteinhardtMar 31, 2026
  19. Justin ToblerMar 31, 2026
  20. Patrick SteinhardtMar 31, 2026
  21. 6/6 odb/transaction: make `write_object_stream()` pluggableJustin Tobler, Mar 31, 2026
  22. Patrick SteinhardtMar 31, 2026
  23. Justin ToblerMar 31, 2026
  24. 0/7 odb: add write operation to ODB transaction interfaceJustin Tobler, Apr 1, 2026
  25. 1/7 odb: split `struct odb_transaction` into separate headerJustin Tobler, Apr 1, 2026
  26. 3/7 odb: update `struct odb_write_stream` read() callbackJustin Tobler, Apr 1, 2026
  27. Patrick SteinhardtApr 1, 2026
  28. 2/7 odb/transaction: use pluggable `begin_transaction()`Justin Tobler, Apr 1, 2026
  29. 4/7 object-file: remove flags from transaction packfile writesJustin Tobler, Apr 1, 2026
  30. Patrick SteinhardtApr 1, 2026
  31. Justin ToblerApr 1, 2026
  32. 5/7 object-file: avoid fd seekback by checking object size upfrontJustin Tobler, Apr 1, 2026
  33. 6/7 object-file: generalize packfile writes to use odb_write_streamJustin Tobler, Apr 1, 2026
  34. 7/7 odb/transaction: make `write_object_stream()` pluggableJustin Tobler, Apr 1, 2026
  35. Patrick SteinhardtApr 1, 2026
  36. 0/7 odb: add write operation to ODB transaction interfaceJustin Tobler, Apr 2, 2026
  37. 1/7 odb: split `struct odb_transaction` into separate headerJustin Tobler, Apr 2, 2026
  38. 2/7 odb/transaction: use pluggable `begin_transaction()`Justin Tobler, Apr 2, 2026
  39. 3/7 odb: update `struct odb_write_stream` read() callbackJustin Tobler, Apr 2, 2026
  40. Jeff KingMay 11, 2026
  41. Justin ToblerMay 12, 2026
  42. 4/7 object-file: remove flags from transaction packfile writesJustin Tobler, Apr 2, 2026
  43. Jeff KingApr 6, 2026
  44. Jeff KingApr 6, 2026
  45. 5/7 object-file: avoid fd seekback by checking object size upfrontJustin Tobler, Apr 2, 2026
  46. 6/7 object-file: generalize packfile writes to use odb_write_streamJustin Tobler, Apr 2, 2026
  47. 7/7 odb/transaction: make `write_object_stream()` pluggableJustin Tobler, Apr 2, 2026
  48. Patrick SteinhardtApr 8, 2026
  49. 0/7 odb: add write operation to ODB transaction interfaceJustin Tobler, May 14, 2026
  50. 1/7 odb: split `struct odb_transaction` into separate headerJustin Tobler, May 14, 2026
  51. 2/7 odb/transaction: use pluggable `begin_transaction()`Justin Tobler, May 14, 2026
  52. 3/7 odb: update `struct odb_write_stream` read() callbackJustin Tobler, May 14, 2026
  53. 4/7 object-file: remove flags from transaction packfile writesJustin Tobler, May 14, 2026
  54. 5/7 object-file: avoid fd seekback by checking object size upfrontJustin Tobler, May 14, 2026
  55. 6/7 object-file: generalize packfile writes to use odb_write_streamJustin Tobler, May 14, 2026
  56. 7/7 odb/transaction: make `write_object_stream()` pluggableJustin Tobler, May 14, 2026
  57. Jeff KingMay 15, 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.