Skip to content

Commit 3ac97b9

Browse files
committed
Make updateSrouces repo cloning cheaper
1 parent ed8fea5 commit 3ac97b9

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

scripts/updateSources.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// It will download third-party source code, modify it,
44
// and install it into the correct locations.
55

6-
import { execSync } from 'child_process'
6+
import { execFileSync } from 'child_process'
77
import { deepList, justFiles, makeNodeDisklet, navigateDisklet } from 'disklet'
88
import { existsSync, mkdirSync } from 'fs'
99
import { join } from 'path'
@@ -160,33 +160,51 @@ async function copySwift(): Promise<void> {
160160
function getRepo(name: string, uri: string, hash: string): void {
161161
const path = join(tmp, name)
162162

163-
// Either clone or fetch:
163+
// Clone cheaply, then fetch just the pinned commit if needed:
164164
if (!existsSync(path)) {
165165
console.log(`Cloning ${name}...`)
166-
loudExec(tmp, ['git', 'clone', uri, name])
167-
} else {
168-
// We might already have the right commit, so fetch lazily:
166+
loudExec(tmp, [
167+
'git',
168+
'clone',
169+
'--no-checkout',
170+
'--filter=blob:none',
171+
'--no-tags',
172+
uri,
173+
name
174+
])
175+
}
176+
177+
if (!hasCommit(path, hash)) {
178+
console.log(`Fetching ${name}...`)
169179
try {
170-
loudExec(path, ['git', 'fetch'])
180+
loudExec(path, ['git', 'fetch', '--depth=1', '--no-tags', 'origin', hash])
171181
} catch (error) {
172182
console.log(error)
173183
}
174184
}
175185

176186
// Checkout:
177187
console.log(`Checking out ${name}...`)
178-
execSync(`git checkout -f ${hash}`, {
179-
cwd: path,
180-
stdio: 'inherit',
181-
encoding: 'utf8'
182-
})
188+
loudExec(path, ['git', 'checkout', '-f', hash])
189+
}
190+
191+
function hasCommit(path: string, hash: string): boolean {
192+
try {
193+
execFileSync('git', ['cat-file', '-e', `${hash}^{commit}`], {
194+
cwd: path,
195+
stdio: 'ignore'
196+
})
197+
return true
198+
} catch (error) {
199+
return false
200+
}
183201
}
184202

185203
/**
186204
* Runs a command and displays its results.
187205
*/
188206
function loudExec(path: string, argv: string[]): void {
189-
execSync(argv.join(' '), {
207+
execFileSync(argv[0], argv.slice(1), {
190208
cwd: path,
191209
stdio: 'inherit',
192210
encoding: 'utf8'

0 commit comments

Comments
 (0)