-
Notifications
You must be signed in to change notification settings - Fork 149
/
git-environment.ts
130 lines (115 loc) · 3.75 KB
/
git-environment.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import * as path from 'path'
function resolveEmbeddedGitDir(): string {
const s = path.sep
return path
.resolve(__dirname, '..', '..', 'git')
.replace(/[\\\/]app.asar[\\\/]/, `${s}app.asar.unpacked${s}`)
}
/**
* Find the path to the embedded Git environment.
*
* If a custom Git directory path is defined as the `LOCAL_GIT_DIRECTORY` environment variable, then
* returns with it after resolving it as a path.
*/
function resolveGitDir(): string {
if (process.env.LOCAL_GIT_DIRECTORY) {
return path.resolve(process.env.LOCAL_GIT_DIRECTORY)
} else {
return resolveEmbeddedGitDir()
}
}
/**
* Find the path to the embedded Git binary.
*/
function resolveGitBinary(): string {
const gitDir = resolveGitDir()
if (
process.platform === 'darwin' ||
process.platform === 'linux' ||
process.platform === 'android'
) {
return path.join(gitDir, 'bin', 'git')
} else if (process.platform === 'win32') {
return path.join(gitDir, 'cmd', 'git.exe')
}
throw new Error('Git not supported on platform: ' + process.platform)
}
/**
* Find the path to the embedded git exec path.
*
* If a custom git exec path is given as the `GIT_EXEC_PATH` environment variable,
* then it returns with it after resolving it as a path.
*/
function resolveGitExecPath(): string {
if (process.env.GIT_EXEC_PATH) {
return path.resolve(process.env.GIT_EXEC_PATH)
}
const gitDir = resolveGitDir()
if (
process.platform === 'darwin' ||
process.platform === 'linux' ||
process.platform === 'android'
) {
return path.join(gitDir, 'libexec', 'git-core')
} else if (process.platform === 'win32') {
return path.join(gitDir, 'mingw64', 'libexec', 'git-core')
}
throw new Error('Git not supported on platform: ' + process.platform)
}
/**
* Setup the process environment before invoking Git.
*
* This method resolves the Git executable and creates the array of key-value
* pairs which should be used as environment variables.
*
* @param additional options to include with the process
*/
export function setupEnvironment(
environmentVariables: Object
): { env: Object; gitLocation: string } {
const gitLocation = resolveGitBinary()
let envPath: string = process.env.PATH || ''
const gitDir = resolveGitDir()
if (process.platform === 'win32') {
envPath = `${gitDir}\\mingw64\\bin;${gitDir}\\mingw64\\usr\\bin;${envPath}`
}
const env = Object.assign(
{},
process.env,
{
GIT_EXEC_PATH: resolveGitExecPath(),
PATH: envPath
},
environmentVariables
)
if (process.platform === 'win32') {
// while reading the environment variable is case-insensitive
// you can create a hash with multiple values, which means the
// wrong value might be used when spawning the child process
//
// this ensures we only ever supply one value for PATH
if (env.Path) {
delete env.Path
}
}
if (process.platform === 'darwin' || process.platform === 'linux') {
// templates are used to populate your .git folder
// when a repository is initialized locally
const templateDir = `${gitDir}/share/git-core/templates`
env.GIT_TEMPLATE_DIR = templateDir
}
if (process.platform === 'linux') {
// when building Git for Linux and then running it from
// an arbitrary location, you should set PREFIX for the
// process to ensure that it knows how to resolve things
env.PREFIX = gitDir
if (!env.GIT_SSL_CAINFO && !env.LOCAL_GIT_DIRECTORY) {
// use the SSL certificate bundle included in the distribution only
// when using embedded Git and not providing your own bundle
const distDir = resolveEmbeddedGitDir()
const sslCABundle = `${distDir}/ssl/cacert.pem`
env.GIT_SSL_CAINFO = sslCABundle
}
}
return { env, gitLocation }
}