refs: extract out refname verification in transactions

Unless the `REF_SKIP_REFNAME_VERIFICATION` flag is set for an update, the refname of the update is verified for: - Ensuring it is not a pseudoref. - Checking the refname format. These checks will also be needed in a following commit where the function to add reflog updates to the transaction is introduced. Extract the code out into a new static function. Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Dec 16, 2024 at 17:44 UTC add2c4f6e225083747e86c2e2e89e80c97b28733
1 file changed +23 -14
refs.c
+23 -14
@@ -1196,6 +1196,28 @@ struct ref_update *ref_transaction_add_update(
1196 return update;
1197 }
1198
1199 +static int transaction_refname_valid(const char *refname,
1200 + const struct object_id *new_oid,
1201 + unsigned int flags, struct strbuf *err)
1202 +{
1203 + if (flags & REF_SKIP_REFNAME_VERIFICATION)
1204 + return 1;
1205 +
1206 + if (is_pseudo_ref(refname)) {
1207 + strbuf_addf(err, _("refusing to update pseudoref '%s'"),
1208 + refname);
1209 + return 0;
1210 + } else if ((new_oid && !is_null_oid(new_oid)) ?
1211 + check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) :
1212 + !refname_is_safe(refname)) {
1213 + strbuf_addf(err, _("refusing to update ref with bad name '%s'"),
1214 + refname);
1215 + return 0;
1216 + }
1217 +
1218 + return 1;
1219 +}
1220 +
1221 int ref_transaction_update(struct ref_transaction *transaction,
1222 const char *refname,
1223 const struct object_id *new_oid,
@@ -1213,21 +1235,8 @@ int ref_transaction_update(struct ref_transaction *transaction,
1235 return -1;
1236 }
1237
1216 - if (!(flags & REF_SKIP_REFNAME_VERIFICATION) &&
1217 - ((new_oid && !is_null_oid(new_oid)) ?
1218 - check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) :
1219 - !refname_is_safe(refname))) {
1220 - strbuf_addf(err, _("refusing to update ref with bad name '%s'"),
1221 - refname);
1238 + if (!transaction_refname_valid(refname, new_oid, flags, err))
1239 return -1;
1223 - }
1224 -
1225 - if (!(flags & REF_SKIP_REFNAME_VERIFICATION) &&
1226 - is_pseudo_ref(refname)) {
1227 - strbuf_addf(err, _("refusing to update pseudoref '%s'"),
1228 - refname);
1229 - return -1;
1230 - }
1240
1241 if (flags & ~REF_TRANSACTION_UPDATE_ALLOWED_FLAGS)
1242 BUG("illegal flags 0x%x passed to ref_transaction_update()", flags);