isomorphic-jj 1.4.2 → 1.4.3

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/dist/index.mjs CHANGED
@@ -6624,8 +6624,8 @@ async function createJJ(options) {
6624
6624
  change.metadata = args.metadata;
6625
6625
  }
6626
6626
 
6627
- // Only snapshot files if describing working copy
6628
- if (isWorkingCopy) {
6627
+ // Only snapshot files if describing working copy AND preserveSnapshot is not true
6628
+ if (isWorkingCopy && !args.preserveSnapshot) {
6629
6629
  // Snapshot current file contents for conflict detection
6630
6630
  // This allows us to load file contents during merge/rebase
6631
6631
  const fileSnapshot = {};
@@ -6858,16 +6858,19 @@ async function createJJ(options) {
6858
6858
  await workingCopy.load();
6859
6859
  await userConfig.load();
6860
6860
 
6861
- // Snapshot filesystem BEFORE operation (for undo)
6862
- const fileSnapshot = await snapshotFilesystem();
6861
+ // Snapshot filesystem BEFORE operation (for undo) unless preserveSnapshot is true
6862
+ let fileSnapshot;
6863
+ if (!args.preserveSnapshot) {
6864
+ fileSnapshot = await snapshotFilesystem();
6863
6865
 
6864
- // Update current working copy change with the file snapshot
6865
- // This ensures files written before jj.new() are captured
6866
- const currentChangeId = workingCopy.getCurrentChangeId();
6867
- const currentChange = await graph.getChange(currentChangeId);
6868
- if (currentChange && fileSnapshot) {
6869
- currentChange.fileSnapshot = fileSnapshot;
6870
- await graph.updateChange(currentChange);
6866
+ // Update current working copy change with the file snapshot
6867
+ // This ensures files written before jj.new() are captured
6868
+ const currentChangeId = workingCopy.getCurrentChangeId();
6869
+ const currentChange = await graph.getChange(currentChangeId);
6870
+ if (currentChange && fileSnapshot) {
6871
+ currentChange.fileSnapshot = fileSnapshot;
6872
+ await graph.updateChange(currentChange);
6873
+ }
6871
6874
  }
6872
6875
 
6873
6876
  // Determine parents
@@ -7232,8 +7235,16 @@ async function createJJ(options) {
7232
7235
  * @returns {Promise<Object>} The newly created change
7233
7236
  */
7234
7237
  async commit(args = {}) {
7235
- await this.describe({ message: args.message, author: args.author, metadata: args.metadata });
7236
- return await this.new({ message: args.nextMessage });
7238
+ await this.describe({
7239
+ message: args.message,
7240
+ author: args.author,
7241
+ metadata: args.metadata,
7242
+ preserveSnapshot: args.preserveSnapshot
7243
+ });
7244
+ return await this.new({
7245
+ message: args.nextMessage,
7246
+ preserveSnapshot: args.preserveSnapshot
7247
+ });
7237
7248
  },
7238
7249
 
7239
7250
  /**
@@ -7269,16 +7280,34 @@ async function createJJ(options) {
7269
7280
 
7270
7281
  const previousChangeId = workingCopy.getCurrentChangeId();
7271
7282
 
7272
- // Snapshot current filesystem into current change before switching
7273
- // This preserves any uncommitted modifications
7283
+ // CRITICAL FIX: Clean working directory BEFORE snapshotting to prevent cross-branch pollution
7284
+ // Remove files that don't belong to the current changeId before we snapshot it
7274
7285
  if (previousChangeId !== args.changeId) {
7286
+ const previousChange = await graph.getChange(previousChangeId);
7287
+ if (previousChange) {
7288
+ const previousSnapshot = previousChange.fileSnapshot || {};
7289
+ const workingDirFiles = await workingCopy.listFiles();
7290
+
7291
+ // Delete files that exist in working dir but NOT in previous changeId's snapshot
7292
+ // This removes pollution from other branches that were previously checked out
7293
+ for (const filePath of workingDirFiles) {
7294
+ if (!previousSnapshot[filePath]) {
7295
+ try {
7296
+ const fullPath = path.join(dir, filePath);
7297
+ await fs.promises.unlink(fullPath);
7298
+ await workingCopy.untrackFile(filePath);
7299
+ } catch (error) {
7300
+ // Ignore errors - file might already be deleted or inaccessible
7301
+ }
7302
+ }
7303
+ }
7304
+ }
7305
+
7306
+ // Now snapshot the CLEAN working directory (no pollution!)
7275
7307
  const currentSnapshot = await snapshotFilesystem();
7276
7308
  if (currentSnapshot && Object.keys(currentSnapshot).length > 0) {
7277
- const previousChange = await graph.getChange(previousChangeId);
7278
- if (previousChange) {
7279
- previousChange.fileSnapshot = currentSnapshot;
7280
- await graph.updateChange(previousChange);
7281
- }
7309
+ previousChange.fileSnapshot = currentSnapshot;
7310
+ await graph.updateChange(previousChange);
7282
7311
  }
7283
7312
  }
7284
7313