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>
This commit is contained in:

committed by
Junio C Hamano

parent
611986f300
commit
add2c4f6e2
37
refs.c
37
refs.c
@ -1196,6 +1196,28 @@ struct ref_update *ref_transaction_add_update(
|
|||||||
return update;
|
return update;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int transaction_refname_valid(const char *refname,
|
||||||
|
const struct object_id *new_oid,
|
||||||
|
unsigned int flags, struct strbuf *err)
|
||||||
|
{
|
||||||
|
if (flags & REF_SKIP_REFNAME_VERIFICATION)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
if (is_pseudo_ref(refname)) {
|
||||||
|
strbuf_addf(err, _("refusing to update pseudoref '%s'"),
|
||||||
|
refname);
|
||||||
|
return 0;
|
||||||
|
} else if ((new_oid && !is_null_oid(new_oid)) ?
|
||||||
|
check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) :
|
||||||
|
!refname_is_safe(refname)) {
|
||||||
|
strbuf_addf(err, _("refusing to update ref with bad name '%s'"),
|
||||||
|
refname);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
int ref_transaction_update(struct ref_transaction *transaction,
|
int ref_transaction_update(struct ref_transaction *transaction,
|
||||||
const char *refname,
|
const char *refname,
|
||||||
const struct object_id *new_oid,
|
const struct object_id *new_oid,
|
||||||
@ -1213,21 +1235,8 @@ int ref_transaction_update(struct ref_transaction *transaction,
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(flags & REF_SKIP_REFNAME_VERIFICATION) &&
|
if (!transaction_refname_valid(refname, new_oid, flags, err))
|
||||||
((new_oid && !is_null_oid(new_oid)) ?
|
|
||||||
check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) :
|
|
||||||
!refname_is_safe(refname))) {
|
|
||||||
strbuf_addf(err, _("refusing to update ref with bad name '%s'"),
|
|
||||||
refname);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
|
|
||||||
if (!(flags & REF_SKIP_REFNAME_VERIFICATION) &&
|
|
||||||
is_pseudo_ref(refname)) {
|
|
||||||
strbuf_addf(err, _("refusing to update pseudoref '%s'"),
|
|
||||||
refname);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (flags & ~REF_TRANSACTION_UPDATE_ALLOWED_FLAGS)
|
if (flags & ~REF_TRANSACTION_UPDATE_ALLOWED_FLAGS)
|
||||||
BUG("illegal flags 0x%x passed to ref_transaction_update()", flags);
|
BUG("illegal flags 0x%x passed to ref_transaction_update()", flags);
|
||||||
|
Reference in New Issue
Block a user