From b2daa4c3b5f3b215fe2bc2f9a7479b4684d421bc Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Fri, 22 Feb 2019 12:22:58 +0800 Subject: [PATCH] feat: resolve git modules on or not on github --- src/components/Node.tsx | 2 +- src/content.less | 7 +++++++ src/driver/core/FileExplorer.ts | 32 +++++++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/components/Node.tsx b/src/components/Node.tsx index f6c8522..f023836 100644 --- a/src/components/Node.tsx +++ b/src/components/Node.tsx @@ -53,7 +53,7 @@ export default class Node extends React.PureComponent { ) } return ( -
+
= dispatch => () => dispatch.call(setStateText, 'Fetching File List...') +const githubSubModuleURLRegex = { + HTTP: /^https:\/\/github.com\/.*?\/.*?\.git$/, + git: /^git@github.com:(.*?)\/(.*?)\.git$/, +} + +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}` +} + +function transformModuleHTTPURL(node: TreeNode, URL: string) { + return URL.replace(/\.git/, `/tree/${node.sha}`) +} function resolveGitModules(root: TreeNode, blobData: BlobData) { if (blobData) { if (blobData.encoding === 'base64' && blobData.content && Array.isArray(root.contents)) { const content = atob(blobData.content) const parsed = ini.parse(content) - Object.values(parsed).map((value: { url: string; path: string }) => { + Object.values(parsed).forEach((value: { url: string; path: string }) => { const { url, path } = value const node = findNode(root, path.split('/')) if (node) { - node.url = url + if (githubSubModuleURLRegex.HTTP.test(url)) { + node.url = transformModuleHTTPURL(node, url) + } else if (githubSubModuleURLRegex.git.test(url)) { + node.url = transformModuleGitURL(node, url) + } else { + node.accessDenied = true + } + } else { + raiseError(Error(`Sub-module node ${path} not found`)) } }) } @@ -318,7 +342,9 @@ const onNodeClick: MethodCreator = dispatch = dispatch.call(focusNode, node, true) if (node.url) DOMHelper.loadWithPJAX(node.url) } else if (node.type === 'commit') { - window.open(node.url, '_blank') + if (node.url) { + window.open(node.url, '_blank') + } } }