odb: transparently handle common transaction behavior

A new ODB transaction is created and returned via `odb_transaction_begin()` and stored in the ODB. Only a single transaction may be pending at a time. If the ODB already has a transaction, the function is expected to return NULL. Similarly, when committing a transaction via `odb_transaction_commit()` the transaction being committed must match the pending transaction and upon commit reset the ODB transaction to NULL. These behaviors apply regardless of the ODB transaction implementation. Move the corresponding logic into `odb_transaction_{begin,commit}()` accordingly. Signed-off-by: Justin Tobler <jltobler@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Justin Tobler committed Feb 2, 2026 at 18:10 UTC 3f67e3d0211dd06d27ee3ee5b23e5f328ff2db12
2 files changed +13 -10
object-file.c
-9
@@ -1994,15 +1994,8 @@ static void odb_transaction_files_commit(struct odb_transaction *base)
1994 {
1995 struct odb_transaction_files *transaction = (struct odb_transaction_files *)base;
1996
1997 - /*
1998 - * Ensure the transaction ending matches the pending transaction.
1999 - */
2000 - ASSERT(base == base->source->odb->transaction);
2001 -
1997 flush_loose_object_transaction(transaction);
1998 flush_packfile_transaction(transaction);
2004 - base->source->odb->transaction = NULL;
2005 - free(transaction);
1999 }
2000
2001 struct odb_transaction *odb_transaction_files_begin(struct odb_source *source)
@@ -2017,8 +2010,6 @@ struct odb_transaction *odb_transaction_files_begin(struct odb_source *source)
2010 transaction->base.source = source;
2011 transaction->base.commit = odb_transaction_files_commit;
2012
2020 - odb->transaction = &transaction->base;
2021 -
2013 return &transaction->base;
2014 }
2015
odb.c
+13 -1
@@ -1153,7 +1153,12 @@ void odb_reprepare(struct object_database *o)
1153
1154 struct odb_transaction *odb_transaction_begin(struct object_database *odb)
1155 {
1156 - return odb_transaction_files_begin(odb->sources);
1156 + if (odb->transaction)
1157 + return NULL;
1158 +
1159 + odb->transaction = odb_transaction_files_begin(odb->sources);
1160 +
1161 + return odb->transaction;
1162 }
1163
1164 void odb_transaction_commit(struct odb_transaction *transaction)
@@ -1161,5 +1166,12 @@ void odb_transaction_commit(struct odb_transaction *transaction)
1166 if (!transaction)
1167 return;
1168
1169 + /*
1170 + * Ensure the transaction ending matches the pending transaction.
1171 + */
1172 + ASSERT(transaction == transaction->source->odb->transaction);
1173 +
1174 transaction->commit(transaction);
1175 + transaction->source->odb->transaction = NULL;
1176 + free(transaction);
1177 }