Skip to content

Commit 49dcbf8

Browse files
committed
Merge pull request #1 from bahmutov/master
Parsing git urls that use `git@...` and `https:git@` format
2 parents ccd3476 + 655d7b6 commit 49dcbf8

File tree

5 files changed

+71
-3
lines changed

5 files changed

+71
-3
lines changed

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: node_js
2+
node_js:
3+
- "0.10"
4+
- "0.12"
5+
- "4"
6+
branches:
7+
only:
8+
- master
9+
script:
10+
- npm test

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ Supports:
55

66
- `<user>/<repo#<commit>`
77
- `git://` and `.git` w/ `#commit` or `@version`
8+
- `git@` and `https:git@`
9+
- `www.github.com`
810
- All 5 different ways you could download a freaking tarball/zipball
911

12+
[![Build status][ci-image] ][ci-url]
13+
1014
## API
1115

1216
### [user, repo, version] = parse(url)
@@ -19,3 +23,6 @@ parse('component/emitter#1') // => ['component', 'emitter', '1']
1923
```
2024

2125
See the tests for all the different types of supported URLs.
26+
27+
[ci-image]: https://travis-ci.org/repo-utils/parse-github-repo-url.png?branch=master
28+
[ci-url]: https://travis-ci.org/repo-utils/parse-github-repo-url

index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@ module.exports = function (string) {
66
var m = /^([\w-]+)\/([\w-.]+)((?:#|@).+)?$/.exec(string)
77
if (m) return format(m)
88

9-
if (!~string.indexOf('://')) return false
9+
string = string.replace('//www.', '//')
10+
// normalize git@ and https:git@ urls
11+
string = string.replace(/^git@/, 'https://')
12+
string = string.replace(/^https:git@/, 'https://')
13+
string = string.replace('.com:', '.com/')
14+
15+
if (!~string.indexOf('://')) {
16+
return false
17+
}
1018
var url = parse(string)
1119

1220
switch (url.hostname) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "parse-github-repo-url",
33
"description": "Parse a GitHub URL for user/project@version",
4-
"version": "1.0.0",
4+
"version": "1.1.0",
55
"author": {
66
"name": "Jonathan Ong",
77
"email": "[email protected]",
@@ -13,6 +13,7 @@
1313
"devDependencies": {
1414
"mocha": "1"
1515
},
16+
"main": "index.js",
1617
"scripts": {
1718
"test": "mocha --reporter spec --bail"
1819
}

test.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
var assert = require('assert')
33

4-
var parse = require('.')
4+
var parse = require('./')
55

66
describe('versionless', function () {
77
[
@@ -17,6 +17,18 @@ describe('versionless', function () {
1717
assert.deepEqual(['component', 'emitter', ''], parse(url))
1818
})
1919
})
20+
21+
it('works for www.github.com', function () {
22+
var url = 'https://www.github.com/component/emitter'
23+
var parsed = parse(url)
24+
assert.deepEqual(['component', 'emitter', ''], parsed)
25+
})
26+
27+
it('works for http://www.github.com', function () {
28+
var url = 'http://www.github.com/component/emitter'
29+
var parsed = parse(url)
30+
assert.deepEqual(['component', 'emitter', ''], parsed)
31+
})
2032
})
2133

2234
describe('versioned', function () {
@@ -37,3 +49,33 @@ describe('versioned', function () {
3749
})
3850
})
3951
})
52+
53+
describe('url parse', function () {
54+
var builtinUrlParse = require('url').parse
55+
56+
it('handles https:// url', function () {
57+
var url = 'https://foo.com/bar'
58+
var parsed = builtinUrlParse(url)
59+
assert.equal('foo.com', parsed.hostname)
60+
})
61+
62+
it('does not handle emails', function () {
63+
var url = '[email protected]/bar'
64+
var parsed = builtinUrlParse(url)
65+
assert.equal(null, parsed.hostname, JSON.stringify(parsed))
66+
})
67+
})
68+
69+
describe('git @ syntax', function () {
70+
it('works for git url', function () {
71+
var url = '[email protected]:bahmutov/lazy-ass.git'
72+
var parsed = parse(url)
73+
assert.deepEqual(['bahmutov', 'lazy-ass', ''], parsed)
74+
});
75+
76+
it('works for https:git url', function () {
77+
var url = 'https:[email protected]:bahmutov/lazy-ass.git'
78+
var parsed = parse(url)
79+
assert.deepEqual(['bahmutov', 'lazy-ass', ''], parsed)
80+
});
81+
})

0 commit comments

Comments
 (0)