<commit_before>// bepo.js

var url = require('url');
var fs = require('fs');
var debug = require('debug')('lib:bepo');
var series = require('array-series');
var gits = require('quick-git');

// Pass in the settings to get a new Repo object
function Repo(obj) {
  for (var key in obj) {
    this[key] = obj[key];
  }

  this.git = new Git(this.worktree)
}

var folders = ['media', '_posts'];
var comMsg = ["'gitpub posted a new post'"];

Repo.prototype.clone = function(cb) {

  // TODO Audit how safe this is....
  // Caution!  Passwords!
  //
  debug('Cloning to ' + this.worktree);

  var repoUrl = url.parse(this.repo);

  // TODO: Dont use env vars
  if (process.env.ENV !== 'development') {
    repoUrl.auth = process.env.USERNAME + ':' + process.env.PASSWORD;
  }

  var clone = ['git clone', url.format(repoUrl), this.worktree].join(' ');
  cp.exec(clone, {
    timeout: 15000
  }, function cloneCb(err, stdout, stderr) {
    if (err && cb) cb(err + stderr);
    if (stdout) debug(stdout);
    debug('Done cloning ' + this.repo)
    cb();
  }.bind(this));
};

Repo.prototype.pull = function(cb) {

  debug('Pulling ' + this.worktree);

  this.git(['pull', this.remoteBranch], {
    timeout: 15000
  }, function pullCb(err, stdout, stderr) {
    if (err) cb(err);
    if (stdout) debug(stdout);
    debug('Finished pulling repo');
    cb();
  })
}

Repo.prototype.setUser = function(cb) {
  debug(this.repo + ' User: ' + this.name || 'GitPub');

  this.git(['config', 'user.name', this.name || 'GitPub'],
    function setUserCb(err, stdout, stderr) {
      if (err) cb(err);
      if (stdout) debug(stdout);
      debug('User Set');
      cb();
    }
  )
};

Repo.prototype.setEmail = function(cb) {
  debug(this.repo + ' email: ' + this.email);

  this.git(['config', 'user.email', this.email],
    function setEmailCb(err, stdout, stderr) {
      if (err) cb(err);
      if (stdout) debug(stdout);
      debug('Email Set');
      cb();
    }
  )
};

Repo.prototype.add = function(files) {
  // folders to commit:
  // folders = ['media', '_posts/ownyourgram']
  var add = ['add'].concat(files || ['-A'])
  return function addCls(cb) {
    this.git(add, function addCb(err, stdout, stderr) {
      if (err) cb(err);
      if (stdout) debug(stdout);
      debug('Added ' + files || '-A');
      cb();
    })
  }.bind(this);
};

Repo.prototype.commit = function(msg) {
  // comMsg: commit message
  // pitfall: "'Ownyourgram posted a file'"
  // The string must be delimited
  return function commitCls(cb) {
    this.git(['commit', '-m', msg],
      function commitCb(err, stdout, stderr) {
        if (err) cb(err);
        if (stdout) debug(stdout);
        debug('Changes committed');
        debug(msg);
        cb();
      })
  }.bind(this);
}

Repo.prototype.push = function(cb) {
  debug('Pushing to ' + this.remoteBranch);

  this.git(['push', this.remoteBranch], function pushCb(err, stdout, stderr) {
    if (err) cb(err);
    if (stdout) debug(stdout);
    debug('Push Succeeded');
    cb();
  })
};

Repo.prototype.publish = function(cb) {
  series([
    this.pull,
    this.add,
    this.commit,
    this.push
  ], this, cb);
};

Repo.prototype.config = function(cb) {
  series([
    this.setUser,
    this.setEmail
  ], this, cb)
};


Repo.prototype.init = function(cb) {
  fs.exists(this.gitdir, function(exists) {
    if (exists === true) {
      debug(that.github.repo + ' aready cloned.  Pulling.');
      this.pull(cb);
    } else {
      async.series([
        this.clone,
        this.config,
      ], cb).bind(this)
    }
  }.bind(this));
};

module.exports = Repo;<commit_msg>First start at rewriting module.<commit_after>// bepo.js

var url = require('url');
var fs = require('fs');
var debug = require('debug')('lib:bepo');
var series = require('array-series');
var gits = require('quick-git');

// Pass in the settings to get a new Repo object
function Repo(obj) {
  var self;
  var git = gits(obj.worktree);
  self = git;

  function clone(cb) {
    if (obj.remote) git.clone(obj.remote, cb);
    else; // TODO new Error then cb(err);
  };
  self.clone = clone;

  function pull(cb) {
    git(['pull'], {
      timeout: 15000
    }, cb);
  };
  self.pull = pull;

  function setUser(cb) {
    git(['config', 'user.name', obj.name || 'bepo user'], cb)
  };
  self.setUser = setUser;

  function setEmail(cb) {
    git(['config', 'user.email', obj.email], cb)
  };
  self.setEmail = setEmail;

  function add(files, cb) {
    var add = ['add'].concat(files || ['-A'])
    git(add, cb);
  };
  self.add = add;

  return self;
}
{'fraction_non_alphanumeric': 0.18411235461926706, 'fraction_numerical': 0.0032916392363396972, 'mean_word_length': 2.915807560137457, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4449541', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1610, 'n_words': 384}