Skip to content

Commit

Permalink
feat: auto get base branch for create PR/MR
Browse files Browse the repository at this point in the history
fixed #47
  • Loading branch information
hotoo committed Feb 22, 2017
1 parent 396e58b commit b10d5cb
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 30 deletions.
8 changes: 7 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{
"globals": {
"module": true,
"exports": true,
"process": true,
"child_process": true
},
"ecmaFeatures": {
"jsx": true
"jsx": false
},
"parser": "espree",
"env": {
Expand Down
58 changes: 29 additions & 29 deletions bin/open-commander.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict';

/* global module, process */
var fs = require('fs');
var path = require('path');
var commander = require('commander');
var child_process = require('child_process');
var inquirer = require('inquirer');
var gitremote = require('../lib/gitremote');

// resolve absolute path to relative path base repository root.
function resolve(filepath, cwd, root) {
Expand Down Expand Up @@ -154,33 +153,34 @@ module.exports = function(argv, option, callback) {
};
}

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;
if (!options.args['branch-A']) {
var cwd = commander.cwd || process.cwd();
var remoteBranches = gitremote.getBaseBranches(cwd);
var remoteBranchLength = remoteBranches.length;
if (remoteBranchLength === 0) {
console.log('Not found base branch, rebase it before create PR/MR.');
return;
// remoteBranches = gitremote.getRemoteBranches(cwd)
// .map(function(br) {
// return br.name;
// })
// .filter(function(name) {
// return name !== option.cwb;
// });
}
if (remoteBranchLength > 1) {
inquirer.prompt([{
name: 'remoteBranch',
type: 'list',
message: 'Choose remote brance to compare:',
choices: remoteBranches,
}]).then(function(answers) {
options.args['branch-A'] = answers.remoteBranch;
return callback(options);
});
return;
}
options.args['branch-A'] = remoteBranches[0];
}

break;
Expand Down
55 changes: 55 additions & 0 deletions lib/gitremote.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,58 @@ exports.getGitRootPath = function(cwd) {
return null;
}
};

var RE_REMOTE_BRANCH_NAME = /^(\w+)\/(.+)/; // "origin/branch-name"
exports.getRemoteBranches = function(cwd) {
var remoteBranches = child_process.execSync(
'git branch -r',
{cwd: cwd}
).toString()
.trim()
.split(/\r\n|\r|\n/)
.filter(function(branchName) {
return branchName &&
branchName.indexOf(' -> ') === -1; // ` origin/HEAD -> origin/master`
})
.map(function(branchName) {
var br = branchName.trim();
var m = RE_REMOTE_BRANCH_NAME.exec(br);
return {
fullName: br,
remote: m[1],
name: m[2],
};
});
return remoteBranches;
};

var RE_CURRENT_BASE_BRANCHES = /^[ -+]*\*/;
var RE_BASE_BRANCHE_NAME = /.*\[([^\]\^~]+).*/; // " +* [MS170216105211~2^2] test: fixed lint"
exports.getBaseBranches = function(cwd) {
var cwb = exports.getCurrentBranch(cwd);
var baseBranches = child_process.execSync(
'git show-branch --no-color',
{cwd: cwd}
).toString()
.trim()
.split(/\r\n|\r|\n/)
.filter(function(line) {
return RE_CURRENT_BASE_BRANCHES.test(line);
})
.map(function(line) {
var m = RE_BASE_BRANCHE_NAME.exec(line);
return m ? m[1] : null;
})
.filter(function(branchName) {
return branchName !== cwb;
});
return baseBranches.length >= 0 ? [baseBranches[0]] : [];
// return unique(baseBranches);
};

// function unique(array) {
// return array.filter(function(item, index) {
// var firstIndex = array.indexOf(item);
// return firstIndex === index;
// });
// }

0 comments on commit b10d5cb

Please sign in to comment.