-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
gitRevParse.ts
39 lines (33 loc) 路 1.04 KB
/
gitRevParse.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
import cp from 'child_process'
import slash from 'slash'
export type GitRevParseResult = {
prefix: string
gitCommonDir: string
}
export function gitRevParse(cwd = process.cwd()): GitRevParseResult {
// https://github.com/typicode/husky/issues/580
// https://github.com/typicode/husky/issues/587
const { status, stderr, stdout } = cp.spawnSync(
'git',
['rev-parse', '--show-prefix', '--git-common-dir'],
{ cwd }
)
if (status !== 0) {
throw new Error(stderr.toString())
}
const [prefix, gitCommonDir] = stdout
.toString()
.split('\n')
.map(s => s.trim())
// Normalize for Windows
.map(slash)
// Git rev-parse returns unknown options as is.
// If we get --absolute-git-dir in the output,
// it probably means that an old version of Git has been used.
// There seem to be a bug with --git-common-dir that was fixed in 2.13.0.
// See issues above.
if (gitCommonDir === '--git-common-dir') {
throw new Error('Husky requires Git >= 2.13.0, please upgrade Git')
}
return { prefix, gitCommonDir }
}