Skip to content

Commit

Permalink
Add suppot of filepath
Browse files Browse the repository at this point in the history
  • Loading branch information
bmeiri committed Dec 8, 2016
1 parent 1a747cf commit e4fba1f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ function parse(str) {
var hasBlob = seg[2] === 'blob';
if (hasBlob && !isChecksum(seg[3])) {
obj.branch = seg[3];
if(seg.length > 4)
{
obj.filepath = seg.slice(4).join('/');
}
}

var blob = str.indexOf('blob');
Expand Down Expand Up @@ -80,14 +84,20 @@ function parse(str) {
}
}

obj.branch = obj.branch || seg[2] || getBranch(obj.path, obj);
if(!obj.branch){
obj.branch = seg[2] || getBranch(obj.path, obj);
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;
}

Expand Down
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ describe('parse-github-url', function() {
assert.equal(gh('https://raw.githubusercontent.com/assemble/verb/dev/README.md').branch, 'dev');
});

it('should get the filepath:', function() {
assert.equal(gh('assemble/verb#branch').filepath, null);
assert.equal(gh('[email protected]:assemble/verb.git#0.6.0').filepath, null);
assert.equal(gh('https://github.com/assemble/verb/blob/foo/README.md').filepath, 'README.md');
assert.equal(gh('https://github.com/assemble/verb/blob/foo/').filepath, null);
assert.equal(gh('https://github.com/assemble/verb/blob/foo').filepath, null);
assert.equal(gh('https://github.com/assemble/verb/blob/foo/bar/README.md').filepath, 'bar/README.md');
assert.equal(gh('https://github.com/assemble/verb/tree/dev').filepath, null);
assert.equal(gh('https://raw.githubusercontent.com/assemble/verb/dev/README.md').filepath, 'README.md');
assert.equal(gh('https://raw.githubusercontent.com/assemble/verb/dev/bar/README.md').filepath, 'bar/README.md');
});

it('should use master branch when another branch is not defined:', function() {
assert.equal(gh('assemble/verb').branch, 'master');
assert.equal(gh('git://github.com/foo/bar.git').branch, 'master');
Expand Down

0 comments on commit e4fba1f

Please sign in to comment.