-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcheckout.ts
More file actions
596 lines (576 loc) · 18.4 KB
/
checkout.ts
File metadata and controls
596 lines (576 loc) · 18.4 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
import { FileSystem } from '../models/FileSystem'
import { Cache } from '../models/Cache'
import { STAGE } from '../commands/STAGE'
import { TREE } from '../commands/TREE'
import { WORKDIR } from '../commands/WORKDIR'
import { _walk } from '../commands/walk'
import { CheckoutConflictError } from '../errors/CheckoutConflictError'
import { CommitNotFetchedError } from '../errors/CommitNotFetchedError'
import { InternalError } from '../errors/InternalError'
import { NotFoundError } from '../errors/NotFoundError'
import { GitConfigManager } from '../managers/GitConfigManager'
import { GitIndexManager } from '../managers/GitIndexManager'
import { GitRefManager } from '../managers/GitRefManager'
import { _readObject as readObject } from '../storage/readObject'
import { worthWalking } from '../utils/worthWalking'
import { ProgressCallback } from '../models/ProgressCallback'
type CheckoutParams = {
fs: FileSystem
cache: Cache
onProgress?: ProgressCallback
dir: string
gitdir: string
remote?: string
ref: string
filepaths?: string[]
noCheckout: boolean
noUpdateHead?: boolean
dryRun?: boolean
force?: boolean
track?: boolean
}
type AnalyzeParams = {
fs: FileSystem
cache: Cache
onProgress?: ProgressCallback
dir: string
gitdir: string
ref: string
force?: boolean
filepaths?: string[]
}
/**
* @param {CheckoutParams} args
* @returns {Promise<void>} Resolves successfully when filesystem operations are complete
* @internal
*/
export async function _checkout({
fs,
cache,
onProgress,
dir,
gitdir,
remote,
ref,
filepaths,
noCheckout,
noUpdateHead,
dryRun,
force,
track = true
}: CheckoutParams): Promise<void> {
// Get tree oid
let oid: string
try {
oid = await GitRefManager.resolve({ fs, gitdir, ref })
// TODO: Figure out what to do if both 'ref' and 'remote' are specified, ref already exists,
// and is configured to track a different remote.
} catch (err) {
if (ref === 'HEAD') throw err
// If `ref` doesn't exist, create a new remote tracking branch
// Figure out the commit to checkout
const remoteRef = `${remote}/${ref}`
oid = await GitRefManager.resolve({
fs,
gitdir,
ref: remoteRef,
})
if (track) {
// Set up remote tracking branch
const config = await GitConfigManager.get({ fs, gitdir })
config.set(`branch.${ref}.remote`, remote!)
config.set(`branch.${ref}.merge`, `refs/heads/${ref}`)
await GitConfigManager.save({ fs, gitdir, config })
}
// Create a new branch that points at that same commit
await GitRefManager.writeRef({
fs,
gitdir,
ref: `refs/heads/${ref}`,
value: oid,
})
}
// Update working dir
if (!noCheckout) {
let ops: string[]
// First pass - just analyze files (not directories) and figure out what needs to be done
try {
ops = await analyze({
fs,
cache,
onProgress,
dir,
gitdir,
ref,
force,
filepaths,
})
} catch (err) {
// Throw a more helpful error message for this common mistake.
if (err instanceof NotFoundError && err.data.what === oid) {
throw new CommitNotFetchedError(ref, oid)
} else {
throw err
}
}
// Report conflicts
const conflicts = ops
.filter(([method]) => method === 'conflict')
.map(([method, fullpath]) => fullpath)
if (conflicts.length > 0) {
throw new CheckoutConflictError(conflicts)
}
// Collect errors
const errors = ops
.filter(([method]) => method === 'error')
.map(([method, fullpath]) => fullpath)
if (errors.length > 0) {
throw new InternalError(errors.join(', '))
}
if (dryRun) {
// Since the format of 'ops' is in flux, I really would rather folk besides myself not start relying on it
// return ops
return
}
// Second pass - execute planned changes
// The cheapest semi-parallel solution without computing a full dependency graph will be
// to just do ops in 4 dumb phases: delete files, delete dirs, create dirs, write files
let count = 0
const total = ops.length
await GitIndexManager.acquire({ fs, gitdir, cache }, async function(index) {
await Promise.all(
ops
.filter(
([method]) => method === 'delete' || method === 'delete-index'
)
.map(async function([method, fullpath]) {
const filepath = `${dir}/${fullpath}`
if (method === 'delete') {
await fs.rm(filepath)
}
index.delete({ filepath: fullpath })
if (onProgress) {
await onProgress({
phase: 'Updating workdir',
loaded: ++count,
total,
})
}
})
)
})
// Note: this is cannot be done naively in parallel
await GitIndexManager.acquire({ fs, gitdir, cache }, async function(index) {
for (const [method, fullpath] of ops) {
if (method === 'rmdir' || method === 'rmdir-index') {
const filepath = `${dir}/${fullpath}`
try {
if (method === 'rmdir-index') {
index.delete({ filepath: fullpath })
}
await fs.rm(filepath)
if (onProgress) {
await onProgress({
phase: 'Updating workdir',
loaded: ++count,
total,
})
}
} catch (e: any) {
if (e.code === 'ENOTEMPTY') {
console.log(
`Did not delete ${fullpath} because directory is not empty`
)
} else {
throw e
}
}
}
}
})
await Promise.all(
ops
.filter(([method]) => method === 'mkdir' || method === 'mkdir-index')
.map(async function([_, fullpath]) {
const filepath = `${dir}/${fullpath}`
await fs.mkdir(filepath)
if (onProgress) {
await onProgress({
phase: 'Updating workdir',
loaded: ++count,
total,
})
}
})
)
await GitIndexManager.acquire({ fs, gitdir, cache }, async function(index) {
const writeOps = ops.filter(
([method]) =>
method === 'create' ||
method === 'create-index' ||
method === 'update' ||
method === 'mkdir-index'
)
// Process files in small batches to balance I/O concurrency with memory usage.
// Full Promise.all would OOM on large packfiles; purely sequential loses I/O overlap.
const BATCH_SIZE = 10
for (let i = 0; i < writeOps.length; i += BATCH_SIZE) {
const batch = writeOps.slice(i, i + BATCH_SIZE)
await Promise.all(batch.map(async ([method, fullpath, oid, mode, chmod]) => {
const modeNum = Number(mode)
const filepath = `${dir}/${fullpath}`
try {
if (method !== 'create-index' && method !== 'mkdir-index') {
const { object } = await readObject({ fs, cache, gitdir, oid })
if (chmod) {
// Note: the mode option of fs.write only works when creating files,
// not updating them. Since the `fs` plugin doesn't expose `chmod` this
// is our only option.
await fs.rm(filepath)
}
if (modeNum === 0o100644) {
// regular file
await fs.write(filepath, object)
} else if (modeNum === 0o100755) {
// executable file
await fs.write(filepath, object, { mode: 0o777 })
} else if (modeNum === 0o120000) {
// symlink
await fs.writelink(filepath, object)
} else {
throw new InternalError(
`Invalid mode 0o${modeNum.toString(8)} detected in blob ${oid}`
)
}
}
const stats = (await fs.lstat(filepath))!
// We can't trust the executable bit returned by lstat on Windows,
// so we need to preserve this value from the TREE.
// TODO: Figure out how git handles this internally.
if (modeNum === 0o100755) {
stats.mode = 0o755
}
// Submodules are present in the git index but use a unique mode different from trees
if (method === 'mkdir-index') {
stats.mode = 0o160000
}
index.insert({
filepath: fullpath,
stats,
oid,
})
if (onProgress) {
await onProgress({
phase: 'Updating workdir',
loaded: ++count,
total,
})
}
} catch (e) {
console.log(e)
}
}))
}
})
}
// Update HEAD
if (!noUpdateHead) {
const fullRef = await GitRefManager.expand({ fs, gitdir, ref })
if (fullRef.startsWith('refs/heads')) {
await GitRefManager.writeSymbolicRef({
fs,
gitdir,
ref: 'HEAD',
value: fullRef,
})
} else {
// detached head
await GitRefManager.writeRef({ fs, gitdir, ref: 'HEAD', value: oid })
}
}
}
async function analyze({
fs,
cache,
onProgress,
dir,
gitdir,
ref,
force,
filepaths,
}: AnalyzeParams): Promise<string[]> {
let count = 0
return _walk({
fs,
cache,
dir,
gitdir,
trees: [TREE({ ref }), WORKDIR(), STAGE()],
map: async function(fullpath, [commit, workdir, stage]) {
if (fullpath === '.') return
// match against base paths
if (filepaths && !filepaths.some(base => worthWalking(fullpath, base))) {
return null
}
// Emit progress event
if (onProgress) {
await onProgress({ phase: 'Analyzing workdir', loaded: ++count })
}
// This is a kind of silly pattern but it worked so well for me in the past
// and it makes intuitively demonstrating exhaustiveness so *easy*.
// This checks for the presense and/or absence of each of the 3 entries,
// converts that to a 3-bit binary representation, and then handles
// every possible combination (2^3 or 8 cases) with a lookup table.
const key = [!!stage, !!commit, !!workdir].map(Number).join('')
switch (key) {
// Impossible case.
case '000':
return
// Ignore workdir files that are not tracked and not part of the new commit.
case '001':
// OK, make an exception for explicitly named files.
if (force && filepaths && filepaths.includes(fullpath)) {
return ['delete', fullpath]
}
return
// New entries
case '010': {
switch (await commit.type()) {
case 'tree': {
return ['mkdir', fullpath]
}
case 'blob': {
return [
'create',
fullpath,
await commit.oid(),
await commit.mode(),
]
}
case 'commit': {
return [
'mkdir-index',
fullpath,
await commit.oid(),
await commit.mode(),
]
}
default: {
return [
'error',
`new entry Unhandled type ${await commit.type()}`,
]
}
}
}
// New entries but there is already something in the workdir there.
case '011': {
switch (`${await commit.type()}-${await workdir.type()}`) {
case 'tree-tree': {
return // noop
}
case 'tree-blob':
case 'blob-tree': {
return ['conflict', fullpath]
}
case 'blob-blob': {
// Is the incoming file different?
if ((await commit.oid()) !== (await workdir.oid())) {
if (force) {
return [
'update',
fullpath,
await commit.oid(),
await commit.mode(),
(await commit.mode()) !== (await workdir.mode()),
]
} else {
return ['conflict', fullpath]
}
} else {
// Is the incoming file a different mode?
if ((await commit.mode()) !== (await workdir.mode())) {
if (force) {
return [
'update',
fullpath,
await commit.oid(),
await commit.mode(),
true,
]
} else {
return ['conflict', fullpath]
}
} else {
return [
'create-index',
fullpath,
await commit.oid(),
await commit.mode(),
]
}
}
}
case 'commit-tree': {
// TODO: submodule
// We'll ignore submodule directories for now.
// Users prefer we not throw an error for lack of submodule support.
// gitlinks
return
}
case 'commit-blob': {
// TODO: submodule
// But... we'll complain if there is a *file* where we would
// put a submodule if we had submodule support.
return ['conflict', fullpath]
}
default: {
return ['error', `new entry Unhandled type ${commit.type}`]
}
}
}
// Something in stage but not in the commit OR the workdir.
// Note: I verified this behavior against canonical git.
case '100': {
return ['delete-index', fullpath]
}
// Deleted entries
// TODO: How to handle if stage type and workdir type mismatch?
case '101': {
switch (await stage.type()) {
case 'tree': {
return ['rmdir', fullpath]
}
case 'blob': {
// Git checks that the workdir.oid === stage.oid before deleting file
if ((await stage.oid()) !== (await workdir.oid())) {
if (force) {
return ['delete', fullpath]
} else {
return ['conflict', fullpath]
}
} else {
return ['delete', fullpath]
}
}
case 'commit': {
return ['rmdir-index', fullpath]
}
default: {
return [
'error',
`delete entry Unhandled type ${await stage.type()}`,
]
}
}
}
// File missing from workdir
case '110':
// Possibly modified entries
case '111': {
switch (`${await stage.type()}-${await commit.type()}`) {
case 'tree-tree': {
return
}
case 'blob-blob': {
// If the file hasn't changed, there is no need to do anything.
// Existing file modifications in the workdir can be be left as is.
if (
(await stage.oid()) === (await commit.oid()) &&
(await stage.mode()) === (await commit.mode()) &&
!force
) {
return
}
// Check for local changes that would be lost
if (workdir) {
// Note: canonical git only compares with the stage. But we're smart enough
// to compare to the stage AND the incoming commit.
if (
(await workdir.oid()) !== (await stage.oid()) &&
(await workdir.oid()) !== (await commit.oid())
) {
if (force) {
return [
'update',
fullpath,
await commit.oid(),
await commit.mode(),
(await commit.mode()) !== (await workdir.mode()),
]
} else {
return ['conflict', fullpath]
}
}
} else if (force) {
return [
'update',
fullpath,
await commit.oid(),
await commit.mode(),
(await commit.mode()) !== (await stage.mode()),
]
}
// Has file mode changed?
if ((await commit.mode()) !== (await stage.mode())) {
return [
'update',
fullpath,
await commit.oid(),
await commit.mode(),
true,
]
}
// TODO: HANDLE SYMLINKS
// Has the file content changed?
if ((await commit.oid()) !== (await stage.oid())) {
return [
'update',
fullpath,
await commit.oid(),
await commit.mode(),
false,
]
} else {
return
}
}
case 'tree-blob': {
return ['update-dir-to-blob', fullpath, await commit.oid()]
}
case 'blob-tree': {
return ['update-blob-to-tree', fullpath]
}
case 'commit-commit': {
return [
'mkdir-index',
fullpath,
await commit.oid(),
await commit.mode(),
]
}
default: {
return [
'error',
`update entry Unhandled type ${await stage.type()}-${await commit.type()}`,
]
}
}
}
}
},
// Modify the default flat mapping
reduce: async function(parent, children) {
children = children.flat()
if (!parent) {
return children
} else if (parent && parent[0] === 'rmdir') {
children.push(parent)
return children
} else {
children.unshift(parent)
return children
}
},
})
}