refs: introduce "initial" transaction flag
There are two different ways to commit a transaction: - `ref_transaction_commit()` can be used to commit a regular transaction and is what almost every caller wants. - `initial_ref_transaction_commit()` can be used when it is known that the ref store that the transaction is committed for is empty and when there are no concurrent processes. This is used when cloning a new repository. Implementing this via two separate functions has a couple of downsides. First, every reference backend needs to implement a separate callback even in the case where they don't special-case the initial transaction. Second, backends are basically forced to reimplement the whole logic for how to commit the transaction like the "files" backend does, even though backends may wish to only tweak certain behaviour of a "normal" commit. Third, it is awkward that callers must never prepare the transaction as this is somewhat different than how a transaction typically works. Refactor the code such that we instead mark initial transactions via a separate flag when starting the transaction. This addresses all of the mentioned painpoints, where the most important part is that it will allow backends to have way more leeway in how exactly they want to handle the initial transaction. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
83b8ed8bba
commit
1c299d03e5
@ -2781,6 +2781,8 @@ static int files_transaction_prepare(struct ref_store *ref_store,
|
||||
|
||||
assert(err);
|
||||
|
||||
if (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)
|
||||
goto cleanup;
|
||||
if (!transaction->nr)
|
||||
goto cleanup;
|
||||
|
||||
@ -2985,13 +2987,10 @@ static int ref_present(const char *refname, const char *referent UNUSED,
|
||||
return string_list_has_string(affected_refnames, refname);
|
||||
}
|
||||
|
||||
static int files_initial_transaction_commit(struct ref_store *ref_store,
|
||||
static int files_transaction_finish_initial(struct files_ref_store *refs,
|
||||
struct ref_transaction *transaction,
|
||||
struct strbuf *err)
|
||||
{
|
||||
struct files_ref_store *refs =
|
||||
files_downcast(ref_store, REF_STORE_WRITE,
|
||||
"initial_ref_transaction_commit");
|
||||
size_t i;
|
||||
int ret = 0;
|
||||
struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
|
||||
@ -2999,8 +2998,8 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,
|
||||
|
||||
assert(err);
|
||||
|
||||
if (transaction->state != REF_TRANSACTION_OPEN)
|
||||
BUG("commit called for transaction that is not open");
|
||||
if (transaction->state != REF_TRANSACTION_PREPARED)
|
||||
BUG("commit called for transaction that is not prepared");
|
||||
|
||||
/* Fail if a refname appears more than once in the transaction: */
|
||||
for (i = 0; i < transaction->nr; i++)
|
||||
@ -3063,7 +3062,7 @@ static int files_initial_transaction_commit(struct ref_store *ref_store,
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (initial_ref_transaction_commit(packed_transaction, err)) {
|
||||
if (ref_transaction_commit(packed_transaction, err)) {
|
||||
ret = TRANSACTION_GENERIC_ERROR;
|
||||
}
|
||||
|
||||
@ -3091,6 +3090,8 @@ static int files_transaction_finish(struct ref_store *ref_store,
|
||||
|
||||
assert(err);
|
||||
|
||||
if (transaction->flags & REF_TRANSACTION_FLAG_INITIAL)
|
||||
return files_transaction_finish_initial(refs, transaction, err);
|
||||
if (!transaction->nr) {
|
||||
transaction->state = REF_TRANSACTION_CLOSED;
|
||||
return 0;
|
||||
@ -3617,7 +3618,6 @@ struct ref_storage_be refs_be_files = {
|
||||
.transaction_prepare = files_transaction_prepare,
|
||||
.transaction_finish = files_transaction_finish,
|
||||
.transaction_abort = files_transaction_abort,
|
||||
.initial_transaction_commit = files_initial_transaction_commit,
|
||||
|
||||
.pack_refs = files_pack_refs,
|
||||
.rename_ref = files_rename_ref,
|
||||
|
Reference in New Issue
Block a user