From 405f4742f768be260fbe95d4449ed73dc50f9a30 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Mon, 11 Mar 2019 14:20:12 +0800 Subject: [PATCH] feat: resolve http git submodule link --- src/driver/core/FileExplorer.ts | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/driver/core/FileExplorer.ts b/src/driver/core/FileExplorer.ts index 31d66bb..8ec537d 100644 --- a/src/driver/core/FileExplorer.ts +++ b/src/driver/core/FileExplorer.ts @@ -53,7 +53,8 @@ const init: MethodCreator = dispatch => () => dispatch.call(setStateText, 'Fetching File List...') const githubSubModuleURLRegex = { - HTTP: /^https:\/\/github.com\/.*?\/.*?\.git$/, + HTTP: /^https?:\/\/.*?$/, + HTTPGit: /^https:\/\/github.com\/.*?\/.*?\.git$/, git: /^git@github.com:(.*?)\/(.*?)\.git$/, } @@ -61,11 +62,23 @@ function transformModuleGitURL(node: TreeNode, URL: string) { const matched = URL.match(githubSubModuleURLRegex.git) if (!matched) return const [_, userName, repoName] = matched - return `https://github.com/${userName}/${repoName}/tree/${node.sha}` + return appendCommitPath(`https://github.com/${userName}/${repoName}`, node) +} + +function cutDotGit(URL: string) { + return URL.replace(/\.git$/, '') +} + +function appendCommitPath(URL: string, node: TreeNode) { + return URL.replace(/\/?$/, `/tree/${node.sha}`) +} + +function transformModuleHTTPDotGitURL(node: TreeNode, URL: string) { + return appendCommitPath(cutDotGit(URL), node) } function transformModuleHTTPURL(node: TreeNode, URL: string) { - return URL.replace(/\.git/, `/tree/${node.sha}`) + return appendCommitPath(URL, node) } type Parsed = { @@ -93,10 +106,12 @@ function handleParsed(root: TreeNode, parsed: Parsed) { if (typeof url === 'string' && typeof path === 'string') { const node = findNode(root, path.split('/')) if (node) { - if (githubSubModuleURLRegex.HTTP.test(url)) { - node.url = transformModuleHTTPURL(node, url) + if (githubSubModuleURLRegex.HTTPGit.test(url)) { + node.url = transformModuleHTTPDotGitURL(node, url) } else if (githubSubModuleURLRegex.git.test(url)) { node.url = transformModuleGitURL(node, url) + } else if (githubSubModuleURLRegex.HTTP.test(url)) { + node.url = transformModuleHTTPURL(node, url) } else { node.accessDenied = true }