|
| 1 | +/* eslint-disable max-len */ |
| 2 | +'use strict' |
| 3 | +const maybeJoin = (...args) => args.every(arg => arg) ? args.join('') : '' |
| 4 | +const maybeEncode = (arg) => arg ? encodeURIComponent(arg) : '' |
| 5 | + |
| 6 | +const defaults = { |
| 7 | + sshtemplate: ({ domain, user, project, committish }) => `git@${domain}:${user}/${project}.git${maybeJoin('#', committish)}`, |
| 8 | + sshurltemplate: ({ domain, user, project, committish }) => `git+ssh://git@${domain}/${user}/${project}.git${maybeJoin('#', committish)}`, |
| 9 | + browsetemplate: ({ domain, user, project, committish, treepath }) => `https://${domain}/${user}/${project}${maybeJoin('/', treepath, '/', maybeEncode(committish))}`, |
| 10 | + browsefiletemplate: ({ domain, user, project, committish, treepath, path, fragment, hashformat }) => `https://${domain}/${user}/${project}/${treepath}/${maybeEncode(committish || 'master')}/${path}${maybeJoin('#', hashformat(fragment || ''))}`, |
| 11 | + docstemplate: ({ domain, user, project, treepath, committish }) => `https://${domain}/${user}/${project}${maybeJoin('/', treepath, '/', maybeEncode(committish))}#readme`, |
| 12 | + httpstemplate: ({ auth, domain, user, project, committish }) => `git+https://${maybeJoin(auth, '@')}${domain}/${user}/${project}.git${maybeJoin('#', committish)}`, |
| 13 | + filetemplate: ({ domain, user, project, committish, path }) => `https://${domain}/${user}/${project}/raw/${maybeEncode(committish) || 'master'}/${path}`, |
| 14 | + shortcuttemplate: ({ type, user, project, committish }) => `${type}:${user}/${project}${maybeJoin('#', committish)}`, |
| 15 | + pathtemplate: ({ user, project, committish }) => `${user}/${project}${maybeJoin('#', committish)}`, |
| 16 | + bugstemplate: ({ domain, user, project }) => `https://${domain}/${user}/${project}/issues`, |
| 17 | + hashformat: formatHashFragment, |
| 18 | +} |
| 19 | + |
| 20 | +const gitHosts = {} |
| 21 | +gitHosts.github = Object.assign({}, defaults, { |
| 22 | + // First two are insecure and generally shouldn't be used any more, but |
| 23 | + // they are still supported. |
| 24 | + protocols: ['git:', 'http:', 'git+ssh:', 'git+https:', 'ssh:', 'https:'], |
| 25 | + domain: 'github.com', |
| 26 | + treepath: 'tree', |
| 27 | + filetemplate: ({ auth, user, project, committish, path }) => `https://${maybeJoin(auth, '@')}raw.githubusercontent.com/${user}/${project}/${maybeEncode(committish) || 'master'}/${path}`, |
| 28 | + gittemplate: ({ auth, domain, user, project, committish }) => `git://${maybeJoin(auth, '@')}${domain}/${user}/${project}.git${maybeJoin('#', committish)}`, |
| 29 | + tarballtemplate: ({ domain, user, project, committish }) => `https://codeload.${domain}/${user}/${project}/tar.gz/${maybeEncode(committish) || 'master'}`, |
| 30 | + extract: (url) => { |
| 31 | + let [, user, project, type, committish] = url.pathname.split('/', 5) |
| 32 | + if (type && type !== 'tree') { |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + if (!type) { |
| 37 | + committish = url.hash.slice(1) |
| 38 | + } |
| 39 | + |
| 40 | + if (project && project.endsWith('.git')) { |
| 41 | + project = project.slice(0, -4) |
| 42 | + } |
| 43 | + |
| 44 | + if (!user || !project) { |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + return { user, project, committish } |
| 49 | + }, |
| 50 | +}) |
| 51 | + |
| 52 | +gitHosts.bitbucket = Object.assign({}, defaults, { |
| 53 | + protocols: ['git+ssh:', 'git+https:', 'ssh:', 'https:'], |
| 54 | + domain: 'bitbucket.org', |
| 55 | + treepath: 'src', |
| 56 | + tarballtemplate: ({ domain, user, project, committish }) => `https://${domain}/${user}/${project}/get/${maybeEncode(committish) || 'master'}.tar.gz`, |
| 57 | + extract: (url) => { |
| 58 | + let [, user, project, aux] = url.pathname.split('/', 4) |
| 59 | + if (['get'].includes(aux)) { |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + if (project && project.endsWith('.git')) { |
| 64 | + project = project.slice(0, -4) |
| 65 | + } |
| 66 | + |
| 67 | + if (!user || !project) { |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + return { user, project, committish: url.hash.slice(1) } |
| 72 | + }, |
| 73 | +}) |
| 74 | + |
| 75 | +gitHosts.gitlab = Object.assign({}, defaults, { |
| 76 | + protocols: ['git+ssh:', 'git+https:', 'ssh:', 'https:'], |
| 77 | + domain: 'gitlab.com', |
| 78 | + treepath: 'tree', |
| 79 | + httpstemplate: ({ auth, domain, user, project, committish }) => `git+https://${maybeJoin(auth, '@')}${domain}/${user}/${project}.git${maybeJoin('#', committish)}`, |
| 80 | + tarballtemplate: ({ domain, user, project, committish }) => `https://${domain}/${user}/${project}/repository/archive.tar.gz?ref=${maybeEncode(committish) || 'master'}`, |
| 81 | + extract: (url) => { |
| 82 | + const path = url.pathname.slice(1) |
| 83 | + if (path.includes('/-/') || path.includes('/archive.tar.gz')) { |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + const segments = path.split('/') |
| 88 | + let project = segments.pop() |
| 89 | + if (project.endsWith('.git')) { |
| 90 | + project = project.slice(0, -4) |
| 91 | + } |
| 92 | + |
| 93 | + const user = segments.join('/') |
| 94 | + if (!user || !project) { |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + return { user, project, committish: url.hash.slice(1) } |
| 99 | + }, |
| 100 | +}) |
| 101 | + |
| 102 | +gitHosts.gist = Object.assign({}, defaults, { |
| 103 | + protocols: ['git:', 'git+ssh:', 'git+https:', 'ssh:', 'https:'], |
| 104 | + domain: 'gist.github.com', |
| 105 | + sshtemplate: ({ domain, project, committish }) => `git@${domain}:${project}.git${maybeJoin('#', committish)}`, |
| 106 | + sshurltemplate: ({ domain, project, committish }) => `git+ssh://git@${domain}/${project}.git${maybeJoin('#', committish)}`, |
| 107 | + browsetemplate: ({ domain, project, committish }) => `https://${domain}/${project}${maybeJoin('/', maybeEncode(committish))}`, |
| 108 | + browsefiletemplate: ({ domain, project, committish, path, hashformat }) => `https://${domain}/${project}${maybeJoin('/', maybeEncode(committish))}${maybeJoin('#', hashformat(path))}`, |
| 109 | + docstemplate: ({ domain, project, committish }) => `https://${domain}/${project}${maybeJoin('/', maybeEncode(committish))}`, |
| 110 | + httpstemplate: ({ domain, project, committish }) => `git+https://${domain}/${project}.git${maybeJoin('#', committish)}`, |
| 111 | + filetemplate: ({ user, project, committish, path }) => `https://gist.githubusercontent.com/${user}/${project}/raw${maybeJoin('/', maybeEncode(committish))}/${path}`, |
| 112 | + shortcuttemplate: ({ type, project, committish }) => `${type}:${project}${maybeJoin('#', committish)}`, |
| 113 | + pathtemplate: ({ project, committish }) => `${project}${maybeJoin('#', committish)}`, |
| 114 | + bugstemplate: ({ domain, project }) => `https://${domain}/${project}`, |
| 115 | + gittemplate: ({ domain, project, committish }) => `git://${domain}/${project}.git${maybeJoin('#', committish)}`, |
| 116 | + tarballtemplate: ({ project, committish }) => `https://codeload.github.com/gist/${project}/tar.gz/${maybeEncode(committish) || 'master'}`, |
| 117 | + extract: (url) => { |
| 118 | + let [, user, project, aux] = url.pathname.split('/', 4) |
| 119 | + if (aux === 'raw') { |
| 120 | + return |
| 121 | + } |
| 122 | + |
| 123 | + if (!project) { |
| 124 | + if (!user) { |
| 125 | + return |
| 126 | + } |
| 127 | + |
| 128 | + project = user |
| 129 | + user = null |
| 130 | + } |
| 131 | + |
| 132 | + if (project.endsWith('.git')) { |
| 133 | + project = project.slice(0, -4) |
| 134 | + } |
| 135 | + |
| 136 | + return { user, project, committish: url.hash.slice(1) } |
| 137 | + }, |
| 138 | + hashformat: function (fragment) { |
| 139 | + return fragment && 'file-' + formatHashFragment(fragment) |
| 140 | + }, |
| 141 | +}) |
| 142 | + |
| 143 | +gitHosts.sourcehut = Object.assign({}, defaults, { |
| 144 | + protocols: ['git+ssh:', 'https:'], |
| 145 | + domain: 'git.sr.ht', |
| 146 | + treepath: 'tree', |
| 147 | + browsefiletemplate: ({ domain, user, project, committish, treepath, path, fragment, hashformat }) => `https://${domain}/${user}/${project}/${treepath}/${maybeEncode(committish || 'main')}/${path}${maybeJoin('#', hashformat(fragment || ''))}`, |
| 148 | + filetemplate: ({ domain, user, project, committish, path }) => `https://${domain}/${user}/${project}/blob/${maybeEncode(committish) || 'main'}/${path}`, |
| 149 | + httpstemplate: ({ domain, user, project, committish }) => `https://${domain}/${user}/${project}.git${maybeJoin('#', committish)}`, |
| 150 | + tarballtemplate: ({ domain, user, project, committish }) => `https://${domain}/${user}/${project}/archive/${maybeEncode(committish) || 'main'}.tar.gz`, |
| 151 | + bugstemplate: ({ domain, user, project }) => `https://todo.sr.ht/${user}/${project}`, |
| 152 | + docstemplate: ({ domain, user, project, treepath, committish }) => `https://${domain}/${user}/${project}${maybeJoin('/', treepath, '/', maybeEncode(committish))}#readme`, |
| 153 | + extract: (url) => { |
| 154 | + let [, user, project, aux] = url.pathname.split('/', 4) |
| 155 | + |
| 156 | + // tarball url |
| 157 | + if (['archive'].includes(aux)) { |
| 158 | + return |
| 159 | + } |
| 160 | + |
| 161 | + if (project && project.endsWith('.git')) { |
| 162 | + project = project.slice(0, -4) |
| 163 | + } |
| 164 | + |
| 165 | + if (!user || !project) { |
| 166 | + return |
| 167 | + } |
| 168 | + |
| 169 | + return { user, project, committish: url.hash.slice(1) } |
| 170 | + }, |
| 171 | +}) |
| 172 | + |
| 173 | +const names = Object.keys(gitHosts) |
| 174 | +gitHosts.byShortcut = {} |
| 175 | +gitHosts.byDomain = {} |
| 176 | +for (const name of names) { |
| 177 | + gitHosts.byShortcut[`${name}:`] = name |
| 178 | + gitHosts.byDomain[gitHosts[name].domain] = name |
| 179 | +} |
| 180 | + |
| 181 | +function formatHashFragment (fragment) { |
| 182 | + return fragment.toLowerCase().replace(/^\W+|\/|\W+$/g, '').replace(/\W+/g, '-') |
| 183 | +} |
| 184 | + |
| 185 | +module.exports = gitHosts |
0 commit comments