bulk-checkin: introduce object database transaction structure

Object database transaction state is stored across several global variables in the bulk-checkin subsystem. Consolidate this state into a single `struct odb_transaction` global. In a subsequent commit, the transactional interfaces will be updated to wire this structure instead of relying on a global variable. Signed-off-by: Justin Tobler <jltobler@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Justin Tobler committed Aug 22, 2025 at 16:34 UTC 98518304c5761ba04cefb6d73c5698db7e46d1c2
1 file changed +24 -22
bulk-checkin.c
+24 -22
@@ -19,11 +19,7 @@
19 #include "object-file.h"
20 #include "odb.h"
21
22 -static int odb_transaction_nesting;
23 -
24 -static struct tmp_objdir *bulk_fsync_objdir;
25 -
26 -static struct bulk_checkin_packfile {
22 +struct bulk_checkin_packfile {
23 char *pack_tmp_name;
24 struct hashfile *f;
25 off_t offset;
@@ -32,7 +28,13 @@ static struct bulk_checkin_packfile {
28 struct pack_idx_entry **written;
29 uint32_t alloc_written;
30 uint32_t nr_written;
35 -} bulk_checkin_packfile;
31 +};
32 +
33 +static struct odb_transaction {
34 + int nesting;
35 + struct tmp_objdir *objdir;
36 + struct bulk_checkin_packfile packfile;
37 +} transaction;
38
39 static void finish_tmp_packfile(struct strbuf *basename,
40 const char *pack_tmp_name,
@@ -101,7 +103,7 @@ static void flush_batch_fsync(void)
103 struct strbuf temp_path = STRBUF_INIT;
104 struct tempfile *temp;
105
104 - if (!bulk_fsync_objdir)
106 + if (!transaction.objdir)
107 return;
108
109 /*
@@ -123,8 +125,8 @@ static void flush_batch_fsync(void)
125 * Make the object files visible in the primary ODB after their data is
126 * fully durable.
127 */
126 - tmp_objdir_migrate(bulk_fsync_objdir);
127 - bulk_fsync_objdir = NULL;
128 + tmp_objdir_migrate(transaction.objdir);
129 + transaction.objdir = NULL;
130 }
131
132 static int already_written(struct bulk_checkin_packfile *state, struct object_id *oid)
@@ -331,12 +333,12 @@ void prepare_loose_object_bulk_checkin(void)
333 * callers may not know whether any objects will be
334 * added at the time they call begin_odb_transaction.
335 */
334 - if (!odb_transaction_nesting || bulk_fsync_objdir)
336 + if (!transaction.nesting || transaction.objdir)
337 return;
338
337 - bulk_fsync_objdir = tmp_objdir_create(the_repository, "bulk-fsync");
338 - if (bulk_fsync_objdir)
339 - tmp_objdir_replace_primary_odb(bulk_fsync_objdir, 0);
339 + transaction.objdir = tmp_objdir_create(the_repository, "bulk-fsync");
340 + if (transaction.objdir)
341 + tmp_objdir_replace_primary_odb(transaction.objdir, 0);
342 }
343
344 void fsync_loose_object_bulk_checkin(int fd, const char *filename)
@@ -348,7 +350,7 @@ void fsync_loose_object_bulk_checkin(int fd, const char *filename)
350 * before renaming the objects to their final names as part of
351 * flush_batch_fsync.
352 */
351 - if (!bulk_fsync_objdir ||
353 + if (!transaction.objdir ||
354 git_fsync(fd, FSYNC_WRITEOUT_ONLY) < 0) {
355 if (errno == ENOSYS)
356 warning(_("core.fsyncMethod = batch is unsupported on this platform"));
@@ -360,31 +362,31 @@ int index_blob_bulk_checkin(struct object_id *oid,
362 int fd, size_t size,
363 const char *path, unsigned flags)
364 {
363 - int status = deflate_blob_to_pack(&bulk_checkin_packfile, oid, fd, size,
365 + int status = deflate_blob_to_pack(&transaction.packfile, oid, fd, size,
366 path, flags);
365 - if (!odb_transaction_nesting)
366 - flush_bulk_checkin_packfile(&bulk_checkin_packfile);
367 + if (!transaction.nesting)
368 + flush_bulk_checkin_packfile(&transaction.packfile);
369 return status;
370 }
371
372 void begin_odb_transaction(void)
373 {
372 - odb_transaction_nesting += 1;
374 + transaction.nesting += 1;
375 }
376
377 void flush_odb_transaction(void)
378 {
379 flush_batch_fsync();
378 - flush_bulk_checkin_packfile(&bulk_checkin_packfile);
380 + flush_bulk_checkin_packfile(&transaction.packfile);
381 }
382
383 void end_odb_transaction(void)
384 {
383 - odb_transaction_nesting -= 1;
384 - if (odb_transaction_nesting < 0)
385 + transaction.nesting -= 1;
386 + if (transaction.nesting < 0)
387 BUG("Unbalanced ODB transaction nesting");
388
387 - if (odb_transaction_nesting)
389 + if (transaction.nesting)
390 return;
391
392 flush_odb_transaction();