forked from vercel/git-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-dir.js
40 lines (30 loc) · 832 Bytes
/
git-dir.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const path = require('path');
function getPathDir() {
if (process.env.INIT_CWD) {
return process.env.INIT_CWD;
}
const thisPkgName = require(path.join(__dirname, 'package.json')).name;
const pkgNamePath = path.join('node_modules', path.format(path.posix.parse(thisPkgName)));
const paths = process.env.PATH
.split(path.delimiter)
.filter(p => p.toLowerCase().indexOf(pkgNamePath) !== -1);
if (paths.length === 0) {
// Last-ditch attempt...
return process.cwd();
}
return paths[0];
}
function detectGitDir() {
let cur = getPathDir();
let lastCur;
let lastFound = cur;
do {
lastCur = cur;
if (path.basename(cur) === 'node_modules') {
lastFound = path.dirname(cur);
}
cur = path.dirname(cur);
} while (cur !== lastCur);
return path.join(lastFound, '.git');
}
module.exports = detectGitDir;