refs: add function to translate errors to strings

The commit 76e760b999 (refs: introduce enum-based transaction error
types, 2025-04-08) introduced enum-based transaction error types. The
refs transaction logic was also modified to propagate these errors. For
clients of the ref transaction system, it would be beneficial to provide
human readable messages for these errors.

There is already an existing mapping in 'builtin/update-ref.c', move it
to 'refs.c' as `ref_transaction_error_msg()` and use the same within the
'builtin/update-ref.c'.

Helped-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Karthik Nayak
2025-05-19 11:58:06 +02:00
committed by Junio C Hamano
parent 1a8a4971cc
commit b3de3832ce
3 changed files with 26 additions and 24 deletions

20
refs.c
View File

@@ -3314,3 +3314,23 @@ int ref_update_expects_existing_old_ref(struct ref_update *update)
return (update->flags & REF_HAVE_OLD) &&
(!is_null_oid(&update->old_oid) || update->old_target);
}
const char *ref_transaction_error_msg(enum ref_transaction_error err)
{
switch (err) {
case REF_TRANSACTION_ERROR_NAME_CONFLICT:
return "refname conflict";
case REF_TRANSACTION_ERROR_CREATE_EXISTS:
return "reference already exists";
case REF_TRANSACTION_ERROR_NONEXISTENT_REF:
return "reference does not exist";
case REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE:
return "incorrect old value provided";
case REF_TRANSACTION_ERROR_INVALID_NEW_VALUE:
return "invalid new value provided";
case REF_TRANSACTION_ERROR_EXPECTED_SYMREF:
return "expected symref but found regular ref";
default:
return "unknown failure";
}
}