isomorphic-git 1.44.13 → 1.44.14

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
@@ -1190,7 +1190,16 @@ class GitIndexManager {
1190
1190
  if (unmergedPaths.length && !allowUnmerged)
1191
1191
  throw new UnmergedPathsError(unmergedPaths)
1192
1192
 
1193
- result = await closure(index);
1193
+ try {
1194
+ result = await closure(index);
1195
+ } catch (err) {
1196
+ // A failed closure may already have mutated the in-memory index. Drop
1197
+ // it so the next acquire reloads the unchanged index from disk, rather
1198
+ // than flushing a half-applied change that was never written.
1199
+ theIndexCache.map.delete(filepath);
1200
+ theIndexCache.stats.delete(filepath);
1201
+ throw err
1202
+ }
1194
1203
  if (index._dirty) {
1195
1204
  // Acquire a file lock while we're writing the index file
1196
1205
  // let fileLock = await Lock(filepath)
@@ -5857,6 +5866,31 @@ class FileSystem {
5857
5866
  }
5858
5867
  }
5859
5868
 
5869
+ /**
5870
+ * Refuse to materialize a working-tree entry whose parent path traverses a
5871
+ * symbolic link. A leading component that is a symlink could otherwise redirect
5872
+ * a write or mkdir outside the working tree. git applies the same check: it does
5873
+ * not follow symlinks in the leading path when writing working-tree files.
5874
+ *
5875
+ * @param {import('../models/FileSystem.js').FileSystem} fs
5876
+ * @param {string} dir
5877
+ * @param {string} fullpath
5878
+ */
5879
+ async function assertNoSymlinkInLeadingPath(fs, dir, fullpath) {
5880
+ const parts = fullpath.split('/');
5881
+ parts.pop(); // the final segment is the entry being written, not a leading dir
5882
+ let current = dir;
5883
+ for (const part of parts) {
5884
+ if (part === '' || part === '.') continue
5885
+ current = `${current}/${part}`;
5886
+ const stats = await fs.lstat(current);
5887
+ // lstat returns null when the path doesn't exist yet (nothing to traverse).
5888
+ if (stats && stats.isSymbolicLink()) {
5889
+ throw new UnsafeFilepathError(fullpath)
5890
+ }
5891
+ }
5892
+ }
5893
+
5860
5894
  function assertParameter(name, value) {
5861
5895
  if (value === undefined) {
5862
5896
  throw new MissingParameterError(name)
@@ -5941,6 +5975,50 @@ async function modified(entry, base) {
5941
5975
  return true
5942
5976
  }
5943
5977
 
5978
+ /**
5979
+ * Materialize one tracked blob in the working tree.
5980
+ *
5981
+ * A symlink must never be followed when writing, neither in the leading path
5982
+ * nor at the destination itself: a tracked symlink can point anywhere, so
5983
+ * following one redirects the write outside the working tree. git behaves the
5984
+ * same way — it replaces the symlink instead of writing through it.
5985
+ *
5986
+ * @param {object} args
5987
+ * @param {import('../models/FileSystem.js').FileSystem} args.fs
5988
+ * @param {string} args.dir - The working tree directory path
5989
+ * @param {string} args.filepath - Path of the entry, relative to `dir`
5990
+ * @param {number} args.mode - The entry's file mode, e.g. 0o100644
5991
+ * @param {Uint8Array|string} args.content - The blob contents
5992
+ * @returns {Promise<void>}
5993
+ */
5994
+ async function writeWorkingTreeEntry({
5995
+ fs,
5996
+ dir,
5997
+ filepath,
5998
+ mode,
5999
+ content,
6000
+ }) {
6001
+ await assertNoSymlinkInLeadingPath(fs, dir, filepath);
6002
+ const path = `${dir}/${filepath}`;
6003
+ const symlink = mode === 0o120000;
6004
+ // lstat does not follow the final component, so an existing symlink is
6005
+ // removed rather than written through. Removing it first also lets `mode`
6006
+ // take effect, which only happens when the file is created.
6007
+ const stats = await fs.lstat(path);
6008
+ if (stats && (symlink || stats.isSymbolicLink())) {
6009
+ await fs.rm(path);
6010
+ }
6011
+ if (symlink) {
6012
+ // `fs.write` creates the missing parent directories itself, `fs.writelink`
6013
+ // does not. The leading path is already checked above, so creating them
6014
+ // here cannot traverse a symlink either.
6015
+ await fs.mkdir(dirname(path));
6016
+ await fs.writelink(path, Buffer.from(content));
6017
+ } else {
6018
+ await fs.write(path, content, { mode });
6019
+ }
6020
+ }
6021
+
5944
6022
  // @ts-check
5945
6023
 
5946
6024
  /**
@@ -6019,6 +6097,14 @@ async function abortMerge({
6019
6097
  },
6020
6098
  });
6021
6099
 
6100
+ // Refuse the whole reset before touching the index or the working tree, so
6101
+ // an unsafe path cannot leave either of them half reset.
6102
+ for (const entry of results) {
6103
+ if (entry && entry.type === 'blob') {
6104
+ await assertNoSymlinkInLeadingPath(fs, dir, entry.path);
6105
+ }
6106
+ }
6107
+
6022
6108
  await GitIndexManager.acquire(
6023
6109
  { fs, gitdir: updatedGitdir, cache },
6024
6110
  async function (index) {
@@ -6036,14 +6122,25 @@ async function abortMerge({
6036
6122
  }
6037
6123
 
6038
6124
  if (entry.type === 'blob') {
6039
- const content = new TextDecoder().decode(entry.content);
6040
- await fs.write(`${dir}/${entry.path}`, content, {
6125
+ await writeWorkingTreeEntry({
6126
+ fs,
6127
+ dir,
6128
+ filepath: entry.path,
6041
6129
  mode: entry.mode,
6130
+ content: entry.content,
6042
6131
  });
6132
+ // Stage the file as it now sits on disk, so the entry does not
6133
+ // look modified afterwards. The mode is the one being restored,
6134
+ // never the one lstat reports: an existing file keeps its old
6135
+ // permissions, because `mode` only applies when a file is
6136
+ // created, and Windows does not report the executable bit.
6137
+ const stats = await fs.lstat(`${dir}/${entry.path}`);
6138
+ stats.mode = entry.mode;
6043
6139
  index.insert({
6044
6140
  filepath: entry.path,
6045
6141
  oid: entry.oid,
6046
6142
  stage: 0,
6143
+ stats,
6047
6144
  });
6048
6145
  }
6049
6146
  }
@@ -7541,31 +7638,6 @@ async function branch({
7541
7638
  }
7542
7639
  }
7543
7640
 
7544
- /**
7545
- * Refuse to materialize a working-tree entry whose parent path traverses a
7546
- * symbolic link. A leading component that is a symlink could otherwise redirect
7547
- * a write or mkdir outside the working tree. git applies the same check: it does
7548
- * not follow symlinks in the leading path when writing working-tree files.
7549
- *
7550
- * @param {import('../models/FileSystem.js').FileSystem} fs
7551
- * @param {string} dir
7552
- * @param {string} fullpath
7553
- */
7554
- async function assertNoSymlinkInLeadingPath(fs, dir, fullpath) {
7555
- const parts = fullpath.split('/');
7556
- parts.pop(); // the final segment is the entry being written, not a leading dir
7557
- let current = dir;
7558
- for (const part of parts) {
7559
- if (part === '' || part === '.') continue
7560
- current = `${current}/${part}`;
7561
- const stats = await fs.lstat(current);
7562
- // lstat returns null when the path doesn't exist yet (nothing to traverse).
7563
- if (stats && stats.isSymbolicLink()) {
7564
- throw new UnsafeFilepathError(fullpath)
7565
- }
7566
- }
7567
- }
7568
-
7569
7641
  const worthWalking = (filepath, root) => {
7570
7642
  if (filepath === '.' || root == null || root.length === 0 || root === '.') {
7571
7643
  return true
@@ -8804,11 +8876,14 @@ async function mergeTree({
8804
8876
  gitdir,
8805
8877
  trees: [TREE({ ref: results.oid })],
8806
8878
  map: async function (filepath, [entry]) {
8807
- const path = `${dir}/${filepath}`;
8808
8879
  if ((await entry.type()) === 'blob') {
8809
- const mode = await entry.mode();
8810
- const content = await entry.content();
8811
- await fs.write(path, content, { mode });
8880
+ await writeWorkingTreeEntry({
8881
+ fs,
8882
+ dir,
8883
+ filepath,
8884
+ mode: await entry.mode(),
8885
+ content: await entry.content(),
8886
+ });
8812
8887
  }
8813
8888
  return true
8814
8889
  },
@@ -10302,8 +10377,8 @@ function filterCapabilities(server, client) {
10302
10377
 
10303
10378
  const pkg = {
10304
10379
  name: 'isomorphic-git',
10305
- version: '1.44.13',
10306
- agent: 'git/isomorphic-git@1.44.13',
10380
+ version: '1.44.14',
10381
+ agent: 'git/isomorphic-git@1.44.14',
10307
10382
  };
10308
10383
 
10309
10384
  class FIFO {
package/index.js CHANGED
@@ -1184,7 +1184,16 @@ class GitIndexManager {
1184
1184
  if (unmergedPaths.length && !allowUnmerged)
1185
1185
  throw new UnmergedPathsError(unmergedPaths)
1186
1186
 
1187
- result = await closure(index);
1187
+ try {
1188
+ result = await closure(index);
1189
+ } catch (err) {
1190
+ // A failed closure may already have mutated the in-memory index. Drop
1191
+ // it so the next acquire reloads the unchanged index from disk, rather
1192
+ // than flushing a half-applied change that was never written.
1193
+ theIndexCache.map.delete(filepath);
1194
+ theIndexCache.stats.delete(filepath);
1195
+ throw err
1196
+ }
1188
1197
  if (index._dirty) {
1189
1198
  // Acquire a file lock while we're writing the index file
1190
1199
  // let fileLock = await Lock(filepath)
@@ -5845,6 +5854,31 @@ class FileSystem {
5845
5854
  }
5846
5855
  }
5847
5856
 
5857
+ /**
5858
+ * Refuse to materialize a working-tree entry whose parent path traverses a
5859
+ * symbolic link. A leading component that is a symlink could otherwise redirect
5860
+ * a write or mkdir outside the working tree. git applies the same check: it does
5861
+ * not follow symlinks in the leading path when writing working-tree files.
5862
+ *
5863
+ * @param {import('../models/FileSystem.js').FileSystem} fs
5864
+ * @param {string} dir
5865
+ * @param {string} fullpath
5866
+ */
5867
+ async function assertNoSymlinkInLeadingPath(fs, dir, fullpath) {
5868
+ const parts = fullpath.split('/');
5869
+ parts.pop(); // the final segment is the entry being written, not a leading dir
5870
+ let current = dir;
5871
+ for (const part of parts) {
5872
+ if (part === '' || part === '.') continue
5873
+ current = `${current}/${part}`;
5874
+ const stats = await fs.lstat(current);
5875
+ // lstat returns null when the path doesn't exist yet (nothing to traverse).
5876
+ if (stats && stats.isSymbolicLink()) {
5877
+ throw new UnsafeFilepathError(fullpath)
5878
+ }
5879
+ }
5880
+ }
5881
+
5848
5882
  function assertParameter(name, value) {
5849
5883
  if (value === undefined) {
5850
5884
  throw new MissingParameterError(name)
@@ -5929,6 +5963,50 @@ async function modified(entry, base) {
5929
5963
  return true
5930
5964
  }
5931
5965
 
5966
+ /**
5967
+ * Materialize one tracked blob in the working tree.
5968
+ *
5969
+ * A symlink must never be followed when writing, neither in the leading path
5970
+ * nor at the destination itself: a tracked symlink can point anywhere, so
5971
+ * following one redirects the write outside the working tree. git behaves the
5972
+ * same way — it replaces the symlink instead of writing through it.
5973
+ *
5974
+ * @param {object} args
5975
+ * @param {import('../models/FileSystem.js').FileSystem} args.fs
5976
+ * @param {string} args.dir - The working tree directory path
5977
+ * @param {string} args.filepath - Path of the entry, relative to `dir`
5978
+ * @param {number} args.mode - The entry's file mode, e.g. 0o100644
5979
+ * @param {Uint8Array|string} args.content - The blob contents
5980
+ * @returns {Promise<void>}
5981
+ */
5982
+ async function writeWorkingTreeEntry({
5983
+ fs,
5984
+ dir,
5985
+ filepath,
5986
+ mode,
5987
+ content,
5988
+ }) {
5989
+ await assertNoSymlinkInLeadingPath(fs, dir, filepath);
5990
+ const path = `${dir}/${filepath}`;
5991
+ const symlink = mode === 0o120000;
5992
+ // lstat does not follow the final component, so an existing symlink is
5993
+ // removed rather than written through. Removing it first also lets `mode`
5994
+ // take effect, which only happens when the file is created.
5995
+ const stats = await fs.lstat(path);
5996
+ if (stats && (symlink || stats.isSymbolicLink())) {
5997
+ await fs.rm(path);
5998
+ }
5999
+ if (symlink) {
6000
+ // `fs.write` creates the missing parent directories itself, `fs.writelink`
6001
+ // does not. The leading path is already checked above, so creating them
6002
+ // here cannot traverse a symlink either.
6003
+ await fs.mkdir(dirname(path));
6004
+ await fs.writelink(path, Buffer.from(content));
6005
+ } else {
6006
+ await fs.write(path, content, { mode });
6007
+ }
6008
+ }
6009
+
5932
6010
  // @ts-check
5933
6011
 
5934
6012
  /**
@@ -6007,6 +6085,14 @@ async function abortMerge({
6007
6085
  },
6008
6086
  });
6009
6087
 
6088
+ // Refuse the whole reset before touching the index or the working tree, so
6089
+ // an unsafe path cannot leave either of them half reset.
6090
+ for (const entry of results) {
6091
+ if (entry && entry.type === 'blob') {
6092
+ await assertNoSymlinkInLeadingPath(fs, dir, entry.path);
6093
+ }
6094
+ }
6095
+
6010
6096
  await GitIndexManager.acquire(
6011
6097
  { fs, gitdir: updatedGitdir, cache },
6012
6098
  async function (index) {
@@ -6024,14 +6110,25 @@ async function abortMerge({
6024
6110
  }
6025
6111
 
6026
6112
  if (entry.type === 'blob') {
6027
- const content = new TextDecoder().decode(entry.content);
6028
- await fs.write(`${dir}/${entry.path}`, content, {
6113
+ await writeWorkingTreeEntry({
6114
+ fs,
6115
+ dir,
6116
+ filepath: entry.path,
6029
6117
  mode: entry.mode,
6118
+ content: entry.content,
6030
6119
  });
6120
+ // Stage the file as it now sits on disk, so the entry does not
6121
+ // look modified afterwards. The mode is the one being restored,
6122
+ // never the one lstat reports: an existing file keeps its old
6123
+ // permissions, because `mode` only applies when a file is
6124
+ // created, and Windows does not report the executable bit.
6125
+ const stats = await fs.lstat(`${dir}/${entry.path}`);
6126
+ stats.mode = entry.mode;
6031
6127
  index.insert({
6032
6128
  filepath: entry.path,
6033
6129
  oid: entry.oid,
6034
6130
  stage: 0,
6131
+ stats,
6035
6132
  });
6036
6133
  }
6037
6134
  }
@@ -7529,31 +7626,6 @@ async function branch({
7529
7626
  }
7530
7627
  }
7531
7628
 
7532
- /**
7533
- * Refuse to materialize a working-tree entry whose parent path traverses a
7534
- * symbolic link. A leading component that is a symlink could otherwise redirect
7535
- * a write or mkdir outside the working tree. git applies the same check: it does
7536
- * not follow symlinks in the leading path when writing working-tree files.
7537
- *
7538
- * @param {import('../models/FileSystem.js').FileSystem} fs
7539
- * @param {string} dir
7540
- * @param {string} fullpath
7541
- */
7542
- async function assertNoSymlinkInLeadingPath(fs, dir, fullpath) {
7543
- const parts = fullpath.split('/');
7544
- parts.pop(); // the final segment is the entry being written, not a leading dir
7545
- let current = dir;
7546
- for (const part of parts) {
7547
- if (part === '' || part === '.') continue
7548
- current = `${current}/${part}`;
7549
- const stats = await fs.lstat(current);
7550
- // lstat returns null when the path doesn't exist yet (nothing to traverse).
7551
- if (stats && stats.isSymbolicLink()) {
7552
- throw new UnsafeFilepathError(fullpath)
7553
- }
7554
- }
7555
- }
7556
-
7557
7629
  const worthWalking = (filepath, root) => {
7558
7630
  if (filepath === '.' || root == null || root.length === 0 || root === '.') {
7559
7631
  return true
@@ -8792,11 +8864,14 @@ async function mergeTree({
8792
8864
  gitdir,
8793
8865
  trees: [TREE({ ref: results.oid })],
8794
8866
  map: async function (filepath, [entry]) {
8795
- const path = `${dir}/${filepath}`;
8796
8867
  if ((await entry.type()) === 'blob') {
8797
- const mode = await entry.mode();
8798
- const content = await entry.content();
8799
- await fs.write(path, content, { mode });
8868
+ await writeWorkingTreeEntry({
8869
+ fs,
8870
+ dir,
8871
+ filepath,
8872
+ mode: await entry.mode(),
8873
+ content: await entry.content(),
8874
+ });
8800
8875
  }
8801
8876
  return true
8802
8877
  },
@@ -10290,8 +10365,8 @@ function filterCapabilities(server, client) {
10290
10365
 
10291
10366
  const pkg = {
10292
10367
  name: 'isomorphic-git',
10293
- version: '1.44.13',
10294
- agent: 'git/isomorphic-git@1.44.13',
10368
+ version: '1.44.14',
10369
+ agent: 'git/isomorphic-git@1.44.14',
10295
10370
  };
10296
10371
 
10297
10372
  class FIFO {