diff --git a/lib/git-hooks.js b/lib/git-hooks.js index 1f98e63..aa5b9c2 100644 --- a/lib/git-hooks.js +++ b/lib/git-hooks.js @@ -1,6 +1,7 @@ var path = require('path'); var util = require('util'); var spawn = require('child_process').spawn; +var execSync = require('child_process').execSync; var fs = require('fs'); var fsHelpers = require('./fs-helpers'); @@ -165,27 +166,21 @@ function spawnHook(hookName, args) { } /** - * Returns the closest git directory. - * It starts looking from the current directory and does it up to the fs root. - * It returns undefined in case where the specified directory isn't found. + * Runs git rev-parse to find the git directory of the currentPath. * * @param {String} [currentPath] Current started path to search. * @returns {String|undefined} */ function getClosestGitPath(currentPath) { - currentPath = currentPath || process.cwd(); - - var dirnamePath = path.join(currentPath, '.git'); - - if (fsHelpers.exists(dirnamePath)) { - return dirnamePath; - } - - var nextPath = path.resolve(currentPath, '..'); - - if (nextPath === currentPath) { - return; + try { + var result = execSync('git rev-parse --absolute-git-dir', {cwd: currentPath}).toString(); + return result.replace(/\n/g, ''); + } catch (error) { + if (error.status === 128) { + // No git dir? + return undefined; + } + // Throw any other errors + throw error; } - - return getClosestGitPath(nextPath); }