From b7985a9d63f056badf7a147d1b1051224cb947ec Mon Sep 17 00:00:00 2001 From: Enix Date: Fri, 25 Oct 2019 14:39:19 +0800 Subject: [PATCH] fix: match commit SHA of different lengths --- src/utils/URLHelper.ts | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/utils/URLHelper.ts b/src/utils/URLHelper.ts index 884e9cb..2e2cac6 100644 --- a/src/utils/URLHelper.ts +++ b/src/utils/URLHelper.ts @@ -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`))