Comparing version 1.0.3 to 1.0.4
73
index.js
var path = require('path'); | ||
function parseRemoteUri(remoteUri) { | ||
var gitRemote = /^git@([^:]+):(.+\/.+)$/, | ||
httpRemote = /^(https?):\/\/([^@]+@)?([^\/]+)\/(.+)$/; | ||
function parseRemoteUri(inputUri) { | ||
// ignore any trailing modifiers, such as the ones supported | ||
// on npm uris (ex: git+ssh://git@github.com:npm/npm.git#v1.0.27) | ||
var uri = inputUri.replace(/#.+$/, ''); | ||
var matches = null; | ||
if (matches = gitRemote.exec(remoteUri)) { | ||
var repository = matches[2].replace(/\.git$/, ''), | ||
splitRepo = repository.split('/'); | ||
// check if its an url with the following known format: | ||
// http://site.com/owner/project(.git) | ||
// any protocols are accepted, to support complex protocols | ||
// (ex. git+https://isaacs@github.com/npm/npm.git) | ||
var generalFormat = /^([^:]+):\/\/([^@]+@)?([^\/:]+)\/([^\/]+)\/([^\/]+)$/; | ||
if (matches = generalFormat.exec(uri)) { | ||
var owner = matches[4], | ||
project = matches[5].replace(/\.git$/, ''); | ||
return { | ||
remote: remoteUri, | ||
protocol: 'git', | ||
host: matches[1], | ||
repository: repository, | ||
owner: splitRepo[0], | ||
project: splitRepo[1] | ||
remote: inputUri, | ||
protocol: matches[1], | ||
host: matches[3], | ||
repository: owner+'/'+project, | ||
owner: owner, | ||
project: project | ||
}; | ||
} | ||
if (matches = httpRemote.exec(remoteUri)) { | ||
var repository = matches[4].replace(/\.git$/, ''), | ||
splitRepo = repository.split('/'); | ||
// check if its an url with the following known format: | ||
// (ssh://)git@site.com:owner/project(.git) | ||
var gitKnownRemote = /^(([^:]+):\/\/)?git@([^:]+):([^\/]+)\/([^\/]+)$/; | ||
if (matches = gitKnownRemote.exec(uri)) { | ||
var owner = matches[4], | ||
project = matches[5].replace(/\.git$/, ''); | ||
return { | ||
remote: remoteUri, | ||
remote: inputUri, | ||
protocol: matches[2] || 'ssh', | ||
host: matches[3], | ||
repository: owner+'/'+project, | ||
owner: owner, | ||
project: project | ||
}; | ||
} | ||
// check if its an uri with any protocol, host and path. | ||
// ex: ssh://user@other.host.com/~/repos/R.git | ||
var looseUriFormat = /^([^:]+):\/\/([^@]+@)?([^\/:]+)\/(.+)$/; | ||
if (matches = looseUriFormat.exec(uri)) { | ||
var repoPath = matches[4], | ||
project = path.basename(repoPath).replace(/\.git$/, ''); | ||
return { | ||
remote: inputUri, | ||
protocol: matches[1], | ||
host: matches[3], | ||
repository: repository, | ||
owner: splitRepo[0], | ||
project: splitRepo[1] | ||
repository: project, | ||
owner: null, | ||
project: project | ||
}; | ||
@@ -40,7 +69,7 @@ } | ||
var dirName = path.basename(remoteUri), | ||
repoName = dirName.split('.git')[0]; | ||
var dirName = path.basename(uri), | ||
repoName = dirName.replace(/\.git$/, ''); | ||
return { | ||
remote: remoteUri, | ||
remote: inputUri, | ||
protocol: 'file', | ||
@@ -47,0 +76,0 @@ host: 'localhost', |
{ | ||
"name": "parse-repo", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Extract repository info from a git remote URI", | ||
@@ -5,0 +5,0 @@ "author": "Diego Guerra <dgoguerra.or@gmail.com>", |
80
test.js
@@ -5,2 +5,3 @@ var tape = require('tape'), | ||
tape('github remotes', function(t) { | ||
// http protocol ending in .git | ||
t.deepEqual(parseRepo('https://github.com/npm/docs.git'), { | ||
@@ -15,2 +16,3 @@ remote: 'https://github.com/npm/docs.git', | ||
// http protocol without .git | ||
t.deepEqual(parseRepo('https://github.com/npm/docs'), { | ||
@@ -25,4 +27,15 @@ remote: 'https://github.com/npm/docs', | ||
// short notation of ssh protocol | ||
t.deepEqual(parseRepo('git@github.com:npm/docs.git'), { | ||
remote: 'git@github.com:npm/docs.git', | ||
protocol: 'ssh', | ||
host: 'github.com', | ||
repository: 'npm/docs', | ||
owner: 'npm', | ||
project: 'docs' | ||
}); | ||
// alternative short notation of ssh | ||
t.deepEqual(parseRepo('git://github.com/npm/docs.git'), { | ||
remote: 'git://github.com/npm/docs.git', | ||
protocol: 'git', | ||
@@ -35,5 +48,16 @@ host: 'github.com', | ||
// ssh protocol long notation | ||
t.deepEqual(parseRepo('ssh://git@github.com:npm/docs.git'), { | ||
remote: 'ssh://git@github.com:npm/docs.git', | ||
protocol: 'ssh', | ||
host: 'github.com', | ||
repository: 'npm/docs', | ||
owner: 'npm', | ||
project: 'docs' | ||
}); | ||
// repo with periods in name (tests issue #2) | ||
t.deepEqual(parseRepo('git@github.com:simshanith/pi.simloovoo.com.git'), { | ||
remote: 'git@github.com:simshanith/pi.simloovoo.com.git', | ||
protocol: 'git', | ||
protocol: 'ssh', | ||
host: 'github.com', | ||
@@ -45,2 +69,3 @@ repository: 'simshanith/pi.simloovoo.com', | ||
// repo with periods in name (tests issue #2) | ||
t.deepEqual(parseRepo('https://github.com/simshanith/pi.simloovoo.com'), { | ||
@@ -55,2 +80,32 @@ remote: 'https://github.com/simshanith/pi.simloovoo.com', | ||
// repo with complex protocol | ||
t.deepEqual(parseRepo('git+ssh://git@github.com:npm/docs.git'), { | ||
remote: 'git+ssh://git@github.com:npm/docs.git', | ||
protocol: 'git+ssh', | ||
host: 'github.com', | ||
repository: 'npm/docs', | ||
owner: 'npm', | ||
project: 'docs' | ||
}); | ||
// repo with complex protocol over https | ||
t.deepEqual(parseRepo('git+https://isaacs@github.com/npm/docs.git'), { | ||
remote: 'git+https://isaacs@github.com/npm/docs.git', | ||
protocol: 'git+https', | ||
host: 'github.com', | ||
repository: 'npm/docs', | ||
owner: 'npm', | ||
project: 'docs' | ||
}); | ||
// ignore trailing modifiers with '#', such as the supported on npm | ||
t.deepEqual(parseRepo('git+ssh://git@github.com:npm/npm.git#v1.0.27'), { | ||
remote: 'git+ssh://git@github.com:npm/npm.git#v1.0.27', | ||
protocol: 'git+ssh', | ||
host: 'github.com', | ||
repository: 'npm/npm', | ||
owner: 'npm', | ||
project: 'npm' | ||
}); | ||
t.end(); | ||
@@ -60,2 +115,3 @@ }); | ||
tape('bitbucket remotes', function(t) { | ||
// http protocol ending in .git | ||
t.deepEqual(parseRepo('https://user@bitbucket.org/owner/org.git'), { | ||
@@ -70,2 +126,3 @@ remote: 'https://user@bitbucket.org/owner/org.git', | ||
// http protocol without .git | ||
t.deepEqual(parseRepo('https://user@bitbucket.org/owner/org'), { | ||
@@ -80,5 +137,6 @@ remote: 'https://user@bitbucket.org/owner/org', | ||
// ssh protocol | ||
t.deepEqual(parseRepo('git@bitbucket.org:owner/org.git'), { | ||
remote: 'git@bitbucket.org:owner/org.git', | ||
protocol: 'git', | ||
protocol: 'ssh', | ||
host: 'bitbucket.org', | ||
@@ -94,2 +152,3 @@ repository: 'owner/org', | ||
tape('gitlab remotes', function(t) { | ||
// http protocol ending in .git | ||
t.deepEqual(parseRepo('https://gitlab.com/gitlab-org/gitlab-ce.git'), { | ||
@@ -104,5 +163,6 @@ remote: 'https://gitlab.com/gitlab-org/gitlab-ce.git', | ||
// ssh protocol | ||
t.deepEqual(parseRepo('git@gitlab.com:gitlab-org/gitlab-ce.git'), { | ||
remote: 'git@gitlab.com:gitlab-org/gitlab-ce.git', | ||
protocol: 'git', | ||
protocol: 'ssh', | ||
host: 'gitlab.com', | ||
@@ -117,3 +177,4 @@ repository: 'gitlab-org/gitlab-ce', | ||
tape('local remotes', function(t) { | ||
tape('ssh and local remotes', function(t) { | ||
// local path ending in .git | ||
t.deepEqual(parseRepo('/full/path/to/repo.git'), { | ||
@@ -128,2 +189,3 @@ remote: '/full/path/to/repo.git', | ||
// local path without .git | ||
t.deepEqual(parseRepo('/full/path/to/repo'), { | ||
@@ -138,3 +200,13 @@ remote: '/full/path/to/repo', | ||
// repos/R repository in the user's home directory of another host | ||
t.deepEqual(parseRepo('ssh://user@other.host.com/~/repos/R.git'), { | ||
remote: 'ssh://user@other.host.com/~/repos/R.git', | ||
protocol: 'ssh', | ||
host: 'other.host.com', | ||
repository: 'R', | ||
owner: null, | ||
project: 'R' | ||
}); | ||
t.end(); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18420
6
243