aboutsummaryrefslogtreecommitdiffstats
path: root/refs
diff options
context:
space:
mode:
authorKarthik Nayak <karthik.188@gmail.com>2025-04-08 10:51:11 +0200
committerJunio C Hamano <gitster@pobox.com>2025-04-08 07:57:21 -0700
commit31726bb90d70236f7afaa345bf45195e2ef62d22 (patch)
treedc023d15f3b1de5d8036a6f043a911c435fd18af /refs
parentrefs: implement batch reference update support (diff)
downloadgit-31726bb90d70236f7afaa345bf45195e2ef62d22.tar.gz
git-31726bb90d70236f7afaa345bf45195e2ef62d22.zip
refs: support rejection in batch updates during F/D checks
The `refs_verify_refnames_available()` is used to batch check refnames for F/D conflicts. While this is the more performant alternative than its individual version, it does not provide rejection capabilities on a single update level. For batched updates, this would mean a rejection of the entire transaction whenever one reference has a F/D conflict. Modify the function to call `ref_transaction_maybe_set_rejected()` to check if a single update can be rejected. Since this function is only internally used within 'refs/' and we want to pass in a `struct ref_transaction *` as a variable. We also move and mark `refs_verify_refnames_available()` to 'refs-internal.h' to be an internal function. Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Acked-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'refs')
-rw-r--r--refs/files-backend.c27
-rw-r--r--refs/refs-internal.h16
-rw-r--r--refs/reftable-backend.c11
3 files changed, 42 insertions, 12 deletions
diff --git a/refs/files-backend.c b/refs/files-backend.c
index 9620dd86fb..8b20e40401 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -677,16 +677,18 @@ static void unlock_ref(struct ref_lock *lock)
* - Generate informative error messages in the case of failure
*/
static enum ref_transaction_error lock_raw_ref(struct files_ref_store *refs,
- const char *refname,
+ struct ref_update *update,
+ size_t update_idx,
int mustexist,
struct string_list *refnames_to_check,
const struct string_list *extras,
struct ref_lock **lock_p,
struct strbuf *referent,
- unsigned int *type,
struct strbuf *err)
{
enum ref_transaction_error ret = REF_TRANSACTION_ERROR_GENERIC;
+ const char *refname = update->refname;
+ unsigned int *type = &update->type;
struct ref_lock *lock;
struct strbuf ref_file = STRBUF_INIT;
int attempts_remaining = 3;
@@ -785,6 +787,8 @@ retry:
if (files_read_raw_ref(&refs->base, refname, &lock->old_oid, referent,
type, &failure_errno)) {
+ struct string_list_item *item;
+
if (failure_errno == ENOENT) {
if (mustexist) {
/* Garden variety missing reference. */
@@ -864,7 +868,9 @@ retry:
* make sure there is no existing packed ref that conflicts
* with refname. This check is deferred so that we can batch it.
*/
- string_list_append(refnames_to_check, refname);
+ item = string_list_append(refnames_to_check, refname);
+ item->util = xmalloc(sizeof(update_idx));
+ memcpy(item->util, &update_idx, sizeof(update_idx));
}
ret = 0;
@@ -2547,6 +2553,7 @@ struct files_transaction_backend_data {
*/
static enum ref_transaction_error lock_ref_for_update(struct files_ref_store *refs,
struct ref_update *update,
+ size_t update_idx,
struct ref_transaction *transaction,
const char *head_ref,
struct string_list *refnames_to_check,
@@ -2575,9 +2582,9 @@ static enum ref_transaction_error lock_ref_for_update(struct files_ref_store *re
if (lock) {
lock->count++;
} else {
- ret = lock_raw_ref(refs, update->refname, mustexist,
+ ret = lock_raw_ref(refs, update, update_idx, mustexist,
refnames_to_check, &transaction->refnames,
- &lock, &referent, &update->type, err);
+ &lock, &referent, err);
if (ret) {
char *reason;
@@ -2849,7 +2856,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,
for (i = 0; i < transaction->nr; i++) {
struct ref_update *update = transaction->updates[i];
- ret = lock_ref_for_update(refs, update, transaction,
+ ret = lock_ref_for_update(refs, update, i, transaction,
head_ref, &refnames_to_check,
err);
if (ret) {
@@ -2905,7 +2912,8 @@ static int files_transaction_prepare(struct ref_store *ref_store,
* So instead, we accept the race for now.
*/
if (refs_verify_refnames_available(refs->packed_ref_store, &refnames_to_check,
- &transaction->refnames, NULL, 0, err)) {
+ &transaction->refnames, NULL, transaction,
+ 0, err)) {
ret = REF_TRANSACTION_ERROR_NAME_CONFLICT;
goto cleanup;
}
@@ -2951,7 +2959,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,
cleanup:
free(head_ref);
- string_list_clear(&refnames_to_check, 0);
+ string_list_clear(&refnames_to_check, 1);
if (ret)
files_transaction_cleanup(refs, transaction);
@@ -3097,7 +3105,8 @@ static int files_transaction_finish_initial(struct files_ref_store *refs,
}
if (refs_verify_refnames_available(&refs->base, &refnames_to_check,
- &affected_refnames, NULL, 1, err)) {
+ &affected_refnames, NULL, transaction,
+ 1, err)) {
packed_refs_unlock(refs->packed_ref_store);
ret = REF_TRANSACTION_ERROR_NAME_CONFLICT;
goto cleanup;
diff --git a/refs/refs-internal.h b/refs/refs-internal.h
index 73a5379b73..f868870851 100644
--- a/refs/refs-internal.h
+++ b/refs/refs-internal.h
@@ -806,4 +806,20 @@ enum ref_transaction_error ref_update_check_old_target(const char *referent,
*/
int ref_update_expects_existing_old_ref(struct ref_update *update);
+/*
+ * Same as `refs_verify_refname_available()`, but checking for a list of
+ * refnames instead of only a single item. This is more efficient in the case
+ * where one needs to check multiple refnames.
+ *
+ * If using batched updates, then individual updates are marked rejected,
+ * reference backends are then in charge of not committing those updates.
+ */
+enum ref_transaction_error refs_verify_refnames_available(struct ref_store *refs,
+ const struct string_list *refnames,
+ const struct string_list *extras,
+ const struct string_list *skip,
+ struct ref_transaction *transaction,
+ unsigned int initial_transaction,
+ struct strbuf *err);
+
#endif /* REFS_REFS_INTERNAL_H */
diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c
index 8fb7d6cc71..a461d1b8e0 100644
--- a/refs/reftable-backend.c
+++ b/refs/reftable-backend.c
@@ -1074,6 +1074,7 @@ static enum ref_transaction_error prepare_single_update(struct reftable_ref_stor
struct ref_transaction *transaction,
struct reftable_backend *be,
struct ref_update *u,
+ size_t update_idx,
struct string_list *refnames_to_check,
unsigned int head_type,
struct strbuf *head_referent,
@@ -1149,6 +1150,7 @@ static enum ref_transaction_error prepare_single_update(struct reftable_ref_stor
if (ret < 0)
return REF_TRANSACTION_ERROR_GENERIC;
if (ret > 0 && !ref_update_expects_existing_old_ref(u)) {
+ struct string_list_item *item;
/*
* The reference does not exist, and we either have no
* old object ID or expect the reference to not exist.
@@ -1158,7 +1160,9 @@ static enum ref_transaction_error prepare_single_update(struct reftable_ref_stor
* can output a proper error message instead of failing
* at a later point.
*/
- string_list_append(refnames_to_check, u->refname);
+ item = string_list_append(refnames_to_check, u->refname);
+ item->util = xmalloc(sizeof(update_idx));
+ memcpy(item->util, &update_idx, sizeof(update_idx));
/*
* There is no need to write the reference deletion
@@ -1368,7 +1372,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
for (i = 0; i < transaction->nr; i++) {
ret = prepare_single_update(refs, tx_data, transaction, be,
- transaction->updates[i],
+ transaction->updates[i], i,
&refnames_to_check, head_type,
&head_referent, &referent, err);
if (ret) {
@@ -1384,6 +1388,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
ret = refs_verify_refnames_available(ref_store, &refnames_to_check,
&transaction->refnames, NULL,
+ transaction,
transaction->flags & REF_TRANSACTION_FLAG_INITIAL,
err);
if (ret < 0)
@@ -1402,7 +1407,7 @@ done:
}
strbuf_release(&referent);
strbuf_release(&head_referent);
- string_list_clear(&refnames_to_check, 0);
+ string_list_clear(&refnames_to_check, 1);
return ret;
}