refs: introduce enum-based transaction error types
Replace preprocessor-defined transaction errors with a strongly-typed
enum `ref_transaction_error`. This change:
- Improves type safety and function signature clarity.
- Makes error handling more explicit and discoverable.
- Maintains existing error cases, while adding new error cases for
common scenarios.
This refactoring paves the way for more comprehensive error handling
which we will utilize in the upcoming commits to add batch reference
update support.
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>
This commit is contained in:
committed by
Junio C Hamano
parent
ca89c18d5c
commit
76e760b999
@@ -1069,20 +1069,20 @@ static int queue_transaction_update(struct reftable_ref_store *refs,
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int prepare_single_update(struct reftable_ref_store *refs,
|
||||
struct reftable_transaction_data *tx_data,
|
||||
struct ref_transaction *transaction,
|
||||
struct reftable_backend *be,
|
||||
struct ref_update *u,
|
||||
struct string_list *refnames_to_check,
|
||||
unsigned int head_type,
|
||||
struct strbuf *head_referent,
|
||||
struct strbuf *referent,
|
||||
struct strbuf *err)
|
||||
static enum ref_transaction_error prepare_single_update(struct reftable_ref_store *refs,
|
||||
struct reftable_transaction_data *tx_data,
|
||||
struct ref_transaction *transaction,
|
||||
struct reftable_backend *be,
|
||||
struct ref_update *u,
|
||||
struct string_list *refnames_to_check,
|
||||
unsigned int head_type,
|
||||
struct strbuf *head_referent,
|
||||
struct strbuf *referent,
|
||||
struct strbuf *err)
|
||||
{
|
||||
enum ref_transaction_error ret = 0;
|
||||
struct object_id current_oid = {0};
|
||||
const char *rewritten_ref;
|
||||
int ret = 0;
|
||||
|
||||
/*
|
||||
* There is no need to reload the respective backends here as
|
||||
@@ -1093,7 +1093,7 @@ static int prepare_single_update(struct reftable_ref_store *refs,
|
||||
*/
|
||||
ret = backend_for(&be, refs, u->refname, &rewritten_ref, 0);
|
||||
if (ret)
|
||||
return ret;
|
||||
return REF_TRANSACTION_ERROR_GENERIC;
|
||||
|
||||
/* Verify that the new object ID is valid. */
|
||||
if ((u->flags & REF_HAVE_NEW) && !is_null_oid(&u->new_oid) &&
|
||||
@@ -1104,13 +1104,13 @@ static int prepare_single_update(struct reftable_ref_store *refs,
|
||||
strbuf_addf(err,
|
||||
_("trying to write ref '%s' with nonexistent object %s"),
|
||||
u->refname, oid_to_hex(&u->new_oid));
|
||||
return -1;
|
||||
return REF_TRANSACTION_ERROR_INVALID_NEW_VALUE;
|
||||
}
|
||||
|
||||
if (o->type != OBJ_COMMIT && is_branch(u->refname)) {
|
||||
strbuf_addf(err, _("trying to write non-commit object %s to branch '%s'"),
|
||||
oid_to_hex(&u->new_oid), u->refname);
|
||||
return -1;
|
||||
return REF_TRANSACTION_ERROR_INVALID_NEW_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1134,7 +1134,7 @@ static int prepare_single_update(struct reftable_ref_store *refs,
|
||||
_("multiple updates for 'HEAD' (including one "
|
||||
"via its referent '%s') are not allowed"),
|
||||
u->refname);
|
||||
return TRANSACTION_NAME_CONFLICT;
|
||||
return REF_TRANSACTION_ERROR_NAME_CONFLICT;
|
||||
}
|
||||
|
||||
ref_transaction_add_update(
|
||||
@@ -1147,7 +1147,7 @@ static int prepare_single_update(struct reftable_ref_store *refs,
|
||||
ret = reftable_backend_read_ref(be, rewritten_ref,
|
||||
¤t_oid, referent, &u->type);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
return REF_TRANSACTION_ERROR_GENERIC;
|
||||
if (ret > 0 && !ref_update_expects_existing_old_ref(u)) {
|
||||
/*
|
||||
* The reference does not exist, and we either have no
|
||||
@@ -1168,7 +1168,7 @@ static int prepare_single_update(struct reftable_ref_store *refs,
|
||||
ret = queue_transaction_update(refs, tx_data, u,
|
||||
¤t_oid, err);
|
||||
if (ret)
|
||||
return ret;
|
||||
return REF_TRANSACTION_ERROR_GENERIC;
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -1180,7 +1180,7 @@ static int prepare_single_update(struct reftable_ref_store *refs,
|
||||
|
||||
"unable to resolve reference '%s'"),
|
||||
ref_update_original_update_refname(u), u->refname);
|
||||
return -1;
|
||||
return REF_TRANSACTION_ERROR_NONEXISTENT_REF;
|
||||
}
|
||||
|
||||
if (u->type & REF_ISSYMREF) {
|
||||
@@ -1196,7 +1196,7 @@ static int prepare_single_update(struct reftable_ref_store *refs,
|
||||
if (u->flags & REF_HAVE_OLD && !resolved) {
|
||||
strbuf_addf(err, _("cannot lock ref '%s': "
|
||||
"error reading reference"), u->refname);
|
||||
return -1;
|
||||
return REF_TRANSACTION_ERROR_GENERIC;
|
||||
}
|
||||
} else {
|
||||
struct ref_update *new_update;
|
||||
@@ -1211,7 +1211,7 @@ static int prepare_single_update(struct reftable_ref_store *refs,
|
||||
_("multiple updates for '%s' (including one "
|
||||
"via symref '%s') are not allowed"),
|
||||
referent->buf, u->refname);
|
||||
return TRANSACTION_NAME_CONFLICT;
|
||||
return REF_TRANSACTION_ERROR_NAME_CONFLICT;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1255,31 +1255,32 @@ static int prepare_single_update(struct reftable_ref_store *refs,
|
||||
"but is a regular ref"),
|
||||
ref_update_original_update_refname(u),
|
||||
u->old_target);
|
||||
return -1;
|
||||
return REF_TRANSACTION_ERROR_EXPECTED_SYMREF;
|
||||
}
|
||||
|
||||
if (ref_update_check_old_target(referent->buf, u, err)) {
|
||||
return -1;
|
||||
}
|
||||
ret = ref_update_check_old_target(referent->buf, u, err);
|
||||
if (ret)
|
||||
return ret;
|
||||
} else if ((u->flags & REF_HAVE_OLD) && !oideq(¤t_oid, &u->old_oid)) {
|
||||
if (is_null_oid(&u->old_oid)) {
|
||||
strbuf_addf(err, _("cannot lock ref '%s': "
|
||||
"reference already exists"),
|
||||
ref_update_original_update_refname(u));
|
||||
return TRANSACTION_CREATE_EXISTS;
|
||||
}
|
||||
else if (is_null_oid(¤t_oid))
|
||||
return REF_TRANSACTION_ERROR_CREATE_EXISTS;
|
||||
} else if (is_null_oid(¤t_oid)) {
|
||||
strbuf_addf(err, _("cannot lock ref '%s': "
|
||||
"reference is missing but expected %s"),
|
||||
ref_update_original_update_refname(u),
|
||||
oid_to_hex(&u->old_oid));
|
||||
else
|
||||
return REF_TRANSACTION_ERROR_NONEXISTENT_REF;
|
||||
} else {
|
||||
strbuf_addf(err, _("cannot lock ref '%s': "
|
||||
"is at %s but expected %s"),
|
||||
ref_update_original_update_refname(u),
|
||||
oid_to_hex(¤t_oid),
|
||||
oid_to_hex(&u->old_oid));
|
||||
return TRANSACTION_NAME_CONFLICT;
|
||||
return REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1296,8 +1297,8 @@ static int prepare_single_update(struct reftable_ref_store *refs,
|
||||
if ((u->type & REF_ISSYMREF) ||
|
||||
(u->flags & REF_LOG_ONLY) ||
|
||||
(u->flags & REF_HAVE_NEW && !oideq(¤t_oid, &u->new_oid)))
|
||||
return queue_transaction_update(refs, tx_data, u,
|
||||
¤t_oid, err);
|
||||
if (queue_transaction_update(refs, tx_data, u, ¤t_oid, err))
|
||||
return REF_TRANSACTION_ERROR_GENERIC;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1385,7 +1386,6 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
|
||||
transaction->state = REF_TRANSACTION_PREPARED;
|
||||
|
||||
done:
|
||||
assert(ret != REFTABLE_API_ERROR);
|
||||
if (ret < 0) {
|
||||
free_transaction_data(tx_data);
|
||||
transaction->state = REF_TRANSACTION_CLOSED;
|
||||
|
||||
Reference in New Issue
Block a user