var Q      = require('q');
var _      = require('lodash');
var spawn  = require('child_process').spawn;

module.exports = function(args, options) {
  var deferred = Q.defer();

  options = options || {};

  var defaults = {
    cwd:     process.cwd(),
    verbose: false
  };

  options = _.extend({}, defaults, options);

  var child = spawn('git', args, _.omit(options, 'verbose'));

  child.on('exit', function(code) {
    if (code === 128) {
      deferred.reject("git did not exit cleanly");
    } else {
      deferred.resolve(code);
    }
  });

  child.stdout.on('data', function(data) {
    deferred.notify(data.toString());
  });

  child.stderr.on('data', function(data) {
    deferred.reject(data.toString());
  });

  return deferred.promise;
};

var Q      = require('q');
var _      = require('lodash');
var spawn  = require('child_process').spawn;

module.exports = function(args, options) {
  var deferred = Q.defer();

  options = options || {};

  var defaults = {
    cwd:     process.cwd(),
    verbose: false
  };

  options = _.extend({}, defaults, options);

  var child = spawn('git', args, _.omit(options, 'verbose'));

  child.on('exit', function(code) {
    if (code === 128) {
      deferred.reject("git did not exit cleanly");
    } else {
      deferred.resolve(code);
    }
  });

  child.stdout.on('data', function(data) {
    deferred.notify(data.toString());
  });

  // TODO: Why does this return non-error output?
  // i.e. Cloning into...
  child.stderr.on('data', function(data) {
    deferred.notify(data.toString());
  });

  return deferred.promise;
};

Fix early cache resolve bug