normalizeReferenceName
Normalize reference name and check validity.
This will normalize the reference name by collapsing runs of adjacent slashes between name components into a single slash. It also validates the name according to the following rules:
- If
ReferenceFormat.AllowOnelevel
is given, the name may contain only capital letters and underscores, and must begin and end with a letter. (e.g. "HEAD", "ORIG_HEAD"). - The flag
ReferenceFormat.RefspecShorthand
has an effect only when combined withReferenceFormat.AllowOnelevel
. If it is given, "shorthand" branch names (i.e. those not prefixed byrefs/
, but consisting of a single word without/
separators) become valid. For example, "main" would be accepted. - If
ReferenceFormat.RefspecPattern
is given, the name may contain a single*
in place of a full pathname component (e.g.foo/*/bar
,foo/bar*
). - Names prefixed with "refs/" can be almost anything. You must avoid the characters '~', '^', ':', '\', '?', '[', and '*', and the sequences ".." and "@{" which have special meaning to revparse.
Signature
ts
function normalizeReferenceName(refname: string, format?: number): string | null;
Parameters
- refnamerequired · string
Reference name to normalize.
- formatnull | number
Reference format flags which used for normalize.
Returns
- null | string
If the reference passes validation, it is returned in normalized form,
otherwise annull
is returned.
Examples
ts
import { normalizeReferenceName, ReferenceFormat } from 'es-git';
console.assert(
normalizeReferenceName('foo//bar'),
'foo/bar'
);
console.assert(
normalizeReferenceName(
'HEAD',
ReferenceFormat.AllowOnelevel
),
'HEAD'
);
console.assert(
normalizeReferenceName(
'refs/heads/*',
ReferenceFormat.RefspecPattern
),
'refs/heads/*'
);
console.assert(
normalizeReferenceName(
'main',
ReferenceFormat.AllowOnelevel | ReferenceFormat.RefspecShorthand
),
'main'
);