Skip to content

Commit

Permalink
lint, improve branch handling
Browse files Browse the repository at this point in the history
now also exposes the entire object from `url.parse` in a completely backward compatible way.
  • Loading branch information
jonschlinkert committed Dec 31, 2016
1 parent b983cb9 commit 0b4e914
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
37 changes: 20 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ function parse(str) {
if (typeof obj.path !== 'string' || !obj.path.length || typeof obj.pathname !== 'string' || !obj.pathname.length) {
return null;
}

obj.path = trimSlash(obj.path);
obj.pathname = trimSlash(obj.pathname);
obj.filepath = null;

if (obj.path.indexOf('repos') === 0) {
obj.path = obj.path.slice(6);
Expand All @@ -39,21 +41,25 @@ function parse(str) {
var hasBlob = seg[2] === 'blob';
if (hasBlob && !isChecksum(seg[3])) {
obj.branch = seg[3];
if(seg.length > 4)
{
if (seg.length > 4) {
obj.filepath = seg.slice(4).join('/');
}
}

var blob = str.indexOf('blob');
if (blob !== -1) {
obj.blob = str.slice(blob + 5);
str = str.slice(0, blob);
}

var tree = str.indexOf('tree');
if (tree !== -1) {
obj.branch = str.slice(tree + 5);
var idx = tree + 5;
var branch = str.slice(idx);
var slash = branch.indexOf('/');
if (slash !== -1) {
branch = branch.slice(0, slash);
}
obj.branch = branch;
}

obj.owner = owner(seg[0]);
Expand Down Expand Up @@ -84,31 +90,28 @@ function parse(str) {
}
}

if(!obj.branch){
if (!obj.branch) {
obj.branch = seg[2] || getBranch(obj.path, obj);
if(seg.length > 3){
if (seg.length > 3) {
obj.filepath = seg.slice(3).join('/');
}
}
var res = {};
res.host = obj.host || 'github.com';
res.owner = obj.owner || null;
res.name = obj.name || null;
res.repo = obj.repo;
res.repository = res.repo;
res.branch = obj.branch;
res.filepath = obj.filepath || null;
return res;

obj.host = obj.host || 'github.com';
obj.owner = obj.owner || null;
obj.name = obj.name || null;
obj.repository = obj.repo;
return obj;
}

function isChecksum(str) {
return /^[a-f0-9]{40}$/i.test(str);
}

function getBranch(str, obj) {
var branch;
var segs = str.split('#');
if (segs.length !== 1) {
var branch;
if (segs.length > 1) {
branch = segs[segs.length - 1];
}
if (!branch && obj.hash && obj.hash.charAt(0) === '#') {
Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('parse-github-url', function() {
assert.equal(gh('[email protected]:assemble/verb.git#v0.6.0').branch, 'v0.6.0');
assert.equal(gh('https://github.com/assemble/verb/blob/foo/README.md').branch, 'foo');
assert.equal(gh('https://github.com/assemble/verb/tree/dev').branch, 'dev');
assert.equal(gh('https://github.com/assemble/verb/tree/feature/dev').branch, 'feature/dev');
assert.equal(gh('https://github.com/assemble/verb/tree/feature/dev').branch, 'feature');
assert.equal(gh('https://github.com/assemble/verb/tree/foo').branch, 'foo');
assert.equal(gh('https://raw.githubusercontent.com/assemble/verb/dev').branch, 'dev');
assert.equal(gh('https://raw.githubusercontent.com/assemble/verb/4d0ebde055557a0d1d988c01e0f070df8cc8fa07').branch, '4d0ebde055557a0d1d988c01e0f070df8cc8fa07');
Expand All @@ -86,7 +86,7 @@ describe('parse-github-url', function() {
assert.equal(gh('[email protected]:assemble/verb.git').branch, 'master');
assert.equal(gh('github:assemble/verb').branch, 'master');
assert.equal(gh('http://github.com/assemble/verb/tree/master').branch, 'master');
assert.equal(gh('http://github.com/assemble/verb/tree/master/foo/bar').branch, 'master/foo/bar');
assert.equal(gh('http://github.com/assemble/verb/tree/master/foo/bar').branch, 'master');
assert.equal(gh('https://github.com/assemble/verb').branch, 'master');
assert.equal(gh('https://raw.githubusercontent.com/assemble/verb').branch, 'master');
assert.equal(gh('https://github.com/assemble/verb/blob/master/foo/index.js').branch, 'master');
Expand Down

0 comments on commit 0b4e914

Please sign in to comment.