[PATCH 1/6] odb/transaction: add transaction release interface
Justin Tobler <[email protected]>
| Newsgroups | org.kernel.vger.git |
|---|---|
| Message-ID | <[email protected]> |
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_release()` to explicitly clean up the transaction accordingly. Note that the release 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. Signed-off-by: Justin Tobler <[email protected]> --- builtin/add.c | 3 ++- builtin/receive-pack.c | 1 + builtin/unpack-objects.c | 1 + builtin/update-index.c | 2 ++ cache-tree.c | 4 +++- object-file.c | 4 +++- odb/transaction.c | 12 +++++++++++- odb/transaction.h | 14 ++++++++++++++ read-cache.c | 4 +++- 9 files changed, 40 insertions(+), 5 deletions(-) diff --git a/builtin/add.c b/builtin/add.c index 60ffbede2b..037491a51e 100644 --- a/builtin/add.c +++ b/builtin/add.c @@ -393,7 +393,7 @@ int cmd_add(int argc, char *seen = NULL; char *ps_matched = NULL; struct lock_file lock_file = LOCK_INIT; - struct odb_transaction *transaction; + struct odb_transaction *transaction = NULL; repo_config(repo, add_config, NULL); @@ -610,5 +610,6 @@ int cmd_add(int argc, free(ps_matched); dir_clear(&dir); clear_pathspec(&pathspec); + odb_transaction_release(transaction); return exit_status; } diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index 86933d8d7e..420de9aa7f 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -2714,6 +2714,7 @@ int cmd_receive_pack(int argc, use_keepalive = KEEPALIVE_ALWAYS; execute_commands(commands, unpack_status, &si, transaction, &push_options); + odb_transaction_release(transaction); delete_tempfile(&pack_lockfile); sigchain_push(SIGPIPE, SIG_IGN); if (report_status_v2) diff --git a/builtin/unpack-objects.c b/builtin/unpack-objects.c index 4263edfbec..13d4b7f1ad 100644 --- a/builtin/unpack-objects.c +++ b/builtin/unpack-objects.c @@ -604,6 +604,7 @@ static void unpack_all(void) display_progress(progress, i + 1); } odb_transaction_commit(transaction); + odb_transaction_release(transaction); stop_progress(&progress); if (delta_list) diff --git a/builtin/update-index.c b/builtin/update-index.c index 241abd4332..1484835ef0 100644 --- a/builtin/update-index.c +++ b/builtin/update-index.c @@ -1157,6 +1157,7 @@ int cmd_update_index(int argc, */ if (transaction && verbose) { odb_transaction_commit(transaction); + odb_transaction_release(transaction); transaction = NULL; } @@ -1225,6 +1226,7 @@ int cmd_update_index(int argc, * By now we have added all of the new objects */ odb_transaction_commit(transaction); + odb_transaction_release(transaction); if (split_index > 0) { if (repo_config_get_split_index(the_repository) == 0) diff --git a/cache-tree.c b/cache-tree.c index d92f513286..5a2fa7f22d 100644 --- a/cache-tree.c +++ b/cache-tree.c @@ -537,8 +537,10 @@ int cache_tree_update(struct index_state *istate, int flags) odb_transaction_begin_or_die(the_repository->objects, &transaction, 0); i = update_one(istate->cache_tree, istate->cache, istate->cache_nr, "", 0, &skip, flags); - if (!inflight) + if (!inflight) { odb_transaction_commit(transaction); + odb_transaction_release(transaction); + } trace2_region_leave("cache_tree", "update", istate->repo); trace_performance_leave("cache_tree_update"); if (i < 0) diff --git a/object-file.c b/object-file.c index ec35c318bc..30b4717d3e 100644 --- a/object-file.c +++ b/object-file.c @@ -964,8 +964,10 @@ int index_fd(struct index_state *istate, struct object_id *oid, &stream, xsize_t(st->st_size), oid); - if (!inflight) + if (!inflight) { odb_transaction_commit(transaction); + odb_transaction_release(transaction); + } } else { ret = hash_blob_stream(&stream, the_repository->hash_algo, oid, diff --git a/odb/transaction.c b/odb/transaction.c index dab7da6a9a..ce1e24f3ed 100644 --- a/odb/transaction.c +++ b/odb/transaction.c @@ -33,11 +33,21 @@ int odb_transaction_commit(struct odb_transaction *transaction) ret = transaction->commit(transaction); transaction->source->odb->transaction = NULL; - free(transaction); return ret; } +void odb_transaction_release(struct odb_transaction *transaction) +{ + if (!transaction) + return; + + if (transaction->release) + transaction->release(transaction); + + free(transaction); +} + int odb_transaction_write_object_stream(struct odb_transaction *transaction, struct odb_write_stream *stream, size_t len, struct object_id *oid) diff --git a/odb/transaction.h b/odb/transaction.h index 4cb2eafcbf..ec0b27c449 100644 --- a/odb/transaction.h +++ b/odb/transaction.h @@ -22,6 +22,13 @@ struct odb_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. + */ + void (*release)(struct odb_transaction *transaction); + /* * This callback is expected to write the given object stream into * the ODB transaction. Note that for now, only blobs support streaming. @@ -75,6 +82,13 @@ static inline void odb_transaction_begin_or_die(struct object_database *odb, */ int odb_transaction_commit(struct odb_transaction *transaction); +/* + * Releases 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. + */ +void odb_transaction_release(struct odb_transaction *transaction); + /* * 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 diff --git a/read-cache.c b/read-cache.c index 6c449f393d..42623f6e10 100644 --- a/read-cache.c +++ b/read-cache.c @@ -4048,8 +4048,10 @@ int add_files_to_cache(struct repository *repo, const char *prefix, if (!inflight) odb_transaction_begin_or_die(repo->objects, &transaction, 0); run_diff_files(&rev, DIFF_RACY_IS_MODIFIED); - if (!inflight) + if (!inflight) { odb_transaction_commit(transaction); + odb_transaction_release(transaction); + } release_revisions(&rev); return !!data.add_errors; -- 2.55.0.424.g13c7afec21