-
-
Notifications
You must be signed in to change notification settings - Fork 999
/
resolveGitDir.ts
40 lines (34 loc) 路 1.18 KB
/
resolveGitDir.ts
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
import findUp from 'find-up'
import fs from 'fs'
import path from 'path'
export default function(cwd: string | undefined): string | null {
const foundPath = findUp.sync('.git', { cwd })
if (foundPath) {
const stats = fs.lstatSync(foundPath)
// If it's a .git file resolve path
if (stats.isFile()) {
// Expect following format
// git: pathToGit
// On Windows pathToGit can contain ':' (example "gitdir: C:/Some/Path")
const gitFileData = fs.readFileSync(foundPath, 'utf-8')
const gitDir = gitFileData
.split(':')
.slice(1)
.join(':')
.trim()
const resolvedGitDir = path.resolve(path.dirname(foundPath), gitDir)
// Fix: For git-worktree check if commondir file exists and return that path
const pathCommonDir = path.join(resolvedGitDir, 'commondir')
if (fs.existsSync(pathCommonDir)) {
const commondir = fs.readFileSync(pathCommonDir, 'utf-8').trim()
const resolvedCommonGitDir = path.join(resolvedGitDir, commondir)
return resolvedCommonGitDir
}
//
return resolvedGitDir
}
// Else return path to .git directory
return foundPath
}
return null
}