isomorphic-git 1.44.4 → 1.44.6

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.
@@ -393,9 +393,11 @@ const schema = {
393
393
  // section is alphanumeric (ASCII) with - and .
394
394
  // section is case insensitive
395
395
  // subsection is optional
396
- // subsection is specified after section and one or more spaces
397
- // subsection is specified between double quotes
398
- const SECTION_LINE_REGEX = /^\[([A-Za-z0-9-.]+)(?: "(.*)")?\]$/;
396
+ // subsection is specified after section and one or more spaces or tabs
397
+ // subsection is specified between double quotes, where \ escapes the next character
398
+ // a comment can follow the section header
399
+ const SECTION_LINE_REGEX =
400
+ /^\[([A-Za-z0-9-.]+)(?:[ \t]+"((?:[^"\\]|\\.)*)")?\][ \t]*(?:[#;].*)?$/;
399
401
  const SECTION_REGEX = /^[A-Za-z0-9-.]+$/;
400
402
 
401
403
  // variable lines contain a name, and equal sign and then a value
@@ -411,7 +413,8 @@ const extractSectionLine = line => {
411
413
  const matches = SECTION_LINE_REGEX.exec(line);
412
414
  if (matches != null) {
413
415
  const [section, subsection] = matches.slice(1);
414
- return [section, subsection]
416
+ // Like canonical git, drop the backslash of an escaped character
417
+ return [section, subsection && subsection.replace(/\\(.)/g, '$1')]
415
418
  }
416
419
  return null
417
420
  };
@@ -4176,6 +4179,69 @@ function otherVarIntDecode(reader, startWith) {
4176
4179
  return result
4177
4180
  }
4178
4181
 
4182
+ // Memory that `fromPack` may use to keep resolved objects for delta bases.
4183
+ // An object larger than a quarter of this is not kept.
4184
+ const RESOLVE_CACHE_BYTES = 32 * 1024 * 1024;
4185
+
4186
+ // A cache that drops the least recently used object when it holds too many bytes.
4187
+ class ObjectCache {
4188
+ constructor(maxBytes) {
4189
+ this.maxBytes = maxBytes;
4190
+ this.bytes = 0;
4191
+ this.map = new Map();
4192
+ }
4193
+
4194
+ get(key) {
4195
+ const value = this.map.get(key);
4196
+ if (value !== undefined) {
4197
+ this.map.delete(key);
4198
+ this.map.set(key, value);
4199
+ }
4200
+ return value
4201
+ }
4202
+
4203
+ set(key, value) {
4204
+ const size = value.object.byteLength;
4205
+ if (size > this.maxBytes / 4) return
4206
+ this.map.set(key, value);
4207
+ this.bytes += size;
4208
+ for (const [oldKey, old] of this.map) {
4209
+ if (this.bytes <= this.maxBytes) break
4210
+ this.map.delete(oldKey);
4211
+ this.bytes -= old.object.byteLength;
4212
+ }
4213
+ }
4214
+ }
4215
+
4216
+ // Resolve an object from the data that `listpack` already inflated.
4217
+ // Return null when the object needs `readSlice`. This happens when the base of
4218
+ // a delta is not in the cache, or when the delta is corrupt.
4219
+ function resolveInflated(
4220
+ { type, data, reference, offset },
4221
+ cache,
4222
+ offsetByOid
4223
+ ) {
4224
+ // `applyDelta` and `shasum` need a Buffer. This is a view and copies nothing.
4225
+ const object = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
4226
+ if (type !== 'ofs-delta' && type !== 'ref-delta') return { type, object }
4227
+ const baseOffset =
4228
+ type === 'ofs-delta'
4229
+ ? offset - decodeVarInt(new BufferCursor(reference))
4230
+ : offsetByOid.get(reference.toString('hex'));
4231
+ const base = cache.get(baseOffset);
4232
+ if (!base) return null
4233
+ try {
4234
+ return {
4235
+ type: base.type,
4236
+ object: Buffer.from(applyDelta(object, base.object)),
4237
+ }
4238
+ } catch (err) {
4239
+ // A corrupt delta. `readSlice` reports it again. Any other error is a bug.
4240
+ if (err instanceof InternalError || err instanceof RangeError) return null
4241
+ throw err
4242
+ }
4243
+ }
4244
+
4179
4245
  class GitPackIndex {
4180
4246
  constructor(stuff) {
4181
4247
  Object.assign(this, stuff);
@@ -4247,6 +4313,9 @@ class GitPackIndex {
4247
4313
  const offsets = new Map();
4248
4314
  let totalObjectCount = null;
4249
4315
  let lastPercent = null;
4316
+ // Objects that `listpack` inflated, by pack offset. A delta finds its base here.
4317
+ const cache = new ObjectCache(RESOLVE_CACHE_BYTES);
4318
+ const offsetByOid = new Map();
4250
4319
 
4251
4320
  await listpack([pack], async ({ data, type, reference, offset, num }) => {
4252
4321
  if (totalObjectCount === null) totalObjectCount = num;
@@ -4282,6 +4351,20 @@ class GitPackIndex {
4282
4351
  offset,
4283
4352
  };
4284
4353
  }
4354
+
4355
+ // The object is already inflated, so get its oid now. Then the loop below
4356
+ // does not inflate it again. That loop reads all other objects.
4357
+ const o = offsetToObject[offset];
4358
+ if (!o) return
4359
+ const resolved = resolveInflated(
4360
+ { type, data, reference, offset },
4361
+ cache,
4362
+ offsetByOid
4363
+ );
4364
+ if (!resolved) return
4365
+ o.oid = await shasum(GitObject.wrap(resolved));
4366
+ offsetByOid.set(o.oid, offset);
4367
+ cache.set(offset, resolved);
4285
4368
  });
4286
4369
 
4287
4370
  // We need to know the lengths of the slices to compute the CRCs.
@@ -4325,20 +4408,21 @@ class GitPackIndex {
4325
4408
  lastPercent = percent;
4326
4409
 
4327
4410
  const o = offsetToObject[offset];
4328
- if (o.oid) continue
4329
- try {
4330
- p.readDepth = 0;
4331
- p.externalReadDepth = 0;
4332
- const { type, object } = await p.readSlice({ start: offset });
4333
- objectsByDepth[p.readDepth] += 1;
4334
- const oid = await shasum(GitObject.wrap({ type, object }));
4335
- o.oid = oid;
4336
- hashes.push(oid);
4337
- offsets.set(oid, offset);
4338
- crcs[oid] = o.crc;
4339
- } catch (err) {
4340
- continue
4411
+ if (!o.oid) {
4412
+ // The first pass did not resolve this object, so read it.
4413
+ try {
4414
+ p.readDepth = 0;
4415
+ p.externalReadDepth = 0;
4416
+ const { type, object } = await p.readSlice({ start: offset });
4417
+ objectsByDepth[p.readDepth] += 1;
4418
+ o.oid = await shasum(GitObject.wrap({ type, object }));
4419
+ } catch (err) {
4420
+ continue
4421
+ }
4341
4422
  }
4423
+ hashes.push(o.oid);
4424
+ offsets.set(o.oid, offset);
4425
+ crcs[o.oid] = o.crc;
4342
4426
  }
4343
4427
 
4344
4428
  hashes.sort();
package/managers/index.js CHANGED
@@ -386,9 +386,11 @@ const schema = {
386
386
  // section is alphanumeric (ASCII) with - and .
387
387
  // section is case insensitive
388
388
  // subsection is optional
389
- // subsection is specified after section and one or more spaces
390
- // subsection is specified between double quotes
391
- const SECTION_LINE_REGEX = /^\[([A-Za-z0-9-.]+)(?: "(.*)")?\]$/;
389
+ // subsection is specified after section and one or more spaces or tabs
390
+ // subsection is specified between double quotes, where \ escapes the next character
391
+ // a comment can follow the section header
392
+ const SECTION_LINE_REGEX =
393
+ /^\[([A-Za-z0-9-.]+)(?:[ \t]+"((?:[^"\\]|\\.)*)")?\][ \t]*(?:[#;].*)?$/;
392
394
  const SECTION_REGEX = /^[A-Za-z0-9-.]+$/;
393
395
 
394
396
  // variable lines contain a name, and equal sign and then a value
@@ -404,7 +406,8 @@ const extractSectionLine = line => {
404
406
  const matches = SECTION_LINE_REGEX.exec(line);
405
407
  if (matches != null) {
406
408
  const [section, subsection] = matches.slice(1);
407
- return [section, subsection]
409
+ // Like canonical git, drop the backslash of an escaped character
410
+ return [section, subsection && subsection.replace(/\\(.)/g, '$1')]
408
411
  }
409
412
  return null
410
413
  };
@@ -4169,6 +4172,69 @@ function otherVarIntDecode(reader, startWith) {
4169
4172
  return result
4170
4173
  }
4171
4174
 
4175
+ // Memory that `fromPack` may use to keep resolved objects for delta bases.
4176
+ // An object larger than a quarter of this is not kept.
4177
+ const RESOLVE_CACHE_BYTES = 32 * 1024 * 1024;
4178
+
4179
+ // A cache that drops the least recently used object when it holds too many bytes.
4180
+ class ObjectCache {
4181
+ constructor(maxBytes) {
4182
+ this.maxBytes = maxBytes;
4183
+ this.bytes = 0;
4184
+ this.map = new Map();
4185
+ }
4186
+
4187
+ get(key) {
4188
+ const value = this.map.get(key);
4189
+ if (value !== undefined) {
4190
+ this.map.delete(key);
4191
+ this.map.set(key, value);
4192
+ }
4193
+ return value
4194
+ }
4195
+
4196
+ set(key, value) {
4197
+ const size = value.object.byteLength;
4198
+ if (size > this.maxBytes / 4) return
4199
+ this.map.set(key, value);
4200
+ this.bytes += size;
4201
+ for (const [oldKey, old] of this.map) {
4202
+ if (this.bytes <= this.maxBytes) break
4203
+ this.map.delete(oldKey);
4204
+ this.bytes -= old.object.byteLength;
4205
+ }
4206
+ }
4207
+ }
4208
+
4209
+ // Resolve an object from the data that `listpack` already inflated.
4210
+ // Return null when the object needs `readSlice`. This happens when the base of
4211
+ // a delta is not in the cache, or when the delta is corrupt.
4212
+ function resolveInflated(
4213
+ { type, data, reference, offset },
4214
+ cache,
4215
+ offsetByOid
4216
+ ) {
4217
+ // `applyDelta` and `shasum` need a Buffer. This is a view and copies nothing.
4218
+ const object = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
4219
+ if (type !== 'ofs-delta' && type !== 'ref-delta') return { type, object }
4220
+ const baseOffset =
4221
+ type === 'ofs-delta'
4222
+ ? offset - decodeVarInt(new BufferCursor(reference))
4223
+ : offsetByOid.get(reference.toString('hex'));
4224
+ const base = cache.get(baseOffset);
4225
+ if (!base) return null
4226
+ try {
4227
+ return {
4228
+ type: base.type,
4229
+ object: Buffer.from(applyDelta(object, base.object)),
4230
+ }
4231
+ } catch (err) {
4232
+ // A corrupt delta. `readSlice` reports it again. Any other error is a bug.
4233
+ if (err instanceof InternalError || err instanceof RangeError) return null
4234
+ throw err
4235
+ }
4236
+ }
4237
+
4172
4238
  class GitPackIndex {
4173
4239
  constructor(stuff) {
4174
4240
  Object.assign(this, stuff);
@@ -4240,6 +4306,9 @@ class GitPackIndex {
4240
4306
  const offsets = new Map();
4241
4307
  let totalObjectCount = null;
4242
4308
  let lastPercent = null;
4309
+ // Objects that `listpack` inflated, by pack offset. A delta finds its base here.
4310
+ const cache = new ObjectCache(RESOLVE_CACHE_BYTES);
4311
+ const offsetByOid = new Map();
4243
4312
 
4244
4313
  await listpack([pack], async ({ data, type, reference, offset, num }) => {
4245
4314
  if (totalObjectCount === null) totalObjectCount = num;
@@ -4275,6 +4344,20 @@ class GitPackIndex {
4275
4344
  offset,
4276
4345
  };
4277
4346
  }
4347
+
4348
+ // The object is already inflated, so get its oid now. Then the loop below
4349
+ // does not inflate it again. That loop reads all other objects.
4350
+ const o = offsetToObject[offset];
4351
+ if (!o) return
4352
+ const resolved = resolveInflated(
4353
+ { type, data, reference, offset },
4354
+ cache,
4355
+ offsetByOid
4356
+ );
4357
+ if (!resolved) return
4358
+ o.oid = await shasum(GitObject.wrap(resolved));
4359
+ offsetByOid.set(o.oid, offset);
4360
+ cache.set(offset, resolved);
4278
4361
  });
4279
4362
 
4280
4363
  // We need to know the lengths of the slices to compute the CRCs.
@@ -4318,20 +4401,21 @@ class GitPackIndex {
4318
4401
  lastPercent = percent;
4319
4402
 
4320
4403
  const o = offsetToObject[offset];
4321
- if (o.oid) continue
4322
- try {
4323
- p.readDepth = 0;
4324
- p.externalReadDepth = 0;
4325
- const { type, object } = await p.readSlice({ start: offset });
4326
- objectsByDepth[p.readDepth] += 1;
4327
- const oid = await shasum(GitObject.wrap({ type, object }));
4328
- o.oid = oid;
4329
- hashes.push(oid);
4330
- offsets.set(oid, offset);
4331
- crcs[oid] = o.crc;
4332
- } catch (err) {
4333
- continue
4404
+ if (!o.oid) {
4405
+ // The first pass did not resolve this object, so read it.
4406
+ try {
4407
+ p.readDepth = 0;
4408
+ p.externalReadDepth = 0;
4409
+ const { type, object } = await p.readSlice({ start: offset });
4410
+ objectsByDepth[p.readDepth] += 1;
4411
+ o.oid = await shasum(GitObject.wrap({ type, object }));
4412
+ } catch (err) {
4413
+ continue
4414
+ }
4334
4415
  }
4416
+ hashes.push(o.oid);
4417
+ offsets.set(o.oid, offset);
4418
+ crcs[o.oid] = o.crc;
4335
4419
  }
4336
4420
 
4337
4421
  hashes.sort();