odb/transaction: add transaction finalize interface

When committing an ODB transaction via `odb_transaction_commit()`, the
staged objects are made visible and the underlying transaction is freed
at the same time. Coupling these two steps does not leave room for any
post-commit transaction operations to be introduced though. Such a
capability is useful if an ODB transaction backend needs to hold on to
lockfiles after transaction commit until references are updated, as is
the case with the existing "files" backend in git-receive-pack(1).

Stop freeing the transaction in `odb_transaction_commit()` and introduce
`odb_transaction_finalize()` to explicitly clean up the transaction
accordingly. Note that the finalize interface also provides an optional
callback for any backend-specific deferred cleanup. In a subsequent
commit, the "files" transaction backend will use this to remove ".keep"
files generated for packfiles received via git-receive-pack(1) after
references have been updated. In preparation for this, the
`odb_transaction_finalize()` call site in git-receive-pack(1) is made
after the reference updates are finished.

All other callers commit a transaction and immediately finalize it
without any work happening in between those two operations.
Consequently, they cannot meaningfully recover in case either of them
would fail, and spelling out these two separate steps with proper error
handling would be quite repetitive and pointless. Introduce a helper
`odb_transaction_commit_and_finalize_or_die()` for those call sites and
update them accordingly.

Signed-off-by: Justin Tobler <jltobler@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
main
Justin Tobler 2026-08-20 18:49:33 -05:00 committed by Junio C Hamano
parent 7874b2c7e1
commit ecdda043e0
9 changed files with 46 additions and 8 deletions

View File

@ -393,7 +393,7 @@ int cmd_add(int argc,
char *seen = NULL; char *seen = NULL;
char *ps_matched = NULL; char *ps_matched = NULL;
struct lock_file lock_file = LOCK_INIT; struct lock_file lock_file = LOCK_INIT;
struct odb_transaction *transaction; struct odb_transaction *transaction = NULL;


repo_config(repo, add_config, NULL); repo_config(repo, add_config, NULL);


@ -600,7 +600,7 @@ int cmd_add(int argc,


if (chmod_arg && pathspec.nr) if (chmod_arg && pathspec.nr)
exit_status |= chmod_pathspec(repo, &pathspec, chmod_arg[0], show_only); exit_status |= chmod_pathspec(repo, &pathspec, chmod_arg[0], show_only);
odb_transaction_commit(transaction); odb_transaction_commit_and_finalize_or_die(transaction);


finish: finish:
if (write_locked_index(repo->index, &lock_file, if (write_locked_index(repo->index, &lock_file,

View File

@ -2720,6 +2720,7 @@ int cmd_receive_pack(int argc,
use_keepalive = KEEPALIVE_ALWAYS; use_keepalive = KEEPALIVE_ALWAYS;
execute_commands(commands, unpack_status, &si, transaction, execute_commands(commands, unpack_status, &si, transaction,
&push_options); &push_options);
odb_transaction_finalize(transaction);
delete_tempfile(&pack_lockfile); delete_tempfile(&pack_lockfile);
sigchain_push(SIGPIPE, SIG_IGN); sigchain_push(SIGPIPE, SIG_IGN);
if (report_status_v2) if (report_status_v2)

View File

@ -603,7 +603,7 @@ static void unpack_all(void)
unpack_one(i); unpack_one(i);
display_progress(progress, i + 1); display_progress(progress, i + 1);
} }
odb_transaction_commit(transaction); odb_transaction_commit_and_finalize_or_die(transaction);
stop_progress(&progress); stop_progress(&progress);


if (delta_list) if (delta_list)

View File

@ -1156,7 +1156,7 @@ int cmd_update_index(int argc,
* a transaction. * a transaction.
*/ */
if (transaction && verbose) { if (transaction && verbose) {
odb_transaction_commit(transaction); odb_transaction_commit_and_finalize_or_die(transaction);
transaction = NULL; transaction = NULL;
} }


@ -1224,7 +1224,7 @@ int cmd_update_index(int argc,
/* /*
* By now we have added all of the new objects * By now we have added all of the new objects
*/ */
odb_transaction_commit(transaction); odb_transaction_commit_and_finalize_or_die(transaction);


if (split_index > 0) { if (split_index > 0) {
if (repo_config_get_split_index(the_repository) == 0) if (repo_config_get_split_index(the_repository) == 0)

View File

@ -538,7 +538,7 @@ int cache_tree_update(struct index_state *istate, int flags)
i = update_one(istate->cache_tree, istate->cache, istate->cache_nr, i = update_one(istate->cache_tree, istate->cache, istate->cache_nr,
"", 0, &skip, flags); "", 0, &skip, flags);
if (!inflight) if (!inflight)
odb_transaction_commit(transaction); odb_transaction_commit_and_finalize_or_die(transaction);
trace2_region_leave("cache_tree", "update", istate->repo); trace2_region_leave("cache_tree", "update", istate->repo);
trace_performance_leave("cache_tree_update"); trace_performance_leave("cache_tree_update");
if (i < 0) if (i < 0)

View File

@ -965,7 +965,7 @@ int index_fd(struct index_state *istate, struct object_id *oid,
xsize_t(st->st_size), xsize_t(st->st_size),
oid); oid);
if (!inflight) if (!inflight)
odb_transaction_commit(transaction); odb_transaction_commit_and_finalize_or_die(transaction);
} else { } else {
ret = hash_blob_stream(&stream, ret = hash_blob_stream(&stream,
the_repository->hash_algo, oid, the_repository->hash_algo, oid,

View File

@ -33,6 +33,20 @@ int odb_transaction_commit(struct odb_transaction *transaction)


ret = transaction->commit(transaction); ret = transaction->commit(transaction);
transaction->source->odb->transaction = NULL; transaction->source->odb->transaction = NULL;

return ret;
}

int odb_transaction_finalize(struct odb_transaction *transaction)
{
int ret = 0;

if (!transaction)
return 0;

if (transaction->finalize)
ret = transaction->finalize(transaction);

free(transaction); free(transaction);


return ret; return ret;

View File

@ -22,6 +22,13 @@ struct odb_transaction {
*/ */
int (*commit)(struct odb_transaction *transaction); int (*commit)(struct odb_transaction *transaction);


/*
* Optional ODB source specific callback invoked when the transaction
* needs to perform any deferred cleanup after objects have been
* committed. Returns 0 on success, a negative error code otherwise.
*/
int (*finalize)(struct odb_transaction *transaction);

/* /*
* This callback is expected to write the given object stream into * This callback is expected to write the given object stream into
* the ODB transaction. Note that for now, only blobs support streaming. * the ODB transaction. Note that for now, only blobs support streaming.
@ -75,6 +82,22 @@ static inline void odb_transaction_begin_or_die(struct object_database *odb,
*/ */
int odb_transaction_commit(struct odb_transaction *transaction); int odb_transaction_commit(struct odb_transaction *transaction);


/*
* Finalizes an ODB transaction, performing any deferred cleanup and freeing it.
* Must be called for every successfully started transaction. Note that, if the
* specified transaction is NULL, the function is a no-op. Returns 0 on success,
* a negative error code otherwise.
*/
int odb_transaction_finalize(struct odb_transaction *transaction);

static inline void odb_transaction_commit_and_finalize_or_die(struct odb_transaction *transaction)
{
if (odb_transaction_commit(transaction))
die(_("failed to commit ODB transaction"));
if (odb_transaction_finalize(transaction))
die(_("failed to finalize ODB transaction"));
}

/* /*
* Writes the object in the provided stream into the transaction. The resulting * Writes the object in the provided stream into the transaction. The resulting
* object ID is written into the out pointer. Returns 0 on success, a negative * object ID is written into the out pointer. Returns 0 on success, a negative

View File

@ -4049,7 +4049,7 @@ int add_files_to_cache(struct repository *repo, const char *prefix,
odb_transaction_begin_or_die(repo->objects, &transaction, 0); odb_transaction_begin_or_die(repo->objects, &transaction, 0);
run_diff_files(&rev, DIFF_RACY_IS_MODIFIED); run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
if (!inflight) if (!inflight)
odb_transaction_commit(transaction); odb_transaction_commit_and_finalize_or_die(transaction);


release_revisions(&rev); release_revisions(&rev);
return !!data.add_errors; return !!data.add_errors;