diff options
author | 2020-06-03 09:41:38 -0400 | |
---|---|---|
committer | 2020-06-03 15:41:38 +0200 | |
commit | b393e2e2465a91ccbddf6ff60def616020712306 (patch) | |
tree | d528df90eb5da0558563c766712e81bfa800cd04 /source/github-helpers/github-url.ts | |
parent | e8eb6aca517b7e2d5fd2e81aaa64696cca0f08dd (diff) | |
download | refined-github-b393e2e2465a91ccbddf6ff60def616020712306.tar.gz refined-github-b393e2e2465a91ccbddf6ff60def616020712306.tar.zst refined-github-b393e2e2465a91ccbddf6ff60def616020712306.zip |
Fix URL parsing in history views (#3153)
Co-Authored-By: Fregante <opensource@bfred.it>
Diffstat (limited to 'source/github-helpers/github-url.ts')
-rw-r--r-- | source/github-helpers/github-url.ts | 75 |
1 files changed, 39 insertions, 36 deletions
diff --git a/source/github-helpers/github-url.ts b/source/github-helpers/github-url.ts index 395723d4..6be68ff7 100644 --- a/source/github-helpers/github-url.ts +++ b/source/github-helpers/github-url.ts @@ -1,33 +1,5 @@ import {getCurrentBranch} from '.'; -function disambiguateReference(ambiguousReference: string[]): {branch: string; filePath: string} { - const branch = ambiguousReference[0]; - const filePath = ambiguousReference.slice(1).join('/'); - - const currentBranch = getCurrentBranch(); - const currentBranchSections = currentBranch.split('/'); - if ( - ambiguousReference.length === 1 || // Ref has no slashes - currentBranchSections.length === 1 || // Current branch has no slashes - /\^|~|@{/.test(branch) // Ref is an extended revision #3137 https://git-scm.com/docs/git-rev-parse#_specifying_revisions - ) { - // Then the reference is not ambiguous - return {branch, filePath}; - } - - for (const [i, section] of currentBranchSections.entries()) { - if (ambiguousReference[i] !== section) { - console.warn(`The supplied path (${ambiguousReference.join('/')}) is ambiguous (current reference is \`${currentBranch}\`)`); - return {branch, filePath}; - } - } - - return { - branch: currentBranch, - filePath: ambiguousReference.slice(currentBranchSections.length).join('/') - }; -} - export default class GitHubURL extends URL { // @ts-ignore https://github.com/microsoft/TypeScript/issues/26792 user: string; @@ -39,14 +11,49 @@ export default class GitHubURL extends URL { branch: string; // @ts-ignore filePath: string; + + assign = Object.assign.bind(null, this); + constructor(url: string) { super(url); this.pathname = super.pathname; } - assign(replacements: Partial<GitHubURL>): this { - Object.assign(this, replacements); - return this; + toString() { + return this.href; + } + + private disambiguateReference(ambiguousReference: string[]): {branch: string; filePath: string} { + const branch = ambiguousReference[0]; + const filePathFromSearch = this.searchParams.getAll('path[]').join('/'); + if (filePathFromSearch) { + this.searchParams.delete('path[]'); + return {branch, filePath: filePathFromSearch}; + } + + const filePath = ambiguousReference.slice(1).join('/'); + + const currentBranch = getCurrentBranch(); + const currentBranchSections = currentBranch.split('/'); + if ( + ambiguousReference.length === 1 || // Ref has no slashes + currentBranchSections.length === 1 // Current branch has no slashes + ) { + // Then the reference is not ambiguous + return {branch, filePath}; + } + + for (const [i, section] of currentBranchSections.entries()) { + if (ambiguousReference[i] !== section) { + console.warn(`The supplied path (${ambiguousReference.join('/')}) is ambiguous (current reference is \`${currentBranch}\`)`); + return {branch, filePath}; + } + } + + return { + branch: currentBranch, + filePath: ambiguousReference.slice(currentBranchSections.length).join('/') + }; } get pathname() { @@ -55,7 +62,7 @@ export default class GitHubURL extends URL { set pathname(pathname) { const [user, repository, route, ...ambiguousReference] = pathname.replace(/^\/|\/$/g, '').split('/'); - const {branch, filePath} = disambiguateReference(ambiguousReference); + const {branch, filePath} = this.disambiguateReference(ambiguousReference); this.assign({user, repository, route, branch, filePath}); } @@ -64,8 +71,4 @@ export default class GitHubURL extends URL { super.pathname = this.pathname; return super.href; } - - toString() { - return this.href; - } } |