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