const _ = require('lodash');

const path = require('path');

const branches = require('../../utils/branches');
const execution = require('../../utils/execution');

const command = path.parse(__filename).name;
const aliases = [ command.charAt(0) ];
const describe = 'Displays the commit\'s history';

const ARG_FORMAT = {
  name: 'format',
  alias: 'f',
  describe: 'The format name. Defaults to pretty',
  choices: _.keys(branches.LOG_FORMATS),
  type: 'string',
  default: 'pretty'
};

const ARG_SKIP = {
  name: 'skip',
  alias: 's',
  describe: 'Skip n commits before starting to show the commit output',
  type: 'integer',
  default: 0
};

const ARG_NUMBER = {
  name: 'number',
  alias: 'n',
  describe: 'Limit the number of commits to output',
  type: 'integer',
  default: 100
};

const ARG_REVERSE = {
  name: 'reverse',
  alias: 'r',
  describe: 'Output the commits chosen to be shown in reverse order.',
  type: 'boolean',
  default: false
};

const historyArgs = (yargs) => {
  return yargs
    .usage(`usage: gut ${path.parse(__filename).name} [options]`)
    .option(ARG_FORMAT.name, ARG_FORMAT)
    .option(ARG_SKIP.name, ARG_SKIP)
    .option(ARG_NUMBER.name, ARG_NUMBER)
    .option(ARG_REVERSE.name, ARG_REVERSE)
    .help();
};

const historyCommand = (args) => {
  const logFormat = branches.LOG_FORMATS[ args.format ];
  const skip = args.skip;
  const number = args.number;
  const reverse = args.reverse;

  const logCommand = `git log --skip ${skip} -n ${number} ${reverse ? '--reverse' : ''} --pretty=format:'${logFormat.format}'`;
  if (logFormat.postProcessing) {
    const logs = execution.execute(logCommand);
    execution.print(logFormat.postProcessing(logs));
  } else {
    execution.executeAndPipe(logCommand);
  }
};

module.exports = {
  ARG_FORMAT,
  ARG_NUMBER,
  ARG_REVERSE,
  ARG_SKIP,

  command,
  aliases,
  describe,
  builder: historyArgs,
  handler: historyCommand
};

@@ -943,24 +943,178 @@
 : false%0A%7D;%0A%0A
+const ARG_FROM_BASE_BRANCH = %7B%0A  name: 'from-base-branch',%0A  alias: 'b',%0A  describe: 'Audit all commits on top of the base branch',%0A  type: 'boolean'%0A%7D;%0A%0A
 const histor
@@ -1377,16 +1377,77 @@
 EVERSE)%0A
+    .option(ARG_FROM_BASE_BRANCH.name, ARG_FROM_BASE_BRANCH)%0A
     .hel
@@ -1570,13 +1570,25 @@
 args
-.skip
+%5B ARG_SKIP.name %5D
 ;%0A  
@@ -1610,15 +1610,27 @@
 args
-.number
+%5B ARG_NUMBER.name %5D
 ;%0A  
@@ -1653,16 +1653,191 @@
 args
-.reverse
+%5B ARG_REVERSE.name %5D;%0A  const fromBaseBranch = args%5B ARG_FROM_BASE_BRANCH.name %5D;%0A%0A  const commitsToInspect = fromBaseBranch%0A    ? _.size(branches.getBranchOnlyCommits())%0A    : number
 ;%0A%0A 
@@ -1885,22 +1885,32 @@
 p%7D -n $%7B
-number
+commitsToInspect
 %7D $%7Breve

Implement show history from base branch