-
-
Notifications
You must be signed in to change notification settings - Fork 936
/
gitChecks.ts
48 lines (43 loc) · 1.18 KB
/
gitChecks.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
41
42
43
44
45
46
47
48
import execa from 'execa'
// git checks logic is from https://github.com/sindresorhus/np/blob/master/source/git-tasks.js
export async function isGitRepo () {
try {
await execa('git', ['rev-parse', '--git-dir'])
} catch (_: any) { // eslint-disable-line
return false
}
return true
}
export async function getCurrentBranch () {
try {
const { stdout } = await execa('git', ['symbolic-ref', '--short', 'HEAD'])
return stdout
} catch (_: any) { // eslint-disable-line
// Command will fail with code 1 if the HEAD is detached.
return null
}
}
export async function isWorkingTreeClean () {
try {
const { stdout: status } = await execa('git', ['status', '--porcelain'])
if (status !== '') {
return false
}
return true
} catch (_: any) { // eslint-disable-line
return false
}
}
export async function isRemoteHistoryClean () {
let history
try { // Gracefully handle no remote set up.
const { stdout } = await execa('git', ['rev-list', '--count', '--left-only', '@{u}...HEAD'])
history = stdout
} catch (_: any) { // eslint-disable-line
history = null
}
if (history && history !== '0') {
return false
}
return true
}