fix: match commit SHA of different lengths

This commit is contained in:
Enix 2019-10-25 14:39:19 +08:00
parent a66b207131
commit b7985a9d63

View file

@ -43,14 +43,18 @@ function isInCodePage(metaData: MetaData = {}) {
const { type, branchName } = mergedRepo
return Boolean(
isInRepoPage() &&
(!type || type === TYPES.TREE || type === TYPES.BLOB) &&
type !== TYPES.COMMIT &&
(branchName || (!type && !branchName))
(!type || type === TYPES.TREE || type === TYPES.BLOB) &&
type !== TYPES.COMMIT &&
(branchName || (!type && !branchName)),
)
}
function isCommitPath(path: string[]) {
return /^[a-z0-9]{40}$/.test(path[0])
return isCompleteCommitSHA(path[0])
}
function isCompleteCommitSHA(sha?: string) {
return typeof sha === 'string' && /^[abcdef0-9]{40}$/i.test(sha)
}
function getCurrentPath(branchName = '') {
@ -61,13 +65,19 @@ function getCurrentPath(branchName = '') {
path.shift()
} else {
// path = branch/name/path/to/item or HEAD/path/to/item
// HEAD is not a valid branch name. Coming up with HEAD, means currently detached.
// HEAD is not a valid branch name. Getting HEAD means being detached.
if (path[0] === 'HEAD') path.shift()
else {
const slicedBranchName = branchName.split('/')
while (slicedBranchName.length) {
if (slicedBranchName[0] === path[0]) {
slicedBranchName.shift()
const splitBranchName = branchName.split('/')
while (splitBranchName.length) {
if (
splitBranchName[0] === path[0] ||
// Keep consuming as their heads are same
(splitBranchName.length === 1 && splitBranchName[0].startsWith(path[0]))
// This happens when visiting URLs like /blob/{commitSHA}/path/to/file
// and {commitSHA} is shorter than we got from DOM
) {
splitBranchName.shift()
path.shift()
} else {
raiseError(new Error(`branch name and path prefix not match`))