implement 'status' and a few others

This commit is contained in:
Yanick Champoux 2021-11-01 10:03:49 -04:00
parent e381929bc3
commit 249248f18e
5 changed files with 148 additions and 21 deletions

15
src/commands/done.js Normal file
View File

@ -0,0 +1,15 @@
const Git = require('simple-git');
const inquirer = require('inquirer');
const report = require('yurnalist');
const _ = require('lodash');
const { currentBranch } = require( '../utils');
module.exports = async (yargs) => {
const current = await currentBranch();
console.log(`marking branch '$current' as done`);
await Git().addConfig( `branch.${current}.mikado-done`, true );
}

View File

@ -2,6 +2,7 @@ const Git = require('simple-git');
const inquirer = require('inquirer'); const inquirer = require('inquirer');
const report = require('yurnalist'); const report = require('yurnalist');
const _ = require('lodash'); const _ = require('lodash');
const { defaults } = require('lodash/fp');
const u = require('updeep'); const u = require('updeep');
const columnify = require('columnify'); const columnify = require('columnify');
const chalk = require('chalk'); const chalk = require('chalk');
@ -15,37 +16,88 @@ module.exports = async (yargs) => {
const branches = {}; const branches = {};
const normalizeArray = value => Array.isArray(value)?value: [value].filter(x=>x);
const initBranch = config => defaults({
upstream: [],
dependencies: [],
},config);
const current = (await Git().raw('branch','--show-current')).replace("\n",''); const current = (await Git().raw('branch','--show-current')).replace("\n",'');
for( const branch in tree.branch ) { for( const branch in tree.branch ) {
const entry = tree.branch[branch]; const entry = tree.branch[branch];
if( 'mikado-done' in entry ) { if( !('mikado-upstream' in entry) ) continue;
let deps = entry['mikado-dependency']; branches[branch] = initBranch({
if(!Array.isArray(deps)) deps = [ deps ].filter(x=>x); name: branch,
upstream: normalizeArray(entry['mikado-upstream']),
branches[branch] = { base: entry['mikado-base'],
bullet: branch === current ? '*': ' ',
dependencies: deps.length > 0 ? deps.map( dep => chalk.magenta(dep) ): '',
name: chalk.blue.bold(branch),
upstream: entry['mikado-upstream'],
base: chalk.cyan.italic(entry['mikado-base'] ?? ''),
done: entry['mikado-done'], done: entry['mikado-done'],
});
} }
for ( const branch of Object.values(branches) ) {
for( const ups of branch['upstream'] ) {
if(!branches[ups]) branches[ups] = initBranch({
name: ups,
upstream: [],
dependencies: [],
});
branches[ups].dependencies.push(branch.name);
} }
} }
console.log( "\nMikado status\n" ); if( branches[ current ] ) branches[current].current = true;
const depColor = current => async ( dep ) => {
let color = branches[dep].done ? chalk.green: chalk.red;
let contains = await Git().raw('branch', '--contains', dep);
contains = contains.split("\n").map( x => x.trim().replace('* ','') );
if( contains.includes(current) )
color = color.underline;
return color(dep);
}
const upstreamColor = current => async ( up ) => {
let color = branches[up].done ? chalk.green: chalk.red;
let contains = await Git().raw('branch', '--contains', current);
contains = contains.split("\n").map( x => x.trim().replace('* ','') );
if( contains.includes(up) )
color = color.underline;
return color(up);
}
console.log( "\n=== Mikado status ===\n" );
const sorted = _.sortBy(Object.values(branches), ['current'])
for( const branch of sorted ) {
console.log( chalk.blue(branch.name), branch.current ? "(current branch)" : "", branch.done ? chalk.green.bold('done'):"" );
console.log( `\tbase: ${chalk.magenta(branch.base ?? '<none>')}` );
if(branch.upstream.length) {
const ups = await Promise.all(branch.upstream.map(upstreamColor(branch.name)));
console.log( "\tupstream:", ups.join(" ") );
}
if(branch.dependencies.length) {
const deps = await Promise.all(branch.dependencies.map(depColor(branch.name)));
console.log( "\tdependencies:", deps.join(" ") );
}
console.log("\n\n");
console.log(
columnify( Object.values(branches), {
showHeaders: false,
columns: [ 'bullet', 'name', 'base', 'dependencies' ],
config: {
base: { minWidth: 10 },
} }
} )
)
} }

12
src/commands/upstream Normal file
View File

@ -0,0 +1,12 @@
const Git = require('simple-git');
const inquirer = require('inquirer');
const report = require('yurnalist');
const _ = require('lodash');
const { currentBranch } = require( '../utils');
module.exports = async ({ branch, upstream }) => {
console.log(`adding '${upstream}' as upstream of '${branch}'`);
await Git().addConfig( `branch.${branch}.mikado-upstream`, upstream, true );
}

View File

@ -1,6 +1,13 @@
const yargs = require("yargs"); const yargs = require("yargs");
const new_branch = require("./commands/new_branch"); const new_branch = require("./commands/new_branch");
const status = require("./commands/status"); const status = require("./commands/status");
const done = require("./commands/done");
const upstream = require("./commands/upstream");
const { currentBranch } = require('./utils');
currentBranch().then(
currentBranch => {
yargs yargs
.scriptName("git-mikado") .scriptName("git-mikado")
@ -10,6 +17,35 @@ yargs
"show status of all mikado branches", "show status of all mikado branches",
status status
) )
.command(
"done [branch]",
"set branch as done",
(yargs) => {
return yargs
.positional("branch", {
describe: "branch to mark as done",
default: currentBranch
})
},
done
)
.command(
"upstream [upstream]",
"set branch as done",
(yargs) => {
return yargs
.positional("upstream", {
describe: "upstream branch to add",
})
.option("branch", {
alias: "b",
describe: "target branch",
default: currentBranch,
})
.demandOption(["upstream"]);
},
upstream
)
.command( .command(
"new [branch]", "new [branch]",
"create new mikado branch", "create new mikado branch",
@ -42,3 +78,5 @@ yargs
.help() .help()
.demandCommand(1, "") .demandCommand(1, "")
.strict().argv; .strict().argv;
});

10
src/utils.js Normal file
View File

@ -0,0 +1,10 @@
const Git = require('simple-git');
async function currentBranch() {
let branch = await Git().raw('branch', '--show-current');
return branch.replace("\n",'');
}
module.exports = {
currentBranch,
}