Skip to content

Commit

Permalink
feat(gitopen commit): Open git diff page with hash.
Browse files Browse the repository at this point in the history
  • Loading branch information
hotoo committed Aug 12, 2015
1 parent 5e750f8 commit 864ba9a
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 29 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -67,6 +67,7 @@ $ gitopen wiki # Open wiki pages.
$ gitopen release # Open releases page.
$ gitopen tags # Open tags page.
$ gitopen commits # Open commits pages.
$ gitopen {hash} # Open commit page by hash code.
$ gitopen README.md # Open remote blob url. default use current working branch.
$ gitopen README.md -b branch # Open remote blob url by given branch.
Expand Down Expand Up @@ -184,6 +185,10 @@ alias:
* `$ gitopen commit`
* `$ gitopen ci`

### $ gitopen {hash}

Open commit page by hash code.

### $ gitopen wiki

Open git repository wiki home page.
Expand Down
8 changes: 4 additions & 4 deletions bin/gitopen
Expand Up @@ -21,9 +21,9 @@ var command = commander(process.argv, {
if (command.category === 'profile') {
var username = command.args.username;
var reponame = command.args.reponame;
var url = 'https://github.com/' + username + (reponame? '/'+reponame : '');
var url = 'https://github.com/' + username + (reponame ? '/' + reponame : '');
if (command.verbose) {
console.log("URL:", url);
console.log('URL:', url);
process.exit(0);
return 0;
}
Expand Down Expand Up @@ -53,8 +53,8 @@ command.protocol = config.protocol;
var url = gitresolve(uri, command);
// 7. open
if (command.verbose) {
console.log("Option:", command)
console.log("URL:", url);
console.log('Option:', command);
console.log('URL:', url);
} else {
xopen(url);
}
Expand Down
21 changes: 15 additions & 6 deletions bin/open-commander.js
Expand Up @@ -89,7 +89,7 @@ module.exports = function(argv, option) {
};

// prepare processing branch alias like `:branch-name`
for (var i=commander.args.length-1; i>=0; i--) {
for (var i = commander.args.length - 1; i >= 0; i--) {
if (commander.args[i].indexOf(':') === 0) {
var br = commander.args[i].substring(1);
options.hash = br;
Expand All @@ -106,6 +106,7 @@ module.exports = function(argv, option) {
// branch-a:branch-b
// branch-a...branch-b
var RE_BRANCH_COMPARE = /^(.*?)(?::|\.{3})(.*)$/;
var RE_HASH = /^[0-9a-fA-F]{6,40}$/;

var category = commander.args[0];
var match;
Expand Down Expand Up @@ -166,7 +167,7 @@ module.exports = function(argv, option) {
options.category = 'milestones/new';
options.args = {
title: commander.args.slice(1).join(' ')
}
};
break;
case 'milestones':
options.category = 'milestones';
Expand Down Expand Up @@ -194,6 +195,11 @@ module.exports = function(argv, option) {
break;
case 'ci':
case 'commit':
//options.category = 'commit';
//if (commander.args[2] && commander.args[2] !== '.') {
//options.hash = commander.args[2];
//}
//break;
case 'commits':
options.category = 'commits';
if (commander.branch) {
Expand Down Expand Up @@ -230,29 +236,32 @@ module.exports = function(argv, option) {
options.args = {
issue_id: category.substring(1),
};
} else if (m = RE_PR_ID.exec(category)) {
} else if ((m = RE_PR_ID.exec(category))) {
options.category = 'pulls/id';
options.args = {
pull_id: m[1],
};
} else if (m = RE_PROFILE.exec(category)) {
} else if ((m = RE_PROFILE.exec(category))) {
var username = m[1];
var reponame = m[2];
options.category = 'profile';
options.args = {
username: username,
reponame: reponame,
};
} else if (m = RE_MILESTONE.exec(category)) {
} else if ((m = RE_MILESTONE.exec(category))) {
options.category = 'milestones/id';
options.args = {
milestone_id: m[1],
};
} else if (m = RE_GIST.exec(category)) {
} else if ((m = RE_GIST.exec(category))) {
options.category = 'snippets/new';
options.args = {
type: m[1],
};
} else if (RE_HASH.test(category)) {
options.category = 'commit';
options.hash = category;
} else {
// FILE/DIR PATH
if (!parseFilePath(options, category)) {
Expand Down
4 changes: 2 additions & 2 deletions lib/gitremote.js
Expand Up @@ -24,15 +24,15 @@ exports.getCurrentBranch = function getCurrentBranch(cwd) {
'git symbolic-ref --short HEAD',
'git rev-parse --abbrev-ref HEAD',
];
for (var i=0, l=cmds.length; i<l; i++) {
for (var i = 0, l = cmds.length; i < l; i++) {
try {
var out = child_process.execSync(cmds[i], {cwd: cwd})
.toString().trim();
if (out.indexOf('fatal: ') === 0) {
return null;
}
return out;
} catch (ex){}
} catch (ex) { /* */ }
}
return null;
};
Expand Down
8 changes: 6 additions & 2 deletions lib/index.js
Expand Up @@ -133,12 +133,16 @@ module.exports = function(uri, options) {
case 'network':
path = scheme.network;
break;
case 'commit':
path = scheme['commit']
.replace('{hash}', options.hash);
break;
case 'commits':
path = scheme.commits;
break;
case 'commits-with-branch':
path = scheme['commits-with-branch']
.replace('{branch-name}', options.args.branch);
.replace('{branch-name}', options.branch);
break;
case 'tree':
path = scheme.tree.replace('{hash}', options.hash)
Expand All @@ -158,7 +162,7 @@ module.exports = function(uri, options) {
break;
default:
path = '';
if (options.hash !== 'master') {
if (options.hash !== 'master') {
path = scheme.tree.replace('{hash}', options.hash);
}
break;
Expand Down
2 changes: 1 addition & 1 deletion lib/scheme/bitbucket.js
@@ -1,4 +1,3 @@
/* global module */
module.exports = {
'base': '{protocol}://{hostname}/{username}/{reponame}',
'home': '',
Expand All @@ -10,6 +9,7 @@ module.exports = {
'network': '/network',
'tree': '/src/{hash}{path}?at={hash}',
'blob': '/src/{hash}{path}?at={hast}',
'commit': '/commit/{hash}',
'commits': '/commits',
'commits-with-branch': '/commits/{branch-name}',
'milestones': '/admin/issues/milestones',
Expand Down
2 changes: 1 addition & 1 deletion lib/scheme/coding.js
@@ -1,4 +1,3 @@
/* global module */
module.exports = {
'base': '{protocol}://{hostname}/u/{username}/p/{reponame}',
'home': '/git',
Expand All @@ -10,6 +9,7 @@ module.exports = {
'network': '/git/graph',
'tree': '/git/tree/{hash}',
'blob': '/git/blob/{hash}',
'commit': '/commit/{hash}',
'commits': '/git/commits',
'commits-with-branch': '/git/commits/{branch-name}',
'milestones': '/milestones',
Expand Down
2 changes: 1 addition & 1 deletion lib/scheme/gitcafe.js
@@ -1,4 +1,3 @@
/* global module */
module.exports = {
'base': '{protocol}://{hostname}/{username}/{reponame}',
'home': '',
Expand All @@ -10,6 +9,7 @@ module.exports = {
'network': '/graphs/master',
'tree': '/tree/{hash}{path}',
'blob': '/blob/{hash}{path}',
'commit': '/commit/{hash}',
'commits': '/commits/master',
'commits-with-branch': '/commits/{branch-name}',
'milestones': '', // NOT SUPPORT.
Expand Down
2 changes: 1 addition & 1 deletion lib/scheme/github.js
@@ -1,4 +1,3 @@
/* global module */
module.exports = {
'base': '{protocol}://{hostname}/{username}/{reponame}',
'home': '',
Expand All @@ -10,6 +9,7 @@ module.exports = {
'network': '/network',
'tree': '/tree/{hash}{path}',
'blob': '/blob/{hash}{path}',
'commit': '/commit/{hash}',
'commits': '/commits',
'commits-with-branch': '/commits/{branch-name}',
'milestones': '/milestones',
Expand Down
2 changes: 1 addition & 1 deletion lib/scheme/gitlab.js
@@ -1,4 +1,3 @@
/* global module */
module.exports = {
'base': '{protocol}://{hostname}/{username}/{reponame}',
'home': '',
Expand All @@ -10,6 +9,7 @@ module.exports = {
'network': '/network',
'tree': '/tree/{hash}{path}',
'blob': '/blob/{hash}{path}',
'commit': '/commit/{hash}',
'commits': '/commits',
'commits-with-branch': '/commits/{branch-name}',
'milestones': '/milestones',
Expand Down
2 changes: 1 addition & 1 deletion lib/scheme/oschina.js
@@ -1,4 +1,3 @@
/* global module */
module.exports = {
'base': '{protocol}://{hostname}/{username}/{reponame}',
'home': '',
Expand All @@ -10,6 +9,7 @@ module.exports = {
'network': '/network',
'tree': '/tree/{hash}{path}',
'blob': '/blob/{hash}{path}',
'commit': '/commit/{hash}',
'commits': '/commits/master',
'commits-with-branch': '/commits/{branch-name}',
'milestones': '/milestones',
Expand Down
30 changes: 21 additions & 9 deletions test/gitopen.test.js
Expand Up @@ -9,12 +9,12 @@ var RE_URL = /^https?:\/\//i;

describe('gitresolve.parse()', function () {
var cases = [
['git@github.com:hotoo/gitopen.git', {hostname: 'github.com', username:'hotoo', reponame:'gitopen'}],
['git://github.com/hotoo/gitopen.git', {hostname: 'github.com', username:'hotoo', reponame:'gitopen'}],
['ssh://github.com/hotoo/gitopen.git', {hostname: 'github.com', username:'hotoo', reponame:'gitopen'}],
['ssh://hg@bitbucket.org/hotoo/gitopen.git', {hostname: 'bitbucket.org', username:'hotoo', reponame:'gitopen'}],
['https://github.com/hotoo/gitopen.git', {hostname: 'github.com', username:'hotoo', reponame:'gitopen'}],
['https://hotoo@bitbucket.org/hotoo/gitopen.git', {hostname: 'bitbucket.org', username:'hotoo', reponame:'gitopen'}],
['git@github.com:hotoo/gitopen.git', {hostname: 'github.com', username: 'hotoo', reponame: 'gitopen'}],
['git://github.com/hotoo/gitopen.git', {hostname: 'github.com', username: 'hotoo', reponame: 'gitopen'}],
['ssh://github.com/hotoo/gitopen.git', {hostname: 'github.com', username: 'hotoo', reponame: 'gitopen'}],
['ssh://hg@bitbucket.org/hotoo/gitopen.git', {hostname: 'bitbucket.org', username: 'hotoo', reponame: 'gitopen'}],
['https://github.com/hotoo/gitopen.git', {hostname: 'github.com', username: 'hotoo', reponame: 'gitopen'}],
['https://hotoo@bitbucket.org/hotoo/gitopen.git', {hostname: 'bitbucket.org', username: 'hotoo', reponame: 'gitopen'}],
];
cases.forEach(function(test){
it('gitresolve.parse(' + test[0] + ')', function () {
Expand Down Expand Up @@ -291,7 +291,7 @@ describe('gitresolve()', function () {
base: '{protocol}://{hostname}/{username}/{reponame}',
'commits-with-branch': '/commits?branch={branch-name}',
},
args: {branch: 'br'}
branch: 'br'
}).should.be.eql('https://' + test[1] + '/commits?branch=br');
});

Expand Down Expand Up @@ -340,6 +340,17 @@ describe('gitresolve()', function () {
}).should.be.eql('https://' + test[1] + '/snippets/new');
});

it('gitresolve(' + test[0] + ', {commit:ed8d9e3})', function () {
gitresolve(test[0], {
category: 'commit',
scheme: {
base: '{protocol}://{hostname}/{username}/{reponame}',
'commit': '/commits/{hash}',
},
hash: 'ed8d9e3'
}).should.be.eql('https://' + test[1] + '/commits/ed8d9e3');
});

});
});

Expand All @@ -356,11 +367,11 @@ describe('gitremote()', function () {
});

it('gitremote.getRemoteUrl({cwd})', function () {
resolve(gitremote.getRemoteUrl({cwd:'.'})).should.be.eql('github.com/hotoo/gitopen');
resolve(gitremote.getRemoteUrl({cwd: '.'})).should.be.eql('github.com/hotoo/gitopen');
});

it('gitremote.getRemoteUrl({remote})', function () {
resolve(gitremote.getRemoteUrl({remote:'origin'})).should.be.eql('github.com/hotoo/gitopen');
resolve(gitremote.getRemoteUrl({remote: 'origin'})).should.be.eql('github.com/hotoo/gitopen');
});

});
Expand Down Expand Up @@ -458,6 +469,7 @@ describe('$ gitopen', function () {
['ci', '/hotoo/gitopen/commits'],
['commit', '/hotoo/gitopen/commits'],
['commits', '/hotoo/gitopen/commits'],
['ed8d9e3', '/hotoo/gitopen/commit/ed8d9e3'],
['@hotoo', '/hotoo'],
['@hotoo/gitopen', '/hotoo/gitopen'],
['snippet', 'https://gist.github.com/'],
Expand Down

0 comments on commit 864ba9a

Please sign in to comment.