Skip to content

Commit c17bc68

Browse files
committed
Merge pull request #35 from hotoo/feat/mr-prompt-base-branch
feat(mr): better way for open pull request, sample to choose remote b…
2 parents 830ae62 + d42851b commit c17bc68

File tree

5 files changed

+115
-76
lines changed

5 files changed

+115
-76
lines changed

bin/gitopen

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,54 @@ var gitroot = gitremote.getGitRootPath(cwd);
1212
// 1. commander
1313
// 2. if help then print help.
1414
// 3. if version then print version.
15-
var command = commander(process.argv, {
15+
commander(process.argv, {
1616
cwd: cwd,
1717
cwb: cwb,
1818
root: gitroot,
19-
});
20-
// 4. if @profile then open @profile
21-
if (command.category === 'profile') {
22-
var username = command.args.username;
23-
var reponame = command.args.reponame;
24-
var url = 'https://github.com/' + username + (reponame ? '/' + reponame : '');
25-
if (command.verbose) {
26-
console.log('URL:', url);
19+
}, function(command) {
20+
21+
// 4. if @profile then open @profile
22+
if (command.category === 'profile') {
23+
var username = command.args.username;
24+
var reponame = command.args.reponame;
25+
var url = 'https://github.com/' + username + (reponame ? '/' + reponame : '');
26+
if (command.verbose) {
27+
console.log('URL:', url);
28+
process.exit(0);
29+
return 0;
30+
}
31+
xopen(url);
2732
process.exit(0);
2833
return 0;
2934
}
30-
xopen(url);
31-
process.exit(0);
32-
return 0;
33-
}
3435

35-
// 5. git/hg/svn remote url.
36-
var uri;
37-
try {
38-
uri = gitremote.getRemoteUrl({cwd: process.cwd()});
39-
} catch(ex) {
40-
if (command.category === 'snippets/new') {
41-
uri = 'https://github.com/hotoo/gitopen';
36+
// 5. git/hg/svn remote url.
37+
var uri;
38+
try {
39+
uri = gitremote.getRemoteUrl({cwd: process.cwd()});
40+
} catch(ex) {
41+
if (command.category === 'snippets/new') {
42+
uri = 'https://github.com/hotoo/gitopen';
43+
} else {
44+
console.error('Not in git repository directory');
45+
process.exit(1);
46+
return 1;
47+
}
48+
}
49+
// 6. get openrc
50+
var config = getConfig(uri);
51+
command.scheme = config.scheme;
52+
command.protocol = config.protocol;
53+
// 6. resolve paths.
54+
var url = gitresolve(uri, command);
55+
// 7. open
56+
if (command.verbose) {
57+
console.log('Option:', command);
58+
console.log('URL:', url);
4259
} else {
43-
console.error('Not in git repository directory');
44-
process.exit(1);
45-
return 1;
60+
xopen(url);
4661
}
47-
}
48-
// 6. get openrc
49-
var config = getConfig(uri);
50-
command.scheme = config.scheme;
51-
command.protocol = config.protocol;
52-
// 6. resolve paths.
53-
var url = gitresolve(uri, command);
54-
// 7. open
55-
if (command.verbose) {
56-
console.log('Option:', command);
57-
console.log('URL:', url);
58-
} else {
59-
xopen(url);
60-
}
62+
63+
});
6164

6265
// vim:ft=javascript

bin/hgopen

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,43 @@ var hgroot = hgremote.getHgRootPath(cwd);
1212
// 1. commander
1313
// 2. if help then print help.
1414
// 3. if version then print version.
15-
var command = commander(process.argv, {
15+
commander(process.argv, {
1616
cwd: cwd,
1717
cwb: cwb,
1818
root: hgroot,
19-
});
20-
// 4. if @profile then open @profile
21-
if (command.category === 'profile') {
22-
var username = command.args.username;
23-
var reponame = command.args.reponame;
24-
var url = 'https://github.com/' + username + (reponame? '/'+reponame : '');
25-
if (command.verbose) {
26-
console.log("URL:", url);
19+
}, function(command) {
20+
21+
// 4. if @profile then open @profile
22+
if (command.category === 'profile') {
23+
var username = command.args.username;
24+
var reponame = command.args.reponame;
25+
var url = 'https://github.com/' + username + (reponame? '/'+reponame : '');
26+
if (command.verbose) {
27+
console.log("URL:", url);
28+
process.exit(0);
29+
return 0;
30+
}
31+
xopen(url);
2732
process.exit(0);
2833
return 0;
2934
}
30-
xopen(url);
31-
process.exit(0);
32-
return 0;
33-
}
3435

35-
// 5. git/hg/svn remote url.
36-
var uri = hgremote.getRemoteUrl({cwd:process.cwd()});
37-
// 6. get openrc
38-
var config = getConfig(uri);
39-
command.scheme = config.scheme;
40-
command.protocol = config.protocol;
41-
// 6. resolve paths.
42-
var url = gitresolve(uri, command);
43-
// 7. open
44-
if (command.verbose) {
45-
console.log("Option:", command)
46-
console.log("URL:", url);
47-
} else {
48-
xopen(url);
49-
}
36+
// 5. git/hg/svn remote url.
37+
var uri = hgremote.getRemoteUrl({cwd:process.cwd()});
38+
// 6. get openrc
39+
var config = getConfig(uri);
40+
command.scheme = config.scheme;
41+
command.protocol = config.protocol;
42+
// 6. resolve paths.
43+
var url = gitresolve(uri, command);
44+
// 7. open
45+
if (command.verbose) {
46+
console.log("Option:", command)
47+
console.log("URL:", url);
48+
} else {
49+
xopen(url);
50+
}
51+
52+
});
5053

5154
// vim:ft=javascript

bin/open-commander.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
var fs = require('fs');
33
var path = require('path');
44
var commander = require('commander');
5+
var child_process = require('child_process');
6+
var inquirer = require('inquirer');
57

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

1214
// @param {argv} process.argv
1315
// @param {String} cwb, current working branch name.
14-
module.exports = function(argv, option) {
16+
module.exports = function(argv, option, callback) {
1517

1618
function parseFilePath(options, cpath) {
1719
try {
@@ -148,6 +150,36 @@ module.exports = function(argv, option) {
148150
'branch-B': commander.args[2] || option.cwb,
149151
};
150152
}
153+
154+
var RE_REMOTE_BRANCH_NAME = /^(\w+)\/(.+)/;
155+
var cwd = commander.cwd || process.cwd();
156+
var remoteBranches = child_process.execSync(
157+
'git branch -r',
158+
{cwd: cwd}
159+
).toString()
160+
.trim()
161+
.split(/\r\n|\r|\n/)
162+
.map(function(branchName) { return branchName.trim(); })
163+
.filter(function(branchName) {
164+
return branchName.replace(RE_REMOTE_BRANCH_NAME, '$2') !== option.cwb &&
165+
branchName.indexOf(' -> ') === -1; // ` origin/HEAD -> origin/master`
166+
});
167+
168+
if (!options.args['branch-A'] && remoteBranches.length > 1) {
169+
inquirer.prompt([{
170+
name: 'remoteBranch',
171+
type: 'list',
172+
message: 'Choose remote brance to compare:',
173+
choices: remoteBranches,
174+
}]).then(function(answers) {
175+
var br = answers.remoteBranch;
176+
var m = RE_REMOTE_BRANCH_NAME.exec(br);
177+
options.args['branch-A'] = m[2];
178+
return callback(options);
179+
});
180+
return;
181+
}
182+
151183
break;
152184
case 'pulls':
153185
case 'prs':
@@ -280,5 +312,5 @@ module.exports = function(argv, option) {
280312
return 1;
281313
}
282314

283-
return options;
315+
return callback(options);
284316
};

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@
3232
"dependencies": {
3333
"commander": "^2.8.1",
3434
"deepmerge": "~0.2.10",
35-
"js-yaml": "^3.3.1"
35+
"js-yaml": "^3.3.1",
36+
"inquirer": "^1.0.3"
3637
},
3738
"devDependencies": {
3839
"coveralls": "^2.11.3",
3940
"istanbul": "^0.3.17",
4041
"mocha": "^2.2.5",
4142
"should": "^7.0.1"
4243
}
43-
}
44+
}

test/gitopen.test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -378,31 +378,31 @@ describe('gitremote()', function () {
378378

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

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

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

404404
it('$ gitopen #1 SHOULD ERROR', function (done) {
405-
child_process.exec('cd .. && ./gitopen/bin/gitopen --verbose "#1"', function(err) {
405+
child_process.exec('./gitopen/bin/gitopen --verbose "#1"', {cwd: '..'}, function(err) {
406406
should(err).be.ok();
407407
done();
408408
});
@@ -499,7 +499,7 @@ describe('$ gitopen', function () {
499499
git_command_case_in_subdir.forEach(function(testcase) {
500500
var cmd = testcase[0] ? ' ' + testcase[0] : '';
501501
it('$ cd bin && gitopen' + cmd, function (done) {
502-
child_process.exec('cd bin && ./gitopen --verbose' + cmd, function(err, stdout) {
502+
child_process.exec('./gitopen --verbose' + cmd, {cwd: './bin'}, function(err, stdout) {
503503
should(err).not.be.ok();
504504
stdout.should.be.containEql('URL: ' + (RE_URL.test(testcase[1]) ? testcase[1] : 'https://github.com' + testcase[1]) + '\n');
505505
done();
@@ -522,7 +522,7 @@ describe('$ hgopen', function () {
522522
hg_command_case.forEach(function(testcase) {
523523
var cmd = testcase[0] ? ' "' + testcase[0] + '"' : '';
524524
it('$ hgopen' + cmd, function (done) {
525-
child_process.exec('cd test/hgssh && ../../bin/hgopen --verbose' + cmd, function(err, stdout) {
525+
child_process.exec('../../bin/hgopen --verbose' + cmd, {cwd: 'test/hgssh'}, function(err, stdout) {
526526
should(err).not.be.ok();
527527
stdout.should.be.containEql('URL: ' + (RE_URL.test(testcase[1]) ? testcase[1] : 'https://bitbucket.org' + testcase[1]) + '\n');
528528
done();
@@ -535,7 +535,7 @@ describe('$ hgopen', function () {
535535
hg_command_case.forEach(function(testcase) {
536536
var cmd = testcase[0] ? ' "' + testcase[0] + '"' : '';
537537
it('$ hgopen' + cmd, function (done) {
538-
child_process.exec('cd test/hghttp && ../../bin/hgopen --verbose' + cmd, function(err, stdout) {
538+
child_process.exec('../../bin/hgopen --verbose' + cmd, {cwd: 'test/hghttp'}, function(err, stdout) {
539539
should(err).not.be.ok();
540540
stdout.should.be.containEql('URL: ' + (RE_URL.test(testcase[1]) ? testcase[1] : 'https://bitbucket.org' + testcase[1]) + '\n');
541541
done();

0 commit comments

Comments
 (0)