Skip to content

Commit add2c4f

Browse files
KarthikNayakgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 611986f commit add2c4f

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

refs.c

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,28 @@ struct ref_update *ref_transaction_add_update(
11961196
return update;
11971197
}
11981198

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+
11991221
int ref_transaction_update(struct ref_transaction *transaction,
12001222
const char *refname,
12011223
const struct object_id *new_oid,
@@ -1213,21 +1235,8 @@ int ref_transaction_update(struct ref_transaction *transaction,
12131235
return -1;
12141236
}
12151237

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))
12221239
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-
}
12311240

12321241
if (flags & ~REF_TRANSACTION_UPDATE_ALLOWED_FLAGS)
12331242
BUG("illegal flags 0x%x passed to ref_transaction_update()", flags);

0 commit comments

Comments
 (0)