From d21ad4f164eae6c3c910a44f4424c917c125625a Mon Sep 17 00:00:00 2001 From: jonschlinkert Date: Tue, 29 Mar 2016 10:31:01 -0400 Subject: [PATCH] use `git-config-path` --- index.js | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 654d731..b1cfcda 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,10 @@ var fs = require('fs'); var path = require('path'); +var extend = require('extend-shallow'); +var configPath = require('git-config-path'); var ini = require('ini'); +var gitCache = {}; /** * Asynchronously parse a `.git/config` file. If only the callback is passed, @@ -40,12 +43,19 @@ function parse(options, cb) { options = options || {}; var filepath = parse.resolve(options); + if (gitCache[filepath]) { + cb(null, gitCache[filepath]); + return; + } + fs.stat(filepath, function(err, stat) { if (err) return cb(err); fs.readFile(filepath, 'utf8', function(err, str) { if (err) return cb(err); - cb(null, ini.parse(str)); + var parsed = ini.parse(str); + gitCache[filepath] = parsed; + cb(null, parsed); }); }); } @@ -67,9 +77,13 @@ function parse(options, cb) { parse.sync = function parseSync(options) { options = options || {}; var filepath = parse.resolve(options); + + if (gitCache[filepath]) { + return gitCache[filepath]; + } if (exists(filepath)) { var str = fs.readFileSync(filepath, 'utf8'); - return ini.parse(str); + return (gitCache[filepath] = ini.parse(str)); } return {}; }; @@ -79,17 +93,14 @@ parse.sync = function parseSync(options) { */ parse.resolve = function resolve(options) { - options = options || {}; - var cwd; if (typeof options === 'string') { - cwd = path.resolve(options); - options = {}; - } else if (options.cwd) { - cwd = options.cwd; - } else { - cwd = process.cwd(); + options = { path: options }; + } + var opts = extend({path: configPath()}, options); + if (opts.cwd) { + opts.path = path.resolve(opts.cwd, opts.path); } - return path.resolve(cwd, options.path || '.git/config'); + return path.resolve(opts.path); }; /**