Skip to content

Commit b3de383

Browse files
KarthikNayakgitster
authored andcommitted
refs: add function to translate errors to strings
The commit 76e760b (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 <[email protected]> Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1a8a497 commit b3de383

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

builtin/update-ref.c

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -575,30 +575,7 @@ static void print_rejected_refs(const char *refname,
575575
void *cb_data UNUSED)
576576
{
577577
struct strbuf sb = STRBUF_INIT;
578-
const char *reason = "";
579-
580-
switch (err) {
581-
case REF_TRANSACTION_ERROR_NAME_CONFLICT:
582-
reason = "refname conflict";
583-
break;
584-
case REF_TRANSACTION_ERROR_CREATE_EXISTS:
585-
reason = "reference already exists";
586-
break;
587-
case REF_TRANSACTION_ERROR_NONEXISTENT_REF:
588-
reason = "reference does not exist";
589-
break;
590-
case REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE:
591-
reason = "incorrect old value provided";
592-
break;
593-
case REF_TRANSACTION_ERROR_INVALID_NEW_VALUE:
594-
reason = "invalid new value provided";
595-
break;
596-
case REF_TRANSACTION_ERROR_EXPECTED_SYMREF:
597-
reason = "expected symref but found regular ref";
598-
break;
599-
default:
600-
reason = "unkown failure";
601-
}
578+
const char *reason = ref_transaction_error_msg(err);
602579

603580
strbuf_addf(&sb, "rejected %s %s %s %s\n", refname,
604581
new_oid ? oid_to_hex(new_oid) : new_target,

refs.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3314,3 +3314,23 @@ int ref_update_expects_existing_old_ref(struct ref_update *update)
33143314
return (update->flags & REF_HAVE_OLD) &&
33153315
(!is_null_oid(&update->old_oid) || update->old_target);
33163316
}
3317+
3318+
const char *ref_transaction_error_msg(enum ref_transaction_error err)
3319+
{
3320+
switch (err) {
3321+
case REF_TRANSACTION_ERROR_NAME_CONFLICT:
3322+
return "refname conflict";
3323+
case REF_TRANSACTION_ERROR_CREATE_EXISTS:
3324+
return "reference already exists";
3325+
case REF_TRANSACTION_ERROR_NONEXISTENT_REF:
3326+
return "reference does not exist";
3327+
case REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE:
3328+
return "incorrect old value provided";
3329+
case REF_TRANSACTION_ERROR_INVALID_NEW_VALUE:
3330+
return "invalid new value provided";
3331+
case REF_TRANSACTION_ERROR_EXPECTED_SYMREF:
3332+
return "expected symref but found regular ref";
3333+
default:
3334+
return "unknown failure";
3335+
}
3336+
}

refs.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,11 @@ void ref_transaction_for_each_rejected_update(struct ref_transaction *transactio
907907
ref_transaction_for_each_rejected_update_fn cb,
908908
void *cb_data);
909909

910+
/*
911+
* Translate errors to human readable error messages.
912+
*/
913+
const char *ref_transaction_error_msg(enum ref_transaction_error err);
914+
910915
/*
911916
* Free `*transaction` and all associated data.
912917
*/

0 commit comments

Comments
 (0)