Skip to content

Commit

Permalink
feat(mr): better way for open pull request, sample to choose remote b…
Browse files Browse the repository at this point in the history
…ase branch
  • Loading branch information
hotoo committed May 31, 2016
1 parent 830ae62 commit d42851b
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 76 deletions.
77 changes: 40 additions & 37 deletions bin/gitopen
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,54 @@ var gitroot = gitremote.getGitRootPath(cwd);
// 1. commander
// 2. if help then print help.
// 3. if version then print version.
var command = commander(process.argv, {
commander(process.argv, {
cwd: cwd,
cwb: cwb,
root: gitroot,
});
// 4. if @profile then open @profile
if (command.category === 'profile') {
var username = command.args.username;
var reponame = command.args.reponame;
var url = 'https://github.com/' + username + (reponame ? '/' + reponame : '');
if (command.verbose) {
console.log('URL:', url);
}, function(command) {

// 4. if @profile then open @profile
if (command.category === 'profile') {
var username = command.args.username;
var reponame = command.args.reponame;
var url = 'https://github.com/' + username + (reponame ? '/' + reponame : '');
if (command.verbose) {
console.log('URL:', url);
process.exit(0);
return 0;
}
xopen(url);
process.exit(0);
return 0;
}
xopen(url);
process.exit(0);
return 0;
}

// 5. git/hg/svn remote url.
var uri;
try {
uri = gitremote.getRemoteUrl({cwd: process.cwd()});
} catch(ex) {
if (command.category === 'snippets/new') {
uri = 'https://github.com/hotoo/gitopen';
// 5. git/hg/svn remote url.
var uri;
try {
uri = gitremote.getRemoteUrl({cwd: process.cwd()});
} catch(ex) {
if (command.category === 'snippets/new') {
uri = 'https://github.com/hotoo/gitopen';
} else {
console.error('Not in git repository directory');
process.exit(1);
return 1;
}
}
// 6. get openrc
var config = getConfig(uri);
command.scheme = config.scheme;
command.protocol = config.protocol;
// 6. resolve paths.
var url = gitresolve(uri, command);
// 7. open
if (command.verbose) {
console.log('Option:', command);
console.log('URL:', url);
} else {
console.error('Not in git repository directory');
process.exit(1);
return 1;
xopen(url);
}
}
// 6. get openrc
var config = getConfig(uri);
command.scheme = config.scheme;
command.protocol = config.protocol;
// 6. resolve paths.
var url = gitresolve(uri, command);
// 7. open
if (command.verbose) {
console.log('Option:', command);
console.log('URL:', url);
} else {
xopen(url);
}

});

// vim:ft=javascript
59 changes: 31 additions & 28 deletions bin/hgopen
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,43 @@ var hgroot = hgremote.getHgRootPath(cwd);
// 1. commander
// 2. if help then print help.
// 3. if version then print version.
var command = commander(process.argv, {
commander(process.argv, {
cwd: cwd,
cwb: cwb,
root: hgroot,
});
// 4. if @profile then open @profile
if (command.category === 'profile') {
var username = command.args.username;
var reponame = command.args.reponame;
var url = 'https://github.com/' + username + (reponame? '/'+reponame : '');
if (command.verbose) {
console.log("URL:", url);
}, function(command) {

// 4. if @profile then open @profile
if (command.category === 'profile') {
var username = command.args.username;
var reponame = command.args.reponame;
var url = 'https://github.com/' + username + (reponame? '/'+reponame : '');
if (command.verbose) {
console.log("URL:", url);
process.exit(0);
return 0;
}
xopen(url);
process.exit(0);
return 0;
}
xopen(url);
process.exit(0);
return 0;
}

// 5. git/hg/svn remote url.
var uri = hgremote.getRemoteUrl({cwd:process.cwd()});
// 6. get openrc
var config = getConfig(uri);
command.scheme = config.scheme;
command.protocol = config.protocol;
// 6. resolve paths.
var url = gitresolve(uri, command);
// 7. open
if (command.verbose) {
console.log("Option:", command)
console.log("URL:", url);
} else {
xopen(url);
}
// 5. git/hg/svn remote url.
var uri = hgremote.getRemoteUrl({cwd:process.cwd()});
// 6. get openrc
var config = getConfig(uri);
command.scheme = config.scheme;
command.protocol = config.protocol;
// 6. resolve paths.
var url = gitresolve(uri, command);
// 7. open
if (command.verbose) {
console.log("Option:", command)
console.log("URL:", url);
} else {
xopen(url);
}

});

// vim:ft=javascript
36 changes: 34 additions & 2 deletions bin/open-commander.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
var fs = require('fs');
var path = require('path');
var commander = require('commander');
var child_process = require('child_process');
var inquirer = require('inquirer');

// resolve absolute path to relative path base repository root.
function resolve(filepath, cwd, root) {
Expand All @@ -11,7 +13,7 @@ function resolve(filepath, cwd, root) {

// @param {argv} process.argv
// @param {String} cwb, current working branch name.
module.exports = function(argv, option) {
module.exports = function(argv, option, callback) {

function parseFilePath(options, cpath) {
try {
Expand Down Expand Up @@ -148,6 +150,36 @@ module.exports = function(argv, option) {
'branch-B': commander.args[2] || option.cwb,
};
}

var RE_REMOTE_BRANCH_NAME = /^(\w+)\/(.+)/;
var cwd = commander.cwd || process.cwd();
var remoteBranches = child_process.execSync(
'git branch -r',
{cwd: cwd}
).toString()
.trim()
.split(/\r\n|\r|\n/)
.map(function(branchName) { return branchName.trim(); })
.filter(function(branchName) {
return branchName.replace(RE_REMOTE_BRANCH_NAME, '$2') !== option.cwb &&
branchName.indexOf(' -> ') === -1; // ` origin/HEAD -> origin/master`
});

if (!options.args['branch-A'] && remoteBranches.length > 1) {
inquirer.prompt([{
name: 'remoteBranch',
type: 'list',
message: 'Choose remote brance to compare:',
choices: remoteBranches,
}]).then(function(answers) {
var br = answers.remoteBranch;
var m = RE_REMOTE_BRANCH_NAME.exec(br);
options.args['branch-A'] = m[2];
return callback(options);
});
return;
}

break;
case 'pulls':
case 'prs':
Expand Down Expand Up @@ -280,5 +312,5 @@ module.exports = function(argv, option) {
return 1;
}

return options;
return callback(options);
};
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@
"dependencies": {
"commander": "^2.8.1",
"deepmerge": "~0.2.10",
"js-yaml": "^3.3.1"
"js-yaml": "^3.3.1",
"inquirer": "^1.0.3"
},
"devDependencies": {
"coveralls": "^2.11.3",
"istanbul": "^0.3.17",
"mocha": "^2.2.5",
"should": "^7.0.1"
}
}
}
14 changes: 7 additions & 7 deletions test/gitopen.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,31 +378,31 @@ describe('gitremote()', function () {

describe('$ cd non-git-dir && gitopen', function () {
it('$ gitopen @hotoo', function (done) {
child_process.exec('cd .. && ./gitopen/bin/gitopen --verbose @hotoo', function(err, stdout) {
child_process.exec('./gitopen/bin/gitopen --verbose @hotoo', {cwd: '..'}, function(err, stdout) {
should(err).not.be.ok();
stdout.should.be.containEql('URL: https://github.com/hotoo\n');
done();
});
});

it('$ gitopen @hotoo/gitopen', function (done) {
child_process.exec('cd .. && ./gitopen/bin/gitopen --verbose @hotoo/gitopen', function(err, stdout) {
child_process.exec('./gitopen/bin/gitopen --verbose @hotoo/gitopen', {cwd: '..'}, function(err, stdout) {
should(err).not.be.ok();
stdout.should.be.containEql('URL: https://github.com/hotoo/gitopen\n');
done();
});
});

it('$ gitopen snippet', function (done) {
child_process.exec('cd .. && ./gitopen/bin/gitopen --verbose snippet', function(err, stdout) {
child_process.exec('./gitopen/bin/gitopen --verbose snippet', {cwd: '..'}, function(err, stdout) {
should(err).not.be.ok();
stdout.should.be.containEql('URL: https://gist.github.com/\n');
done();
});
});

it('$ gitopen #1 SHOULD ERROR', function (done) {
child_process.exec('cd .. && ./gitopen/bin/gitopen --verbose "#1"', function(err) {
child_process.exec('./gitopen/bin/gitopen --verbose "#1"', {cwd: '..'}, function(err) {
should(err).be.ok();
done();
});
Expand Down Expand Up @@ -499,7 +499,7 @@ describe('$ gitopen', function () {
git_command_case_in_subdir.forEach(function(testcase) {
var cmd = testcase[0] ? ' ' + testcase[0] : '';
it('$ cd bin && gitopen' + cmd, function (done) {
child_process.exec('cd bin && ./gitopen --verbose' + cmd, function(err, stdout) {
child_process.exec('./gitopen --verbose' + cmd, {cwd: './bin'}, function(err, stdout) {
should(err).not.be.ok();
stdout.should.be.containEql('URL: ' + (RE_URL.test(testcase[1]) ? testcase[1] : 'https://github.com' + testcase[1]) + '\n');
done();
Expand All @@ -522,7 +522,7 @@ describe('$ hgopen', function () {
hg_command_case.forEach(function(testcase) {
var cmd = testcase[0] ? ' "' + testcase[0] + '"' : '';
it('$ hgopen' + cmd, function (done) {
child_process.exec('cd test/hgssh && ../../bin/hgopen --verbose' + cmd, function(err, stdout) {
child_process.exec('../../bin/hgopen --verbose' + cmd, {cwd: 'test/hgssh'}, function(err, stdout) {
should(err).not.be.ok();
stdout.should.be.containEql('URL: ' + (RE_URL.test(testcase[1]) ? testcase[1] : 'https://bitbucket.org' + testcase[1]) + '\n');
done();
Expand All @@ -535,7 +535,7 @@ describe('$ hgopen', function () {
hg_command_case.forEach(function(testcase) {
var cmd = testcase[0] ? ' "' + testcase[0] + '"' : '';
it('$ hgopen' + cmd, function (done) {
child_process.exec('cd test/hghttp && ../../bin/hgopen --verbose' + cmd, function(err, stdout) {
child_process.exec('../../bin/hgopen --verbose' + cmd, {cwd: 'test/hghttp'}, function(err, stdout) {
should(err).not.be.ok();
stdout.should.be.containEql('URL: ' + (RE_URL.test(testcase[1]) ? testcase[1] : 'https://bitbucket.org' + testcase[1]) + '\n');
done();
Expand Down

0 comments on commit d42851b

Please sign in to comment.