-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgittest.js
55 lines (49 loc) · 1.29 KB
/
gittest.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const { execSync } = require('child_process')
const setSafeCrlf = () => {
let crlf
try {
crlf = runAndReturnGitCommand('git config --get core.safecrlf')
crlf = crlf.trim()
} catch (error) {
crlf = 'notSet'
}
if (crlf !== 'false') {
runAndReturnGitCommand('git config --global core.safecrlf false')
}
return crlf
}
const resetSafeCrlf = (prevCrlf) => {
if (prevCrlf === 'notSet') {
runAndReturnGitCommand('git config --global --unset core.safecrlf')
} else if (prevCrlf !== 'false') {
runAndReturnGitCommand(`git config --global core.safecrlf ${prevCrlf}`)
}
}
const showCrlf = () => {
let crlf
try {
crlf = runAndReturnGitCommand('git config --get core.safecrlf')
crlf = crlf.trim()
} catch (error) {
crlf = 'notSet'
}
console.log(crlf)
}
const runAndReturnGitCommand = command => {
try {
const res = execSync(command)
return res.toString()
} catch (err) {
if (err.message.includes('Not a git repository')) {
throw new Error('Directory is not a Git repository')
}
throw err
}
}
showCrlf()
const cr = setSafeCrlf()
console.log('Expecting false: ')
showCrlf()
resetSafeCrlf(cr)
console.log(`Expecting ${cr}`)
showCrlf()