<commit_before>var defaults = {
  timeout: 6000,
}
var required = ['api_key', 'api_uri', 'repo', 'branch', 'dirname', 'host'];
var config = {
  missing: [],
};
var fs = require('fs');
var path = require('path');
var currentPath;
var colors = require('colors');
var ini = require('ini');
var os = require('os');

var recurse = function(fpath) {
  try {
    var next = require(fpath + path.sep + '.benchmarket.js');
    Object.keys(next).forEach(function(key) {
      if (!config[key]) config[key] = next[key];
    });
  } catch (e) {
  } finally {
    if (fpath.length > 6) {
      fpath = path.dirname(fpath);
      recurse(fpath);
    }
  }
}

// var findPackageDirAndRepoName = function() {
//   // walk up dir tree (limit 4) in search of package.json
//   try {
//     require HATE WINDOWS
//   }
// }

module.exports.load = function(caller) {
  if (caller === currentPath) {
    // new test, same testfile, same config
    return config;
  }

  var repoHome, gitDetails, repoName;

  // new test, different testfile, different config
  // recursively load config from .benchmarket.js files
  config = {
    missing: [],
  }
  currentPath = caller;
  recurse(path.dirname(caller));

  repoHome = module.exports.findRepoHome(path.dirname(caller));

  if (repoHome) {

    if (!config.dirname) {
      config.dirname = repoHome;
    }

    gitDetails = module.exports.loadGitDetails(repoHome);

    if (!config.repo) {
      try {
        repoName = gitDetails.config['remote "origin"'].url;
        // <EMAIL>:happner/benchmarket.git
        repoName = repoName.replace(/^git.*:/, '');
        // https://github.com/happner/benchmarket.git
        repoName = repoName.replace(/^https.*\.com\//, '');
        repoName = repoName.replace(/\.git$/, '');
        config.repo = repoName;
      } catch (e) {}
    }

    try {
      config.branch = gitDetails.branch || 'undefined';
    } catch (e) {
      config.branch = 'undefined';
    }
  }

  if (!config.host) {
    config.host = os.hostname();
  }

  Object.keys(defaults).forEach(function(key) {
    if (typeof config[key] === 'undefined') {
      config[key] = defaults[key];
    }
  });

  required.forEach(function(key) {
    if (typeof config[key] === 'undefined' || typeof config[key] == null) {
      config.missing.push(key);
    }
  });

  if (config.missing.length > 0) {
    console.error('benchmarket error: missing configs %j'.red, config.missing);
  }

  // env vars override
  if (typeof process.env.BENCHMARKET_API_KEY) {
    config.api_key = process.env.BENCHMARKET_API_KEY;
  }
  if (typeof process.env.BENCHMARKET_API_URI) {
    config.api_uri = process.env.BENCHMARKET_API_URI;
  }
  if (typeof process.env.BENCHMARKET_HOST) {
    config.host = process.env.BENCHMARKET_HOST;
  }

  return config;
}

module.exports.findRepoHome = function(fpath) {
  var files, next;
  try {
    files = fs.readdirSync(fpath);
  } catch (e) {
    console.error('error finding repo home', e);
    return false;
  }
  if (files.indexOf('.git') >= 0) return fpath;
  next = path.dirname(fpath);<commit_msg>move env loaders to before "missing detector"<commit_after>var defaults = {
  timeout: 6000,
}
var required = ['api_key', 'api_uri', 'repo', 'branch', 'dirname', 'host'];
var config = {
  missing: [],
};
var fs = require('fs');
var path = require('path');
var currentPath;
var colors = require('colors');
var ini = require('ini');
var os = require('os');

var recurse = function(fpath) {
  try {
    var next = require(fpath + path.sep + '.benchmarket.js');
    Object.keys(next).forEach(function(key) {
      if (!config[key]) config[key] = next[key];
    });
  } catch (e) {
  } finally {
    if (fpath.length > 6) {
      fpath = path.dirname(fpath);
      recurse(fpath);
    }
  }
}

// var findPackageDirAndRepoName = function() {
//   // walk up dir tree (limit 4) in search of package.json
//   try {
//     require HATE WINDOWS
//   }
// }

module.exports.load = function(caller) {
  if (caller === currentPath) {
    // new test, same testfile, same config
    return config;
  }

  var repoHome, gitDetails, repoName;

  // new test, different testfile, different config
  // recursively load config from .benchmarket.js files
  config = {
    missing: [],
  }
  currentPath = caller;
  recurse(path.dirname(caller));

  repoHome = module.exports.findRepoHome(path.dirname(caller));

  if (repoHome) {

    if (!config.dirname) {
      config.dirname = repoHome;
    }

    gitDetails = module.exports.loadGitDetails(repoHome);

    if (!config.repo) {
      try {
        repoName = gitDetails.config['remote "origin"'].url;
        // git@github.com:happner/benchmarket.git
        repoName = repoName.replace(/^git.*:/, '');
        // https://github.com/happner/benchmarket.git
        repoName = repoName.replace(/^https.*\.com\//, '');
        repoName = repoName.replace(/\.git$/, '');
        config.repo = repoName;
      } catch (e) {}
    }

    try {
      config.branch = gitDetails.branch || 'undefined';
    } catch (e) {
      config.branch = 'undefined';
    }
  }

  if (!config.host) {
    config.host = os.hostname();
  }

  Object.keys(defaults).forEach(function(key) {
    if (typeof config[key] === 'undefined') {
      config[key] = defaults[key];
    }
  });

  // env vars override
  if (typeof process.env.BENCHMARKET_API_KEY) {
    config.api_key = process.env.BENCHMARKET_API_KEY;
  }
  if (typeof process.env.BENCHMARKET_API_URI) {
    config.api_uri = process.env.BENCHMARKET_API_URI;
  }
  if (typeof process.env.BENCHMARKET_HOST) {
    config.host = process.env.BENCHMARKET_HOST;
  }

  required.forEach(function(key) {
    if (typeof config[key] === 'undefined' || typeof config[key] == null) {
      config.missing.push(key);
    }
  });

  if (config.missing.length > 0) {
    console.error('benchmarket error: missing configs %j'.red, config.missing);
  }

  return config;
}

module.exports.findRepoHome = function(fpath) {
  var files, next;
  try {
    files = fs.readdirSync(fpath);
  } catch (e) {
    console.error('error finding repo home', e);
    return false;
  }
  if (files.indexOf('.git') >= 0) return fpath;
  next = path.dirname(fpath);
{'fraction_non_alphanumeric': 0.16138983601233967, 'fraction_numerical': 0.002597824322130216, 'mean_word_length': 2.888888888888889, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '7607690', 'n_tokens_mistral': 2126, 'n_tokens_neox': 2085, 'n_words': 435}