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