diff --git a/index.js b/index.js index ced5130..a167729 100644 --- a/index.js +++ b/index.js @@ -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); @@ -39,8 +41,7 @@ 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('/'); } } @@ -48,12 +49,17 @@ function parse(str) { 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]); @@ -84,21 +90,18 @@ 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) { @@ -106,9 +109,9 @@ function isChecksum(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) === '#') { diff --git a/test.js b/test.js index 3195b86..c8d566f 100644 --- a/test.js +++ b/test.js @@ -60,7 +60,7 @@ describe('parse-github-url', function() { assert.equal(gh('git@github.com: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'); @@ -86,7 +86,7 @@ describe('parse-github-url', function() { assert.equal(gh('git@github.com: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');