Skip to content

Commit 8595523

Browse files
authored
feat: Support Sourcehut (#55)
https://sourcehut.org/
1 parent 9a05ec2 commit 8595523

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

Diff for: git-host-info.js

+30
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,36 @@ gitHosts.gist = Object.assign({}, defaults, {
139139
}
140140
})
141141

142+
gitHosts.sourcehut = Object.assign({}, defaults, {
143+
protocols: ['git+ssh:', 'https:'],
144+
domain: 'git.sr.ht',
145+
treepath: 'tree',
146+
browsefiletemplate: ({ domain, user, project, committish, treepath, path, fragment, hashformat }) => `https://${domain}/${user}/${project}/${treepath}/${maybeEncode(committish || 'main')}/${path}${maybeJoin('#', hashformat(fragment || ''))}`,
147+
filetemplate: ({ domain, user, project, committish, path }) => `https://${domain}/${user}/${project}/blob/${maybeEncode(committish) || 'main'}/${path}`,
148+
httpstemplate: ({ domain, user, project, committish }) => `https://${domain}/${user}/${project}.git${maybeJoin('#', committish)}`,
149+
tarballtemplate: ({ domain, user, project, committish }) => `https://${domain}/${user}/${project}/archive/${maybeEncode(committish) || 'main'}.tar.gz`,
150+
bugstemplate: ({ domain, user, project }) => `https://todo.sr.ht/${user}/${project}`,
151+
docstemplate: ({ domain, user, project, treepath, committish }) => `https://${domain}/${user}/${project}${maybeJoin('/', treepath, '/', maybeEncode(committish))}#readme`,
152+
extract: (url) => {
153+
let [, user, project, aux] = url.pathname.split('/', 4)
154+
155+
// tarball url
156+
if (['archive'].includes(aux)) {
157+
return
158+
}
159+
160+
if (project && project.endsWith('.git')) {
161+
project = project.slice(0, -4)
162+
}
163+
164+
if (!user || !project) {
165+
return
166+
}
167+
168+
return { user, project, committish: url.hash.slice(1) }
169+
}
170+
})
171+
142172
const names = Object.keys(gitHosts)
143173
gitHosts.byShortcut = {}
144174
gitHosts.byDomain = {}

Diff for: test/sourcehut.js

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
'use strict'
2+
const HostedGit = require('../index')
3+
const t = require('tap')
4+
5+
const invalid = [
6+
// missing project
7+
'https://git.sr.ht/~foo',
8+
// invalid protocos
9+
'git://[email protected]:~foo/bar',
10+
'ssh://git.sr.ht:~foo/bar',
11+
// tarball url
12+
'https://git.sr.ht/~foo/bar/archive/main.tar.gz'
13+
]
14+
15+
// assigning the constructor here is hacky, but the only way to make assertions that compare
16+
// a subset of properties to a found object pass as you would expect
17+
const GitHost = require('../git-host')
18+
const defaults = { constructor: GitHost, type: 'sourcehut', user: '~foo', project: 'bar' }
19+
20+
const valid = {
21+
// shortucts
22+
'sourcehut:~foo/bar': { ...defaults, default: 'shortcut' },
23+
'sourcehut:~foo/bar#branch': { ...defaults, default: 'shortcut', committish: 'branch' },
24+
25+
// shortcuts (.git)
26+
'sourcehut:~foo/bar.git': { ...defaults, default: 'shortcut' },
27+
'sourcehut:~foo/bar.git#branch': { ...defaults, default: 'shortcut', committish: 'branch' },
28+
29+
// no-protocol git+ssh
30+
'[email protected]:~foo/bar': { ...defaults, default: 'sshurl', auth: null },
31+
'[email protected]:~foo/bar#branch': { ...defaults, default: 'sshurl', auth: null, committish: 'branch' },
32+
33+
// no-protocol git+ssh (.git)
34+
'[email protected]:~foo/bar.git': { ...defaults, default: 'sshurl', auth: null },
35+
'[email protected]:~foo/bar.git#branch': { ...defaults, default: 'sshurl', auth: null, committish: 'branch' },
36+
37+
// git+ssh urls
38+
'git+ssh://[email protected]:~foo/bar': { ...defaults, default: 'sshurl' },
39+
'git+ssh://[email protected]:~foo/bar#branch': { ...defaults, default: 'sshurl', committish: 'branch' },
40+
41+
// git+ssh urls (.git)
42+
'git+ssh://[email protected]:~foo/bar.git': { ...defaults, default: 'sshurl' },
43+
'git+ssh://[email protected]:~foo/bar.git#branch': { ...defaults, default: 'sshurl', committish: 'branch' },
44+
45+
// https urls
46+
'https://git.sr.ht/~foo/bar': { ...defaults, default: 'https' },
47+
'https://git.sr.ht/~foo/bar#branch': { ...defaults, default: 'https', committish: 'branch' },
48+
49+
'https://git.sr.ht/~foo/bar.git': { ...defaults, default: 'https' },
50+
'https://git.sr.ht/~foo/bar.git#branch': { ...defaults, default: 'https', committish: 'branch' }
51+
}
52+
53+
t.test('valid urls parse properly', t => {
54+
t.plan(Object.keys(valid).length)
55+
for (const [url, result] of Object.entries(valid)) {
56+
t.hasStrict(HostedGit.fromUrl(url), result, `${url} parses`)
57+
}
58+
})
59+
60+
t.test('invalid urls return undefined', t => {
61+
t.plan(invalid.length)
62+
for (const url of invalid) {
63+
t.equal(HostedGit.fromUrl(url), undefined, `${url} returns undefined`)
64+
}
65+
})
66+
67+
t.test('toString respects defaults', t => {
68+
const sshurl = HostedGit.fromUrl('git+ssh://git.sr.ht/~foo/bar')
69+
t.equal(sshurl.default, 'sshurl', 'got the right default')
70+
t.equal(sshurl.toString(), sshurl.sshurl(), 'toString calls sshurl')
71+
72+
const https = HostedGit.fromUrl('https://git.sr.ht/~foo/bar')
73+
t.equal(https.default, 'https', 'got the right default')
74+
t.equal(https.toString(), https.https(), 'toString calls https')
75+
76+
const shortcut = HostedGit.fromUrl('sourcehut:~foo/bar')
77+
t.equal(shortcut.default, 'shortcut', 'got the right default')
78+
t.equal(shortcut.toString(), shortcut.shortcut(), 'toString calls shortcut')
79+
80+
t.end()
81+
})
82+
83+
t.test('string methods populate correctly', t => {
84+
const parsed = HostedGit.fromUrl('git+ssh://git.sr.ht/~foo/bar')
85+
t.equal(parsed.getDefaultRepresentation(), parsed.default, 'getDefaultRepresentation()')
86+
t.equal(parsed.hash(), '', 'hash() returns empty string when committish is unset')
87+
t.equal(parsed.ssh(), '[email protected]:~foo/bar.git')
88+
t.equal(parsed.sshurl(), 'git+ssh://[email protected]/~foo/bar.git')
89+
t.equal(parsed.browse(), 'https://git.sr.ht/~foo/bar')
90+
t.equal(parsed.browse('/lib/index.js'), 'https://git.sr.ht/~foo/bar/tree/main/lib/index.js')
91+
t.equal(parsed.browse('/lib/index.js', 'L100'), 'https://git.sr.ht/~foo/bar/tree/main/lib/index.js#l100')
92+
t.equal(parsed.docs(), 'https://git.sr.ht/~foo/bar#readme')
93+
t.equal(parsed.https(), 'https://git.sr.ht/~foo/bar.git')
94+
t.equal(parsed.shortcut(), 'sourcehut:~foo/bar')
95+
t.equal(parsed.path(), '~foo/bar')
96+
t.equal(parsed.tarball(), 'https://git.sr.ht/~foo/bar/archive/main.tar.gz')
97+
t.equal(parsed.file(), 'https://git.sr.ht/~foo/bar/blob/main/')
98+
t.equal(parsed.file('/lib/index.js'), 'https://git.sr.ht/~foo/bar/blob/main/lib/index.js')
99+
t.equal(parsed.bugs(), 'https://todo.sr.ht/~foo/bar')
100+
101+
t.equal(parsed.docs({ committish: 'fix/bug' }), 'https://git.sr.ht/~foo/bar/tree/fix%2Fbug#readme', 'allows overriding options')
102+
103+
t.same(parsed.git(), null, 'git() returns null')
104+
105+
const extra = HostedGit.fromUrl('https://@git.sr.ht/~foo/bar#fix/bug')
106+
t.equal(extra.hash(), '#fix/bug')
107+
t.equal(extra.https(), 'https://git.sr.ht/~foo/bar.git#fix/bug')
108+
t.equal(extra.shortcut(), 'sourcehut:~foo/bar#fix/bug')
109+
t.equal(extra.ssh(), '[email protected]:~foo/bar.git#fix/bug')
110+
t.equal(extra.sshurl(), 'git+ssh://[email protected]/~foo/bar.git#fix/bug')
111+
t.equal(extra.browse(), 'https://git.sr.ht/~foo/bar/tree/fix%2Fbug')
112+
t.equal(extra.browse('/lib/index.js'), 'https://git.sr.ht/~foo/bar/tree/fix%2Fbug/lib/index.js')
113+
t.equal(extra.browse('/lib/index.js', 'L200'), 'https://git.sr.ht/~foo/bar/tree/fix%2Fbug/lib/index.js#l200')
114+
t.equal(extra.docs(), 'https://git.sr.ht/~foo/bar/tree/fix%2Fbug#readme')
115+
t.equal(extra.file(), 'https://git.sr.ht/~foo/bar/blob/fix%2Fbug/')
116+
t.equal(extra.file('/lib/index.js'), 'https://git.sr.ht/~foo/bar/blob/fix%2Fbug/lib/index.js')
117+
118+
t.equal(extra.sshurl({ noCommittish: true }), 'git+ssh://[email protected]/~foo/bar.git', 'noCommittish drops committish from urls')
119+
t.equal(extra.sshurl({ noGitPlus: true }), 'ssh://[email protected]/~foo/bar.git#fix/bug', 'noGitPlus drops git+ prefix from urls')
120+
121+
t.end()
122+
})

0 commit comments

Comments
 (0)