Skip to content

Commit 73da366

Browse files
committed
Use git rev-parse --git-dir to find a git dir
1 parent 7004bdd commit 73da366

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

lib/git-hooks.js

+8-17
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

@@ -165,27 +166,17 @@ function spawnHook(hookName, args) {
165166
}
166167

167168
/**
168-
* Returns the closest git directory.
169-
* It starts looking from the current directory and does it up to the fs root.
170-
* It returns undefined in case where the specified directory isn't found.
169+
* Runs git rev-parse to find the git directory of the currentPath.
171170
*
172171
* @param {String} [currentPath] Current started path to search.
173172
* @returns {String|undefined}
174173
*/
175174
function getClosestGitPath(currentPath) {
176-
currentPath = currentPath || process.cwd();
177-
178-
var dirnamePath = path.join(currentPath, '.git');
179-
180-
if (fsHelpers.exists(dirnamePath)) {
181-
return dirnamePath;
182-
}
183-
184-
var nextPath = path.resolve(currentPath, '..');
185-
186-
if (nextPath === currentPath) {
187-
return;
175+
try {
176+
var result = execSync('git rev-parse --git-dir', {cwd: currentPath}).toString();
177+
return result.replace(/\n/g, '');
178+
} catch (error) {
179+
// No git dir?
180+
return undefined;
188181
}
189-
190-
return getClosestGitPath(nextPath);
191182
}

0 commit comments

Comments
 (0)