+61
index ca0a6b61b8..6edc79262a 100644
--- a/refs.c
+++ b/refs.c
tr->ref_store = refs;
tr->flags = flags;
string_list_init_dup(&tr->refnames);
+
+ if (flags & REF_TRANSACTION_ALLOW_FAILURE)
+ CALLOC_ARRAY(tr->rejections, 1);
+
return tr;
}
free((char *)transaction->updates[i]->old_target);
free(transaction->updates[i]);
}
+
+ if (transaction->rejections)
+ free(transaction->rejections->update_indices);
+ free(transaction->rejections);
+
string_list_clear(&transaction->refnames, 0);
free(transaction->updates);
free(transaction);
}
+int ref_transaction_maybe_set_rejected(struct ref_transaction *transaction,
+ size_t update_idx,
+ enum ref_transaction_error err)
+{
+ if (update_idx >= transaction->nr)
+ BUG("trying to set rejection on invalid update index");
+
+ if (!(transaction->flags & REF_TRANSACTION_ALLOW_FAILURE))
+ return 0;
+
+ if (!transaction->rejections)
+ BUG("transaction not inititalized with failure support");
+
+ /*
+ * Don't accept generic errors, since these errors are not user
+ * input related.
+ */
+ if (err == REF_TRANSACTION_ERROR_GENERIC)
+ return 0;
+
+ transaction->updates[update_idx]->rejection_err = err;
+ ALLOC_GROW(transaction->rejections->update_indices,
+ transaction->rejections->nr + 1,
+ transaction->rejections->alloc);
+ transaction->rejections->update_indices[transaction->rejections->nr++] = update_idx;
+
+ return 1;
+}
+
struct ref_update *ref_transaction_add_update(
struct ref_transaction *transaction,
const char *refname, unsigned int flags,
transaction->updates[transaction->nr++] = update;
update->flags = flags;
+ update->rejection_err = 0;
update->new_target = xstrdup_or_null(new_target);
update->old_target = xstrdup_or_null(old_target);
}
}
+void ref_transaction_for_each_rejected_update(struct ref_transaction *transaction,
+ ref_transaction_for_each_rejected_update_fn cb,
+ void *cb_data)
+{
+ if (!transaction->rejections)
+ return;
+
+ for (size_t i = 0; i < transaction->rejections->nr; i++) {
+ size_t update_index = transaction->rejections->update_indices[i];
+ struct ref_update *update = transaction->updates[update_index];
+
+ if (!update->rejection_err)
+ continue;
+
+ cb(update->refname,
+ (update->flags & REF_HAVE_OLD) ? &update->old_oid : NULL,
+ (update->flags & REF_HAVE_NEW) ? &update->new_oid : NULL,
+ update->old_target, update->new_target,
+ update->rejection_err, cb_data);
+ }
+}
+
int refs_delete_refs(struct ref_store *refs, const char *logmsg,
struct string_list *refnames, unsigned int flags)
{
+22
index d4af4ceeb2..43f2041edf 100644
--- a/refs.h
+++ b/refs.h
* either be absent or null_oid.
*/
REF_TRANSACTION_FLAG_INITIAL = (1 << 0),
+
+ /*
+ * The transaction mechanism by default fails all updates if any conflict
+ * is detected. This flag allows transactions to partially apply updates
+ * while rejecting updates which do not match the expected state.
+ */
+ REF_TRANSACTION_ALLOW_FAILURE = (1 << 1),
};
/*
ref_transaction_for_each_queued_update_fn cb,
void *cb_data);
+/*
+ * Execute the given callback function for each of the reference updates which
+ * have been rejected in the given transaction.
+ */
+typedef void ref_transaction_for_each_rejected_update_fn(const char *refname,
+ const struct object_id *old_oid,
+ const struct object_id *new_oid,
+ const char *old_target,
+ const char *new_target,
+ enum ref_transaction_error err,
+ void *cb_data);
+void ref_transaction_for_each_rejected_update(struct ref_transaction *transaction,
+ ref_transaction_for_each_rejected_update_fn cb,
+ void *cb_data);
+
/*
* Free `*transaction` and all associated data.
*/
+11
-1
index 770acdfa97..9620dd86fb 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
ret = lock_ref_for_update(refs, update, transaction,
head_ref, &refnames_to_check,
err);
- if (ret)
+ if (ret) {
+ if (ref_transaction_maybe_set_rejected(transaction, i, ret)) {
+ strbuf_reset(err);
+ ret = 0;
+
+ continue;
+ }
goto cleanup;
+ }
if (update->flags & REF_DELETING &&
!(update->flags & REF_LOG_ONLY) &&
struct ref_update *update = transaction->updates[i];
struct ref_lock *lock = update->backend_data;
+ if (update->rejection_err)
+ continue;
+
if (update->flags & REF_NEEDS_COMMIT ||
update->flags & REF_LOG_ONLY) {
if (parse_and_write_reflog(refs, update, lock, err)) {
+25
-2
index d90bd815a3..debca86a2b 100644
--- a/refs/packed-backend.c
+++ b/refs/packed-backend.c
* remain locked when it is done.
*/
static enum ref_transaction_error write_with_updates(struct packed_ref_store *refs,
- struct string_list *updates,
+ struct ref_transaction *transaction,
struct strbuf *err)
{
enum ref_transaction_error ret = REF_TRANSACTION_ERROR_GENERIC;
+ struct string_list *updates = &transaction->refnames;
struct ref_iterator *iter = NULL;
size_t i;
int ok;
"reference already exists",
update->refname);
ret = REF_TRANSACTION_ERROR_CREATE_EXISTS;
+
+ if (ref_transaction_maybe_set_rejected(transaction, i, ret)) {
+ strbuf_reset(err);
+ ret = 0;
+ continue;
+ }
+
goto error;
} else if (!oideq(&update->old_oid, iter->oid)) {
strbuf_addf(err, "cannot update ref '%s': "
oid_to_hex(iter->oid),
oid_to_hex(&update->old_oid));
ret = REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE;
+
+ if (ref_transaction_maybe_set_rejected(transaction, i, ret)) {
+ strbuf_reset(err);
+ ret = 0;
+ continue;
+ }
+
goto error;
}
}
update->refname,
oid_to_hex(&update->old_oid));
ret = REF_TRANSACTION_ERROR_NONEXISTENT_REF;
+
+ if (ref_transaction_maybe_set_rejected(transaction, i, ret)) {
+ strbuf_reset(err);
+ ret = 0;
+ continue;
+ }
+
goto error;
}
}
write_error:
strbuf_addf(err, "error writing to %s: %s",
get_tempfile_path(refs->tempfile), strerror(errno));
+ ret = REF_TRANSACTION_ERROR_GENERIC;
error:
ref_iterator_free(iter);
data->own_lock = 1;
}
- ret = write_with_updates(refs, &transaction->refnames, err);
+ ret = write_with_updates(refs, transaction, err);
if (ret)
goto failure;
+26
index 3f1d19abd9..73a5379b73 100644
--- a/refs/refs-internal.h
+++ b/refs/refs-internal.h
*/
uint64_t index;
+ /*
+ * Used in batched reference updates to mark if a given update
+ * was rejected.
+ */
+ enum ref_transaction_error rejection_err;
+
/*
* If this ref_update was split off of a symref update via
* split_symref_update(), then this member points at that
struct object_id *oid, struct strbuf *referent,
unsigned int *type, int *failure_errno);
+/*
+ * Mark a given update as rejected with a given reason.
+ */
+int ref_transaction_maybe_set_rejected(struct ref_transaction *transaction,
+ size_t update_idx,
+ enum ref_transaction_error err);
+
/*
* Add a ref_update with the specified properties to transaction, and
* return a pointer to the new object. This function does not verify
REF_TRANSACTION_CLOSED = 2
};
+/*
+ * Data structure to hold indices of updates which were rejected, for batched
+ * reference updates. While the updates themselves hold the rejection error,
+ * this structure allows a transaction to iterate only over the rejected
+ * updates.
+ */
+struct ref_transaction_rejections {
+ size_t *update_indices;
+ size_t alloc;
+ size_t nr;
+};
+
/*
* Data structure for holding a reference transaction, which can
* consist of checks and updates to multiple references, carried out
size_t alloc;
size_t nr;
enum ref_transaction_state state;
+ struct ref_transaction_rejections *rejections;
void *backend_data;
unsigned int flags;
uint64_t max_index;
+11
-1
index e318e6270e..8fb7d6cc71 100644
--- a/refs/reftable-backend.c
+++ b/refs/reftable-backend.c
transaction->updates[i],
&refnames_to_check, head_type,
&head_referent, &referent, err);
- if (ret)
+ if (ret) {
+ if (ref_transaction_maybe_set_rejected(transaction, i, ret)) {
+ strbuf_reset(err);
+ ret = 0;
+
+ continue;
+ }
goto done;
+ }
}
ret = refs_verify_refnames_available(ref_store, &refnames_to_check,
struct reftable_transaction_update *tx_update = &arg->updates[i];
struct ref_update *u = tx_update->update;
+ if (u->rejection_err)
+ continue;
+
/*
* Write a reflog entry when updating a ref to point to
* something new in either of the following cases: