From 7dcb10feb199cf05fd8c973e9744e62a2f1c541b Mon Sep 17 00:00:00 2001 From: Vincent Date: Wed, 28 Oct 2026 14:34:22 -0400 Subject: [PATCH] fix(shim): preserve snapshot isolation across delete and recreate --- .../it/git_shim_daily_workflow_regression.rs | 5 + .../it/git_snapshot_lifecycle_regression.rs | 271 ++++++++++++++++++ atomic-repository/src/repository/insert.rs | 9 +- atomic-repository/src/repository/record.rs | 7 + .../src/repository/tests/snapshot_tests.rs | 223 ++++++++++++++ 5 files changed, 514 insertions(+), 1 deletion(-) create mode 100644 atomic-cli/tests/it/git_snapshot_lifecycle_regression.rs diff --git a/atomic-cli/tests/it/git_shim_daily_workflow_regression.rs b/atomic-cli/tests/it/git_shim_daily_workflow_regression.rs index 9b56d696..0bb9eeec 100644 --- a/atomic-cli/tests/it/git_shim_daily_workflow_regression.rs +++ b/atomic-cli/tests/it/git_shim_daily_workflow_regression.rs @@ -1532,3 +1532,8 @@ fn importing_a_crlf_file_committed_before_eol_normalization() { "the recorded bytes are the committed blob" ); } + +// Keep snapshot regressions under the shim suite so its platform and optional +// build-feature gates also cover these entry-boundary workflows. +#[path = "git_snapshot_lifecycle_regression.rs"] +mod snapshot_lifecycle; diff --git a/atomic-cli/tests/it/git_snapshot_lifecycle_regression.rs b/atomic-cli/tests/it/git_snapshot_lifecycle_regression.rs new file mode 100644 index 00000000..94b22d3b --- /dev/null +++ b/atomic-cli/tests/it/git_snapshot_lifecycle_regression.rs @@ -0,0 +1,271 @@ +//! Entry-boundary snapshots must not consume durable changes or invent identities. +#![cfg(unix)] + +use std::fs; +use std::os::unix::fs::{symlink, PermissionsExt}; +use std::process::{Command, Output}; + +use atomic_core::change::InodeKind; +use atomic_core::pristine::TreeTxnT; +use atomic_repository::Repository; +use tempfile::TempDir; + +struct Fixture { + root: TempDir, + home: TempDir, +} + +impl Fixture { + fn command(&self, program: &str, args: &[&str]) -> Output { + let mut command = Command::new(program); + for (name, _) in std::env::vars_os() { + let text = name.to_string_lossy(); + if text.starts_with("ATOMIC_") + || text.starts_with("OPENCODE_") + || text.starts_with("GIT_") + { + command.env_remove(name); + } + } + command + .args(args) + .current_dir(self.root.path()) + .env("HOME", self.home.path()) + .env("ATOMIC_HOME", self.home.path().join(".atomic")) + .env("ATOMIC_CONFIG_DIR", self.home.path().join(".atomic")) + .env("ATOMIC_SERVICE", "local") + .env("ATOMIC_NONINTERACTIVE", "1") + .env("GIT_CONFIG_NOSYSTEM", "1") + .env("GIT_CONFIG_GLOBAL", "/dev/null") + .env("GIT_AUTHOR_NAME", "Snapshot regression") + .env("GIT_AUTHOR_EMAIL", "snapshot@example.invalid") + .env("GIT_COMMITTER_NAME", "Snapshot regression") + .env("GIT_COMMITTER_EMAIL", "snapshot@example.invalid") + .env("NO_COLOR", "1") + .output() + .unwrap() + } + + fn ok(&self, program: &str, args: &[&str]) -> String { + let output = self.command(program, args); + assert!(output.status.success(), "{program} {args:?}: {output:?}"); + String::from_utf8(output.stdout).unwrap() + } + + fn atomic(&self, args: &[&str]) -> String { + self.ok(env!("CARGO_BIN_EXE_atomic"), args) + } + + fn git(&self, args: &[&str]) -> String { + self.ok("git", args) + } + + fn new(rolling: bool, full: bool) -> Self { + let fixture = Self { + root: TempDir::new().unwrap(), + home: TempDir::new().unwrap(), + }; + fixture.git(&["init", "-q", "-b", "main"]); + fs::write(fixture.root.path().join("tracked.txt"), b"first\nsecond\n").unwrap(); + if full { + fs::create_dir(fixture.root.path().join("dir")).unwrap(); + fs::write(fixture.root.path().join("dir/café.txt"), b"cafe\n").unwrap(); + fs::write(fixture.root.path().join("literal%20path.txt"), b"percent\n").unwrap(); + fs::write( + fixture.root.path().join(".gitattributes"), + b"*.txt text eol=lf\n", + ) + .unwrap(); + fs::write( + fixture.root.path().join("run.sh"), + b"#!/bin/sh\necho fixture\n", + ) + .unwrap(); + fs::set_permissions( + fixture.root.path().join("run.sh"), + fs::Permissions::from_mode(0o755), + ) + .unwrap(); + symlink("tracked.txt", fixture.root.path().join("tracked-link")).unwrap(); + } + fixture.git(&["add", "-A"]); + fixture.git(&["commit", "-qm", "seed"]); + if full { + fs::write( + fixture.root.path().join("tracked.txt"), + b"first\nsecond commit\n", + ) + .unwrap(); + fixture.git(&["add", "-A"]); + fixture.git(&["commit", "-qm", "text edit"]); + fixture.git(&["commit", "--allow-empty", "-qm", "empty"]); + } + let mut import = vec!["git", "import", "--no-vault"]; + if !rolling { + import.push("--no-stream"); + } + fixture.atomic(&import); + let key = fixture.home.path().join("binding.key"); + fs::write(&key, "ab".repeat(32)).unwrap(); + fixture.atomic(&[ + "git", + "shim", + "enable", + "--no-refspecs", + "--binding-key-file", + key.to_str().unwrap(), + ]); + fixture.atomic(&["git", "shim", "reconcile"]); + fs::write( + fixture.root.path().join("tracked.txt"), + b"first\nnative edit\n", + ) + .unwrap(); + if full { + fixture.atomic(&["move", "dir/café.txt", "dir/moved café.txt"]); + fs::write( + fixture.root.path().join("dir/moved café.txt"), + b"cafe native edit\n", + ) + .unwrap(); + } + fixture.atomic(&["record", "-am", "native edit and move"]); + fixture + } + + fn neighbors(&self) -> Vec { + self.git(&["ls-tree", "-rz", "HEAD"]) + .split('\0') + .filter(|entry| !entry.is_empty() && !entry.ends_with("\ttracked.txt")) + .map(str::to_owned) + .collect() + } + + fn assert_deleted(&self) { + self.atomic(&["git", "shim", "verify"]); + assert!(self + .git(&["ls-tree", "HEAD", "--", "tracked.txt"]) + .is_empty()); + let repo = Repository::open(self.root.path()).unwrap(); + assert!(repo + .get_file_content_on_view("tracked.txt", "main") + .unwrap() + .is_none()); + } +} + +#[test] +fn first_deletion_record_succeeds_without_status_retry_or_reopen() { + for rolling in [false, true] { + let fixture = Fixture::new(rolling, false); + let before = fixture + .git(&["rev-list", "--count", "HEAD"]) + .trim() + .parse::() + .unwrap(); + // Do not insert any observation between unlink and this first record. + fs::remove_file(fixture.root.path().join("tracked.txt")).unwrap(); + fixture.atomic(&["record", "-am", "native deletion"]); + fixture.assert_deleted(); + let after = fixture + .git(&["rev-list", "--count", "HEAD"]) + .trim() + .parse::() + .unwrap(); + assert_eq!( + after, + before + 1, + "rolling={rolling}: record deletion exactly once" + ); + assert!(fixture.git(&["status", "--porcelain"]).is_empty()); + let no_op = fixture.command(env!("CARGO_BIN_EXE_atomic"), &["record", "-am", "no-op"]); + assert!(!no_op.status.success()); + assert!(String::from_utf8_lossy(&no_op.stderr).contains("Nothing to record")); + assert_eq!( + fixture + .git(&["rev-list", "--count", "HEAD"]) + .trim() + .parse::() + .unwrap(), + after + ); + } +} + +#[test] +fn recreate_add_and_record_preserve_identity_bytes_attributes_and_neighbors() { + // Keep both the one-file minimization and the richer retained scenario. + for rolling in [false, true] { + for full in [false, true] { + for kind in ["regular", "executable", "symlink"] { + let fixture = Fixture::new(rolling, full); + let (inode, position) = { + let repo = Repository::open(fixture.root.path()).unwrap(); + let inode = repo.get_file_inode("tracked.txt").unwrap().unwrap(); + let txn = repo.pristine().read_txn().unwrap(); + (inode, txn.inode_position(inode).unwrap().unwrap()) + }; + let neighbors = fixture.neighbors(); + let path = fixture.root.path().join("tracked.txt"); + fs::remove_file(&path).unwrap(); + // Deliberately isolate recreate/add from the first-record bug. + fixture.atomic(&["status"]); + fixture.atomic(&["record", "-am", "delete"]); + fixture.assert_deleted(); + let (bytes, git_mode, expected_kind) = if kind == "symlink" { + symlink("literal%20path.txt", &path).unwrap(); + ( + b"literal%20path.txt".as_slice(), + "120000", + InodeKind::Symlink, + ) + } else { + fs::write(&path, b"restored\n").unwrap(); + let executable = kind == "executable"; + fs::set_permissions( + &path, + fs::Permissions::from_mode(if executable { 0o755 } else { 0o644 }), + ) + .unwrap(); + ( + b"restored\n".as_slice(), + if executable { "100755" } else { "100644" }, + InodeKind::Regular, + ) + }; + fixture.atomic(&["add", "tracked.txt"]); + fixture.atomic(&["record", "-am", "recreated"]); + fixture.atomic(&["git", "shim", "verify"]); + assert_eq!(fixture.git(&["show", "HEAD:tracked.txt"]).as_bytes(), bytes); + assert!(fixture + .git(&["ls-tree", "HEAD", "--", "tracked.txt"]) + .starts_with(git_mode)); + assert_eq!(fixture.neighbors(), neighbors); + assert!(fixture.git(&["status", "--porcelain"]).is_empty()); + let repo = Repository::open(fixture.root.path()).unwrap(); + assert_eq!(repo.get_file_inode("tracked.txt").unwrap(), Some(inode)); + assert_eq!( + repo.pristine() + .read_txn() + .unwrap() + .inode_position(inode) + .unwrap(), + Some(position) + ); + let policy = atomic_repository::ConversionPolicy::new( + atomic_core::operation::GitHashAlgorithm::Sha1, + ); + let projected = repo.project_tree("main", &policy).unwrap(); + let entry = projected + .manifest + .entries + .iter() + .find(|entry| entry.path.as_bytes() == b"tracked.txt") + .unwrap(); + assert_eq!(entry.repository_bytes.as_ref(), bytes); + assert_eq!(entry.kind, expected_kind); + assert_eq!(entry.git_mode(), u32::from_str_radix(git_mode, 8).unwrap()); + } + } + } +} diff --git a/atomic-repository/src/repository/insert.rs b/atomic-repository/src/repository/insert.rs index 94cec399..c28782f1 100644 --- a/atomic-repository/src/repository/insert.rs +++ b/atomic-repository/src/repository/insert.rs @@ -2398,7 +2398,14 @@ impl Repository { })?; txn.update_view(&removal_view) .map_err(|error| RepositoryError::Database(error.to_string()))?; - self.realign_tree_projection_in_txn(&mut txn, removal_view_name)?; + // A private snapshot's membership changed, not the selected + // working-copy closure. Projecting its namespace here would hide + // pending deletions (or rename paths) from the next durable + // record. Preserve the same selected-view boundary as the write + // above, including separately staged working-copy paths. + if removal_view_name == self.current_view { + self.realign_tree_projection_in_txn(&mut txn, removal_view_name)?; + } if removal_view_name == view_name { let final_view = txn .get_view(view_name) diff --git a/atomic-repository/src/repository/record.rs b/atomic-repository/src/repository/record.rs index 158beaa7..daec8d03 100644 --- a/atomic-repository/src/repository/record.rs +++ b/atomic-repository/src/repository/record.rs @@ -1629,7 +1629,14 @@ impl Repository { // binding carries a fresh inode with no graph position, so anchor // attributes to the resurrected claimant; otherwise they queue // against a FileAdd this record will never create. + // Entry-boundary snapshots also capture untracked reappearances + // before `add` has staged any inode. They follow the same undelete + // content path and must use that claimant's attributes too. let inode = match (inode, move_position) { + (None, None) if !fresh_additions.contains(&path) => projected_absent + .get(&path) + .filter(|absent| !absent.directory) + .map(|absent| absent.inode), (Some(current), None) if !fresh_additions.contains(&path) && shared_txn diff --git a/atomic-repository/src/repository/tests/snapshot_tests.rs b/atomic-repository/src/repository/tests/snapshot_tests.rs index 40843e8e..b1c156e8 100644 --- a/atomic-repository/src/repository/tests/snapshot_tests.rs +++ b/atomic-repository/src/repository/tests/snapshot_tests.rs @@ -1167,3 +1167,226 @@ fn snapshot_retention_refuses_when_enumeration_fails() { let repo = Repository::open(directory.path()).unwrap(); assert_eq!(persisted_change_bytes(&repo, &candidate), before_bytes); } + +/// Replacing a private edit snapshot with a deletion must not hide the +/// deletion from the next durable record on the same repository handle. +#[test] +fn snapshot_replacement_keeps_deletion_recordable_without_reopen() { + let (directory, mut repo) = create_temp_repo(); + let snapshots = snapshot_chain(&directory, &mut repo, &[b"pending edit\n"]); + let working_copy = repo.working_copy(); + let path = directory.path().join("retention.txt"); + std::fs::remove_file(&path).unwrap(); + let snapshot = repo + .snapshot( + working_copy, + ChangeHeader::new("pending delete"), + record_options(), + ) + .unwrap(); + assert_eq!(snapshot.change().supersedes(), Some(&snapshots[0])); + + // No status, reopen, retry, or repair between capture and this record. + let deletion = repo + .record(ChangeHeader::new("durable delete"), record_options()) + .expect("a private snapshot must not consume the baseline deletion"); + assert_eq!(deletion.deleted_files(), &["retention.txt".to_string()]); + assert!(deletion.change().hunks().iter().any(|op| matches!( + op, GraphOp::FileDel { path, .. } if path == "retention.txt" + ))); + assert!(deletion.change().dependencies().iter().all(|hash| repo + .load_change(hash) + .unwrap() + .kind() + .is_durable())); + assert!(repo.get_file_content("retention.txt").unwrap().is_none()); + assert!(repo.status(StatusOptions::default()).unwrap().is_clean()); + assert!(matches!( + repo.record(ChangeHeader::new("actual no-op"), record_options()), + Err(RecordError::NothingToRecord) + )); + drop(repo); + let reopened = Repository::open(directory.path()).unwrap(); + assert!(reopened + .get_file_content("retention.txt") + .unwrap() + .is_none()); +} + +/// An untracked reappearance is captured before `add` can stage an inode. +/// Both snapshot and durable record must undelete the original claimant. +#[test] +fn snapshot_of_unstaged_reappearance_reuses_recorded_identity() { + let (directory, repo) = create_temp_repo(); + let working_copy = repo.working_copy(); + let path = directory.path().join("recreated.txt"); + std::fs::write(&path, b"original\n").unwrap(); + repo.add("recreated.txt", TrackingOptions::default()) + .unwrap(); + let original = repo + .record(ChangeHeader::new("original"), record_options()) + .unwrap(); + let identity = repo + .get_inode_and_position("recreated.txt") + .unwrap() + .unwrap(); + std::fs::remove_file(&path).unwrap(); + repo.record(ChangeHeader::new("delete"), record_options()) + .unwrap(); + std::fs::write(&path, b"restored\n").unwrap(); + + let snapshot = repo + .snapshot( + working_copy, + ChangeHeader::new("capture before add"), + record_options().include_untracked(true), + ) + .expect("snapshot attributes must target the undeleted claimant"); + assert!(snapshot.change().hunks().iter().any(|op| matches!( + op, GraphOp::FileUndel { path, .. } if path == "recreated.txt" + ))); + assert!(!snapshot + .change() + .hunks() + .iter() + .any(|op| matches!(op, GraphOp::FileAdd { .. }))); + assert!( + repo.get_file_content("recreated.txt").unwrap().is_none(), + "a private capture must not resurrect the file in the durable view" + ); + assert!(direct_view_hashes(&repo, "dev").contains(original.hash())); + assert!(!direct_view_hashes(&repo, "dev").contains(snapshot.hash())); + + repo.add("recreated.txt", TrackingOptions::default()) + .unwrap(); + let restored = repo + .record(ChangeHeader::new("restore"), record_options()) + .unwrap(); + assert!(!restored.change().dependencies().contains(snapshot.hash())); + assert_eq!( + repo.get_inode_and_position("recreated.txt") + .unwrap() + .unwrap(), + identity + ); + assert_eq!( + repo.get_file_content("recreated.txt").unwrap().unwrap(), + b"restored\n" + ); + assert!(repo.status(StatusOptions::default()).unwrap().is_clean()); + drop(repo); + let reopened = Repository::open(directory.path()).unwrap(); + assert_eq!( + reopened + .get_inode_and_position("recreated.txt") + .unwrap() + .unwrap(), + identity + ); + assert_eq!( + reopened.get_file_content("recreated.txt").unwrap().unwrap(), + b"restored\n" + ); +} + +#[test] +fn snapshot_replacement_preserves_staged_additions_until_durable_record() { + let (directory, mut repo) = create_temp_repo(); + snapshot_chain(&directory, &mut repo, &[b"pending edit\n"]); + let working_copy = repo.working_copy(); + std::fs::write(directory.path().join("staged.txt"), b"keep staged\n").unwrap(); + repo.add("staged.txt", TrackingOptions::default()).unwrap(); + std::fs::remove_file(directory.path().join("retention.txt")).unwrap(); + repo.snapshot( + working_copy, + ChangeHeader::new("replace capture"), + record_options(), + ) + .unwrap(); + assert_eq!( + std::fs::read(directory.path().join("staged.txt")).unwrap(), + b"keep staged\n" + ); + assert!(repo.is_tracked("staged.txt").unwrap()); + assert!(repo.get_file_content("staged.txt").unwrap().is_none()); + let staged = repo + .record( + ChangeHeader::new("durable deletion and staged addition"), + record_options(), + ) + .unwrap(); + assert_eq!(staged.deleted_files(), &["retention.txt".to_string()]); + assert!(repo.get_file_content("retention.txt").unwrap().is_none()); + assert!(staged.change().dependencies().iter().all(|hash| repo + .load_change(hash) + .unwrap() + .kind() + .is_durable())); + assert_eq!( + repo.get_file_content("staged.txt").unwrap().unwrap(), + b"keep staged\n" + ); +} + +#[test] +fn snapshot_reappearance_refuses_stale_promotion_without_losing_newer_work() { + let (directory, repo) = create_temp_repo(); + let working_copy = repo.working_copy(); + let path = directory.path().join("recreated.txt"); + std::fs::write(&path, b"original\n").unwrap(); + repo.add("recreated.txt", TrackingOptions::default()) + .unwrap(); + repo.record(ChangeHeader::new("original"), record_options()) + .unwrap(); + std::fs::remove_file(&path).unwrap(); + repo.record(ChangeHeader::new("deleted"), record_options()) + .unwrap(); + std::fs::write(&path, b"first reappearance\n").unwrap(); + let snapshot = repo + .snapshot( + working_copy, + ChangeHeader::new("first capture"), + record_options().include_untracked(true), + ) + .unwrap(); + let baseline = direct_view_hashes(&repo, "dev"); + let private_view = Repository::snapshot_view_name(working_copy); + let captured = persisted_change_bytes(&repo, snapshot.hash()); + std::fs::write(&path, b"newer work must survive\n").unwrap(); + let error = repo + .promote_snapshot( + working_copy, + ChangeHeader::new("stale promotion"), + record_options().include_untracked(true), + ) + .unwrap_err(); + assert!( + error.to_string().contains("create a new snapshot"), + "{error}" + ); + assert_eq!(std::fs::read(&path).unwrap(), b"newer work must survive\n"); + assert_eq!(direct_view_hashes(&repo, "dev"), baseline); + assert_eq!( + direct_view_hashes(&repo, &private_view), + vec![*snapshot.hash()] + ); + assert_eq!(persisted_change_bytes(&repo, snapshot.hash()), captured); + assert!(repo.get_file_content("recreated.txt").unwrap().is_none()); + + repo.snapshot( + working_copy, + ChangeHeader::new("fresh capture"), + record_options().include_untracked(true), + ) + .unwrap(); + repo.promote_snapshot( + working_copy, + ChangeHeader::new("fresh promotion"), + record_options().include_untracked(true), + ) + .unwrap(); + assert_eq!( + repo.get_file_content("recreated.txt").unwrap().unwrap(), + b"newer work must survive\n" + ); +}