Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
#!/usr/bin/env node

const program = require('commander');
const R = require('ramda');
const exec = require('child_process').exec;
const tree = require('./src/tree');
const diffStrToChanges = require('./src/diffStrToChanges');
const drawTree = require('./src/drawTree');
const print = require('./src/print');
const interactive = require('./src/interactive');

const execDiffandDrawTree = (branchB) => {
exec(`git diff --name-status --no-renames ${diffAgainstBranch}...${branchB}`, (err, stdout, stderr) => {
const execDiffandDrawTree = (branchA, branchB) => {
exec(`git diff --name-status --no-renames ${branchA}...${branchB}`, (err, stdout, stderr) => {
const changes = diffStrToChanges(stdout);
const treeData = tree(changes);
console.log(drawTree(treeData));
});
};

const diffAgainstBranch = process.argv[2] || 'master';
if (process.argv[3] === undefined) {
exec('git symbolic-ref --short HEAD', (err, stdout, stderr) => {
const checkedOutBranch = stdout.trim();
execDiffandDrawTree(checkedOutBranch);
});
} else {
const branchB = process.argv[3];
execDiffandDrawTree(branchB);
program
.usage('[options] [branchA [branchB]]')
.option('-i --interactive', 'Interactive mode', false)
.parse(process.argv);

if (program.interactive) {
console.log('do interactive');
interactive(execDiffandDrawTree);
} else {
const diffAgainstBranch = R.defaultTo('master', program.args[0]);
if (program.args.length <= 1) {
exec('git symbolic-ref --short HEAD', (err, stdout, stderr) => {
const checkedOutBranch = stdout.trim();
execDiffandDrawTree(diffAgainstBranch, checkedOutBranch);
});
} else {
const branchB = program.args[1];
execDiffandDrawTree(diffAgainstBranch, branchB);
}
}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
"jest": "^19.0.2"
},
"dependencies": {
"commander": "^2.11.0",
"fuzzy": "^0.1.3",
"inquirer": "^3.2.3",
"inquirer-autocomplete-prompt": "^0.11.1",
"ramda": "^0.24.1"
}
}
54 changes: 54 additions & 0 deletions src/interactive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const inquirer = require('inquirer');
const autocomplete = require('inquirer-autocomplete-prompt')
const fuzzy = require('fuzzy');
const exec = require('child_process').exec;

inquirer.registerPrompt('autocomplete', autocomplete);

let branches = [];

const fuzzySearch = (input, list) => {
input = input || '';
return fuzzy
.filter(input, list)
.map(el => el.original)
};

const getBranches = (answers, input) => {
return new Promise((resolve, reject) => {
exec('git branch -a --sort creatordate', (err, stdout, stderr) => {
branches = stdout
.split('\n')
.map(str => str.replace(/(.+->|\*)/, '').trim())
.filter(str => str && !/remotes/.test(str))
resolve(fuzzySearch(input, branches));
});
});
};

module.exports = (cb) => {
inquirer.prompt([
{
type: 'autocomplete',
name: 'diffAgainstBranch',
message: 'Which branch do you want to compare against?',
pageSize: 6,
source: getBranches,
},
{
type: 'autocomplete',
name: 'targetBranch',
message: 'What is the target branch?',
pageSize: 6,
source: (answers, input) => (
new Promise(resolve => {
branches = branches.filter(br => br !== answers.diffAgainstBranch);
resolve(fuzzySearch(input, branches));
})
),
},
]).then(function(answers) {
const { diffAgainstBranch, targetBranch} = answers;
cb(diffAgainstBranch, targetBranch);
})
}