<commit_before>var fs = require("fs");
var nodePath = require("path");
var util = require("./util");

var files = module.exports = {
  inRepo: function(cwd) {
    return gitletDir(cwd) !== undefined;
  },

  assertInRepo: function() {
    if (!files.inRepo()) {
      throw new Error("not a Gitlet repository");
    }
  },

  pathFromRepoRoot: function(path) {
    return nodePath.relative(files.workingCopyPath(), nodePath.join(process.cwd(), path));
  },

  write: function(path, content) {
    files.writeFilesFromTree(util.assocIn({}, path.split(nodePath.sep).concat(content)), "/");
  },

  writeFilesFromTree: function(tree, prefix) {
    Object.keys(tree).forEach(function(name) {
      var path = nodePath.join(prefix, name);
      if (util.isString(tree[name])) {
        fs.writeFileSync(path, tree[name]);
      } else {
        if (!fs.existsSync(path)) {
          fs.mkdirSync(path, "777");
        }

        files.writeFilesFromTree(tree[name], path);
      }
    });
  },

  rmEmptyDirs: function(path) {
    if (fs.statSync(path).isDirectory()) {
      fs.readdirSync(path).forEach(function(c) { files.rmEmptyDirs(nodePath.join(path, c)); });
      if (fs.readdirSync(path).length === 0) {
        fs.rmdirSync(path);
      }
    }
  },

  read: function(path) {
    if (fs.existsSync(path)) {
      return fs.readFileSync(path, "utf8");
    }
  },

  gitletPath: function(path) {
    return nodePath.join(gitletDir(), path || "");
  },

  workingCopyPath: function(path) {
    return nodePath.join(workingCopyDir(), path || "");
  },

  lsRecursive: function(path) {
    if (!fs.existsSync(path)) {
      return [];
    } else if (fs.statSync(path).isFile()) {
      return [path];
    } else if (fs.statSync(path).isDirectory()) {
      var self = this;
      return fs.readdirSync(path).reduce(function(fileList, dirChild) {
        return fileList.concat(files.lsRecursive(nodePath.join(path, dirChild)));
      }, []);
    }
  },

  nestFlatTree: function(obj) {
    return Object.keys(obj).reduce(function(tree, wholePath) {
      return util.assocIn(tree, wholePath.split(nodePath.sep).concat(obj[wholePath]));
    }, {});
  },

  flattenNestedTree: function(tree, obj, prefix) {
    if (obj === undefined) { return files.flattenNestedTree(tree, {}, ""); }

    Object.keys(tree).forEach(function(dir) {
      var path = nodePath.join(prefix, dir);
      if (util.isString(tree[dir])) {
        obj[path] = tree[dir];
      } else {
        files.flattenNestedTree(tree[dir], obj, path);
      }
    });

    return obj;
  }
};

function gitletDir(dir) {
  if (dir === undefined) { return gitletDir(process.cwd()); }

  if (fs.existsSync(dir)) {
    var potentialConfigFile = nodePath.join(dir, "config");
    var potentialGitletPath = nodePath.join(dir, ".gitlet");
    if (fs.existsSync(potentialConfigFile) &&
        files.read(potentialConfigFile).match(/\[core\]/)) {
      return dir;
    } else if (fs.existsSync(potentialGitletPath)) {
      return potentialGitletPath;
    } else if (dir !== "/") {
      return gitletDir(nodePath.join(dir, ".."));
    }
  }
};

function workingCopyDir() {
  if (gitletDir() !== undefined) {
    return nodePath.join(gitletDir(), "..")
  }
};<commit_msg>No need for separate workingCopyDir()<commit_after>var fs = require("fs");
var nodePath = require("path");
var util = require("./util");

var files = module.exports = {
  inRepo: function(cwd) {
    return gitletDir(cwd) !== undefined;
  },

  assertInRepo: function() {
    if (!files.inRepo()) {
      throw new Error("not a Gitlet repository");
    }
  },

  pathFromRepoRoot: function(path) {
    return nodePath.relative(files.workingCopyPath(), nodePath.join(process.cwd(), path));
  },

  write: function(path, content) {
    files.writeFilesFromTree(util.assocIn({}, path.split(nodePath.sep).concat(content)), "/");
  },

  writeFilesFromTree: function(tree, prefix) {
    Object.keys(tree).forEach(function(name) {
      var path = nodePath.join(prefix, name);
      if (util.isString(tree[name])) {
        fs.writeFileSync(path, tree[name]);
      } else {
        if (!fs.existsSync(path)) {
          fs.mkdirSync(path, "777");
        }

        files.writeFilesFromTree(tree[name], path);
      }
    });
  },

  rmEmptyDirs: function(path) {
    if (fs.statSync(path).isDirectory()) {
      fs.readdirSync(path).forEach(function(c) { files.rmEmptyDirs(nodePath.join(path, c)); });
      if (fs.readdirSync(path).length === 0) {
        fs.rmdirSync(path);
      }
    }
  },

  read: function(path) {
    if (fs.existsSync(path)) {
      return fs.readFileSync(path, "utf8");
    }
  },

  gitletPath: function(path) {
    return nodePath.join(gitletDir(), path || "");
  },

  workingCopyPath: function(path) {
    return nodePath.join(nodePath.join(gitletDir(), ".."), path || "");
  },

  lsRecursive: function(path) {
    if (!fs.existsSync(path)) {
      return [];
    } else if (fs.statSync(path).isFile()) {
      return [path];
    } else if (fs.statSync(path).isDirectory()) {
      var self = this;
      return fs.readdirSync(path).reduce(function(fileList, dirChild) {
        return fileList.concat(files.lsRecursive(nodePath.join(path, dirChild)));
      }, []);
    }
  },

  nestFlatTree: function(obj) {
    return Object.keys(obj).reduce(function(tree, wholePath) {
      return util.assocIn(tree, wholePath.split(nodePath.sep).concat(obj[wholePath]));
    }, {});
  },

  flattenNestedTree: function(tree, obj, prefix) {
    if (obj === undefined) { return files.flattenNestedTree(tree, {}, ""); }

    Object.keys(tree).forEach(function(dir) {
      var path = nodePath.join(prefix, dir);
      if (util.isString(tree[dir])) {
        obj[path] = tree[dir];
      } else {
        files.flattenNestedTree(tree[dir], obj, path);
      }
    });

    return obj;
  }
};

function gitletDir(dir) {
  if (dir === undefined) { return gitletDir(process.cwd()); }

  if (fs.existsSync(dir)) {
    var potentialConfigFile = nodePath.join(dir, "config");
    var potentialGitletPath = nodePath.join(dir, ".gitlet");
    if (fs.existsSync(potentialConfigFile) &&
        files.read(potentialConfigFile).match(/\[core\]/)) {
      return dir;
    } else if (fs.existsSync(potentialGitletPath)) {
      return potentialGitletPath;
    } else if (dir !== "/") {
      return gitletDir(nodePath.join(dir, ".."));
    }
  }
};
{'fraction_non_alphanumeric': 0.17485481086171717, 'fraction_numerical': 0.0015696123057604772, 'mean_word_length': 3.164705882352941, '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': '7504781', 'n_tokens_mistral': 2164, 'n_tokens_neox': 2120, 'n_words': 353}