[Pkg-javascript-commits] [node-hosted-git-info] 01/07: Import Upstream version 2.1.5

Sruthi Chandran srud-guest at moszumanska.debian.org
Tue Oct 18 16:48:40 UTC 2016


This is an automated email from the git hooks/post-receive script.

srud-guest pushed a commit to branch master
in repository node-hosted-git-info.

commit c5cfcca4b2f89d67a23a739a64ee00ce79de92cc
Author: Sruthi <srud at disroot.org>
Date:   Tue Oct 18 21:56:04 2016 +0530

    Import Upstream version 2.1.5
---
 .gitignore                                 |   3 +
 .npmignore                                 |   2 +
 .travis.yml                                |   5 ++
 LICENSE                                    |  13 ++++
 README.md                                  |  99 +++++++++++++++++++++++++++
 git-host-info.js                           |  64 ++++++++++++++++++
 git-host.js                                |  96 +++++++++++++++++++++++++++
 index.js                                   | 103 +++++++++++++++++++++++++++++
 package.json                               |  29 ++++++++
 test/basic.js                              |  15 +++++
 test/bitbucket-https-with-embedded-auth.js |  27 ++++++++
 test/bitbucket.js                          |  23 +++++++
 test/gist.js                               |  41 ++++++++++++
 test/github.js                             |  40 +++++++++++
 test/gitlab.js                             |  23 +++++++
 test/https-with-inline-auth.js             |  39 +++++++++++
 test/lib/standard-tests.js                 |  27 ++++++++
 17 files changed, 649 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..58e97a7
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+*~
+.#
+node_modules
diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..efab07f
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,2 @@
+test
+.travis.yml
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..7dc6619
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,5 @@
+language: node_js
+node_js:
+  - "0.11"
+  - "0.10"
+script: "npm test"
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..4505576
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,13 @@
+Copyright (c) 2015, Rebecca Turner
+
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+PERFORMANCE OF THIS SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..1db47dd
--- /dev/null
+++ b/README.md
@@ -0,0 +1,99 @@
+# hosted-git-info
+
+This will let you identify and transform various git hosts URLs between
+protocols.  It also can tell you what the URL is for the raw path for
+particular file for direct access without git.
+
+## Usage
+
+```javascript
+var hostedGitInfo = require("hosted-git-info")
+var info = hostedGitInfo.fromUrl("git at github.com:npm/hosted-git-info.git")
+/* info looks like:
+{
+  type: "github",
+  domain: "github.com",
+  user: "npm",
+  project: "hosted-git-info"
+}
+*/
+```
+
+If the URL can't be matched with a git host, `null` will be returned.  We
+can match git, ssh and https urls.  Additionally, we can match ssh connect
+strings (`git at github.com:npm/hosted-git-info`) and shortcuts (eg,
+`github:npm/hosted-git-info`).  Github specifically, is detected in the case
+of a third, unprefixed, form: `npm/hosted-git-info`.
+
+If it does match, the returned object has properties of:
+
+* info.type -- The short name of the service
+* info.domain -- The domain for git protocol use
+* info.user -- The name of the user/org on the git host
+* info.project -- The name of the project on the git host
+
+And methods of:
+
+* info.file(path)
+
+Given the path of a file relative to the repository, returns a URL for
+directly fetching it from the githost.  If no committish was set then
+`master` will be used as the default.
+
+For example `hostedGitInfo.fromUrl("git at github.com:npm/hosted-git-info.git#v1.0.0").file("package.json")`
+would return `https://raw.githubusercontent.com/npm/hosted-git-info/v1.0.0/package.json`
+
+* info.shortcut()
+
+eg, `github:npm/hosted-git-info`
+
+* info.browse()
+
+eg, `https://github.com/npm/hosted-git-info/tree/v1.2.0`
+
+* info.bugs()
+
+eg, `https://github.com/npm/hosted-git-info/issues`
+
+* info.docs()
+
+eg, `https://github.com/npm/hosted-git-info/tree/v1.2.0#readme`
+
+* info.https()
+
+eg, `git+https://github.com/npm/hosted-git-info.git`
+
+* info.sshurl()
+
+eg, `git+ssh://git@github.com/npm/hosted-git-info.git`
+
+* info.ssh()
+
+eg, `git at github.com:npm/hosted-git-info.git`
+
+* info.path()
+
+eg, `npm/hosted-git-info`
+
+* info.getDefaultRepresentation()
+
+Returns the default output type. The default output type is based on the
+string you passed in to be parsed
+
+* info.toString()
+
+Uses the getDefaultRepresentation to call one of the other methods to get a URL for
+this resource. As such `hostedGitInfo.fromUrl(url).toString()` will give
+you a normalized version of the URL that still uses the same protocol.
+
+Shortcuts will still be returned as shortcuts, but the special case github
+form of `org/project` will be normalized to `github:org/project`.
+
+SSH connect strings will be normalized into `git+ssh` URLs.
+
+
+## Supported hosts
+
+Currently this supports Github, Bitbucket and Gitlab. Pull requests for
+additional hosts welcome.
+
diff --git a/git-host-info.js b/git-host-info.js
new file mode 100644
index 0000000..2234333
--- /dev/null
+++ b/git-host-info.js
@@ -0,0 +1,64 @@
+'use strict'
+
+var gitHosts = module.exports = {
+  github: {
+    // First two are insecure and generally shouldn't be used any more, but
+    // they are still supported.
+    'protocols': [ 'git', 'http', 'git+ssh', 'git+https', 'ssh', 'https' ],
+    'domain': 'github.com',
+    'treepath': 'tree',
+    'filetemplate': 'https://{auth@}raw.githubusercontent.com/{user}/{project}/{committish}/{path}',
+    'bugstemplate': 'https://{domain}/{user}/{project}/issues',
+    'gittemplate': 'git://{auth@}{domain}/{user}/{project}.git{#committish}'
+  },
+  bitbucket: {
+    'protocols': [ 'git+ssh', 'git+https', 'ssh', 'https' ],
+    'domain': 'bitbucket.org',
+    'treepath': 'src'
+  },
+  gitlab: {
+    'protocols': [ 'git+ssh', 'git+https', 'ssh', 'https' ],
+    'domain': 'gitlab.com',
+    'treepath': 'tree',
+    'docstemplate': 'https://{domain}/{user}/{project}{/tree/committish}#README',
+    'bugstemplate': 'https://{domain}/{user}/{project}/issues'
+  },
+  gist: {
+    'protocols': [ 'git', 'git+ssh', 'git+https', 'ssh', 'https' ],
+    'domain': 'gist.github.com',
+    'pathmatch': /^[/](?:([^/]+)[/])?([a-z0-9]+)(?:[.]git)?$/,
+    'filetemplate': 'https://gist.githubusercontent.com/{user}/{project}/raw{/committish}/{path}',
+    'bugstemplate': 'https://{domain}/{project}',
+    'gittemplate': 'git://{domain}/{project}.git{#committish}',
+    'sshtemplate': 'git@{domain}:/{project}.git{#committish}',
+    'sshurltemplate': 'git+ssh://git@{domain}/{project}.git{#committish}',
+    'browsetemplate': 'https://{domain}/{project}{/committish}',
+    'docstemplate': 'https://{domain}/{project}{/committish}',
+    'httpstemplate': 'git+https://{domain}/{project}.git{#committish}',
+    'shortcuttemplate': '{type}:{project}{#committish}',
+    'pathtemplate': '{project}{#committish}'
+  }
+}
+
+var gitHostDefaults = {
+  'sshtemplate': 'git@{domain}:{user}/{project}.git{#committish}',
+  'sshurltemplate': 'git+ssh://git@{domain}/{user}/{project}.git{#committish}',
+  'browsetemplate': 'https://{domain}/{user}/{project}{/tree/committish}',
+  'docstemplate': 'https://{domain}/{user}/{project}{/tree/committish}#readme',
+  'httpstemplate': 'git+https://{auth@}{domain}/{user}/{project}.git{#committish}',
+  'filetemplate': 'https://{domain}/{user}/{project}/raw/{committish}/{path}',
+  'shortcuttemplate': '{type}:{user}/{project}{#committish}',
+  'pathtemplate': '{user}/{project}{#committish}',
+  'pathmatch': /^[/]([^/]+)[/]([^/]+?)(?:[.]git)?$/
+}
+
+Object.keys(gitHosts).forEach(function (name) {
+  Object.keys(gitHostDefaults).forEach(function (key) {
+    if (gitHosts[name][key]) return
+    gitHosts[name][key] = gitHostDefaults[key]
+  })
+  gitHosts[name].protocols_re = RegExp('^(' +
+    gitHosts[name].protocols.map(function (protocol) {
+      return protocol.replace(/([\\+*{}()\[\]$^|])/g, '\\$1')
+    }).join('|') + '):$')
+})
diff --git a/git-host.js b/git-host.js
new file mode 100644
index 0000000..ea31380
--- /dev/null
+++ b/git-host.js
@@ -0,0 +1,96 @@
+'use strict'
+var gitHosts = require('./git-host-info.js')
+
+var GitHost = module.exports = function (type, user, auth, project, committish, defaultRepresentation) {
+  var gitHostInfo = this
+  gitHostInfo.type = type
+  Object.keys(gitHosts[type]).forEach(function (key) {
+    gitHostInfo[key] = gitHosts[type][key]
+  })
+  gitHostInfo.user = user
+  gitHostInfo.auth = auth
+  gitHostInfo.project = project
+  gitHostInfo.committish = committish
+  gitHostInfo.default = defaultRepresentation
+}
+GitHost.prototype = {}
+
+GitHost.prototype.hash = function () {
+  return this.committish ? '#' + this.committish : ''
+}
+
+GitHost.prototype._fill = function (template, vars) {
+  if (!template) return
+  if (!vars) vars = {}
+  var self = this
+  Object.keys(this).forEach(function (key) {
+    if (self[key] != null && vars[key] == null) vars[key] = self[key]
+  })
+  var rawAuth = vars.auth
+  var rawComittish = vars.committish
+  Object.keys(vars).forEach(function (key) {
+    vars[key] = encodeURIComponent(vars[key])
+  })
+  vars['auth@'] = rawAuth ? rawAuth + '@' : ''
+  vars['#committish'] = rawComittish ? '#' + rawComittish : ''
+  vars['/tree/committish'] = vars.committish
+                          ? '/' + vars.treepath + '/' + vars.committish
+                          : ''
+  vars['/committish'] = vars.committish ? '/' + vars.committish : ''
+  vars.committish = vars.committish || 'master'
+  var res = template
+  Object.keys(vars).forEach(function (key) {
+    res = res.replace(new RegExp('[{]' + key + '[}]', 'g'), vars[key])
+  })
+  return res
+}
+
+GitHost.prototype.ssh = function () {
+  return this._fill(this.sshtemplate)
+}
+
+GitHost.prototype.sshurl = function () {
+  return this._fill(this.sshurltemplate)
+}
+
+GitHost.prototype.browse = function () {
+  return this._fill(this.browsetemplate)
+}
+
+GitHost.prototype.docs = function () {
+  return this._fill(this.docstemplate)
+}
+
+GitHost.prototype.bugs = function () {
+  return this._fill(this.bugstemplate)
+}
+
+GitHost.prototype.https = function () {
+  return this._fill(this.httpstemplate)
+}
+
+GitHost.prototype.git = function () {
+  return this._fill(this.gittemplate)
+}
+
+GitHost.prototype.shortcut = function () {
+  return this._fill(this.shortcuttemplate)
+}
+
+GitHost.prototype.path = function () {
+  return this._fill(this.pathtemplate)
+}
+
+GitHost.prototype.file = function (P) {
+  return this._fill(this.filetemplate, {
+    path: P.replace(/^[/]+/g, '')
+  })
+}
+
+GitHost.prototype.getDefaultRepresentation = function () {
+  return this.default
+}
+
+GitHost.prototype.toString = function () {
+  return (this[this.default] || this.sshurl).call(this)
+}
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..453ce87
--- /dev/null
+++ b/index.js
@@ -0,0 +1,103 @@
+'use strict'
+var url = require('url')
+var gitHosts = require('./git-host-info.js')
+var GitHost = module.exports = require('./git-host.js')
+
+var protocolToRepresentationMap = {
+  'git+ssh': 'sshurl',
+  'git+https': 'https',
+  'ssh': 'sshurl',
+  'git': 'git'
+}
+
+function protocolToRepresentation (protocol) {
+  if (protocol.substr(-1) === ':') protocol = protocol.slice(0, -1)
+  return protocolToRepresentationMap[protocol] || protocol
+}
+
+var authProtocols = {
+  'git:': true,
+  'https:': true,
+  'git+https:': true,
+  'http:': true,
+  'git+http:': true
+}
+
+module.exports.fromUrl = function (giturl) {
+  if (giturl == null || giturl === '') return
+  var url = fixupUnqualifiedGist(
+    isGitHubShorthand(giturl) ? 'github:' + giturl : giturl
+  )
+  var parsed = parseGitUrl(url)
+  var matches = Object.keys(gitHosts).map(function (gitHostName) {
+    var gitHostInfo = gitHosts[gitHostName]
+    var auth = null
+    if (parsed.auth && authProtocols[parsed.protocol]) {
+      auth = decodeURIComponent(parsed.auth)
+    }
+    var committish = parsed.hash ? decodeURIComponent(parsed.hash.substr(1)) : null
+    var user = null
+    var project = null
+    var defaultRepresentation = null
+    if (parsed.protocol === gitHostName + ':') {
+      user = decodeURIComponent(parsed.host)
+      project = parsed.path && decodeURIComponent(parsed.path.replace(/^[/](.*?)(?:[.]git)?$/, '$1'))
+      defaultRepresentation = 'shortcut'
+    } else {
+      if (parsed.host !== gitHostInfo.domain) return
+      if (!gitHostInfo.protocols_re.test(parsed.protocol)) return
+      var pathmatch = gitHostInfo.pathmatch
+      var matched = parsed.path.match(pathmatch)
+      if (!matched) return
+      if (matched[1] != null) user = decodeURIComponent(matched[1])
+      if (matched[2] != null) project = decodeURIComponent(matched[2])
+      defaultRepresentation = protocolToRepresentation(parsed.protocol)
+    }
+    return new GitHost(gitHostName, user, auth, project, committish, defaultRepresentation)
+  }).filter(function (gitHostInfo) { return gitHostInfo })
+  if (matches.length !== 1) return
+  return matches[0]
+}
+
+function isGitHubShorthand (arg) {
+  // Note: This does not fully test the git ref format.
+  // See https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
+  //
+  // The only way to do this properly would be to shell out to
+  // git-check-ref-format, and as this is a fast sync function,
+  // we don't want to do that.  Just let git fail if it turns
+  // out that the commit-ish is invalid.
+  // GH usernames cannot start with . or -
+  return /^[^:@%/\s.-][^:@%/\s]*[/][^:@\s/%]+(?:#.*)?$/.test(arg)
+}
+
+function fixupUnqualifiedGist (giturl) {
+  // necessary for round-tripping gists
+  var parsed = url.parse(giturl)
+  if (parsed.protocol === 'gist:' && parsed.host && !parsed.path) {
+    return parsed.protocol + '/' + parsed.host
+  } else {
+    return giturl
+  }
+}
+
+function parseGitUrl (giturl) {
+  if (typeof giturl !== 'string') giturl = '' + giturl
+  var matched = giturl.match(/^([^@]+)@([^:]+):[/]?((?:[^/]+[/])?[^/]+?)(?:[.]git)?(#.*)?$/)
+  if (!matched) return url.parse(giturl)
+  return {
+    protocol: 'git+ssh:',
+    slashes: true,
+    auth: matched[1],
+    host: matched[2],
+    port: null,
+    hostname: matched[2],
+    hash: matched[4],
+    search: null,
+    query: null,
+    pathname: '/' + matched[3],
+    path: '/' + matched[3],
+    href: 'git+ssh://' + matched[1] + '@' + matched[2] +
+          '/' + matched[3] + (matched[4] || '')
+  }
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..f24e39d
--- /dev/null
+++ b/package.json
@@ -0,0 +1,29 @@
+{
+  "name": "hosted-git-info",
+  "version": "2.1.5",
+  "description": "Provides metadata and conversions from repository urls for Github, Bitbucket and Gitlab",
+  "main": "index.js",
+  "repository": {
+    "type": "git",
+    "url": "git+https://github.com/npm/hosted-git-info.git"
+  },
+  "keywords": [
+    "git",
+    "github",
+    "bitbucket",
+    "gitlab"
+  ],
+  "author": "Rebecca Turner <me at re-becca.org> (http://re-becca.org)",
+  "license": "ISC",
+  "bugs": {
+    "url": "https://github.com/npm/hosted-git-info/issues"
+  },
+  "homepage": "https://github.com/npm/hosted-git-info",
+  "scripts": {
+    "test": "standard && tap test/*.js"
+  },
+  "devDependencies": {
+    "standard": "^3.3.2",
+    "tap": "^0.4.13"
+  }
+}
diff --git a/test/basic.js b/test/basic.js
new file mode 100644
index 0000000..0b93f50
--- /dev/null
+++ b/test/basic.js
@@ -0,0 +1,15 @@
+'use strict'
+var HostedGit = require('../index')
+var test = require('tap').test
+
+test('basic', function (t) {
+  t.is(HostedGit.fromUrl('https://google.com'), undefined, 'null on failure')
+  t.is(HostedGit.fromUrl('https://github.com/abc/def').getDefaultRepresentation(), 'https', 'match https urls')
+  t.is(HostedGit.fromUrl('ssh://git@github.com/abc/def').getDefaultRepresentation(), 'sshurl', 'match ssh urls')
+  t.is(HostedGit.fromUrl('git+ssh://git@github.com/abc/def').getDefaultRepresentation(), 'sshurl', 'match git+ssh urls')
+  t.is(HostedGit.fromUrl('git+https://github.com/abc/def').getDefaultRepresentation(), 'https', 'match git+https urls')
+  t.is(HostedGit.fromUrl('git at github.com:abc/def').getDefaultRepresentation(), 'sshurl', 'match ssh connect strings')
+  t.is(HostedGit.fromUrl('git://github.com/abc/def').getDefaultRepresentation(), 'git', 'match git urls')
+  t.is(HostedGit.fromUrl('github:abc/def').getDefaultRepresentation(), 'shortcut', 'match shortcuts')
+  t.end()
+})
diff --git a/test/bitbucket-https-with-embedded-auth.js b/test/bitbucket-https-with-embedded-auth.js
new file mode 100644
index 0000000..a2feb2f
--- /dev/null
+++ b/test/bitbucket-https-with-embedded-auth.js
@@ -0,0 +1,27 @@
+'use strict'
+var HostedGit = require('../index')
+var test = require('tap').test
+
+test('Bitbucket HTTPS URLs with embedded auth', function (t) {
+  t.is(
+    HostedGit.fromUrl('https://user:pass@bitbucket.org/user/repo.git').toString(),
+    'git+https://user:pass@bitbucket.org/user/repo.git',
+    'credentials were included in URL'
+  )
+  t.is(
+    HostedGit.fromUrl('https://user:pass@bitbucket.org/user/repo').toString(),
+    'git+https://user:pass@bitbucket.org/user/repo.git',
+    'credentials were included in URL'
+  )
+  t.is(
+    HostedGit.fromUrl('git+https://user:pass@bitbucket.org/user/repo.git').toString(),
+    'git+https://user:pass@bitbucket.org/user/repo.git',
+    'credentials were included in URL'
+  )
+  t.is(
+    HostedGit.fromUrl('git+https://user:pass@bitbucket.org/user/repo').toString(),
+    'git+https://user:pass@bitbucket.org/user/repo.git',
+    'credentials were included in URL'
+  )
+  t.end()
+})
diff --git a/test/bitbucket.js b/test/bitbucket.js
new file mode 100644
index 0000000..72e4ba1
--- /dev/null
+++ b/test/bitbucket.js
@@ -0,0 +1,23 @@
+'use strict'
+var HostedGit = require('../index')
+var test = require('tap').test
+
+test('fromUrl(bitbucket url)', function (t) {
+  function verify (host, label, branch) {
+    var hostinfo = HostedGit.fromUrl(host)
+    var hash = branch ? '#' + branch : ''
+    t.ok(hostinfo, label)
+    if (!hostinfo) return
+    t.is(hostinfo.https(), 'git+https://bitbucket.org/111/222.git' + hash, label + ' -> https')
+    t.is(hostinfo.browse(), 'https://bitbucket.org/111/222' + (branch ? '/src/' + branch : ''), label + ' -> browse')
+    t.is(hostinfo.docs(), 'https://bitbucket.org/111/222' + (branch ? '/src/' + branch : '') + '#readme', label + ' -> docs')
+    t.is(hostinfo.ssh(), 'git at bitbucket.org:111/222.git' + hash, label + ' -> ssh')
+    t.is(hostinfo.sshurl(), 'git+ssh://git@bitbucket.org/111/222.git' + hash, label + ' -> sshurl')
+    t.is(hostinfo.shortcut(), 'bitbucket:111/222' + hash, label + ' -> shortcut')
+    t.is(hostinfo.file('C'), 'https://bitbucket.org/111/222/raw/' + (branch || 'master') + '/C', label + ' -> file')
+  }
+
+  require('./lib/standard-tests')(verify, 'bitbucket.org', 'bitbucket')
+
+  t.end()
+})
diff --git a/test/gist.js b/test/gist.js
new file mode 100644
index 0000000..cf36d3c
--- /dev/null
+++ b/test/gist.js
@@ -0,0 +1,41 @@
+'use strict'
+var HostedGit = require('../index')
+var test = require('tap').test
+
+test('fromUrl(gist url)', function (t) {
+  function verify (host, label, branch) {
+    var hostinfo = HostedGit.fromUrl(host)
+    var hash = branch ? '#' + branch : ''
+    t.ok(hostinfo, label)
+    if (!hostinfo) return
+    t.is(hostinfo.https(), 'git+https://gist.github.com/222.git' + hash, label + ' -> https')
+    t.is(hostinfo.git(), 'git://gist.github.com/222.git' + hash, label + ' -> git')
+    t.is(hostinfo.browse(), 'https://gist.github.com/222' + (branch ? '/' + branch : ''), label + ' -> browse')
+    t.is(hostinfo.bugs(), 'https://gist.github.com/222', label + ' -> bugs')
+    t.is(hostinfo.docs(), 'https://gist.github.com/222' + (branch ? '/' + branch : ''), label + ' -> docs')
+    t.is(hostinfo.ssh(), 'git at gist.github.com:/222.git' + hash, label + ' -> ssh')
+    t.is(hostinfo.sshurl(), 'git+ssh://git@gist.github.com/222.git' + hash, label + ' -> sshurl')
+    t.is(hostinfo.shortcut(), 'gist:222' + hash, label + ' -> shortcut')
+    if (hostinfo.user) {
+      t.is(hostinfo.file('C'), 'https://gist.githubusercontent.com/111/222/raw/' + (branch ? branch + '/' : '') + 'C', label + ' -> file')
+    }
+  }
+
+  verify('git at gist.github.com:222.git', 'git@')
+  var hostinfo = HostedGit.fromUrl('git at gist.github.com:/ef860c7z5e0de3179341.git')
+  if (t.ok(hostinfo, 'git at hex')) {
+    t.is(hostinfo.https(), 'git+https://gist.github.com/ef860c7z5e0de3179341.git', 'git at hex -> https')
+  }
+  verify('git at gist.github.com:/222.git', 'git@/')
+  verify('git://gist.github.com/222', 'git')
+  verify('git://gist.github.com/222.git', 'git.git')
+  verify('git://gist.github.com/222#branch', 'git#branch', 'branch')
+  verify('git://gist.github.com/222.git#branch', 'git.git#branch', 'branch')
+
+  require('./lib/standard-tests')(verify, 'gist.github.com', 'gist')
+
+  verify(HostedGit.fromUrl('gist:111/222').toString(), 'round-tripped shortcut')
+  verify('gist:222', 'shortened shortcut')
+
+  t.end()
+})
diff --git a/test/github.js b/test/github.js
new file mode 100644
index 0000000..56098a3
--- /dev/null
+++ b/test/github.js
@@ -0,0 +1,40 @@
+'use strict'
+var HostedGit = require('../index')
+var test = require('tap').test
+
+test('fromUrl(github url)', function (t) {
+  function verify (host, label, branch) {
+    var hostinfo = HostedGit.fromUrl(host)
+    var hash = branch ? '#' + branch : ''
+    t.ok(hostinfo, label)
+    if (!hostinfo) return
+    t.is(hostinfo.https(), 'git+https://github.com/111/222.git' + hash, label + ' -> https')
+    t.is(hostinfo.git(), 'git://github.com/111/222.git' + hash, label + ' -> git')
+    t.is(hostinfo.browse(), 'https://github.com/111/222' + (branch ? '/tree/' + branch : ''), label + ' -> browse')
+    t.is(hostinfo.bugs(), 'https://github.com/111/222/issues', label + ' -> bugs')
+    t.is(hostinfo.docs(), 'https://github.com/111/222' + (branch ? '/tree/' + branch : '') + '#readme', label + ' -> docs')
+    t.is(hostinfo.ssh(), 'git at github.com:111/222.git' + hash, label + ' -> ssh')
+    t.is(hostinfo.sshurl(), 'git+ssh://git@github.com/111/222.git' + hash, label + ' -> sshurl')
+    t.is(hostinfo.shortcut(), 'github:111/222' + hash, label + ' -> shortcut')
+    t.is(hostinfo.file('C'), 'https://raw.githubusercontent.com/111/222/' + (branch || 'master') + '/C', label + ' -> file')
+  }
+
+  // github shorturls
+  verify('111/222', 'github-short')
+  verify('111/222#branch', 'github-short#branch', 'branch')
+
+  // insecure protocols
+  verify('git://github.com/111/222', 'git')
+  verify('git://github.com/111/222.git', 'git.git')
+  verify('git://github.com/111/222#branch', 'git#branch', 'branch')
+  verify('git://github.com/111/222.git#branch', 'git.git#branch', 'branch')
+
+  verify('http://github.com/111/222', 'http')
+  verify('http://github.com/111/222.git', 'http.git')
+  verify('http://github.com/111/222#branch', 'http#branch', 'branch')
+  verify('http://github.com/111/222.git#branch', 'http.git#branch', 'branch')
+
+  require('./lib/standard-tests')(verify, 'github.com', 'github')
+
+  t.end()
+})
diff --git a/test/gitlab.js b/test/gitlab.js
new file mode 100644
index 0000000..315c908
--- /dev/null
+++ b/test/gitlab.js
@@ -0,0 +1,23 @@
+'use strict'
+var HostedGit = require('../index')
+var test = require('tap').test
+
+test('fromUrl(gitlab url)', function (t) {
+  function verify (host, label, branch) {
+    var hostinfo = HostedGit.fromUrl(host)
+    var hash = branch ? '#' + branch : ''
+    t.ok(hostinfo, label)
+    if (!hostinfo) return
+    t.is(hostinfo.https(), 'git+https://gitlab.com/111/222.git' + hash, label + ' -> https')
+    t.is(hostinfo.browse(), 'https://gitlab.com/111/222' + (branch ? '/tree/' + branch : ''), label + ' -> browse')
+    t.is(hostinfo.docs(), 'https://gitlab.com/111/222' + (branch ? '/tree/' + branch : '') + '#README', label + ' -> docs')
+    t.is(hostinfo.ssh(), 'git at gitlab.com:111/222.git' + hash, label + ' -> ssh')
+    t.is(hostinfo.sshurl(), 'git+ssh://git@gitlab.com/111/222.git' + hash, label + ' -> sshurl')
+    t.is(hostinfo.shortcut(), 'gitlab:111/222' + hash, label + ' -> shortcut')
+    t.is(hostinfo.file('C'), 'https://gitlab.com/111/222/raw/' + (branch || 'master') + '/C', label + ' -> file')
+  }
+
+  require('./lib/standard-tests')(verify, 'gitlab.com', 'gitlab')
+
+  t.end()
+})
diff --git a/test/https-with-inline-auth.js b/test/https-with-inline-auth.js
new file mode 100644
index 0000000..5e2f5b5
--- /dev/null
+++ b/test/https-with-inline-auth.js
@@ -0,0 +1,39 @@
+'use strict'
+var HostedGit = require('../index')
+var test = require('tap').test
+
+test('HTTPS GitHub URL with embedded auth -- generally not a good idea', function (t) {
+  function verify (host, label, branch) {
+    var hostinfo = HostedGit.fromUrl(host)
+    var hash = branch ? '#' + branch : ''
+    t.ok(hostinfo, label)
+    if (!hostinfo) return
+    t.is(hostinfo.https(), 'git+https://user:pass@github.com/111/222.git' + hash, label + ' -> https')
+    t.is(hostinfo.git(), 'git://user:pass@github.com/111/222.git' + hash, label + ' -> git')
+    t.is(hostinfo.browse(), 'https://github.com/111/222' + (branch ? '/tree/' + branch : ''), label + ' -> browse')
+    t.is(hostinfo.bugs(), 'https://github.com/111/222/issues', label + ' -> bugs')
+    t.is(hostinfo.docs(), 'https://github.com/111/222' + (branch ? '/tree/' + branch : '') + '#readme', label + ' -> docs')
+    t.is(hostinfo.ssh(), 'git at github.com:111/222.git' + hash, label + ' -> ssh')
+    t.is(hostinfo.sshurl(), 'git+ssh://git@github.com/111/222.git' + hash, label + ' -> sshurl')
+    t.is(hostinfo.shortcut(), 'github:111/222' + hash, label + ' -> shortcut')
+    t.is(hostinfo.file('C'), 'https://user:pass@raw.githubusercontent.com/111/222/' + (branch || 'master') + '/C', label + ' -> file')
+  }
+
+  // insecure protocols
+  verify('git://user:pass@github.com/111/222', 'git')
+  verify('git://user:pass@github.com/111/222.git', 'git.git')
+  verify('git://user:pass@github.com/111/222#branch', 'git#branch', 'branch')
+  verify('git://user:pass@github.com/111/222.git#branch', 'git.git#branch', 'branch')
+
+  verify('https://user:pass@github.com/111/222', 'https')
+  verify('https://user:pass@github.com/111/222.git', 'https.git')
+  verify('https://user:pass@github.com/111/222#branch', 'https#branch', 'branch')
+  verify('https://user:pass@github.com/111/222.git#branch', 'https.git#branch', 'branch')
+
+  verify('http://user:pass@github.com/111/222', 'http')
+  verify('http://user:pass@github.com/111/222.git', 'http.git')
+  verify('http://user:pass@github.com/111/222#branch', 'http#branch', 'branch')
+  verify('http://user:pass@github.com/111/222.git#branch', 'http.git#branch', 'branch')
+
+  t.end()
+})
diff --git a/test/lib/standard-tests.js b/test/lib/standard-tests.js
new file mode 100644
index 0000000..929fcca
--- /dev/null
+++ b/test/lib/standard-tests.js
@@ -0,0 +1,27 @@
+'use strict'
+module.exports = function (verify, domain, shortname) {
+  verify('https://' + domain + '/111/222', 'https')
+  verify('https://' + domain + '/111/222.git', 'https.git')
+  verify('https://' + domain + '/111/222#branch', 'https#branch', 'branch')
+  verify('https://' + domain + '/111/222.git#branch', 'https.git#branch', 'branch')
+
+  verify('git+https://' + domain + '/111/222', 'git+https')
+  verify('git+https://' + domain + '/111/222.git', 'git+https.git')
+  verify('git+https://' + domain + '/111/222#branch', 'git+https#branch', 'branch')
+  verify('git+https://' + domain + '/111/222.git#branch', 'git+https.git#branch', 'branch')
+
+  verify('git@' + domain + ':111/222', 'ssh')
+  verify('git@' + domain + ':111/222.git', 'ssh.git')
+  verify('git@' + domain + ':111/222#branch', 'ssh', 'branch')
+  verify('git@' + domain + ':111/222.git#branch', 'ssh.git', 'branch')
+
+  verify('git+ssh://git@' + domain + '/111/222', 'ssh url')
+  verify('git+ssh://git@' + domain + '/111/222.git', 'ssh url.git')
+  verify('git+ssh://git@' + domain + '/111/222#branch', 'ssh url#branch', 'branch')
+  verify('git+ssh://git@' + domain + '/111/222.git#branch', 'ssh url.git#branch', 'branch')
+
+  verify(shortname + ':111/222', 'shortcut')
+  verify(shortname + ':111/222.git', 'shortcut.git')
+  verify(shortname + ':111/222#branch', 'shortcut#branch', 'branch')
+  verify(shortname + ':111/222.git#branch', 'shortcut.git#branch', 'branch')
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-hosted-git-info.git



More information about the Pkg-javascript-commits mailing list