isomorphic-git 1.44.5 → 1.44.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/index.cjs +157 -25
- package/index.js +157 -25
- package/index.umd.min.js +1 -1
- package/index.umd.min.js.map +1 -1
- package/managers/index.cjs +143 -20
- package/managers/index.js +143 -20
- package/managers/index.umd.min.js +1 -1
- package/managers/index.umd.min.js.map +1 -1
- package/package.json +1 -1
package/managers/index.cjs
CHANGED
|
@@ -1281,13 +1281,25 @@ function parseCacheEntryFlags(bits) {
|
|
|
1281
1281
|
extended: Boolean(bits & 0b0100000000000000),
|
|
1282
1282
|
stage: (bits & 0b0011000000000000) >> 12,
|
|
1283
1283
|
nameLength: bits & 0b0000111111111111,
|
|
1284
|
+
skipWorktree: false,
|
|
1285
|
+
intentToAdd: false,
|
|
1284
1286
|
}
|
|
1285
1287
|
}
|
|
1286
1288
|
|
|
1289
|
+
// Bits of the version 3 extended flags field that git understands:
|
|
1290
|
+
// 1-bit reserved, 1-bit skip-worktree, 1-bit intent-to-add, 13 unused bits
|
|
1291
|
+
const SKIP_WORKTREE = 0b0100000000000000;
|
|
1292
|
+
const INTENT_TO_ADD = 0b0010000000000000;
|
|
1293
|
+
|
|
1294
|
+
function hasExtendedFlags(entry) {
|
|
1295
|
+
return Boolean(entry.flags.skipWorktree || entry.flags.intentToAdd)
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1287
1298
|
function renderCacheEntryFlags(entry) {
|
|
1288
1299
|
const flags = entry.flags;
|
|
1289
|
-
// 1-bit extended flag
|
|
1290
|
-
flags
|
|
1300
|
+
// 1-bit extended flag, set only when the entry needs the extended
|
|
1301
|
+
// flags field that version 3 adds
|
|
1302
|
+
flags.extended = hasExtendedFlags(entry);
|
|
1291
1303
|
// 12-bit name length if the length is less than 0xFFF; otherwise 0xFFF
|
|
1292
1304
|
// is stored in this field.
|
|
1293
1305
|
flags.nameLength = Math.min(Buffer.from(entry.path).length, 0xfff);
|
|
@@ -1299,6 +1311,13 @@ function renderCacheEntryFlags(entry) {
|
|
|
1299
1311
|
)
|
|
1300
1312
|
}
|
|
1301
1313
|
|
|
1314
|
+
function renderCacheEntryExtendedFlags(entry) {
|
|
1315
|
+
return (
|
|
1316
|
+
(entry.flags.skipWorktree ? SKIP_WORKTREE : 0) +
|
|
1317
|
+
(entry.flags.intentToAdd ? INTENT_TO_ADD : 0)
|
|
1318
|
+
)
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1302
1321
|
class GitIndex {
|
|
1303
1322
|
/*::
|
|
1304
1323
|
_entries: Map<string, CacheEntry>
|
|
@@ -1358,7 +1377,7 @@ class GitIndex {
|
|
|
1358
1377
|
}
|
|
1359
1378
|
|
|
1360
1379
|
const version = reader.readUInt32BE();
|
|
1361
|
-
if (version !== 2) {
|
|
1380
|
+
if (version !== 2 && version !== 3) {
|
|
1362
1381
|
throw new InternalError(`Unsupported dircache version: ${version}`)
|
|
1363
1382
|
}
|
|
1364
1383
|
const numEntries = reader.readUInt32BE();
|
|
@@ -1378,7 +1397,16 @@ class GitIndex {
|
|
|
1378
1397
|
entry.oid = reader.slice(20).toString('hex');
|
|
1379
1398
|
const flags = reader.readUInt16BE();
|
|
1380
1399
|
entry.flags = parseCacheEntryFlags(flags);
|
|
1381
|
-
|
|
1400
|
+
if (entry.flags.extended) {
|
|
1401
|
+
const extendedFlags = reader.readUInt16BE();
|
|
1402
|
+
if (extendedFlags & ~(SKIP_WORKTREE | INTENT_TO_ADD)) {
|
|
1403
|
+
throw new InternalError(
|
|
1404
|
+
`Unknown index entry format: 0x${extendedFlags.toString(16)}`
|
|
1405
|
+
)
|
|
1406
|
+
}
|
|
1407
|
+
entry.flags.skipWorktree = Boolean(extendedFlags & SKIP_WORKTREE);
|
|
1408
|
+
entry.flags.intentToAdd = Boolean(extendedFlags & INTENT_TO_ADD);
|
|
1409
|
+
}
|
|
1382
1410
|
const pathlength = buffer.indexOf(0, reader.tell() + 1) - reader.tell();
|
|
1383
1411
|
if (pathlength < 1) {
|
|
1384
1412
|
throw new InternalError(`Got a path length of: ${pathlength}`)
|
|
@@ -1457,6 +1485,9 @@ class GitIndex {
|
|
|
1457
1485
|
}
|
|
1458
1486
|
stats = normalizeStats(stats);
|
|
1459
1487
|
const bfilepath = Buffer.from(filepath);
|
|
1488
|
+
// Keep the skip-worktree bit of a path outside the sparse checkout.
|
|
1489
|
+
// Intent-to-add is cleared, since the path is now staged.
|
|
1490
|
+
const existing = stage === 0 ? this._entries.get(filepath) : undefined;
|
|
1460
1491
|
const entry = {
|
|
1461
1492
|
ctimeSeconds: stats.ctimeSeconds,
|
|
1462
1493
|
ctimeNanoseconds: stats.ctimeNanoseconds,
|
|
@@ -1478,6 +1509,8 @@ class GitIndex {
|
|
|
1478
1509
|
extended: false,
|
|
1479
1510
|
stage,
|
|
1480
1511
|
nameLength: bfilepath.length < 0xfff ? bfilepath.length : 0xfff,
|
|
1512
|
+
skipWorktree: Boolean(existing?.flags.skipWorktree),
|
|
1513
|
+
intentToAdd: false,
|
|
1481
1514
|
},
|
|
1482
1515
|
stages: [],
|
|
1483
1516
|
};
|
|
@@ -1521,8 +1554,11 @@ class GitIndex {
|
|
|
1521
1554
|
|
|
1522
1555
|
static async _entryToBuffer(entry) {
|
|
1523
1556
|
const bpath = Buffer.from(entry.path);
|
|
1524
|
-
|
|
1525
|
-
|
|
1557
|
+
const extended = hasExtendedFlags(entry);
|
|
1558
|
+
// the fixed length + the extended flags (if any) + the filename + at
|
|
1559
|
+
// least one null char => align by 8
|
|
1560
|
+
const length =
|
|
1561
|
+
Math.ceil((62 + (extended ? 2 : 0) + bpath.length + 1) / 8) * 8;
|
|
1526
1562
|
const written = Buffer.alloc(length);
|
|
1527
1563
|
const writer = new BufferCursor(written);
|
|
1528
1564
|
const stat = normalizeStats(entry);
|
|
@@ -1538,6 +1574,9 @@ class GitIndex {
|
|
|
1538
1574
|
writer.writeUInt32BE(stat.size);
|
|
1539
1575
|
writer.write(entry.oid, 20, 'hex');
|
|
1540
1576
|
writer.writeUInt16BE(renderCacheEntryFlags(entry));
|
|
1577
|
+
if (extended) {
|
|
1578
|
+
writer.writeUInt16BE(renderCacheEntryExtendedFlags(entry));
|
|
1579
|
+
}
|
|
1541
1580
|
writer.write(entry.path, bpath.length, 'utf8');
|
|
1542
1581
|
return written
|
|
1543
1582
|
}
|
|
@@ -1546,7 +1585,8 @@ class GitIndex {
|
|
|
1546
1585
|
const header = Buffer.alloc(12);
|
|
1547
1586
|
const writer = new BufferCursor(header);
|
|
1548
1587
|
writer.write('DIRC', 4, 'utf8');
|
|
1549
|
-
|
|
1588
|
+
// Like canonical git, only use version 3 when an entry needs it
|
|
1589
|
+
writer.writeUInt32BE(this.entriesFlat.some(hasExtendedFlags) ? 3 : 2);
|
|
1550
1590
|
writer.writeUInt32BE(this.entriesFlat.length);
|
|
1551
1591
|
|
|
1552
1592
|
let entryBuffers = [];
|
|
@@ -4179,6 +4219,69 @@ function otherVarIntDecode(reader, startWith) {
|
|
|
4179
4219
|
return result
|
|
4180
4220
|
}
|
|
4181
4221
|
|
|
4222
|
+
// Memory that `fromPack` may use to keep resolved objects for delta bases.
|
|
4223
|
+
// An object larger than a quarter of this is not kept.
|
|
4224
|
+
const RESOLVE_CACHE_BYTES = 32 * 1024 * 1024;
|
|
4225
|
+
|
|
4226
|
+
// A cache that drops the least recently used object when it holds too many bytes.
|
|
4227
|
+
class ObjectCache {
|
|
4228
|
+
constructor(maxBytes) {
|
|
4229
|
+
this.maxBytes = maxBytes;
|
|
4230
|
+
this.bytes = 0;
|
|
4231
|
+
this.map = new Map();
|
|
4232
|
+
}
|
|
4233
|
+
|
|
4234
|
+
get(key) {
|
|
4235
|
+
const value = this.map.get(key);
|
|
4236
|
+
if (value !== undefined) {
|
|
4237
|
+
this.map.delete(key);
|
|
4238
|
+
this.map.set(key, value);
|
|
4239
|
+
}
|
|
4240
|
+
return value
|
|
4241
|
+
}
|
|
4242
|
+
|
|
4243
|
+
set(key, value) {
|
|
4244
|
+
const size = value.object.byteLength;
|
|
4245
|
+
if (size > this.maxBytes / 4) return
|
|
4246
|
+
this.map.set(key, value);
|
|
4247
|
+
this.bytes += size;
|
|
4248
|
+
for (const [oldKey, old] of this.map) {
|
|
4249
|
+
if (this.bytes <= this.maxBytes) break
|
|
4250
|
+
this.map.delete(oldKey);
|
|
4251
|
+
this.bytes -= old.object.byteLength;
|
|
4252
|
+
}
|
|
4253
|
+
}
|
|
4254
|
+
}
|
|
4255
|
+
|
|
4256
|
+
// Resolve an object from the data that `listpack` already inflated.
|
|
4257
|
+
// Return null when the object needs `readSlice`. This happens when the base of
|
|
4258
|
+
// a delta is not in the cache, or when the delta is corrupt.
|
|
4259
|
+
function resolveInflated(
|
|
4260
|
+
{ type, data, reference, offset },
|
|
4261
|
+
cache,
|
|
4262
|
+
offsetByOid
|
|
4263
|
+
) {
|
|
4264
|
+
// `applyDelta` and `shasum` need a Buffer. This is a view and copies nothing.
|
|
4265
|
+
const object = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
|
|
4266
|
+
if (type !== 'ofs-delta' && type !== 'ref-delta') return { type, object }
|
|
4267
|
+
const baseOffset =
|
|
4268
|
+
type === 'ofs-delta'
|
|
4269
|
+
? offset - decodeVarInt(new BufferCursor(reference))
|
|
4270
|
+
: offsetByOid.get(reference.toString('hex'));
|
|
4271
|
+
const base = cache.get(baseOffset);
|
|
4272
|
+
if (!base) return null
|
|
4273
|
+
try {
|
|
4274
|
+
return {
|
|
4275
|
+
type: base.type,
|
|
4276
|
+
object: Buffer.from(applyDelta(object, base.object)),
|
|
4277
|
+
}
|
|
4278
|
+
} catch (err) {
|
|
4279
|
+
// A corrupt delta. `readSlice` reports it again. Any other error is a bug.
|
|
4280
|
+
if (err instanceof InternalError || err instanceof RangeError) return null
|
|
4281
|
+
throw err
|
|
4282
|
+
}
|
|
4283
|
+
}
|
|
4284
|
+
|
|
4182
4285
|
class GitPackIndex {
|
|
4183
4286
|
constructor(stuff) {
|
|
4184
4287
|
Object.assign(this, stuff);
|
|
@@ -4250,6 +4353,9 @@ class GitPackIndex {
|
|
|
4250
4353
|
const offsets = new Map();
|
|
4251
4354
|
let totalObjectCount = null;
|
|
4252
4355
|
let lastPercent = null;
|
|
4356
|
+
// Objects that `listpack` inflated, by pack offset. A delta finds its base here.
|
|
4357
|
+
const cache = new ObjectCache(RESOLVE_CACHE_BYTES);
|
|
4358
|
+
const offsetByOid = new Map();
|
|
4253
4359
|
|
|
4254
4360
|
await listpack([pack], async ({ data, type, reference, offset, num }) => {
|
|
4255
4361
|
if (totalObjectCount === null) totalObjectCount = num;
|
|
@@ -4285,6 +4391,20 @@ class GitPackIndex {
|
|
|
4285
4391
|
offset,
|
|
4286
4392
|
};
|
|
4287
4393
|
}
|
|
4394
|
+
|
|
4395
|
+
// The object is already inflated, so get its oid now. Then the loop below
|
|
4396
|
+
// does not inflate it again. That loop reads all other objects.
|
|
4397
|
+
const o = offsetToObject[offset];
|
|
4398
|
+
if (!o) return
|
|
4399
|
+
const resolved = resolveInflated(
|
|
4400
|
+
{ type, data, reference, offset },
|
|
4401
|
+
cache,
|
|
4402
|
+
offsetByOid
|
|
4403
|
+
);
|
|
4404
|
+
if (!resolved) return
|
|
4405
|
+
o.oid = await shasum(GitObject.wrap(resolved));
|
|
4406
|
+
offsetByOid.set(o.oid, offset);
|
|
4407
|
+
cache.set(offset, resolved);
|
|
4288
4408
|
});
|
|
4289
4409
|
|
|
4290
4410
|
// We need to know the lengths of the slices to compute the CRCs.
|
|
@@ -4328,20 +4448,21 @@ class GitPackIndex {
|
|
|
4328
4448
|
lastPercent = percent;
|
|
4329
4449
|
|
|
4330
4450
|
const o = offsetToObject[offset];
|
|
4331
|
-
if (o.oid)
|
|
4332
|
-
|
|
4333
|
-
|
|
4334
|
-
|
|
4335
|
-
|
|
4336
|
-
|
|
4337
|
-
|
|
4338
|
-
|
|
4339
|
-
|
|
4340
|
-
|
|
4341
|
-
|
|
4342
|
-
} catch (err) {
|
|
4343
|
-
continue
|
|
4451
|
+
if (!o.oid) {
|
|
4452
|
+
// The first pass did not resolve this object, so read it.
|
|
4453
|
+
try {
|
|
4454
|
+
p.readDepth = 0;
|
|
4455
|
+
p.externalReadDepth = 0;
|
|
4456
|
+
const { type, object } = await p.readSlice({ start: offset });
|
|
4457
|
+
objectsByDepth[p.readDepth] += 1;
|
|
4458
|
+
o.oid = await shasum(GitObject.wrap({ type, object }));
|
|
4459
|
+
} catch (err) {
|
|
4460
|
+
continue
|
|
4461
|
+
}
|
|
4344
4462
|
}
|
|
4463
|
+
hashes.push(o.oid);
|
|
4464
|
+
offsets.set(o.oid, offset);
|
|
4465
|
+
crcs[o.oid] = o.crc;
|
|
4345
4466
|
}
|
|
4346
4467
|
|
|
4347
4468
|
hashes.sort();
|
|
@@ -5970,9 +6091,11 @@ class GitWalkerFs {
|
|
|
5970
6091
|
// 1) if we can (because the oid and mode are the same)
|
|
5971
6092
|
// 2) and only if we need to (because other stats differ)
|
|
5972
6093
|
// 3) and only if the caller opted in to refreshing the index
|
|
6094
|
+
// 4) and not for an intent-to-add entry, which inserting would stage
|
|
5973
6095
|
if (
|
|
5974
6096
|
self.refresh &&
|
|
5975
6097
|
stage &&
|
|
6098
|
+
!stage.flags.intentToAdd &&
|
|
5976
6099
|
oid === stage.oid &&
|
|
5977
6100
|
(!filemode || stats.mode === stage.mode) &&
|
|
5978
6101
|
compareStats(stats, stage, filemode, trustino)
|
package/managers/index.js
CHANGED
|
@@ -1274,13 +1274,25 @@ function parseCacheEntryFlags(bits) {
|
|
|
1274
1274
|
extended: Boolean(bits & 0b0100000000000000),
|
|
1275
1275
|
stage: (bits & 0b0011000000000000) >> 12,
|
|
1276
1276
|
nameLength: bits & 0b0000111111111111,
|
|
1277
|
+
skipWorktree: false,
|
|
1278
|
+
intentToAdd: false,
|
|
1277
1279
|
}
|
|
1278
1280
|
}
|
|
1279
1281
|
|
|
1282
|
+
// Bits of the version 3 extended flags field that git understands:
|
|
1283
|
+
// 1-bit reserved, 1-bit skip-worktree, 1-bit intent-to-add, 13 unused bits
|
|
1284
|
+
const SKIP_WORKTREE = 0b0100000000000000;
|
|
1285
|
+
const INTENT_TO_ADD = 0b0010000000000000;
|
|
1286
|
+
|
|
1287
|
+
function hasExtendedFlags(entry) {
|
|
1288
|
+
return Boolean(entry.flags.skipWorktree || entry.flags.intentToAdd)
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1280
1291
|
function renderCacheEntryFlags(entry) {
|
|
1281
1292
|
const flags = entry.flags;
|
|
1282
|
-
// 1-bit extended flag
|
|
1283
|
-
flags
|
|
1293
|
+
// 1-bit extended flag, set only when the entry needs the extended
|
|
1294
|
+
// flags field that version 3 adds
|
|
1295
|
+
flags.extended = hasExtendedFlags(entry);
|
|
1284
1296
|
// 12-bit name length if the length is less than 0xFFF; otherwise 0xFFF
|
|
1285
1297
|
// is stored in this field.
|
|
1286
1298
|
flags.nameLength = Math.min(Buffer.from(entry.path).length, 0xfff);
|
|
@@ -1292,6 +1304,13 @@ function renderCacheEntryFlags(entry) {
|
|
|
1292
1304
|
)
|
|
1293
1305
|
}
|
|
1294
1306
|
|
|
1307
|
+
function renderCacheEntryExtendedFlags(entry) {
|
|
1308
|
+
return (
|
|
1309
|
+
(entry.flags.skipWorktree ? SKIP_WORKTREE : 0) +
|
|
1310
|
+
(entry.flags.intentToAdd ? INTENT_TO_ADD : 0)
|
|
1311
|
+
)
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1295
1314
|
class GitIndex {
|
|
1296
1315
|
/*::
|
|
1297
1316
|
_entries: Map<string, CacheEntry>
|
|
@@ -1351,7 +1370,7 @@ class GitIndex {
|
|
|
1351
1370
|
}
|
|
1352
1371
|
|
|
1353
1372
|
const version = reader.readUInt32BE();
|
|
1354
|
-
if (version !== 2) {
|
|
1373
|
+
if (version !== 2 && version !== 3) {
|
|
1355
1374
|
throw new InternalError(`Unsupported dircache version: ${version}`)
|
|
1356
1375
|
}
|
|
1357
1376
|
const numEntries = reader.readUInt32BE();
|
|
@@ -1371,7 +1390,16 @@ class GitIndex {
|
|
|
1371
1390
|
entry.oid = reader.slice(20).toString('hex');
|
|
1372
1391
|
const flags = reader.readUInt16BE();
|
|
1373
1392
|
entry.flags = parseCacheEntryFlags(flags);
|
|
1374
|
-
|
|
1393
|
+
if (entry.flags.extended) {
|
|
1394
|
+
const extendedFlags = reader.readUInt16BE();
|
|
1395
|
+
if (extendedFlags & ~(SKIP_WORKTREE | INTENT_TO_ADD)) {
|
|
1396
|
+
throw new InternalError(
|
|
1397
|
+
`Unknown index entry format: 0x${extendedFlags.toString(16)}`
|
|
1398
|
+
)
|
|
1399
|
+
}
|
|
1400
|
+
entry.flags.skipWorktree = Boolean(extendedFlags & SKIP_WORKTREE);
|
|
1401
|
+
entry.flags.intentToAdd = Boolean(extendedFlags & INTENT_TO_ADD);
|
|
1402
|
+
}
|
|
1375
1403
|
const pathlength = buffer.indexOf(0, reader.tell() + 1) - reader.tell();
|
|
1376
1404
|
if (pathlength < 1) {
|
|
1377
1405
|
throw new InternalError(`Got a path length of: ${pathlength}`)
|
|
@@ -1450,6 +1478,9 @@ class GitIndex {
|
|
|
1450
1478
|
}
|
|
1451
1479
|
stats = normalizeStats(stats);
|
|
1452
1480
|
const bfilepath = Buffer.from(filepath);
|
|
1481
|
+
// Keep the skip-worktree bit of a path outside the sparse checkout.
|
|
1482
|
+
// Intent-to-add is cleared, since the path is now staged.
|
|
1483
|
+
const existing = stage === 0 ? this._entries.get(filepath) : undefined;
|
|
1453
1484
|
const entry = {
|
|
1454
1485
|
ctimeSeconds: stats.ctimeSeconds,
|
|
1455
1486
|
ctimeNanoseconds: stats.ctimeNanoseconds,
|
|
@@ -1471,6 +1502,8 @@ class GitIndex {
|
|
|
1471
1502
|
extended: false,
|
|
1472
1503
|
stage,
|
|
1473
1504
|
nameLength: bfilepath.length < 0xfff ? bfilepath.length : 0xfff,
|
|
1505
|
+
skipWorktree: Boolean(existing?.flags.skipWorktree),
|
|
1506
|
+
intentToAdd: false,
|
|
1474
1507
|
},
|
|
1475
1508
|
stages: [],
|
|
1476
1509
|
};
|
|
@@ -1514,8 +1547,11 @@ class GitIndex {
|
|
|
1514
1547
|
|
|
1515
1548
|
static async _entryToBuffer(entry) {
|
|
1516
1549
|
const bpath = Buffer.from(entry.path);
|
|
1517
|
-
|
|
1518
|
-
|
|
1550
|
+
const extended = hasExtendedFlags(entry);
|
|
1551
|
+
// the fixed length + the extended flags (if any) + the filename + at
|
|
1552
|
+
// least one null char => align by 8
|
|
1553
|
+
const length =
|
|
1554
|
+
Math.ceil((62 + (extended ? 2 : 0) + bpath.length + 1) / 8) * 8;
|
|
1519
1555
|
const written = Buffer.alloc(length);
|
|
1520
1556
|
const writer = new BufferCursor(written);
|
|
1521
1557
|
const stat = normalizeStats(entry);
|
|
@@ -1531,6 +1567,9 @@ class GitIndex {
|
|
|
1531
1567
|
writer.writeUInt32BE(stat.size);
|
|
1532
1568
|
writer.write(entry.oid, 20, 'hex');
|
|
1533
1569
|
writer.writeUInt16BE(renderCacheEntryFlags(entry));
|
|
1570
|
+
if (extended) {
|
|
1571
|
+
writer.writeUInt16BE(renderCacheEntryExtendedFlags(entry));
|
|
1572
|
+
}
|
|
1534
1573
|
writer.write(entry.path, bpath.length, 'utf8');
|
|
1535
1574
|
return written
|
|
1536
1575
|
}
|
|
@@ -1539,7 +1578,8 @@ class GitIndex {
|
|
|
1539
1578
|
const header = Buffer.alloc(12);
|
|
1540
1579
|
const writer = new BufferCursor(header);
|
|
1541
1580
|
writer.write('DIRC', 4, 'utf8');
|
|
1542
|
-
|
|
1581
|
+
// Like canonical git, only use version 3 when an entry needs it
|
|
1582
|
+
writer.writeUInt32BE(this.entriesFlat.some(hasExtendedFlags) ? 3 : 2);
|
|
1543
1583
|
writer.writeUInt32BE(this.entriesFlat.length);
|
|
1544
1584
|
|
|
1545
1585
|
let entryBuffers = [];
|
|
@@ -4172,6 +4212,69 @@ function otherVarIntDecode(reader, startWith) {
|
|
|
4172
4212
|
return result
|
|
4173
4213
|
}
|
|
4174
4214
|
|
|
4215
|
+
// Memory that `fromPack` may use to keep resolved objects for delta bases.
|
|
4216
|
+
// An object larger than a quarter of this is not kept.
|
|
4217
|
+
const RESOLVE_CACHE_BYTES = 32 * 1024 * 1024;
|
|
4218
|
+
|
|
4219
|
+
// A cache that drops the least recently used object when it holds too many bytes.
|
|
4220
|
+
class ObjectCache {
|
|
4221
|
+
constructor(maxBytes) {
|
|
4222
|
+
this.maxBytes = maxBytes;
|
|
4223
|
+
this.bytes = 0;
|
|
4224
|
+
this.map = new Map();
|
|
4225
|
+
}
|
|
4226
|
+
|
|
4227
|
+
get(key) {
|
|
4228
|
+
const value = this.map.get(key);
|
|
4229
|
+
if (value !== undefined) {
|
|
4230
|
+
this.map.delete(key);
|
|
4231
|
+
this.map.set(key, value);
|
|
4232
|
+
}
|
|
4233
|
+
return value
|
|
4234
|
+
}
|
|
4235
|
+
|
|
4236
|
+
set(key, value) {
|
|
4237
|
+
const size = value.object.byteLength;
|
|
4238
|
+
if (size > this.maxBytes / 4) return
|
|
4239
|
+
this.map.set(key, value);
|
|
4240
|
+
this.bytes += size;
|
|
4241
|
+
for (const [oldKey, old] of this.map) {
|
|
4242
|
+
if (this.bytes <= this.maxBytes) break
|
|
4243
|
+
this.map.delete(oldKey);
|
|
4244
|
+
this.bytes -= old.object.byteLength;
|
|
4245
|
+
}
|
|
4246
|
+
}
|
|
4247
|
+
}
|
|
4248
|
+
|
|
4249
|
+
// Resolve an object from the data that `listpack` already inflated.
|
|
4250
|
+
// Return null when the object needs `readSlice`. This happens when the base of
|
|
4251
|
+
// a delta is not in the cache, or when the delta is corrupt.
|
|
4252
|
+
function resolveInflated(
|
|
4253
|
+
{ type, data, reference, offset },
|
|
4254
|
+
cache,
|
|
4255
|
+
offsetByOid
|
|
4256
|
+
) {
|
|
4257
|
+
// `applyDelta` and `shasum` need a Buffer. This is a view and copies nothing.
|
|
4258
|
+
const object = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
|
|
4259
|
+
if (type !== 'ofs-delta' && type !== 'ref-delta') return { type, object }
|
|
4260
|
+
const baseOffset =
|
|
4261
|
+
type === 'ofs-delta'
|
|
4262
|
+
? offset - decodeVarInt(new BufferCursor(reference))
|
|
4263
|
+
: offsetByOid.get(reference.toString('hex'));
|
|
4264
|
+
const base = cache.get(baseOffset);
|
|
4265
|
+
if (!base) return null
|
|
4266
|
+
try {
|
|
4267
|
+
return {
|
|
4268
|
+
type: base.type,
|
|
4269
|
+
object: Buffer.from(applyDelta(object, base.object)),
|
|
4270
|
+
}
|
|
4271
|
+
} catch (err) {
|
|
4272
|
+
// A corrupt delta. `readSlice` reports it again. Any other error is a bug.
|
|
4273
|
+
if (err instanceof InternalError || err instanceof RangeError) return null
|
|
4274
|
+
throw err
|
|
4275
|
+
}
|
|
4276
|
+
}
|
|
4277
|
+
|
|
4175
4278
|
class GitPackIndex {
|
|
4176
4279
|
constructor(stuff) {
|
|
4177
4280
|
Object.assign(this, stuff);
|
|
@@ -4243,6 +4346,9 @@ class GitPackIndex {
|
|
|
4243
4346
|
const offsets = new Map();
|
|
4244
4347
|
let totalObjectCount = null;
|
|
4245
4348
|
let lastPercent = null;
|
|
4349
|
+
// Objects that `listpack` inflated, by pack offset. A delta finds its base here.
|
|
4350
|
+
const cache = new ObjectCache(RESOLVE_CACHE_BYTES);
|
|
4351
|
+
const offsetByOid = new Map();
|
|
4246
4352
|
|
|
4247
4353
|
await listpack([pack], async ({ data, type, reference, offset, num }) => {
|
|
4248
4354
|
if (totalObjectCount === null) totalObjectCount = num;
|
|
@@ -4278,6 +4384,20 @@ class GitPackIndex {
|
|
|
4278
4384
|
offset,
|
|
4279
4385
|
};
|
|
4280
4386
|
}
|
|
4387
|
+
|
|
4388
|
+
// The object is already inflated, so get its oid now. Then the loop below
|
|
4389
|
+
// does not inflate it again. That loop reads all other objects.
|
|
4390
|
+
const o = offsetToObject[offset];
|
|
4391
|
+
if (!o) return
|
|
4392
|
+
const resolved = resolveInflated(
|
|
4393
|
+
{ type, data, reference, offset },
|
|
4394
|
+
cache,
|
|
4395
|
+
offsetByOid
|
|
4396
|
+
);
|
|
4397
|
+
if (!resolved) return
|
|
4398
|
+
o.oid = await shasum(GitObject.wrap(resolved));
|
|
4399
|
+
offsetByOid.set(o.oid, offset);
|
|
4400
|
+
cache.set(offset, resolved);
|
|
4281
4401
|
});
|
|
4282
4402
|
|
|
4283
4403
|
// We need to know the lengths of the slices to compute the CRCs.
|
|
@@ -4321,20 +4441,21 @@ class GitPackIndex {
|
|
|
4321
4441
|
lastPercent = percent;
|
|
4322
4442
|
|
|
4323
4443
|
const o = offsetToObject[offset];
|
|
4324
|
-
if (o.oid)
|
|
4325
|
-
|
|
4326
|
-
|
|
4327
|
-
|
|
4328
|
-
|
|
4329
|
-
|
|
4330
|
-
|
|
4331
|
-
|
|
4332
|
-
|
|
4333
|
-
|
|
4334
|
-
|
|
4335
|
-
} catch (err) {
|
|
4336
|
-
continue
|
|
4444
|
+
if (!o.oid) {
|
|
4445
|
+
// The first pass did not resolve this object, so read it.
|
|
4446
|
+
try {
|
|
4447
|
+
p.readDepth = 0;
|
|
4448
|
+
p.externalReadDepth = 0;
|
|
4449
|
+
const { type, object } = await p.readSlice({ start: offset });
|
|
4450
|
+
objectsByDepth[p.readDepth] += 1;
|
|
4451
|
+
o.oid = await shasum(GitObject.wrap({ type, object }));
|
|
4452
|
+
} catch (err) {
|
|
4453
|
+
continue
|
|
4454
|
+
}
|
|
4337
4455
|
}
|
|
4456
|
+
hashes.push(o.oid);
|
|
4457
|
+
offsets.set(o.oid, offset);
|
|
4458
|
+
crcs[o.oid] = o.crc;
|
|
4338
4459
|
}
|
|
4339
4460
|
|
|
4340
4461
|
hashes.sort();
|
|
@@ -5957,9 +6078,11 @@ class GitWalkerFs {
|
|
|
5957
6078
|
// 1) if we can (because the oid and mode are the same)
|
|
5958
6079
|
// 2) and only if we need to (because other stats differ)
|
|
5959
6080
|
// 3) and only if the caller opted in to refreshing the index
|
|
6081
|
+
// 4) and not for an intent-to-add entry, which inserting would stage
|
|
5960
6082
|
if (
|
|
5961
6083
|
self.refresh &&
|
|
5962
6084
|
stage &&
|
|
6085
|
+
!stage.flags.intentToAdd &&
|
|
5963
6086
|
oid === stage.oid &&
|
|
5964
6087
|
(!filemode || stats.mode === stage.mode) &&
|
|
5965
6088
|
compareStats(stats, stage, filemode, trustino)
|