forked from git-for-windows/setup-git-for-windows-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.ts
241 lines (226 loc) · 6.62 KB
/
git.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import * as core from '@actions/core'
import {ChildProcess, spawn} from 'child_process'
import {Octokit} from '@octokit/rest'
import {delimiter} from 'path'
import * as fs from 'fs'
// If present, do prefer the build agent's copy of Git
const externalsGitDir = `${process.env.AGENT_HOMEDIRECTORY}/externals/git`
const gitForWindowsRoot = 'C:/Program Files/Git'
const gitRoot = fs.existsSync(externalsGitDir)
? externalsGitDir
: gitForWindowsRoot
const gitForWindowsBinPaths = ['clangarm64', 'mingw64', 'mingw32', 'usr'].map(
p => `${gitRoot}/${p}/bin`
)
export const gitForWindowsUsrBinPath =
gitForWindowsBinPaths[gitForWindowsBinPaths.length - 1]
const gitExePath = `${gitRoot}/cmd/git.exe`
/*
* It looks a bit ridiculous to use 56 workers on a build agent that has only
* a two-core CPU, yet manual testing revealed that 64 workers would be _even
* better_. But at 92 workers, resources are starved so much that the checkout
* is not only much faster, but also fails.
*
* Let's stick with 56, which should avoid running out of resources, but still
* is much faster than, say, using only 2 workers.
*/
const GIT_CONFIG_PARAMETERS = `'checkout.workers=56'`
export function getArtifactMetadata(
flavor: string,
architecture: string
): {repo: string; artifactName: string} {
const repo = {
i686: 'git-sdk-32',
x86_64: 'git-sdk-64',
aarch64: 'git-sdk-arm64'
}[architecture]
if (repo === undefined) {
throw new Error(`Invalid architecture ${architecture} specified`)
}
const artifactName = `${repo}-${flavor}`
return {repo, artifactName}
}
async function clone(
url: string,
destination: string,
verbose: number | boolean,
cloneExtraOptions: string[] = []
): Promise<void> {
if (verbose) core.info(`Cloning ${url} to ${destination}`)
const child = spawn(
gitExePath,
[
'clone',
'--depth=1',
'--single-branch',
'--branch=main',
...cloneExtraOptions,
url,
destination
],
{
env: {
GIT_CONFIG_PARAMETERS
},
stdio: [undefined, 'inherit', 'inherit']
}
)
return new Promise<void>((resolve, reject) => {
child.on('close', code => {
if (code === 0) {
resolve()
} else {
reject(new Error(`git clone: exited with code ${code}`))
}
})
})
}
async function updateHEAD(
bareRepositoryPath: string,
headSHA: string
): Promise<void> {
const child = spawn(
gitExePath,
['--git-dir', bareRepositoryPath, 'update-ref', 'HEAD', headSHA],
{
env: {
GIT_CONFIG_PARAMETERS
},
stdio: [undefined, 'inherit', 'inherit']
}
)
return new Promise<void>((resolve, reject) => {
child.on('close', code => {
if (code === 0) {
resolve()
} else {
reject(new Error(`git: exited with code ${code}`))
}
})
})
}
export async function getViaGit(
flavor: string,
architecture: string,
githubToken?: string
): Promise<{
artifactName: string
id: string
download: (
outputDirectory: string,
verbose?: number | boolean
) => Promise<void>
}> {
const owner = 'git-for-windows'
const {repo, artifactName} = getArtifactMetadata(flavor, architecture)
const octokit = githubToken ? new Octokit({auth: githubToken}) : new Octokit()
let head_sha: string
if (flavor === 'minimal') {
const info = await octokit.actions.listWorkflowRuns({
owner,
repo,
workflow_id: 938271,
status: 'success',
branch: 'main',
event: 'push',
per_page: 1
})
head_sha = info.data.workflow_runs[0].head_sha
/*
* There was a GCC upgrade to v14.1 that broke the build with `DEVELOPER=1`,
* and `ci-artifacts` was not updated to test-build with `DEVELOPER=1` (this
* was fixed in https://github.com/git-for-windows/git-sdk-64/pull/83).
*
* Work around that by forcing the incorrectly-passing revision back to the
* last one before that GCC upgrade.
*/
if (head_sha === '5f6ba092f690c0bbf84c7201be97db59cdaeb891') {
head_sha = 'e37e3f44c1934f0f263dabbf4ed50a3cfb6eaf71'
}
} else {
const info = await octokit.repos.getBranch({
owner,
repo,
branch: 'main'
})
head_sha = info.data.commit.sha
}
const id = `${artifactName}-${head_sha}${head_sha === 'e37e3f44c1934f0f263dabbf4ed50a3cfb6eaf71' ? '-2' : ''}`
core.info(`Got commit ${head_sha} for ${repo}`)
return {
artifactName,
id,
download: async (
outputDirectory: string,
verbose: number | boolean = false
): Promise<void> => {
core.startGroup(`Cloning ${repo}`)
const partialCloneArg = flavor === 'full' ? [] : ['--filter=blob:none']
await clone(`https://github.com/${owner}/${repo}`, `.tmp`, verbose, [
'--bare',
...partialCloneArg
])
core.endGroup()
let child: ChildProcess
if (flavor === 'full') {
core.startGroup(`Checking out ${repo}`)
child = spawn(
gitExePath,
[`--git-dir=.tmp`, 'worktree', 'add', outputDirectory, head_sha],
{
env: {
GIT_CONFIG_PARAMETERS
},
stdio: [undefined, 'inherit', 'inherit']
}
)
} else {
await updateHEAD('.tmp', head_sha)
core.startGroup('Cloning build-extra')
await clone(
`https://github.com/${owner}/build-extra`,
'.tmp/build-extra',
verbose
)
core.endGroup()
core.startGroup(`Creating ${flavor} artifact`)
const traceArg = verbose ? ['-x'] : []
child = spawn(
`${gitForWindowsUsrBinPath}/bash.exe`,
[
...traceArg,
'.tmp/build-extra/please.sh',
'create-sdk-artifact',
`--architecture=${architecture}`,
`--out=${outputDirectory}`,
'--sdk=.tmp',
flavor
],
{
env: {
GIT_CONFIG_PARAMETERS,
COMSPEC:
process.env.COMSPEC ||
`${process.env.WINDIR}\\system32\\cmd.exe`,
LC_CTYPE: 'C.UTF-8',
CHERE_INVOKING: '1',
MSYSTEM: 'MINGW64',
PATH: `${gitForWindowsBinPaths.join(delimiter)}${delimiter}${process.env.PATH}`
},
stdio: [undefined, 'inherit', 'inherit']
}
)
}
return new Promise<void>((resolve, reject) => {
child.on('close', code => {
core.endGroup()
if (code === 0) {
fs.rm('.tmp', {recursive: true}, () => resolve())
} else {
reject(new Error(`process exited with code ${code}`))
}
})
})
}
}
}