odb/transaction: propagate begin errors

When `odb_transaction_begin()` is invoked, the function returns the transaction pointer directly. There is no way for the backend to signal that it failed to set up its state, such as when creating the temporary object directory backing the transaction. In a subsequent commit, git-receive-pack(1) starts using ODB transactions and needs to be able to report such failures rather than silently ignore them. Refactor `odb_transaction_begin()` to return an int error code and write the resulting transaction into an out parameter. Also introduce `odb_transaction_begin_or_die()` as a convenience for callsites that do not need to handle errors explicitly. Note that `odb_transaction_begin()` now returns an error when the ODB already has an inflight transaction pending. ODB transaction call sites that may encounter an inflight transaction are updated to explicitly handle this case. Signed-off-by: Justin Tobler <jltobler@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Justin Tobler committed Jul 10, 2026 at 11:37 UTC 4576d6c5225fec402fa8d4190abb63e6f938b98d
8 files changed +45 -18
builtin/add.c
+1 -1
@@ -581,7 +581,7 @@ int cmd_add(int argc,
581 string_list_clear(&only_match_skip_worktree, 0);
582 }
583
584 - transaction = odb_transaction_begin(repo->objects);
584 + odb_transaction_begin_or_die(repo->objects, &transaction);
585
586 ps_matched = xcalloc(pathspec.nr, 1);
587 if (add_renormalize)
builtin/unpack-objects.c
+1 -1
@@ -596,7 +596,7 @@ static void unpack_all(void)
596 progress = start_progress(the_repository,
597 _("Unpacking objects"), nr_objects);
598 CALLOC_ARRAY(obj_list, nr_objects);
599 - transaction = odb_transaction_begin(the_repository->objects);
599 + odb_transaction_begin_or_die(the_repository->objects, &transaction);
600 for (i = 0; i < nr_objects; i++) {
601 unpack_one(i);
602 display_progress(progress, i + 1);
builtin/update-index.c
+1 -1
@@ -1124,7 +1124,7 @@ int cmd_update_index(int argc,
1124 * Allow the object layer to optimize adding multiple objects in
1125 * a batch.
1126 */
1127 - transaction = odb_transaction_begin(the_repository->objects);
1127 + odb_transaction_begin_or_die(the_repository->objects, &transaction);
1128 while (ctx.argc) {
1129 if (parseopt_state != PARSE_OPT_DONE)
1130 parseopt_state = parse_options_step(&ctx, options,
cache-tree.c
+5 -2
@@ -474,6 +474,7 @@ static int update_one(struct cache_tree *it,
474
475 int cache_tree_update(struct index_state *istate, int flags)
476 {
477 + int inflight = !!the_repository->objects->transaction;
478 struct odb_transaction *transaction;
479 int skip, i;
480
@@ -490,10 +491,12 @@ int cache_tree_update(struct index_state *istate, int flags)
491
492 trace_performance_enter();
493 trace2_region_enter("cache_tree", "update", istate->repo);
493 - transaction = odb_transaction_begin(the_repository->objects);
494 + if (!inflight)
495 + odb_transaction_begin_or_die(the_repository->objects, &transaction);
496 i = update_one(istate->cache_tree, istate->cache, istate->cache_nr,
497 "", 0, &skip, flags);
496 - odb_transaction_commit(transaction);
498 + if (!inflight)
499 + odb_transaction_commit(transaction);
500 trace2_region_leave("cache_tree", "update", istate->repo);
501 trace_performance_leave("cache_tree_update");
502 if (i < 0)
object-file.c
+7 -3
@@ -1352,13 +1352,17 @@ int index_fd(struct index_state *istate, struct object_id *oid,
1352
1353 if (flags & INDEX_WRITE_OBJECT) {
1354 struct object_database *odb = the_repository->objects;
1355 - struct odb_transaction *transaction = odb_transaction_begin(odb);
1355 + struct odb_transaction *transaction = odb->transaction;
1356 + int inflight = !!transaction;
1357
1357 - ret = odb_transaction_write_object_stream(odb->transaction,
1358 + if (!inflight)
1359 + odb_transaction_begin_or_die(odb, &transaction);
1360 + ret = odb_transaction_write_object_stream(transaction,
1361 &stream,
1362 xsize_t(st->st_size),
1363 oid);
1361 - odb_transaction_commit(transaction);
1364 + if (!inflight)
1365 + odb_transaction_commit(transaction);
1366 } else {
1367 ret = hash_blob_stream(&stream,
1368 the_repository->hash_algo, oid,
odb/transaction.c
+10 -4
@@ -1,15 +1,21 @@
1 #include "git-compat-util.h"
2 +#include "gettext.h"
3 #include "odb/source.h"
4 #include "odb/transaction.h"
5
5 -struct odb_transaction *odb_transaction_begin(struct object_database *odb)
6 +int odb_transaction_begin(struct object_database *odb,
7 + struct odb_transaction **out)
8 {
9 + int ret;
10 +
11 if (odb->transaction)
8 - return NULL;
12 + return error(_("object database transaction already pending"));
13
10 - odb_source_begin_transaction(odb->sources, &odb->transaction);
14 + ret = odb_source_begin_transaction(odb->sources, out);
15 + if (!ret)
16 + odb->transaction = *out;
17
12 - return odb->transaction;
18 + return ret;
19 }
20
21 void odb_transaction_commit(struct odb_transaction *transaction)
odb/transaction.h
+15 -4
@@ -1,6 +1,7 @@
1 #ifndef ODB_TRANSACTION_H
2 #define ODB_TRANSACTION_H
3
4 +#include "gettext.h"
5 #include "odb.h"
6 #include "odb/source.h"
7
@@ -36,11 +37,21 @@ struct odb_transaction {
37 };
38
39 /*
39 - * Starts an ODB transaction. Subsequent objects are written to the transaction
40 - * and not committed until odb_transaction_commit() is invoked on the
41 - * transaction. If the ODB already has a pending transaction, NULL is returned.
40 + * Starts an ODB transaction and returns it via `out`. Subsequent objects are
41 + * written to the transaction and not committed until odb_transaction_commit()
42 + * is invoked on the transaction. Returns 0 on success and a negative value on
43 + * error. Note that it is considered an error to start a new transaction if the
44 + * ODB already has an inflight transaction pending.
45 */
43 -struct odb_transaction *odb_transaction_begin(struct object_database *odb);
46 +int odb_transaction_begin(struct object_database *odb,
47 + struct odb_transaction **out);
48 +
49 +static inline void odb_transaction_begin_or_die(struct object_database *odb,
50 + struct odb_transaction **out)
51 +{
52 + if (odb_transaction_begin(odb, out))
53 + die(_("failed to start ODB transaction"));
54 +}
55
56 /*
57 * Commits an ODB transaction making the written objects visible. If the
read-cache.c
+5 -2
@@ -4012,6 +4012,7 @@ int add_files_to_cache(struct repository *repo, const char *prefix,
4012 const struct pathspec *pathspec, char *ps_matched,
4013 int include_sparse, int flags, int ignored_too )
4014 {
4015 + int inflight = !!repo->objects->transaction;
4016 struct odb_transaction *transaction;
4017 struct update_callback_data data;
4018 struct rev_info rev;
@@ -4042,9 +4043,11 @@ int add_files_to_cache(struct repository *repo, const char *prefix,
4043 * This function is invoked from commands other than 'add', which
4044 * may not have their own transaction active.
4045 */
4045 - transaction = odb_transaction_begin(repo->objects);
4046 + if (!inflight)
4047 + odb_transaction_begin_or_die(repo->objects, &transaction);
4048 run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
4047 - odb_transaction_commit(transaction);
4049 + if (!inflight)
4050 + odb_transaction_commit(transaction);
4051
4052 release_revisions(&rev);
4053 return !!data.add_errors;