diff --git a/lib/git-host-info.js b/lib/git-host-info.js index 9a9720f..cdc1e46 100644 --- a/lib/git-host-info.js +++ b/lib/git-host-info.js @@ -6,6 +6,7 @@ const maybeEncode = (arg) => arg ? encodeURIComponent(arg) : '' const defaults = { sshtemplate: ({ domain, user, project, committish }) => `git@${domain}:${user}/${project}.git${maybeJoin('#', committish)}`, sshurltemplate: ({ domain, user, project, committish }) => `git+ssh://git@${domain}/${user}/${project}.git${maybeJoin('#', committish)}`, + edittemplate: ({ domain, user, project, committish, editpath, path }) => `https://${domain}/${user}/${project}${maybeJoin('/', editpath, '/', maybeEncode(committish || 'master'), '/', path)}`, browsetemplate: ({ domain, user, project, committish, treepath }) => `https://${domain}/${user}/${project}${maybeJoin('/', treepath, '/', maybeEncode(committish))}`, browsefiletemplate: ({ domain, user, project, committish, treepath, path, fragment, hashformat }) => `https://${domain}/${user}/${project}/${treepath}/${maybeEncode(committish || 'master')}/${path}${maybeJoin('#', hashformat(fragment || ''))}`, docstemplate: ({ domain, user, project, treepath, committish }) => `https://${domain}/${user}/${project}${maybeJoin('/', treepath, '/', maybeEncode(committish))}#readme`, @@ -24,6 +25,7 @@ gitHosts.github = Object.assign({}, defaults, { protocols: ['git:', 'http:', 'git+ssh:', 'git+https:', 'ssh:', 'https:'], domain: 'github.com', treepath: 'tree', + editpath: 'edit', filetemplate: ({ auth, user, project, committish, path }) => `https://${maybeJoin(auth, '@')}raw.githubusercontent.com/${user}/${project}/${maybeEncode(committish) || 'master'}/${path}`, gittemplate: ({ auth, domain, user, project, committish }) => `git://${maybeJoin(auth, '@')}${domain}/${user}/${project}.git${maybeJoin('#', committish)}`, tarballtemplate: ({ domain, user, project, committish }) => `https://codeload.${domain}/${user}/${project}/tar.gz/${maybeEncode(committish) || 'master'}`, @@ -53,6 +55,8 @@ gitHosts.bitbucket = Object.assign({}, defaults, { protocols: ['git+ssh:', 'git+https:', 'ssh:', 'https:'], domain: 'bitbucket.org', treepath: 'src', + editpath: '?mode=edit', + edittemplate: ({ domain, user, project, committish, treepath, path, editpath }) => `https://${domain}/${user}/${project}${maybeJoin('/', treepath, '/', maybeEncode(committish || 'master'), '/', path, editpath)}`, tarballtemplate: ({ domain, user, project, committish }) => `https://${domain}/${user}/${project}/get/${maybeEncode(committish) || 'master'}.tar.gz`, extract: (url) => { let [, user, project, aux] = url.pathname.split('/', 4) @@ -76,6 +80,7 @@ gitHosts.gitlab = Object.assign({}, defaults, { protocols: ['git+ssh:', 'git+https:', 'ssh:', 'https:'], domain: 'gitlab.com', treepath: 'tree', + editpath: '-/edit', httpstemplate: ({ auth, domain, user, project, committish }) => `git+https://${maybeJoin(auth, '@')}${domain}/${user}/${project}.git${maybeJoin('#', committish)}`, tarballtemplate: ({ domain, user, project, committish }) => `https://${domain}/${user}/${project}/repository/archive.tar.gz?ref=${maybeEncode(committish) || 'master'}`, extract: (url) => { @@ -102,8 +107,10 @@ gitHosts.gitlab = Object.assign({}, defaults, { gitHosts.gist = Object.assign({}, defaults, { protocols: ['git:', 'git+ssh:', 'git+https:', 'ssh:', 'https:'], domain: 'gist.github.com', + editpath: 'edit', sshtemplate: ({ domain, project, committish }) => `git@${domain}:${project}.git${maybeJoin('#', committish)}`, sshurltemplate: ({ domain, project, committish }) => `git+ssh://git@${domain}/${project}.git${maybeJoin('#', committish)}`, + edittemplate: ({ domain, user, project, committish, editpath }) => `https://${domain}/${user}/${project}${maybeJoin('/', maybeEncode(committish))}/${editpath}`, browsetemplate: ({ domain, project, committish }) => `https://${domain}/${project}${maybeJoin('/', maybeEncode(committish))}`, browsefiletemplate: ({ domain, project, committish, path, hashformat }) => `https://${domain}/${project}${maybeJoin('/', maybeEncode(committish))}${maybeJoin('#', hashformat(path))}`, docstemplate: ({ domain, project, committish }) => `https://${domain}/${project}${maybeJoin('/', maybeEncode(committish))}`, diff --git a/lib/git-host.js b/lib/git-host.js index 8a975e9..bb65d4d 100644 --- a/lib/git-host.js +++ b/lib/git-host.js @@ -95,6 +95,10 @@ class GitHost { return this._fill(this.filetemplate, { ...opts, path }) } + edit (path, opts) { + return this._fill(this.edittemplate, { ...opts, path }) + } + getDefaultRepresentation () { return this.default } diff --git a/test/bitbucket.js b/test/bitbucket.js index 4668e59..88795f4 100644 --- a/test/bitbucket.js +++ b/test/bitbucket.js @@ -181,6 +181,8 @@ t.test('string methods populate correctly', t => { t.equal(parsed.hash(), '', 'hash() returns empty string when committish is unset') t.equal(parsed.ssh(), 'git@bitbucket.org:foo/bar.git') t.equal(parsed.sshurl(), 'git+ssh://git@bitbucket.org/foo/bar.git') + t.equal(parsed.edit(), 'https://bitbucket.org/foo/bar') + t.equal(parsed.edit('/lib/index.js'), 'https://bitbucket.org/foo/bar/src/master/lib/index.js?mode=edit') t.equal(parsed.browse(), 'https://bitbucket.org/foo/bar') t.equal(parsed.browse('/lib/index.js'), 'https://bitbucket.org/foo/bar/src/master/lib/index.js') t.equal(parsed.browse('/lib/index.js', 'L100'), 'https://bitbucket.org/foo/bar/src/master/lib/index.js#l100') diff --git a/test/gist.js b/test/gist.js index 9313550..09c8392 100644 --- a/test/gist.js +++ b/test/gist.js @@ -345,6 +345,8 @@ t.test('string methods populate correctly', t => { t.equal(parsed.hash(), '', 'hash() returns empty string when committish is unset') t.equal(parsed.ssh(), 'git@gist.github.com:feedbeef.git') t.equal(parsed.sshurl(), 'git+ssh://git@gist.github.com/feedbeef.git') + t.equal(parsed.edit(), 'https://gist.github.com/foo/feedbeef/edit', 'gist link only redirects with a user') + t.equal(parsed.edit('/lib/index.js'), 'https://gist.github.com/foo/feedbeef/edit', 'gist link only redirects with a user') t.equal(parsed.browse(), 'https://gist.github.com/feedbeef') t.equal(parsed.browse('/lib/index.js'), 'https://gist.github.com/feedbeef#file-libindex-js') t.equal(parsed.browse('/lib/index.js', 'L100'), 'https://gist.github.com/feedbeef#file-libindex-js') diff --git a/test/github.js b/test/github.js index 23be141..3311f32 100644 --- a/test/github.js +++ b/test/github.js @@ -230,6 +230,9 @@ t.test('string methods populate correctly', t => { t.equal(parsed.hash(), '', 'hash() returns empty string when committish is unset') t.equal(parsed.ssh(), 'git@github.com:foo/bar.git') t.equal(parsed.sshurl(), 'git+ssh://git@github.com/foo/bar.git') + t.equal(parsed.edit(), 'https://github.com/foo/bar') + t.equal(parsed.edit('/lib/index.js'), 'https://github.com/foo/bar/edit/master/lib/index.js') + t.equal(parsed.edit('/lib/index.js', { committish: 'docs' }), 'https://github.com/foo/bar/edit/docs/lib/index.js') t.equal(parsed.browse(), 'https://github.com/foo/bar') t.equal(parsed.browse('/lib/index.js'), 'https://github.com/foo/bar/tree/master/lib/index.js') t.equal(parsed.browse('/lib/index.js', 'L100'), 'https://github.com/foo/bar/tree/master/lib/index.js#l100') diff --git a/test/gitlab.js b/test/gitlab.js index 1975b3e..be5187b 100644 --- a/test/gitlab.js +++ b/test/gitlab.js @@ -287,6 +287,8 @@ t.test('string methods populate correctly', t => { t.equal(parsed.hash(), '', 'hash() returns empty string when committish is unset') t.equal(parsed.ssh(), 'git@gitlab.com:foo/bar.git') t.equal(parsed.sshurl(), 'git+ssh://git@gitlab.com/foo/bar.git') + t.equal(parsed.edit(), 'https://gitlab.com/foo/bar') + t.equal(parsed.edit('/lib/index.js'), 'https://gitlab.com/foo/bar/-/edit/master/lib/index.js') t.equal(parsed.browse(), 'https://gitlab.com/foo/bar') t.equal(parsed.browse('/lib/index.js'), 'https://gitlab.com/foo/bar/tree/master/lib/index.js') t.equal(parsed.browse('/lib/index.js', 'L100'), 'https://gitlab.com/foo/bar/tree/master/lib/index.js#l100') diff --git a/test/sourcehut.js b/test/sourcehut.js index 779bacd..f0f3a76 100644 --- a/test/sourcehut.js +++ b/test/sourcehut.js @@ -94,6 +94,8 @@ t.test('string methods populate correctly', t => { 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.edit('/lib/index.js'), 'https://git.sr.ht/~foo/bar', 'no editing, link to browse') + t.equal(parsed.edit(), 'https://git.sr.ht/~foo/bar', 'no editing, link to browse') 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(