Skip to content

Commit 3e5b949

Browse files
committed
Use git rev-parse --git-dir to find a git dir
1 parent 51c309c commit 3e5b949

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

lib/git-hooks.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var path = require('path');
22
var util = require('util');
33
var spawn = require('child_process').spawn;
4+
var execSync = require('child_process').execSync;
45
var fs = require('fs');
56
var fsHelpers = require('./fs-helpers');
67

@@ -171,27 +172,17 @@ function spawnHook(hookName, args) {
171172
}
172173

173174
/**
174-
* Returns the closest git directory.
175-
* It starts looking from the current directory and does it up to the fs root.
176-
* It returns undefined in case where the specified directory isn't found.
175+
* Runs git rev-parse to find the git directory of the currentPath.
177176
*
178177
* @param {String} [currentPath] Current started path to search.
179178
* @returns {String|undefined}
180179
*/
181180
function getClosestGitPath(currentPath) {
182-
currentPath = currentPath || process.cwd();
183-
184-
var dirnamePath = path.join(currentPath, '.git');
185-
186-
if (fsHelpers.exists(dirnamePath)) {
187-
return dirnamePath;
188-
}
189-
190-
var nextPath = path.resolve(currentPath, '..');
191-
192-
if (nextPath === currentPath) {
193-
return;
181+
try {
182+
var result = execSync('git rev-parse --git-dir', {cwd: currentPath}).toString();
183+
return result.replace(/\n/g, '');
184+
} catch (error) {
185+
// No git dir?
186+
return undefined;
194187
}
195-
196-
return getClosestGitPath(nextPath);
197188
}

0 commit comments

Comments
 (0)