isomorphic-git 1.44.2 → 1.44.4

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.
@@ -407,9 +407,6 @@ const SECTION_REGEX = /^[A-Za-z0-9-.]+$/;
407
407
  const VARIABLE_LINE_REGEX = /^([A-Za-z][A-Za-z0-9-]*)(?:\s*=\s*(.*))?$/;
408
408
  const VARIABLE_NAME_REGEX = /^[A-Za-z][A-Za-z0-9-]*$/;
409
409
 
410
- // Comments start with either # or ; and extend to the end of line
411
- const VARIABLE_VALUE_COMMENT_REGEX = /^(.*?)( *[#;].*)$/;
412
-
413
410
  const extractSectionLine = line => {
414
411
  const matches = SECTION_LINE_REGEX.exec(line);
415
412
  if (matches != null) {
@@ -423,43 +420,61 @@ const extractVariableLine = line => {
423
420
  const matches = VARIABLE_LINE_REGEX.exec(line);
424
421
  if (matches != null) {
425
422
  const [name, rawValue = 'true'] = matches.slice(1);
426
- const valueWithoutComments = removeComments(rawValue);
427
- const valueWithoutQuotes = removeQuotes(valueWithoutComments);
428
- return [name, valueWithoutQuotes]
423
+ return [name, parseValue(rawValue)]
429
424
  }
430
425
  return null
431
426
  };
432
427
 
433
- const removeComments = rawValue => {
434
- const commentMatches = VARIABLE_VALUE_COMMENT_REGEX.exec(rawValue);
435
- if (commentMatches == null) {
436
- return rawValue
437
- }
438
- const [valueWithoutComment, comment] = commentMatches.slice(1);
439
- // if odd number of quotes before and after comment => comment is escaped
440
- if (
441
- hasOddNumberOfQuotes(valueWithoutComment) &&
442
- hasOddNumberOfQuotes(comment)
443
- ) {
444
- return `${valueWithoutComment}${comment}`
428
+ const ESCAPED_CHARS = new Map([
429
+ ['t', '\t'],
430
+ ['b', '\b'],
431
+ ['n', '\n'],
432
+ ['\\', '\\'],
433
+ ['"', '"'],
434
+ ]);
435
+
436
+ // This follows parse_value in config.c of canonical git: double quotes are
437
+ // removed, # and ; outside of quotes start a comment, whitespace outside of
438
+ // quotes is kept between words but trimmed at the end, and \t, \b, \n, \\ and
439
+ // \" are unescaped. Canonical git rejects any other escape sequence, here the
440
+ // backslash is kept as is.
441
+ const parseValue = rawValue => {
442
+ let value = '';
443
+ let pendingSpace = '';
444
+ let inQuotes = false;
445
+ for (let i = 0; i < rawValue.length; i++) {
446
+ const c = rawValue[i];
447
+ if (!inQuotes && /[ \t\r\v\f]/.test(c)) {
448
+ if (value !== '') pendingSpace += c;
449
+ continue
450
+ }
451
+ if (!inQuotes && (c === '#' || c === ';')) break
452
+ value += pendingSpace;
453
+ pendingSpace = '';
454
+ if (c === '\\' && ESCAPED_CHARS.has(rawValue[i + 1])) {
455
+ value += ESCAPED_CHARS.get(rawValue[++i]);
456
+ } else if (c === '"') {
457
+ inQuotes = !inQuotes;
458
+ } else {
459
+ value += c;
460
+ }
445
461
  }
446
- return valueWithoutComment
447
- };
448
-
449
- const hasOddNumberOfQuotes = text => {
450
- const numberOfQuotes = (text.match(/(?:^|[^\\])"/g) || []).length;
451
- return numberOfQuotes % 2 !== 0
462
+ return value
452
463
  };
453
464
 
454
- const removeQuotes = text => {
455
- return text.split('').reduce((newText, c, idx, text) => {
456
- const isQuote = c === '"' && text[idx - 1] !== '\\';
457
- const isEscapeForQuote = c === '\\' && text[idx + 1] === '"';
458
- if (isQuote || isEscapeForQuote) {
459
- return newText
460
- }
461
- return newText + c
462
- }, '')
465
+ // This follows write_pair in config.c of canonical git.
466
+ const serializeValue = value => {
467
+ if (typeof value !== 'string') return value
468
+ const quote =
469
+ value.startsWith(' ') || value.endsWith(' ') || /[#;]/.test(value)
470
+ ? '"'
471
+ : '';
472
+ const escaped = value.replace(/[\\"\n\t]/g, c => {
473
+ if (c === '\n') return '\\n'
474
+ if (c === '\t') return '\\t'
475
+ return `\\${c}`
476
+ });
477
+ return `${quote}${escaped}${quote}`
463
478
  };
464
479
 
465
480
  const lower = text => {
@@ -631,11 +646,7 @@ class GitConfig {
631
646
  return line
632
647
  }
633
648
  if (name != null && value != null) {
634
- if (typeof value === 'string' && /[#;]/.test(value)) {
635
- // A `#` or `;` symbol denotes a comment, so we have to wrap it in double quotes
636
- return `\t${name} = "${value}"`
637
- }
638
- return `\t${name} = ${value}`
649
+ return `\t${name} = ${serializeValue(value)}`
639
650
  }
640
651
  if (subsection != null) {
641
652
  return `[${section} "${subsection}"]`
@@ -888,12 +899,7 @@ class GitIgnoreManager {
888
899
  }
889
900
  let ignoredStatus = false;
890
901
  for (const [index, p] of pairs.entries()) {
891
- let file;
892
- try {
893
- file = await fs.read(p.gitignore, 'utf8');
894
- } catch (err) {
895
- if (err.code === 'NOENT') continue
896
- }
902
+ const file = await fs.read(p.gitignore, 'utf8');
897
903
  const ign = ignore();
898
904
  // The patterns in .git/info/exclude are relative to the working directory,
899
905
  // so only the first pair, which holds the full filepath, can use them.
@@ -2331,8 +2337,9 @@ class GitRefManager {
2331
2337
  ref: 'HEAD',
2332
2338
  depth: 2,
2333
2339
  });
2334
- } catch (_) {
2335
- return false
2340
+ } catch (err) {
2341
+ if (err instanceof NotFoundError) return false
2342
+ throw err
2336
2343
  }
2337
2344
  if (!target.startsWith('refs/heads/')) return false
2338
2345
  return ref === 'HEAD' || refpaths(ref).includes(target)
@@ -2352,7 +2359,8 @@ class GitRefManager {
2352
2359
  await GitRefManager.expand({ fs, gitdir, ref });
2353
2360
  return true
2354
2361
  } catch (err) {
2355
- return false
2362
+ if (err instanceof NotFoundError) return false
2363
+ throw err
2356
2364
  }
2357
2365
  }
2358
2366
 
@@ -2472,7 +2480,7 @@ class GitRefManager {
2472
2480
  * @returns {Promise<string[]>} - A sorted list of refs.
2473
2481
  */
2474
2482
  static async listRefs({ fs, gitdir, filepath }) {
2475
- const packedMap = GitRefManager.packedRefs({ fs, gitdir });
2483
+ const packedMap = await GitRefManager.packedRefs({ fs, gitdir });
2476
2484
  let files = null;
2477
2485
  try {
2478
2486
  files = await fs.readdirDeep(`${gitdir}/${filepath}`);
@@ -2481,7 +2489,7 @@ class GitRefManager {
2481
2489
  files = [];
2482
2490
  }
2483
2491
 
2484
- for (let key of (await packedMap).keys()) {
2492
+ for (let key of packedMap.keys()) {
2485
2493
  // filter by prefix
2486
2494
  if (key.startsWith(filepath)) {
2487
2495
  // remove prefix
@@ -4507,6 +4515,12 @@ function readPackIndex({
4507
4515
  emitterPrefix,
4508
4516
  });
4509
4517
  cache[PackfileCache].set(filename, p);
4518
+ // Let the next call retry a failed load instead of reusing it.
4519
+ p.catch(() => {
4520
+ if (cache[PackfileCache].get(filename) === p) {
4521
+ cache[PackfileCache].delete(filename);
4522
+ }
4523
+ });
4510
4524
  }
4511
4525
  return p
4512
4526
  }
@@ -4550,7 +4564,12 @@ async function readObjectPacked({
4550
4564
  // Derive the .pack path from the .idx path
4551
4565
  const packFile = indexFile.replace(/idx$/, 'pack');
4552
4566
  if (!p.pack) {
4553
- p.pack = fs.read(packFile);
4567
+ const read = fs.read(packFile);
4568
+ p.pack = read;
4569
+ // Let the next call retry a failed read instead of reusing it.
4570
+ read.catch(() => {
4571
+ if (p.pack === read) p.pack = null;
4572
+ });
4554
4573
  }
4555
4574
  const pack = await p.pack;
4556
4575
  if (!pack) {
package/managers/index.js CHANGED
@@ -400,9 +400,6 @@ const SECTION_REGEX = /^[A-Za-z0-9-.]+$/;
400
400
  const VARIABLE_LINE_REGEX = /^([A-Za-z][A-Za-z0-9-]*)(?:\s*=\s*(.*))?$/;
401
401
  const VARIABLE_NAME_REGEX = /^[A-Za-z][A-Za-z0-9-]*$/;
402
402
 
403
- // Comments start with either # or ; and extend to the end of line
404
- const VARIABLE_VALUE_COMMENT_REGEX = /^(.*?)( *[#;].*)$/;
405
-
406
403
  const extractSectionLine = line => {
407
404
  const matches = SECTION_LINE_REGEX.exec(line);
408
405
  if (matches != null) {
@@ -416,43 +413,61 @@ const extractVariableLine = line => {
416
413
  const matches = VARIABLE_LINE_REGEX.exec(line);
417
414
  if (matches != null) {
418
415
  const [name, rawValue = 'true'] = matches.slice(1);
419
- const valueWithoutComments = removeComments(rawValue);
420
- const valueWithoutQuotes = removeQuotes(valueWithoutComments);
421
- return [name, valueWithoutQuotes]
416
+ return [name, parseValue(rawValue)]
422
417
  }
423
418
  return null
424
419
  };
425
420
 
426
- const removeComments = rawValue => {
427
- const commentMatches = VARIABLE_VALUE_COMMENT_REGEX.exec(rawValue);
428
- if (commentMatches == null) {
429
- return rawValue
430
- }
431
- const [valueWithoutComment, comment] = commentMatches.slice(1);
432
- // if odd number of quotes before and after comment => comment is escaped
433
- if (
434
- hasOddNumberOfQuotes(valueWithoutComment) &&
435
- hasOddNumberOfQuotes(comment)
436
- ) {
437
- return `${valueWithoutComment}${comment}`
421
+ const ESCAPED_CHARS = new Map([
422
+ ['t', '\t'],
423
+ ['b', '\b'],
424
+ ['n', '\n'],
425
+ ['\\', '\\'],
426
+ ['"', '"'],
427
+ ]);
428
+
429
+ // This follows parse_value in config.c of canonical git: double quotes are
430
+ // removed, # and ; outside of quotes start a comment, whitespace outside of
431
+ // quotes is kept between words but trimmed at the end, and \t, \b, \n, \\ and
432
+ // \" are unescaped. Canonical git rejects any other escape sequence, here the
433
+ // backslash is kept as is.
434
+ const parseValue = rawValue => {
435
+ let value = '';
436
+ let pendingSpace = '';
437
+ let inQuotes = false;
438
+ for (let i = 0; i < rawValue.length; i++) {
439
+ const c = rawValue[i];
440
+ if (!inQuotes && /[ \t\r\v\f]/.test(c)) {
441
+ if (value !== '') pendingSpace += c;
442
+ continue
443
+ }
444
+ if (!inQuotes && (c === '#' || c === ';')) break
445
+ value += pendingSpace;
446
+ pendingSpace = '';
447
+ if (c === '\\' && ESCAPED_CHARS.has(rawValue[i + 1])) {
448
+ value += ESCAPED_CHARS.get(rawValue[++i]);
449
+ } else if (c === '"') {
450
+ inQuotes = !inQuotes;
451
+ } else {
452
+ value += c;
453
+ }
438
454
  }
439
- return valueWithoutComment
440
- };
441
-
442
- const hasOddNumberOfQuotes = text => {
443
- const numberOfQuotes = (text.match(/(?:^|[^\\])"/g) || []).length;
444
- return numberOfQuotes % 2 !== 0
455
+ return value
445
456
  };
446
457
 
447
- const removeQuotes = text => {
448
- return text.split('').reduce((newText, c, idx, text) => {
449
- const isQuote = c === '"' && text[idx - 1] !== '\\';
450
- const isEscapeForQuote = c === '\\' && text[idx + 1] === '"';
451
- if (isQuote || isEscapeForQuote) {
452
- return newText
453
- }
454
- return newText + c
455
- }, '')
458
+ // This follows write_pair in config.c of canonical git.
459
+ const serializeValue = value => {
460
+ if (typeof value !== 'string') return value
461
+ const quote =
462
+ value.startsWith(' ') || value.endsWith(' ') || /[#;]/.test(value)
463
+ ? '"'
464
+ : '';
465
+ const escaped = value.replace(/[\\"\n\t]/g, c => {
466
+ if (c === '\n') return '\\n'
467
+ if (c === '\t') return '\\t'
468
+ return `\\${c}`
469
+ });
470
+ return `${quote}${escaped}${quote}`
456
471
  };
457
472
 
458
473
  const lower = text => {
@@ -624,11 +639,7 @@ class GitConfig {
624
639
  return line
625
640
  }
626
641
  if (name != null && value != null) {
627
- if (typeof value === 'string' && /[#;]/.test(value)) {
628
- // A `#` or `;` symbol denotes a comment, so we have to wrap it in double quotes
629
- return `\t${name} = "${value}"`
630
- }
631
- return `\t${name} = ${value}`
642
+ return `\t${name} = ${serializeValue(value)}`
632
643
  }
633
644
  if (subsection != null) {
634
645
  return `[${section} "${subsection}"]`
@@ -881,12 +892,7 @@ class GitIgnoreManager {
881
892
  }
882
893
  let ignoredStatus = false;
883
894
  for (const [index, p] of pairs.entries()) {
884
- let file;
885
- try {
886
- file = await fs.read(p.gitignore, 'utf8');
887
- } catch (err) {
888
- if (err.code === 'NOENT') continue
889
- }
895
+ const file = await fs.read(p.gitignore, 'utf8');
890
896
  const ign = ignore();
891
897
  // The patterns in .git/info/exclude are relative to the working directory,
892
898
  // so only the first pair, which holds the full filepath, can use them.
@@ -2324,8 +2330,9 @@ class GitRefManager {
2324
2330
  ref: 'HEAD',
2325
2331
  depth: 2,
2326
2332
  });
2327
- } catch (_) {
2328
- return false
2333
+ } catch (err) {
2334
+ if (err instanceof NotFoundError) return false
2335
+ throw err
2329
2336
  }
2330
2337
  if (!target.startsWith('refs/heads/')) return false
2331
2338
  return ref === 'HEAD' || refpaths(ref).includes(target)
@@ -2345,7 +2352,8 @@ class GitRefManager {
2345
2352
  await GitRefManager.expand({ fs, gitdir, ref });
2346
2353
  return true
2347
2354
  } catch (err) {
2348
- return false
2355
+ if (err instanceof NotFoundError) return false
2356
+ throw err
2349
2357
  }
2350
2358
  }
2351
2359
 
@@ -2465,7 +2473,7 @@ class GitRefManager {
2465
2473
  * @returns {Promise<string[]>} - A sorted list of refs.
2466
2474
  */
2467
2475
  static async listRefs({ fs, gitdir, filepath }) {
2468
- const packedMap = GitRefManager.packedRefs({ fs, gitdir });
2476
+ const packedMap = await GitRefManager.packedRefs({ fs, gitdir });
2469
2477
  let files = null;
2470
2478
  try {
2471
2479
  files = await fs.readdirDeep(`${gitdir}/${filepath}`);
@@ -2474,7 +2482,7 @@ class GitRefManager {
2474
2482
  files = [];
2475
2483
  }
2476
2484
 
2477
- for (let key of (await packedMap).keys()) {
2485
+ for (let key of packedMap.keys()) {
2478
2486
  // filter by prefix
2479
2487
  if (key.startsWith(filepath)) {
2480
2488
  // remove prefix
@@ -4500,6 +4508,12 @@ function readPackIndex({
4500
4508
  emitterPrefix,
4501
4509
  });
4502
4510
  cache[PackfileCache].set(filename, p);
4511
+ // Let the next call retry a failed load instead of reusing it.
4512
+ p.catch(() => {
4513
+ if (cache[PackfileCache].get(filename) === p) {
4514
+ cache[PackfileCache].delete(filename);
4515
+ }
4516
+ });
4503
4517
  }
4504
4518
  return p
4505
4519
  }
@@ -4537,7 +4551,12 @@ async function readObjectPacked({
4537
4551
  // Derive the .pack path from the .idx path
4538
4552
  const packFile = indexFile.replace(/idx$/, 'pack');
4539
4553
  if (!p.pack) {
4540
- p.pack = fs.read(packFile);
4554
+ const read = fs.read(packFile);
4555
+ p.pack = read;
4556
+ // Let the next call retry a failed read instead of reusing it.
4557
+ read.catch(() => {
4558
+ if (p.pack === read) p.pack = null;
4559
+ });
4541
4560
  }
4542
4561
  const pack = await p.pack;
4543
4562
  if (!pack) {