summaryrefslogtreecommitdiff
path: root/source/github-helpers/github-url.ts
diff options
context:
space:
mode:
authorGravatar yakov116 <16872793+yakov116@users.noreply.github.com> 2020-06-03 09:41:38 -0400
committerGravatar GitHub <noreply@github.com> 2020-06-03 15:41:38 +0200
commitb393e2e2465a91ccbddf6ff60def616020712306 (patch)
treed528df90eb5da0558563c766712e81bfa800cd04 /source/github-helpers/github-url.ts
parente8eb6aca517b7e2d5fd2e81aaa64696cca0f08dd (diff)
downloadrefined-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.ts75
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;
- }
}