diff --git a/git-host-info.js b/git-host-info.js index d491934..ba55248 100644 --- a/git-host-info.js +++ b/git-host-info.js @@ -139,6 +139,36 @@ gitHosts.gist = Object.assign({}, defaults, { } }) +gitHosts.sourcehut = Object.assign({}, defaults, { + protocols: ['git+ssh:', 'https:'], + domain: 'git.sr.ht', + treepath: 'tree', + browsefiletemplate: ({ domain, user, project, committish, treepath, path, fragment, hashformat }) => `https://${domain}/${user}/${project}/${treepath}/${maybeEncode(committish || 'main')}/${path}${maybeJoin('#', hashformat(fragment || ''))}`, + filetemplate: ({ domain, user, project, committish, path }) => `https://${domain}/${user}/${project}/blob/${maybeEncode(committish) || 'main'}/${path}`, + httpstemplate: ({ domain, user, project, committish }) => `https://${domain}/${user}/${project}.git${maybeJoin('#', committish)}`, + tarballtemplate: ({ domain, user, project, committish }) => `https://${domain}/${user}/${project}/archive/${maybeEncode(committish) || 'main'}.tar.gz`, + bugstemplate: ({ domain, user, project }) => `https://todo.sr.ht/${user}/${project}`, + docstemplate: ({ domain, user, project, treepath, committish }) => `https://${domain}/${user}/${project}${maybeJoin('/', treepath, '/', maybeEncode(committish))}#readme`, + extract: (url) => { + let [, user, project, aux] = url.pathname.split('/', 4) + + // tarball url + if (['archive'].includes(aux)) { + return + } + + if (project && project.endsWith('.git')) { + project = project.slice(0, -4) + } + + if (!user || !project) { + return + } + + return { user, project, committish: url.hash.slice(1) } + } +}) + const names = Object.keys(gitHosts) gitHosts.byShortcut = {} gitHosts.byDomain = {} diff --git a/test/sourcehut.js b/test/sourcehut.js new file mode 100644 index 0000000..17a9c52 --- /dev/null +++ b/test/sourcehut.js @@ -0,0 +1,122 @@ +'use strict' +const HostedGit = require('../index') +const t = require('tap') + +const invalid = [ + // missing project + 'https://git.sr.ht/~foo', + // invalid protocos + 'git://git@git.sr.ht:~foo/bar', + 'ssh://git.sr.ht:~foo/bar', + // tarball url + 'https://git.sr.ht/~foo/bar/archive/main.tar.gz' +] + +// assigning the constructor here is hacky, but the only way to make assertions that compare +// a subset of properties to a found object pass as you would expect +const GitHost = require('../git-host') +const defaults = { constructor: GitHost, type: 'sourcehut', user: '~foo', project: 'bar' } + +const valid = { + // shortucts + 'sourcehut:~foo/bar': { ...defaults, default: 'shortcut' }, + 'sourcehut:~foo/bar#branch': { ...defaults, default: 'shortcut', committish: 'branch' }, + + // shortcuts (.git) + 'sourcehut:~foo/bar.git': { ...defaults, default: 'shortcut' }, + 'sourcehut:~foo/bar.git#branch': { ...defaults, default: 'shortcut', committish: 'branch' }, + + // no-protocol git+ssh + 'git@git.sr.ht:~foo/bar': { ...defaults, default: 'sshurl', auth: null }, + 'git@git.sr.ht:~foo/bar#branch': { ...defaults, default: 'sshurl', auth: null, committish: 'branch' }, + + // no-protocol git+ssh (.git) + 'git@git.sr.ht:~foo/bar.git': { ...defaults, default: 'sshurl', auth: null }, + 'git@git.sr.ht:~foo/bar.git#branch': { ...defaults, default: 'sshurl', auth: null, committish: 'branch' }, + + // git+ssh urls + 'git+ssh://git@git.sr.ht:~foo/bar': { ...defaults, default: 'sshurl' }, + 'git+ssh://git@git.sr.ht:~foo/bar#branch': { ...defaults, default: 'sshurl', committish: 'branch' }, + + // git+ssh urls (.git) + 'git+ssh://git@git.sr.ht:~foo/bar.git': { ...defaults, default: 'sshurl' }, + 'git+ssh://git@git.sr.ht:~foo/bar.git#branch': { ...defaults, default: 'sshurl', committish: 'branch' }, + + // https urls + 'https://git.sr.ht/~foo/bar': { ...defaults, default: 'https' }, + 'https://git.sr.ht/~foo/bar#branch': { ...defaults, default: 'https', committish: 'branch' }, + + 'https://git.sr.ht/~foo/bar.git': { ...defaults, default: 'https' }, + 'https://git.sr.ht/~foo/bar.git#branch': { ...defaults, default: 'https', committish: 'branch' } +} + +t.test('valid urls parse properly', t => { + t.plan(Object.keys(valid).length) + for (const [url, result] of Object.entries(valid)) { + t.hasStrict(HostedGit.fromUrl(url), result, `${url} parses`) + } +}) + +t.test('invalid urls return undefined', t => { + t.plan(invalid.length) + for (const url of invalid) { + t.equal(HostedGit.fromUrl(url), undefined, `${url} returns undefined`) + } +}) + +t.test('toString respects defaults', t => { + const sshurl = HostedGit.fromUrl('git+ssh://git.sr.ht/~foo/bar') + t.equal(sshurl.default, 'sshurl', 'got the right default') + t.equal(sshurl.toString(), sshurl.sshurl(), 'toString calls sshurl') + + const https = HostedGit.fromUrl('https://git.sr.ht/~foo/bar') + t.equal(https.default, 'https', 'got the right default') + t.equal(https.toString(), https.https(), 'toString calls https') + + const shortcut = HostedGit.fromUrl('sourcehut:~foo/bar') + t.equal(shortcut.default, 'shortcut', 'got the right default') + t.equal(shortcut.toString(), shortcut.shortcut(), 'toString calls shortcut') + + t.end() +}) + +t.test('string methods populate correctly', t => { + const parsed = HostedGit.fromUrl('git+ssh://git.sr.ht/~foo/bar') + t.equal(parsed.getDefaultRepresentation(), parsed.default, 'getDefaultRepresentation()') + t.equal(parsed.hash(), '', 'hash() returns empty string when committish is unset') + t.equal(parsed.ssh(), 'git@git.sr.ht:~foo/bar.git') + t.equal(parsed.sshurl(), 'git+ssh://git@git.sr.ht/~foo/bar.git') + t.equal(parsed.browse(), 'https://git.sr.ht/~foo/bar') + t.equal(parsed.browse('/lib/index.js'), 'https://git.sr.ht/~foo/bar/tree/main/lib/index.js') + t.equal(parsed.browse('/lib/index.js', 'L100'), 'https://git.sr.ht/~foo/bar/tree/main/lib/index.js#l100') + t.equal(parsed.docs(), 'https://git.sr.ht/~foo/bar#readme') + t.equal(parsed.https(), 'https://git.sr.ht/~foo/bar.git') + t.equal(parsed.shortcut(), 'sourcehut:~foo/bar') + t.equal(parsed.path(), '~foo/bar') + t.equal(parsed.tarball(), 'https://git.sr.ht/~foo/bar/archive/main.tar.gz') + t.equal(parsed.file(), 'https://git.sr.ht/~foo/bar/blob/main/') + t.equal(parsed.file('/lib/index.js'), 'https://git.sr.ht/~foo/bar/blob/main/lib/index.js') + t.equal(parsed.bugs(), 'https://todo.sr.ht/~foo/bar') + + t.equal(parsed.docs({ committish: 'fix/bug' }), 'https://git.sr.ht/~foo/bar/tree/fix%2Fbug#readme', 'allows overriding options') + + t.same(parsed.git(), null, 'git() returns null') + + const extra = HostedGit.fromUrl('https://@git.sr.ht/~foo/bar#fix/bug') + t.equal(extra.hash(), '#fix/bug') + t.equal(extra.https(), 'https://git.sr.ht/~foo/bar.git#fix/bug') + t.equal(extra.shortcut(), 'sourcehut:~foo/bar#fix/bug') + t.equal(extra.ssh(), 'git@git.sr.ht:~foo/bar.git#fix/bug') + t.equal(extra.sshurl(), 'git+ssh://git@git.sr.ht/~foo/bar.git#fix/bug') + t.equal(extra.browse(), 'https://git.sr.ht/~foo/bar/tree/fix%2Fbug') + t.equal(extra.browse('/lib/index.js'), 'https://git.sr.ht/~foo/bar/tree/fix%2Fbug/lib/index.js') + t.equal(extra.browse('/lib/index.js', 'L200'), 'https://git.sr.ht/~foo/bar/tree/fix%2Fbug/lib/index.js#l200') + t.equal(extra.docs(), 'https://git.sr.ht/~foo/bar/tree/fix%2Fbug#readme') + t.equal(extra.file(), 'https://git.sr.ht/~foo/bar/blob/fix%2Fbug/') + t.equal(extra.file('/lib/index.js'), 'https://git.sr.ht/~foo/bar/blob/fix%2Fbug/lib/index.js') + + t.equal(extra.sshurl({ noCommittish: true }), 'git+ssh://git@git.sr.ht/~foo/bar.git', 'noCommittish drops committish from urls') + t.equal(extra.sshurl({ noGitPlus: true }), 'ssh://git@git.sr.ht/~foo/bar.git#fix/bug', 'noGitPlus drops git+ prefix from urls') + + t.end() +})