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.
package/index.js CHANGED
@@ -1830,9 +1830,6 @@ const SECTION_REGEX = /^[A-Za-z0-9-.]+$/;
1830
1830
  const VARIABLE_LINE_REGEX = /^([A-Za-z][A-Za-z0-9-]*)(?:\s*=\s*(.*))?$/;
1831
1831
  const VARIABLE_NAME_REGEX = /^[A-Za-z][A-Za-z0-9-]*$/;
1832
1832
 
1833
- // Comments start with either # or ; and extend to the end of line
1834
- const VARIABLE_VALUE_COMMENT_REGEX = /^(.*?)( *[#;].*)$/;
1835
-
1836
1833
  const extractSectionLine = line => {
1837
1834
  const matches = SECTION_LINE_REGEX.exec(line);
1838
1835
  if (matches != null) {
@@ -1846,43 +1843,61 @@ const extractVariableLine = line => {
1846
1843
  const matches = VARIABLE_LINE_REGEX.exec(line);
1847
1844
  if (matches != null) {
1848
1845
  const [name, rawValue = 'true'] = matches.slice(1);
1849
- const valueWithoutComments = removeComments(rawValue);
1850
- const valueWithoutQuotes = removeQuotes(valueWithoutComments);
1851
- return [name, valueWithoutQuotes]
1846
+ return [name, parseValue(rawValue)]
1852
1847
  }
1853
1848
  return null
1854
1849
  };
1855
1850
 
1856
- const removeComments = rawValue => {
1857
- const commentMatches = VARIABLE_VALUE_COMMENT_REGEX.exec(rawValue);
1858
- if (commentMatches == null) {
1859
- return rawValue
1860
- }
1861
- const [valueWithoutComment, comment] = commentMatches.slice(1);
1862
- // if odd number of quotes before and after comment => comment is escaped
1863
- if (
1864
- hasOddNumberOfQuotes(valueWithoutComment) &&
1865
- hasOddNumberOfQuotes(comment)
1866
- ) {
1867
- return `${valueWithoutComment}${comment}`
1851
+ const ESCAPED_CHARS = new Map([
1852
+ ['t', '\t'],
1853
+ ['b', '\b'],
1854
+ ['n', '\n'],
1855
+ ['\\', '\\'],
1856
+ ['"', '"'],
1857
+ ]);
1858
+
1859
+ // This follows parse_value in config.c of canonical git: double quotes are
1860
+ // removed, # and ; outside of quotes start a comment, whitespace outside of
1861
+ // quotes is kept between words but trimmed at the end, and \t, \b, \n, \\ and
1862
+ // \" are unescaped. Canonical git rejects any other escape sequence, here the
1863
+ // backslash is kept as is.
1864
+ const parseValue = rawValue => {
1865
+ let value = '';
1866
+ let pendingSpace = '';
1867
+ let inQuotes = false;
1868
+ for (let i = 0; i < rawValue.length; i++) {
1869
+ const c = rawValue[i];
1870
+ if (!inQuotes && /[ \t\r\v\f]/.test(c)) {
1871
+ if (value !== '') pendingSpace += c;
1872
+ continue
1873
+ }
1874
+ if (!inQuotes && (c === '#' || c === ';')) break
1875
+ value += pendingSpace;
1876
+ pendingSpace = '';
1877
+ if (c === '\\' && ESCAPED_CHARS.has(rawValue[i + 1])) {
1878
+ value += ESCAPED_CHARS.get(rawValue[++i]);
1879
+ } else if (c === '"') {
1880
+ inQuotes = !inQuotes;
1881
+ } else {
1882
+ value += c;
1883
+ }
1868
1884
  }
1869
- return valueWithoutComment
1870
- };
1871
-
1872
- const hasOddNumberOfQuotes = text => {
1873
- const numberOfQuotes = (text.match(/(?:^|[^\\])"/g) || []).length;
1874
- return numberOfQuotes % 2 !== 0
1885
+ return value
1875
1886
  };
1876
1887
 
1877
- const removeQuotes = text => {
1878
- return text.split('').reduce((newText, c, idx, text) => {
1879
- const isQuote = c === '"' && text[idx - 1] !== '\\';
1880
- const isEscapeForQuote = c === '\\' && text[idx + 1] === '"';
1881
- if (isQuote || isEscapeForQuote) {
1882
- return newText
1883
- }
1884
- return newText + c
1885
- }, '')
1888
+ // This follows write_pair in config.c of canonical git.
1889
+ const serializeValue = value => {
1890
+ if (typeof value !== 'string') return value
1891
+ const quote =
1892
+ value.startsWith(' ') || value.endsWith(' ') || /[#;]/.test(value)
1893
+ ? '"'
1894
+ : '';
1895
+ const escaped = value.replace(/[\\"\n\t]/g, c => {
1896
+ if (c === '\n') return '\\n'
1897
+ if (c === '\t') return '\\t'
1898
+ return `\\${c}`
1899
+ });
1900
+ return `${quote}${escaped}${quote}`
1886
1901
  };
1887
1902
 
1888
1903
  const lower = text => {
@@ -2054,11 +2069,7 @@ class GitConfig {
2054
2069
  return line
2055
2070
  }
2056
2071
  if (name != null && value != null) {
2057
- if (typeof value === 'string' && /[#;]/.test(value)) {
2058
- // A `#` or `;` symbol denotes a comment, so we have to wrap it in double quotes
2059
- return `\t${name} = "${value}"`
2060
- }
2061
- return `\t${name} = ${value}`
2072
+ return `\t${name} = ${serializeValue(value)}`
2062
2073
  }
2063
2074
  if (subsection != null) {
2064
2075
  return `[${section} "${subsection}"]`
@@ -2477,8 +2488,9 @@ class GitRefManager {
2477
2488
  ref: 'HEAD',
2478
2489
  depth: 2,
2479
2490
  });
2480
- } catch (_) {
2481
- return false
2491
+ } catch (err) {
2492
+ if (err instanceof NotFoundError) return false
2493
+ throw err
2482
2494
  }
2483
2495
  if (!target.startsWith('refs/heads/')) return false
2484
2496
  return ref === 'HEAD' || refpaths(ref).includes(target)
@@ -2498,7 +2510,8 @@ class GitRefManager {
2498
2510
  await GitRefManager.expand({ fs, gitdir, ref });
2499
2511
  return true
2500
2512
  } catch (err) {
2501
- return false
2513
+ if (err instanceof NotFoundError) return false
2514
+ throw err
2502
2515
  }
2503
2516
  }
2504
2517
 
@@ -2618,7 +2631,7 @@ class GitRefManager {
2618
2631
  * @returns {Promise<string[]>} - A sorted list of refs.
2619
2632
  */
2620
2633
  static async listRefs({ fs, gitdir, filepath }) {
2621
- const packedMap = GitRefManager.packedRefs({ fs, gitdir });
2634
+ const packedMap = await GitRefManager.packedRefs({ fs, gitdir });
2622
2635
  let files = null;
2623
2636
  try {
2624
2637
  files = await fs.readdirDeep(`${gitdir}/${filepath}`);
@@ -2627,7 +2640,7 @@ class GitRefManager {
2627
2640
  files = [];
2628
2641
  }
2629
2642
 
2630
- for (let key of (await packedMap).keys()) {
2643
+ for (let key of packedMap.keys()) {
2631
2644
  // filter by prefix
2632
2645
  if (key.startsWith(filepath)) {
2633
2646
  // remove prefix
@@ -3690,6 +3703,12 @@ function readPackIndex({
3690
3703
  emitterPrefix,
3691
3704
  });
3692
3705
  cache[PackfileCache].set(filename, p);
3706
+ // Let the next call retry a failed load instead of reusing it.
3707
+ p.catch(() => {
3708
+ if (cache[PackfileCache].get(filename) === p) {
3709
+ cache[PackfileCache].delete(filename);
3710
+ }
3711
+ });
3693
3712
  }
3694
3713
  return p
3695
3714
  }
@@ -3727,7 +3746,12 @@ async function readObjectPacked({
3727
3746
  // Derive the .pack path from the .idx path
3728
3747
  const packFile = indexFile.replace(/idx$/, 'pack');
3729
3748
  if (!p.pack) {
3730
- p.pack = fs.read(packFile);
3749
+ const read = fs.read(packFile);
3750
+ p.pack = read;
3751
+ // Let the next call retry a failed read instead of reusing it.
3752
+ read.catch(() => {
3753
+ if (p.pack === read) p.pack = null;
3754
+ });
3731
3755
  }
3732
3756
  const pack = await p.pack;
3733
3757
  if (!pack) {
@@ -5357,6 +5381,14 @@ function isFunction(obj) {
5357
5381
  return typeof obj === 'function'
5358
5382
  }
5359
5383
 
5384
+ // React Native file systems on iOS report a missing file with codes like
5385
+ // ENSCOCOAERRORDOMAIN260 instead of ENOENT.
5386
+ function isNotFoundError(err) {
5387
+ const code = err.code;
5388
+ if (typeof code !== 'string') return false
5389
+ return code === 'ENOENT' || code === 'ENOTDIR' || code.includes('ENS')
5390
+ }
5391
+
5360
5392
  function isPromiseFs(fs) {
5361
5393
  const test = targetFs => {
5362
5394
  try {
@@ -5446,11 +5478,7 @@ class FileSystem {
5446
5478
  await this._stat(filepath);
5447
5479
  return true
5448
5480
  } catch (err) {
5449
- if (
5450
- err.code === 'ENOENT' ||
5451
- err.code === 'ENOTDIR' ||
5452
- (err.code || '').includes('ENS')
5453
- ) {
5481
+ if (isNotFoundError(err)) {
5454
5482
  return false
5455
5483
  } else {
5456
5484
  console.log('Unhandled error in "FileSystem.exists()" function', err);
@@ -5461,31 +5489,38 @@ class FileSystem {
5461
5489
 
5462
5490
  /**
5463
5491
  * Return the contents of a file if it exists, otherwise returns null.
5492
+ * Rethrows errors that aren't related to file existence.
5464
5493
  *
5465
5494
  * @param {string} filepath - The path to the file.
5466
5495
  * @param {Object} [options] - Options for reading the file.
5467
5496
  * @returns {Promise<Buffer|string|null>} - The file contents, or `null` if the file doesn't exist.
5468
5497
  */
5469
5498
  async read(filepath, options = {}) {
5499
+ let buffer;
5470
5500
  try {
5471
- let buffer = await this._readFile(filepath, options);
5472
- if (options.autocrlf === 'true') {
5473
- try {
5474
- buffer = new TextDecoder('utf8', { fatal: true }).decode(buffer);
5475
- buffer = buffer.replace(/\r\n/g, '\n');
5476
- buffer = new TextEncoder().encode(buffer);
5477
- } catch (error) {
5478
- // non utf8 file
5479
- }
5480
- }
5481
- // Convert plain ArrayBuffers to Buffers
5482
- if (typeof buffer !== 'string') {
5483
- buffer = Buffer.from(buffer);
5484
- }
5485
- return buffer
5501
+ buffer = await this._readFile(filepath, options);
5486
5502
  } catch (err) {
5487
- return null
5503
+ // Ref lookup probes candidate paths that may be missing, may be a
5504
+ // directory (`refs/remotes/origin`), or may run through a file.
5505
+ if (isNotFoundError(err) || err.code === 'EISDIR') return null
5506
+ throw err
5488
5507
  }
5508
+ // LightningFS resolves a directory read to undefined instead of EISDIR.
5509
+ if (buffer == null) return null
5510
+ if (options.autocrlf === 'true') {
5511
+ try {
5512
+ buffer = new TextDecoder('utf8', { fatal: true }).decode(buffer);
5513
+ buffer = buffer.replace(/\r\n/g, '\n');
5514
+ buffer = new TextEncoder().encode(buffer);
5515
+ } catch (error) {
5516
+ // non utf8 file
5517
+ }
5518
+ }
5519
+ // Convert plain ArrayBuffers to Buffers
5520
+ if (typeof buffer !== 'string') {
5521
+ buffer = Buffer.from(buffer);
5522
+ }
5523
+ return buffer
5489
5524
  }
5490
5525
 
5491
5526
  /**
@@ -5557,22 +5592,25 @@ class FileSystem {
5557
5592
  }
5558
5593
 
5559
5594
  /**
5560
- * Read a directory without throwing an error is the directory doesn't exist
5595
+ * Read a directory without throwing an error if the directory doesn't exist.
5596
+ * Rethrows errors that aren't related to directory existence.
5561
5597
  *
5562
5598
  * @param {string} filepath - The path to the directory.
5563
5599
  * @returns {Promise<string[]|null>} - An array of file names, or `null` if the path is not a directory.
5564
5600
  */
5565
5601
  async readdir(filepath) {
5602
+ let names;
5566
5603
  try {
5567
- const names = await this._readdir(filepath);
5568
- // Ordering is not guaranteed, and system specific (Windows vs Unix)
5569
- // so we must sort them ourselves.
5570
- names.sort(compareStrings);
5571
- return names
5604
+ names = await this._readdir(filepath);
5572
5605
  } catch (err) {
5573
5606
  if (err.code === 'ENOTDIR') return null
5574
- return []
5607
+ if (isNotFoundError(err)) return []
5608
+ throw err
5575
5609
  }
5610
+ // Ordering is not guaranteed, and system specific (Windows vs Unix)
5611
+ // so we must sort them ourselves.
5612
+ names.sort(compareStrings);
5613
+ return names
5576
5614
  }
5577
5615
 
5578
5616
  /**
@@ -5609,7 +5647,7 @@ class FileSystem {
5609
5647
  const stats = await this._lstat(filename);
5610
5648
  return stats
5611
5649
  } catch (err) {
5612
- if (err.code === 'ENOENT' || (err.code || '').includes('ENS')) {
5650
+ if (isNotFoundError(err)) {
5613
5651
  return null
5614
5652
  }
5615
5653
  throw err
@@ -5631,7 +5669,7 @@ class FileSystem {
5631
5669
  const link = await this._readlink(filename, opts);
5632
5670
  return Buffer.isBuffer(link) ? link : Buffer.from(link)
5633
5671
  } catch (err) {
5634
- if (err.code === 'ENOENT' || (err.code || '').includes('ENS')) {
5672
+ if (isNotFoundError(err)) {
5635
5673
  return null
5636
5674
  }
5637
5675
  throw err
@@ -5890,12 +5928,7 @@ class GitIgnoreManager {
5890
5928
  }
5891
5929
  let ignoredStatus = false;
5892
5930
  for (const [index, p] of pairs.entries()) {
5893
- let file;
5894
- try {
5895
- file = await fs.read(p.gitignore, 'utf8');
5896
- } catch (err) {
5897
- if (err.code === 'NOENT') continue
5898
- }
5931
+ const file = await fs.read(p.gitignore, 'utf8');
5899
5932
  const ign = ignore();
5900
5933
  // The patterns in .git/info/exclude are relative to the working directory,
5901
5934
  // so only the first pair, which holds the full filepath, can use them.
@@ -6454,7 +6487,8 @@ async function _commit({
6454
6487
  gitdir,
6455
6488
  ref,
6456
6489
  });
6457
- } catch {
6490
+ } catch (err) {
6491
+ if (!(err instanceof NotFoundError)) throw err
6458
6492
  // We assume that there's no commit and this is the initial commit
6459
6493
  initialCommit = true;
6460
6494
  }
@@ -7264,6 +7298,7 @@ async function _branch({
7264
7298
  oid = await GitRefManager.resolve({ fs, gitdir, ref: object || 'HEAD' });
7265
7299
  } catch (e) {
7266
7300
  // Probably an empty repo
7301
+ if (!(e instanceof NotFoundError)) throw e
7267
7302
  }
7268
7303
 
7269
7304
  // Create a new ref that points at the current commit,
@@ -7437,7 +7472,7 @@ async function _checkout({
7437
7472
  // TODO: Figure out what to do if both 'ref' and 'remote' are specified, ref already exists,
7438
7473
  // and is configured to track a different remote.
7439
7474
  } catch (err) {
7440
- if (ref === 'HEAD') throw err
7475
+ if (ref === 'HEAD' || !(err instanceof NotFoundError)) throw err
7441
7476
  // If `ref` doesn't exist, create a new remote tracking branch
7442
7477
  // Figure out the commit to checkout
7443
7478
  const remoteRef = `${remote}/${ref}`;
@@ -9374,8 +9409,9 @@ async function _currentBranch({
9374
9409
  if (test) {
9375
9410
  try {
9376
9411
  await GitRefManager.resolve({ fs, gitdir, ref });
9377
- } catch (_) {
9378
- return
9412
+ } catch (err) {
9413
+ if (err instanceof NotFoundError) return
9414
+ throw err
9379
9415
  }
9380
9416
  }
9381
9417
  // Return `undefined` for detached HEAD
@@ -10077,8 +10113,8 @@ function filterCapabilities(server, client) {
10077
10113
 
10078
10114
  const pkg = {
10079
10115
  name: 'isomorphic-git',
10080
- version: '1.44.2',
10081
- agent: 'git/isomorphic-git@1.44.2',
10116
+ version: '1.44.4',
10117
+ agent: 'git/isomorphic-git@1.44.4',
10082
10118
  };
10083
10119
 
10084
10120
  class FIFO {
@@ -13215,6 +13251,7 @@ async function _listNotes({ fs, cache, gitdir, ref }) {
13215
13251
  if (err instanceof NotFoundError) {
13216
13252
  return []
13217
13253
  }
13254
+ throw err
13218
13255
  }
13219
13256
 
13220
13257
  // Create the current note tree
@@ -16105,10 +16142,8 @@ async function resetIndex({
16105
16142
  ref: ref || 'HEAD',
16106
16143
  });
16107
16144
  } catch (e) {
16108
- if (ref) {
16109
- // Only throw the error if a ref is explicitly provided
16110
- throw e
16111
- }
16145
+ // Without an explicit ref, a branch with no commits yet is fine
16146
+ if (ref || !(e instanceof NotFoundError)) throw e
16112
16147
  }
16113
16148
 
16114
16149
  // Not having an oid at this point means `resetIndex()` was called without explicit `ref` on a new git
@@ -16124,7 +16159,14 @@ async function resetIndex({
16124
16159
  filepath,
16125
16160
  });
16126
16161
  } catch (e) {
16127
- // This means we're resetting the file to a "deleted" state
16162
+ // The path is not in the commit, so reset it to a "deleted" state
16163
+ if (
16164
+ !(e instanceof NotFoundError) &&
16165
+ !(e instanceof InvalidFilepathError) &&
16166
+ !(e instanceof ObjectTypeError && e.data.filepath)
16167
+ ) {
16168
+ throw e
16169
+ }
16128
16170
  oid = null;
16129
16171
  }
16130
16172
  }
@@ -17022,8 +17064,7 @@ async function status({
17022
17064
  // size of -1 happens over a BrowserFS HTTP Backend that doesn't serve Content-Length headers
17023
17065
  // (like the Karma webserver) because BrowserFS HTTP Backend uses HTTP HEAD requests to do fs.stat
17024
17066
  if (stats.size !== -1) {
17025
- // We don't await this so we can return faster for one-off cases.
17026
- GitIndexManager.acquire(
17067
+ await GitIndexManager.acquire(
17027
17068
  { fs, gitdir: updatedGitdir, cache },
17028
17069
  async function (index) {
17029
17070
  index.insert({ filepath, stats, oid: workdirOid });
@@ -17151,6 +17192,7 @@ async function getHeadTree({ fs, cache, gitdir: updatedGitdir }) {
17151
17192
  if (e instanceof NotFoundError) {
17152
17193
  return []
17153
17194
  }
17195
+ throw e
17154
17196
  }
17155
17197
  const { tree } = await _readTree({ fs, cache, gitdir: updatedGitdir, oid });
17156
17198
  return tree