Re: [PATCH v2 1/4] refs: add function to translate errors to strings
- From
Karthik Nayak <karthik.188@gmail.com>
- Date
- May 16, 2025, 09:12 UTC
- Message-ID
- <CAOLa=ZTwchMMg81JnHagQ10rpRw6pR5+JQUWLUcaxusGFBL8PA@mail.gmail.com>
- In-Reply-To
- <xmqqbjrtzkm1.fsf@gitster.g>
Junio C Hamano <gitster@pobox.com> writes:
[snip]
Show 31 quoted lines
>> +char *ref_transaction_error_msg(enum ref_transaction_error err)
>> +{
>> + const char *reason = "";
>> +
>> + switch (err) {
>> + case REF_TRANSACTION_ERROR_NAME_CONFLICT:
>> + reason = "refname conflict";
>> + break;
>> + case REF_TRANSACTION_ERROR_CREATE_EXISTS:
>> + reason = "reference already exists";
>> + break;
>> + case REF_TRANSACTION_ERROR_NONEXISTENT_REF:
>> + reason = "reference does not exist";
>> + break;
>> + case REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE:
>> + reason = "incorrect old value provided";
>> + break;
>> + case REF_TRANSACTION_ERROR_INVALID_NEW_VALUE:
>> + reason = "invalid new value provided";
>> + break;
>> + case REF_TRANSACTION_ERROR_EXPECTED_SYMREF:
>> + reason = "expected symref but found regular ref";
>> + break;
>> + default:
>> + reason = "unkown failure";
>> + }
>> +
>> + return xstrdup(reason);
>> +}
>
> Why can't this return "const char *", without xstrdup()?Yeah I think that is much better, string literals are anyways not on the stack and can be passed around. Will change, this would make it much simpler.