@@ -30,11 +30,13 @@ struct bulk_checkin_packfile {
3030 uint32_t nr_written ;
3131};
3232
33- static struct odb_transaction {
33+ struct odb_transaction {
34+ struct object_database * odb ;
35+
3436 int nesting ;
3537 struct tmp_objdir * objdir ;
3638 struct bulk_checkin_packfile packfile ;
37- } transaction ;
39+ };
3840
3941static void finish_tmp_packfile (struct strbuf * basename ,
4042 const char * pack_tmp_name ,
@@ -98,12 +100,12 @@ static void flush_bulk_checkin_packfile(struct bulk_checkin_packfile *state)
98100/*
99101 * Cleanup after batch-mode fsync_object_files.
100102 */
101- static void flush_batch_fsync (void )
103+ static void flush_batch_fsync (struct odb_transaction * transaction )
102104{
103105 struct strbuf temp_path = STRBUF_INIT ;
104106 struct tempfile * temp ;
105107
106- if (!transaction . objdir )
108+ if (!transaction -> objdir )
107109 return ;
108110
109111 /*
@@ -125,8 +127,8 @@ static void flush_batch_fsync(void)
125127 * Make the object files visible in the primary ODB after their data is
126128 * fully durable.
127129 */
128- tmp_objdir_migrate (transaction . objdir );
129- transaction . objdir = NULL ;
130+ tmp_objdir_migrate (transaction -> objdir );
131+ transaction -> objdir = NULL ;
130132}
131133
132134static int already_written (struct bulk_checkin_packfile * state , struct object_id * oid )
@@ -325,23 +327,24 @@ static int deflate_blob_to_pack(struct bulk_checkin_packfile *state,
325327 return 0 ;
326328}
327329
328- void prepare_loose_object_bulk_checkin (void )
330+ void prepare_loose_object_bulk_checkin (struct odb_transaction * transaction )
329331{
330332 /*
331333 * We lazily create the temporary object directory
332334 * the first time an object might be added, since
333335 * callers may not know whether any objects will be
334336 * added at the time they call begin_odb_transaction.
335337 */
336- if (!transaction . nesting || transaction . objdir )
338+ if (!transaction || transaction -> objdir )
337339 return ;
338340
339- transaction . objdir = tmp_objdir_create (the_repository , "bulk-fsync" );
340- if (transaction . objdir )
341- tmp_objdir_replace_primary_odb (transaction . objdir , 0 );
341+ transaction -> objdir = tmp_objdir_create (the_repository , "bulk-fsync" );
342+ if (transaction -> objdir )
343+ tmp_objdir_replace_primary_odb (transaction -> objdir , 0 );
342344}
343345
344- void fsync_loose_object_bulk_checkin (int fd , const char * filename )
346+ void fsync_loose_object_bulk_checkin (struct odb_transaction * transaction ,
347+ int fd , const char * filename )
345348{
346349 /*
347350 * If we have an active ODB transaction, we issue a call that
@@ -350,44 +353,65 @@ void fsync_loose_object_bulk_checkin(int fd, const char *filename)
350353 * before renaming the objects to their final names as part of
351354 * flush_batch_fsync.
352355 */
353- if (!transaction . objdir ||
356+ if (!transaction || ! transaction -> objdir ||
354357 git_fsync (fd , FSYNC_WRITEOUT_ONLY ) < 0 ) {
355358 if (errno == ENOSYS )
356359 warning (_ ("core.fsyncMethod = batch is unsupported on this platform" ));
357360 fsync_or_die (fd , filename );
358361 }
359362}
360363
361- int index_blob_bulk_checkin (struct object_id * oid ,
362- int fd , size_t size ,
364+ int index_blob_bulk_checkin (struct odb_transaction * transaction ,
365+ struct object_id * oid , int fd , size_t size ,
363366 const char * path , unsigned flags )
364367{
365- int status = deflate_blob_to_pack (& transaction .packfile , oid , fd , size ,
366- path , flags );
367- if (!transaction .nesting )
368- flush_bulk_checkin_packfile (& transaction .packfile );
368+ int status ;
369+
370+ if (transaction ) {
371+ status = deflate_blob_to_pack (& transaction -> packfile , oid , fd ,
372+ size , path , flags );
373+ } else {
374+ struct bulk_checkin_packfile state = { 0 };
375+
376+ status = deflate_blob_to_pack (& state , oid , fd , size , path , flags );
377+ flush_bulk_checkin_packfile (& state );
378+ }
379+
369380 return status ;
370381}
371382
372- void begin_odb_transaction (void )
383+ struct odb_transaction * begin_odb_transaction (struct object_database * odb )
373384{
374- transaction .nesting += 1 ;
385+ if (!odb -> transaction ) {
386+ CALLOC_ARRAY (odb -> transaction , 1 );
387+ odb -> transaction -> odb = odb ;
388+ }
389+
390+ odb -> transaction -> nesting += 1 ;
391+
392+ return odb -> transaction ;
375393}
376394
377- void flush_odb_transaction (void )
395+ void flush_odb_transaction (struct odb_transaction * transaction )
378396{
379- flush_batch_fsync ();
380- flush_bulk_checkin_packfile (& transaction .packfile );
397+ if (!transaction )
398+ return ;
399+
400+ flush_batch_fsync (transaction );
401+ flush_bulk_checkin_packfile (& transaction -> packfile );
381402}
382403
383- void end_odb_transaction (void )
404+ void end_odb_transaction (struct odb_transaction * transaction )
384405{
385- transaction .nesting -= 1 ;
386- if (transaction .nesting < 0 )
406+ if (!transaction || transaction -> nesting == 0 )
387407 BUG ("Unbalanced ODB transaction nesting" );
388408
389- if (transaction .nesting )
409+ transaction -> nesting -= 1 ;
410+
411+ if (transaction -> nesting )
390412 return ;
391413
392- flush_odb_transaction ();
414+ flush_odb_transaction (transaction );
415+ transaction -> odb -> transaction = NULL ;
416+ free (transaction );
393417}
0 commit comments