var async = require('async');
var execFile = require('child_process').execFile;

module.exports = {
  name: 'grep',
  description: 'runs git log -S across your repos, use -- to separate bosco options from git grep options',
  usage: '<search term>',
};

function grepRepo(bosco, args, repo, repoPath, callback) {
  var gitArgs = ['log', '-S'].concat(args);

  execFile('git', gitArgs, {
    cwd: repoPath,
  }, function(err, stdout) {
    if (err) return callback(err);

    var result = null;

    if (stdout) {
      bosco.log(repo.blue + ':\n' + stdout);
      result = {
        repo: repo,
        grep: stdout,
      };
    }

    callback(null, result);
  });
}

function cmd(bosco, args, next) {
  var repoPattern = bosco.options.repo;
  var repoRegex = new RegExp(repoPattern);

  var repos = bosco.getRepos();
  if (!repos) return bosco.error('You are repo-less :( You need to initialise bosco first, try \'bosco clone\'.');

  bosco.log('Running git log -S across all repos...');

  function grepRepos(callback) {
    async.mapLimit(repos, bosco.concurrency.network, function(repo, grepCallback) {
      if (!repo.match(repoRegex)) return grepCallback();

      var repoPath = bosco.getRepoPath(repo);

      grepRepo(bosco, args, repo, repoPath, function(err, result) {
        // err.code is 1 when nothing is found.
        if (err && err.code !== 1) bosco.error(err.message.substring(0, err.message.indexOf('\n')));
        grepCallback(null, result);
      });
    }, callback);
  }

  grepRepos(function(err, results) {
    if (err) bosco.error(err);
    if (next) next(err, results);
  });
}

module.exports.cmd = cmd;

@@ -102,20 +102,23 @@
  name: '
-grep
+history
 ',%0A  des
@@ -132,12 +132,78 @@
 n: '
-runs
+search for mention of %3Csearch term%3E in any previous commit, by running
  git
@@ -271,20 +271,19 @@
 rom git 
-grep
+log
  options
@@ -324,24 +324,33 @@
 unction 
-grepRepo
+searchRepoHistory
 (bosco, 
@@ -673,20 +673,23 @@
         
-grep
+history
 : stdout
@@ -1076,24 +1076,34 @@
 unction 
-grepRepo
+searchRepoHistorie
 s(callba
@@ -1176,20 +1176,23 @@
 n(repo, 
-grep
+history
 Callback
@@ -1236,20 +1236,23 @@
  return 
-grep
+history
 Callback
@@ -1309,24 +1309,33 @@
 %0A%0A      
-grepRepo
+searchRepoHistory
 (bosco, 
@@ -1537,20 +1537,23 @@
         
-grep
+history
 Callback
@@ -1607,16 +1607,26 @@
 %0A%0A  
-grepRepo
+searchRepoHistorie
 s(fu

clean up copy/paste issues