Skip to content

Commit 915dda7

Browse files
wraithgarnlf
authored andcommitted
1 parent e26548f commit 915dda7

File tree

23 files changed

+2760
-16
lines changed

23 files changed

+2760
-16
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright (c) 2015, Rebecca Turner
2+
3+
Permission to use, copy, modify, and/or distribute this software for any
4+
purpose with or without fee is hereby granted, provided that the above
5+
copyright notice and this permission notice appear in all copies.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
9+
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13+
PERFORMANCE OF THIS SOFTWARE.
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
'use strict'
2+
const gitHosts = require('./git-host-info.js')
3+
4+
class GitHost {
5+
constructor (type, user, auth, project, committish, defaultRepresentation, opts = {}) {
6+
Object.assign(this, gitHosts[type])
7+
this.type = type
8+
this.user = user
9+
this.auth = auth
10+
this.project = project
11+
this.committish = committish
12+
this.default = defaultRepresentation
13+
this.opts = opts
14+
}
15+
16+
hash () {
17+
return this.committish ? `#${this.committish}` : ''
18+
}
19+
20+
ssh (opts) {
21+
return this._fill(this.sshtemplate, opts)
22+
}
23+
24+
_fill (template, opts) {
25+
if (typeof template === 'function') {
26+
const options = { ...this, ...this.opts, ...opts }
27+
28+
// the path should always be set so we don't end up with 'undefined' in urls
29+
if (!options.path) {
30+
options.path = ''
31+
}
32+
33+
// template functions will insert the leading slash themselves
34+
if (options.path.startsWith('/')) {
35+
options.path = options.path.slice(1)
36+
}
37+
38+
if (options.noCommittish) {
39+
options.committish = null
40+
}
41+
42+
const result = template(options)
43+
return options.noGitPlus && result.startsWith('git+') ? result.slice(4) : result
44+
}
45+
46+
return null
47+
}
48+
49+
sshurl (opts) {
50+
return this._fill(this.sshurltemplate, opts)
51+
}
52+
53+
browse (path, fragment, opts) {
54+
// not a string, treat path as opts
55+
if (typeof path !== 'string') {
56+
return this._fill(this.browsetemplate, path)
57+
}
58+
59+
if (typeof fragment !== 'string') {
60+
opts = fragment
61+
fragment = null
62+
}
63+
return this._fill(this.browsefiletemplate, { ...opts, fragment, path })
64+
}
65+
66+
docs (opts) {
67+
return this._fill(this.docstemplate, opts)
68+
}
69+
70+
bugs (opts) {
71+
return this._fill(this.bugstemplate, opts)
72+
}
73+
74+
https (opts) {
75+
return this._fill(this.httpstemplate, opts)
76+
}
77+
78+
git (opts) {
79+
return this._fill(this.gittemplate, opts)
80+
}
81+
82+
shortcut (opts) {
83+
return this._fill(this.shortcuttemplate, opts)
84+
}
85+
86+
path (opts) {
87+
return this._fill(this.pathtemplate, opts)
88+
}
89+
90+
tarball (opts) {
91+
return this._fill(this.tarballtemplate, { ...opts, noCommittish: false })
92+
}
93+
94+
file (path, opts) {
95+
return this._fill(this.filetemplate, { ...opts, path })
96+
}
97+
98+
getDefaultRepresentation () {
99+
return this.default
100+
}
101+
102+
toString (opts) {
103+
if (this.default && typeof this[this.default] === 'function') {
104+
return this[this.default](opts)
105+
}
106+
107+
return this.sshurl(opts)
108+
}
109+
}
110+
module.exports = GitHost

0 commit comments

Comments
 (0)