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.cjs CHANGED
@@ -1837,9 +1837,6 @@ const SECTION_REGEX = /^[A-Za-z0-9-.]+$/;
1837
1837
  const VARIABLE_LINE_REGEX = /^([A-Za-z][A-Za-z0-9-]*)(?:\s*=\s*(.*))?$/;
1838
1838
  const VARIABLE_NAME_REGEX = /^[A-Za-z][A-Za-z0-9-]*$/;
1839
1839
 
1840
- // Comments start with either # or ; and extend to the end of line
1841
- const VARIABLE_VALUE_COMMENT_REGEX = /^(.*?)( *[#;].*)$/;
1842
-
1843
1840
  const extractSectionLine = line => {
1844
1841
  const matches = SECTION_LINE_REGEX.exec(line);
1845
1842
  if (matches != null) {
@@ -1853,43 +1850,61 @@ const extractVariableLine = line => {
1853
1850
  const matches = VARIABLE_LINE_REGEX.exec(line);
1854
1851
  if (matches != null) {
1855
1852
  const [name, rawValue = 'true'] = matches.slice(1);
1856
- const valueWithoutComments = removeComments(rawValue);
1857
- const valueWithoutQuotes = removeQuotes(valueWithoutComments);
1858
- return [name, valueWithoutQuotes]
1853
+ return [name, parseValue(rawValue)]
1859
1854
  }
1860
1855
  return null
1861
1856
  };
1862
1857
 
1863
- const removeComments = rawValue => {
1864
- const commentMatches = VARIABLE_VALUE_COMMENT_REGEX.exec(rawValue);
1865
- if (commentMatches == null) {
1866
- return rawValue
1867
- }
1868
- const [valueWithoutComment, comment] = commentMatches.slice(1);
1869
- // if odd number of quotes before and after comment => comment is escaped
1870
- if (
1871
- hasOddNumberOfQuotes(valueWithoutComment) &&
1872
- hasOddNumberOfQuotes(comment)
1873
- ) {
1874
- return `${valueWithoutComment}${comment}`
1858
+ const ESCAPED_CHARS = new Map([
1859
+ ['t', '\t'],
1860
+ ['b', '\b'],
1861
+ ['n', '\n'],
1862
+ ['\\', '\\'],
1863
+ ['"', '"'],
1864
+ ]);
1865
+
1866
+ // This follows parse_value in config.c of canonical git: double quotes are
1867
+ // removed, # and ; outside of quotes start a comment, whitespace outside of
1868
+ // quotes is kept between words but trimmed at the end, and \t, \b, \n, \\ and
1869
+ // \" are unescaped. Canonical git rejects any other escape sequence, here the
1870
+ // backslash is kept as is.
1871
+ const parseValue = rawValue => {
1872
+ let value = '';
1873
+ let pendingSpace = '';
1874
+ let inQuotes = false;
1875
+ for (let i = 0; i < rawValue.length; i++) {
1876
+ const c = rawValue[i];
1877
+ if (!inQuotes && /[ \t\r\v\f]/.test(c)) {
1878
+ if (value !== '') pendingSpace += c;
1879
+ continue
1880
+ }
1881
+ if (!inQuotes && (c === '#' || c === ';')) break
1882
+ value += pendingSpace;
1883
+ pendingSpace = '';
1884
+ if (c === '\\' && ESCAPED_CHARS.has(rawValue[i + 1])) {
1885
+ value += ESCAPED_CHARS.get(rawValue[++i]);
1886
+ } else if (c === '"') {
1887
+ inQuotes = !inQuotes;
1888
+ } else {
1889
+ value += c;
1890
+ }
1875
1891
  }
1876
- return valueWithoutComment
1877
- };
1878
-
1879
- const hasOddNumberOfQuotes = text => {
1880
- const numberOfQuotes = (text.match(/(?:^|[^\\])"/g) || []).length;
1881
- return numberOfQuotes % 2 !== 0
1892
+ return value
1882
1893
  };
1883
1894
 
1884
- const removeQuotes = text => {
1885
- return text.split('').reduce((newText, c, idx, text) => {
1886
- const isQuote = c === '"' && text[idx - 1] !== '\\';
1887
- const isEscapeForQuote = c === '\\' && text[idx + 1] === '"';
1888
- if (isQuote || isEscapeForQuote) {
1889
- return newText
1890
- }
1891
- return newText + c
1892
- }, '')
1895
+ // This follows write_pair in config.c of canonical git.
1896
+ const serializeValue = value => {
1897
+ if (typeof value !== 'string') return value
1898
+ const quote =
1899
+ value.startsWith(' ') || value.endsWith(' ') || /[#;]/.test(value)
1900
+ ? '"'
1901
+ : '';
1902
+ const escaped = value.replace(/[\\"\n\t]/g, c => {
1903
+ if (c === '\n') return '\\n'
1904
+ if (c === '\t') return '\\t'
1905
+ return `\\${c}`
1906
+ });
1907
+ return `${quote}${escaped}${quote}`
1893
1908
  };
1894
1909
 
1895
1910
  const lower = text => {
@@ -2061,11 +2076,7 @@ class GitConfig {
2061
2076
  return line
2062
2077
  }
2063
2078
  if (name != null && value != null) {
2064
- if (typeof value === 'string' && /[#;]/.test(value)) {
2065
- // A `#` or `;` symbol denotes a comment, so we have to wrap it in double quotes
2066
- return `\t${name} = "${value}"`
2067
- }
2068
- return `\t${name} = ${value}`
2079
+ return `\t${name} = ${serializeValue(value)}`
2069
2080
  }
2070
2081
  if (subsection != null) {
2071
2082
  return `[${section} "${subsection}"]`
@@ -2484,8 +2495,9 @@ class GitRefManager {
2484
2495
  ref: 'HEAD',
2485
2496
  depth: 2,
2486
2497
  });
2487
- } catch (_) {
2488
- return false
2498
+ } catch (err) {
2499
+ if (err instanceof NotFoundError) return false
2500
+ throw err
2489
2501
  }
2490
2502
  if (!target.startsWith('refs/heads/')) return false
2491
2503
  return ref === 'HEAD' || refpaths(ref).includes(target)
@@ -2505,7 +2517,8 @@ class GitRefManager {
2505
2517
  await GitRefManager.expand({ fs, gitdir, ref });
2506
2518
  return true
2507
2519
  } catch (err) {
2508
- return false
2520
+ if (err instanceof NotFoundError) return false
2521
+ throw err
2509
2522
  }
2510
2523
  }
2511
2524
 
@@ -2625,7 +2638,7 @@ class GitRefManager {
2625
2638
  * @returns {Promise<string[]>} - A sorted list of refs.
2626
2639
  */
2627
2640
  static async listRefs({ fs, gitdir, filepath }) {
2628
- const packedMap = GitRefManager.packedRefs({ fs, gitdir });
2641
+ const packedMap = await GitRefManager.packedRefs({ fs, gitdir });
2629
2642
  let files = null;
2630
2643
  try {
2631
2644
  files = await fs.readdirDeep(`${gitdir}/${filepath}`);
@@ -2634,7 +2647,7 @@ class GitRefManager {
2634
2647
  files = [];
2635
2648
  }
2636
2649
 
2637
- for (let key of (await packedMap).keys()) {
2650
+ for (let key of packedMap.keys()) {
2638
2651
  // filter by prefix
2639
2652
  if (key.startsWith(filepath)) {
2640
2653
  // remove prefix
@@ -3697,6 +3710,12 @@ function readPackIndex({
3697
3710
  emitterPrefix,
3698
3711
  });
3699
3712
  cache[PackfileCache].set(filename, p);
3713
+ // Let the next call retry a failed load instead of reusing it.
3714
+ p.catch(() => {
3715
+ if (cache[PackfileCache].get(filename) === p) {
3716
+ cache[PackfileCache].delete(filename);
3717
+ }
3718
+ });
3700
3719
  }
3701
3720
  return p
3702
3721
  }
@@ -3740,7 +3759,12 @@ async function readObjectPacked({
3740
3759
  // Derive the .pack path from the .idx path
3741
3760
  const packFile = indexFile.replace(/idx$/, 'pack');
3742
3761
  if (!p.pack) {
3743
- p.pack = fs.read(packFile);
3762
+ const read = fs.read(packFile);
3763
+ p.pack = read;
3764
+ // Let the next call retry a failed read instead of reusing it.
3765
+ read.catch(() => {
3766
+ if (p.pack === read) p.pack = null;
3767
+ });
3744
3768
  }
3745
3769
  const pack = await p.pack;
3746
3770
  if (!pack) {
@@ -5370,6 +5394,14 @@ function isFunction(obj) {
5370
5394
  return typeof obj === 'function'
5371
5395
  }
5372
5396
 
5397
+ // React Native file systems on iOS report a missing file with codes like
5398
+ // ENSCOCOAERRORDOMAIN260 instead of ENOENT.
5399
+ function isNotFoundError(err) {
5400
+ const code = err.code;
5401
+ if (typeof code !== 'string') return false
5402
+ return code === 'ENOENT' || code === 'ENOTDIR' || code.includes('ENS')
5403
+ }
5404
+
5373
5405
  function isPromiseFs(fs) {
5374
5406
  const test = targetFs => {
5375
5407
  try {
@@ -5459,11 +5491,7 @@ class FileSystem {
5459
5491
  await this._stat(filepath);
5460
5492
  return true
5461
5493
  } catch (err) {
5462
- if (
5463
- err.code === 'ENOENT' ||
5464
- err.code === 'ENOTDIR' ||
5465
- (err.code || '').includes('ENS')
5466
- ) {
5494
+ if (isNotFoundError(err)) {
5467
5495
  return false
5468
5496
  } else {
5469
5497
  console.log('Unhandled error in "FileSystem.exists()" function', err);
@@ -5474,31 +5502,38 @@ class FileSystem {
5474
5502
 
5475
5503
  /**
5476
5504
  * Return the contents of a file if it exists, otherwise returns null.
5505
+ * Rethrows errors that aren't related to file existence.
5477
5506
  *
5478
5507
  * @param {string} filepath - The path to the file.
5479
5508
  * @param {Object} [options] - Options for reading the file.
5480
5509
  * @returns {Promise<Buffer|string|null>} - The file contents, or `null` if the file doesn't exist.
5481
5510
  */
5482
5511
  async read(filepath, options = {}) {
5512
+ let buffer;
5483
5513
  try {
5484
- let buffer = await this._readFile(filepath, options);
5485
- if (options.autocrlf === 'true') {
5486
- try {
5487
- buffer = new TextDecoder('utf8', { fatal: true }).decode(buffer);
5488
- buffer = buffer.replace(/\r\n/g, '\n');
5489
- buffer = new TextEncoder().encode(buffer);
5490
- } catch (error) {
5491
- // non utf8 file
5492
- }
5493
- }
5494
- // Convert plain ArrayBuffers to Buffers
5495
- if (typeof buffer !== 'string') {
5496
- buffer = Buffer.from(buffer);
5497
- }
5498
- return buffer
5514
+ buffer = await this._readFile(filepath, options);
5499
5515
  } catch (err) {
5500
- return null
5516
+ // Ref lookup probes candidate paths that may be missing, may be a
5517
+ // directory (`refs/remotes/origin`), or may run through a file.
5518
+ if (isNotFoundError(err) || err.code === 'EISDIR') return null
5519
+ throw err
5501
5520
  }
5521
+ // LightningFS resolves a directory read to undefined instead of EISDIR.
5522
+ if (buffer == null) return null
5523
+ if (options.autocrlf === 'true') {
5524
+ try {
5525
+ buffer = new TextDecoder('utf8', { fatal: true }).decode(buffer);
5526
+ buffer = buffer.replace(/\r\n/g, '\n');
5527
+ buffer = new TextEncoder().encode(buffer);
5528
+ } catch (error) {
5529
+ // non utf8 file
5530
+ }
5531
+ }
5532
+ // Convert plain ArrayBuffers to Buffers
5533
+ if (typeof buffer !== 'string') {
5534
+ buffer = Buffer.from(buffer);
5535
+ }
5536
+ return buffer
5502
5537
  }
5503
5538
 
5504
5539
  /**
@@ -5570,22 +5605,25 @@ class FileSystem {
5570
5605
  }
5571
5606
 
5572
5607
  /**
5573
- * Read a directory without throwing an error is the directory doesn't exist
5608
+ * Read a directory without throwing an error if the directory doesn't exist.
5609
+ * Rethrows errors that aren't related to directory existence.
5574
5610
  *
5575
5611
  * @param {string} filepath - The path to the directory.
5576
5612
  * @returns {Promise<string[]|null>} - An array of file names, or `null` if the path is not a directory.
5577
5613
  */
5578
5614
  async readdir(filepath) {
5615
+ let names;
5579
5616
  try {
5580
- const names = await this._readdir(filepath);
5581
- // Ordering is not guaranteed, and system specific (Windows vs Unix)
5582
- // so we must sort them ourselves.
5583
- names.sort(compareStrings);
5584
- return names
5617
+ names = await this._readdir(filepath);
5585
5618
  } catch (err) {
5586
5619
  if (err.code === 'ENOTDIR') return null
5587
- return []
5620
+ if (isNotFoundError(err)) return []
5621
+ throw err
5588
5622
  }
5623
+ // Ordering is not guaranteed, and system specific (Windows vs Unix)
5624
+ // so we must sort them ourselves.
5625
+ names.sort(compareStrings);
5626
+ return names
5589
5627
  }
5590
5628
 
5591
5629
  /**
@@ -5622,7 +5660,7 @@ class FileSystem {
5622
5660
  const stats = await this._lstat(filename);
5623
5661
  return stats
5624
5662
  } catch (err) {
5625
- if (err.code === 'ENOENT' || (err.code || '').includes('ENS')) {
5663
+ if (isNotFoundError(err)) {
5626
5664
  return null
5627
5665
  }
5628
5666
  throw err
@@ -5644,7 +5682,7 @@ class FileSystem {
5644
5682
  const link = await this._readlink(filename, opts);
5645
5683
  return Buffer.isBuffer(link) ? link : Buffer.from(link)
5646
5684
  } catch (err) {
5647
- if (err.code === 'ENOENT' || (err.code || '').includes('ENS')) {
5685
+ if (isNotFoundError(err)) {
5648
5686
  return null
5649
5687
  }
5650
5688
  throw err
@@ -5903,12 +5941,7 @@ class GitIgnoreManager {
5903
5941
  }
5904
5942
  let ignoredStatus = false;
5905
5943
  for (const [index, p] of pairs.entries()) {
5906
- let file;
5907
- try {
5908
- file = await fs.read(p.gitignore, 'utf8');
5909
- } catch (err) {
5910
- if (err.code === 'NOENT') continue
5911
- }
5944
+ const file = await fs.read(p.gitignore, 'utf8');
5912
5945
  const ign = ignore();
5913
5946
  // The patterns in .git/info/exclude are relative to the working directory,
5914
5947
  // so only the first pair, which holds the full filepath, can use them.
@@ -6467,7 +6500,8 @@ async function _commit({
6467
6500
  gitdir,
6468
6501
  ref,
6469
6502
  });
6470
- } catch {
6503
+ } catch (err) {
6504
+ if (!(err instanceof NotFoundError)) throw err
6471
6505
  // We assume that there's no commit and this is the initial commit
6472
6506
  initialCommit = true;
6473
6507
  }
@@ -7277,6 +7311,7 @@ async function _branch({
7277
7311
  oid = await GitRefManager.resolve({ fs, gitdir, ref: object || 'HEAD' });
7278
7312
  } catch (e) {
7279
7313
  // Probably an empty repo
7314
+ if (!(e instanceof NotFoundError)) throw e
7280
7315
  }
7281
7316
 
7282
7317
  // Create a new ref that points at the current commit,
@@ -7450,7 +7485,7 @@ async function _checkout({
7450
7485
  // TODO: Figure out what to do if both 'ref' and 'remote' are specified, ref already exists,
7451
7486
  // and is configured to track a different remote.
7452
7487
  } catch (err) {
7453
- if (ref === 'HEAD') throw err
7488
+ if (ref === 'HEAD' || !(err instanceof NotFoundError)) throw err
7454
7489
  // If `ref` doesn't exist, create a new remote tracking branch
7455
7490
  // Figure out the commit to checkout
7456
7491
  const remoteRef = `${remote}/${ref}`;
@@ -9387,8 +9422,9 @@ async function _currentBranch({
9387
9422
  if (test) {
9388
9423
  try {
9389
9424
  await GitRefManager.resolve({ fs, gitdir, ref });
9390
- } catch (_) {
9391
- return
9425
+ } catch (err) {
9426
+ if (err instanceof NotFoundError) return
9427
+ throw err
9392
9428
  }
9393
9429
  }
9394
9430
  // Return `undefined` for detached HEAD
@@ -10090,8 +10126,8 @@ function filterCapabilities(server, client) {
10090
10126
 
10091
10127
  const pkg = {
10092
10128
  name: 'isomorphic-git',
10093
- version: '1.44.2',
10094
- agent: 'git/isomorphic-git@1.44.2',
10129
+ version: '1.44.4',
10130
+ agent: 'git/isomorphic-git@1.44.4',
10095
10131
  };
10096
10132
 
10097
10133
  class FIFO {
@@ -13228,6 +13264,7 @@ async function _listNotes({ fs, cache, gitdir, ref }) {
13228
13264
  if (err instanceof NotFoundError) {
13229
13265
  return []
13230
13266
  }
13267
+ throw err
13231
13268
  }
13232
13269
 
13233
13270
  // Create the current note tree
@@ -16118,10 +16155,8 @@ async function resetIndex({
16118
16155
  ref: ref || 'HEAD',
16119
16156
  });
16120
16157
  } catch (e) {
16121
- if (ref) {
16122
- // Only throw the error if a ref is explicitly provided
16123
- throw e
16124
- }
16158
+ // Without an explicit ref, a branch with no commits yet is fine
16159
+ if (ref || !(e instanceof NotFoundError)) throw e
16125
16160
  }
16126
16161
 
16127
16162
  // Not having an oid at this point means `resetIndex()` was called without explicit `ref` on a new git
@@ -16137,7 +16172,14 @@ async function resetIndex({
16137
16172
  filepath,
16138
16173
  });
16139
16174
  } catch (e) {
16140
- // This means we're resetting the file to a "deleted" state
16175
+ // The path is not in the commit, so reset it to a "deleted" state
16176
+ if (
16177
+ !(e instanceof NotFoundError) &&
16178
+ !(e instanceof InvalidFilepathError) &&
16179
+ !(e instanceof ObjectTypeError && e.data.filepath)
16180
+ ) {
16181
+ throw e
16182
+ }
16141
16183
  oid = null;
16142
16184
  }
16143
16185
  }
@@ -17035,8 +17077,7 @@ async function status({
17035
17077
  // size of -1 happens over a BrowserFS HTTP Backend that doesn't serve Content-Length headers
17036
17078
  // (like the Karma webserver) because BrowserFS HTTP Backend uses HTTP HEAD requests to do fs.stat
17037
17079
  if (stats.size !== -1) {
17038
- // We don't await this so we can return faster for one-off cases.
17039
- GitIndexManager.acquire(
17080
+ await GitIndexManager.acquire(
17040
17081
  { fs, gitdir: updatedGitdir, cache },
17041
17082
  async function (index) {
17042
17083
  index.insert({ filepath, stats, oid: workdirOid });
@@ -17164,6 +17205,7 @@ async function getHeadTree({ fs, cache, gitdir: updatedGitdir }) {
17164
17205
  if (e instanceof NotFoundError) {
17165
17206
  return []
17166
17207
  }
17208
+ throw e
17167
17209
  }
17168
17210
  const { tree } = await _readTree({ fs, cache, gitdir: updatedGitdir, oid });
17169
17211
  return tree