<commit_before>var File = require('vinyl');
var execSync = require('child_process').execSync;
var fs = require('fs');
var gulpFilter = require('gulp-filter');
var gulpIf = require('gulp-if');
var streamify = require('stream-array');
var stripBomBuf = require('strip-bom-buf');
var through2 = require('through2');
var util = require('util');
var path = require('path');
var yargs = require('yargs');

var hooks = [
  'pre-commit',
  'pre-push',
  'commit-msg'
];

function Gilp (gulp) {
  var gilp = this;
  this._gulp = gulp;
  this._installed = [];
  this._gulp.task('gilp-install', function (callback) {
    fs.readFile(path.join(__dirname, '../templates/hook.js'), 'utf8', function (err, template) {
      if (err) {
        return callback(err);
      }
      var directory = path.join(gilp.getBaseDirectory(), '.git/hooks');
      var installed = gilp._installed.concat();
      var saveNext = function () {
        var hook = installed.shift();
        if (hook === undefined) {
          return callback();
        }
        var content = util.format(template, hook);
        var filepath = path.join(directory, hook);
        fs.writeFile(filepath, content, function (err) {
          if (err) {
            return callback(err);
          }
          fs.chmodSync(filepath, '700');
          saveNext();
        });
      };
      saveNext();
    });
  });
}

Gilp.prototype.hook = function (name, dep, fn) {
  if (hooks.indexOf(name) === -1) {
    throw new Error(util.format('Unsupported hook: %s', name));
  }

  if (!fn && typeof dep === 'function') {
    fn = dep;
    dep = undefined;
  }
  dep = dep || [];
  fn = fn || function () {};

  this._installed.push(name);
  this._gulp.task(util.format('gilp-%s', name), dep, fn);
};

Gilp.prototype.getBaseDirectory = function () {
  return execSync('git rev-parse --show-toplevel').toString().trim();
};

Gilp.prototype.getBranch = function () {
  return execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
};

Gilp.prototype.getArgs = function () {
  return yargs.parse(yargs.argv.gilpArgs || []);
};

Gilp.prototype._getStream = function (objectFormat, paths, glob) {
  return streamify(paths)
    .pipe(through2.obj(function (path, enc, callback) {
      var contents = execSync(util.format('git cat-file blob %s', util.format(objectFormat, path)));
      var file = new File({
        gilp: true,
        path: path,
        contents: stripBomBuf(contents)
      });
      this.push(file);
      callback();
    }))
    .pipe(gulpIf(glob !== undefined, gulpFilter(glob || [])));
};

Gilp.prototype.srcFromStaged = function (glob) {
  return this._getStream(
    ':0:%s',
    execSync('git diff --cached --name-only --diff-filter=ACM')
      .toString()
      .trim()
      .split('\n'),
    glob
  );
};

Gilp.prototype.srcFromCommit = function (commit, glob) {
  return this._getStream(
    util.format('%s:%%s', commit),
    execSync(util.format('git diff --name-only --diff-filter=ACM %s^ %s', commit, commit))
      .toString()
      .trim()
      .split('\n'),
    glob
  );
};

module.exports = function (gulp) {
  return new Gilp(gulp);
};<commit_msg>Remove empty paths from git diff.<commit_after>var File = require('vinyl');
var execSync = require('child_process').execSync;
var fs = require('fs');
var gulpFilter = require('gulp-filter');
var gulpIf = require('gulp-if');
var streamify = require('stream-array');
var stripBomBuf = require('strip-bom-buf');
var through2 = require('through2');
var util = require('util');
var path = require('path');
var yargs = require('yargs');

var hooks = [
  'pre-commit',
  'pre-push',
  'commit-msg'
];

function Gilp (gulp) {
  var gilp = this;
  this._gulp = gulp;
  this._installed = [];
  this._gulp.task('gilp-install', function (callback) {
    fs.readFile(path.join(__dirname, '../templates/hook.js'), 'utf8', function (err, template) {
      if (err) {
        return callback(err);
      }
      var directory = path.join(gilp.getBaseDirectory(), '.git/hooks');
      var installed = gilp._installed.concat();
      var saveNext = function () {
        var hook = installed.shift();
        if (hook === undefined) {
          return callback();
        }
        var content = util.format(template, hook);
        var filepath = path.join(directory, hook);
        fs.writeFile(filepath, content, function (err) {
          if (err) {
            return callback(err);
          }
          fs.chmodSync(filepath, '700');
          saveNext();
        });
      };
      saveNext();
    });
  });
}

Gilp.prototype.hook = function (name, dep, fn) {
  if (hooks.indexOf(name) === -1) {
    throw new Error(util.format('Unsupported hook: %s', name));
  }

  if (!fn && typeof dep === 'function') {
    fn = dep;
    dep = undefined;
  }
  dep = dep || [];
  fn = fn || function () {};

  this._installed.push(name);
  this._gulp.task(util.format('gilp-%s', name), dep, fn);
};

Gilp.prototype.getBaseDirectory = function () {
  return execSync('git rev-parse --show-toplevel').toString().trim();
};

Gilp.prototype.getBranch = function () {
  return execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
};

Gilp.prototype.getArgs = function () {
  return yargs.parse(yargs.argv.gilpArgs || []);
};

Gilp.prototype._getStream = function (objectFormat, paths, glob) {
  return streamify(paths)
    .pipe(through2.obj(function (path, enc, callback) {

      var contents = execSync(util.format('git cat-file blob %s', util.format(objectFormat, path)));
      var file = new File({
        gilp: true,
        path: path,
        contents: stripBomBuf(contents)
      });
      this.push(file);
      callback();
    }))
    .pipe(gulpIf(glob !== undefined, gulpFilter(glob || [])));
};

Gilp.prototype.srcFromStaged = function (glob) {
  return this._getStream(
    ':0:%s',
    execSync('git diff --cached --name-only --diff-filter=ACM')
      .toString()
      .trim()
      .split('\n')
      .filter(function (path) { return path !== '' }),
    glob
  );
};

Gilp.prototype.srcFromCommit = function (commit, glob) {
  return this._getStream(
    util.format('%s:%%s', commit),
    execSync(util.format('git diff --name-only --diff-filter=ACM %s^ %s', commit, commit))
      .toString()
      .trim()
      .split('\n')
      .filter(function (path) { return path !== '' }),
    glob
  );
};

module.exports = function (gulp) {
  return new Gilp(gulp);
};
{'fraction_non_alphanumeric': 0.17955930614158463, 'fraction_numerical': 0.0028129395218002813, 'mean_word_length': 2.9119804400977993, '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': '5122468', 'n_tokens_mistral': 2117, 'n_tokens_neox': 2083, 'n_words': 459}