diff --git a/bin/gitopen b/bin/gitopen index 8168749..aab638f 100755 --- a/bin/gitopen +++ b/bin/gitopen @@ -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 diff --git a/bin/hgopen b/bin/hgopen index e812303..89d9c4a 100755 --- a/bin/hgopen +++ b/bin/hgopen @@ -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 diff --git a/bin/open-commander.js b/bin/open-commander.js index eb96b1a..3b2dd1b 100644 --- a/bin/open-commander.js +++ b/bin/open-commander.js @@ -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) { @@ -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 { @@ -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': @@ -280,5 +312,5 @@ module.exports = function(argv, option) { return 1; } - return options; + return callback(options); }; diff --git a/package.json b/package.json index 135c8c5..f595235 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,8 @@ "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", @@ -40,4 +41,4 @@ "mocha": "^2.2.5", "should": "^7.0.1" } -} +} \ No newline at end of file diff --git a/test/gitopen.test.js b/test/gitopen.test.js index 4666826..55f27ec 100644 --- a/test/gitopen.test.js +++ b/test/gitopen.test.js @@ -378,7 +378,7 @@ 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(); @@ -386,7 +386,7 @@ describe('$ cd non-git-dir && gitopen', function () { }); 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(); @@ -394,7 +394,7 @@ describe('$ cd non-git-dir && gitopen', function () { }); 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(); @@ -402,7 +402,7 @@ describe('$ cd non-git-dir && gitopen', function () { }); 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(); }); @@ -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(); @@ -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(); @@ -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();