Skip to content

Commit

Permalink
feat: add method to get an edit link to a file
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Aug 9, 2022
1 parent afcf39b commit ad02952
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/git-host-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand All @@ -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'}`,
Expand Down Expand Up @@ -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)
Expand All @@ -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) => {
Expand All @@ -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))}`,
Expand Down
4 changes: 4 additions & 0 deletions lib/git-host.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions test/bitbucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(), '[email protected]:foo/bar.git')
t.equal(parsed.sshurl(), 'git+ssh://[email protected]/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')
Expand Down
2 changes: 2 additions & 0 deletions test/gist.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(), '[email protected]:feedbeef.git')
t.equal(parsed.sshurl(), 'git+ssh://[email protected]/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')
Expand Down
3 changes: 3 additions & 0 deletions test/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(), '[email protected]:foo/bar.git')
t.equal(parsed.sshurl(), 'git+ssh://[email protected]/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')
Expand Down
2 changes: 2 additions & 0 deletions test/gitlab.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(), '[email protected]:foo/bar.git')
t.equal(parsed.sshurl(), 'git+ssh://[email protected]/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')
Expand Down
2 changes: 2 additions & 0 deletions test/sourcehut.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(), '[email protected]:~foo/bar.git')
t.equal(parsed.sshurl(), 'git+ssh://[email protected]/~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(
Expand Down

0 comments on commit ad02952

Please sign in to comment.