Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(copy-ignored): give copied files their source's mtime (#4518)
Maximilian Roos7d59f69ea9c1
diff --git a/src/commands/step/copy_ignored.rs b/src/commands/step/copy_ignored.rsindex 7d1e49737..b04f73d41 100644--- a/src/commands/step/copy_ignored.rs+++ b/src/commands/step/copy_ignored.rs@@ -2,7 +2,6 @@use std::fs;use std::path::PathBuf;-use std::time::SystemTime;use anyhow::Context;use color_print::cformat;@@ -258,10 +257,6 @@ pub fn step_copy_ignored(Progress::start("Copying")};- // One mtime for everything this run copies, taken before the first file- // lands — see `worktrunk::copy` for why build caches need it.- let stamp = Some(SystemTime::now());-for (src_entry, is_dir) in &entries_to_copy {let relative = src_entry.strip_prefix(&source_path)@@ -270,15 +265,11 @@ pub fn step_copy_ignored(if *is_dir {// A pure copy deletes no source, so the skip count has nothing to guard.- let _skipped = copy_dir_recursive(- src_entry,- &dest_entry,- Some(&dest_path),- force,- stamp,- &progress,- )- .with_context(|| format!("copying directory {}", format_path_for_display(relative)))?;+ let _skipped =+ copy_dir_recursive(src_entry, &dest_entry, Some(&dest_path), force, &progress)+ .with_context(|| {+ format!("copying directory {}", format_path_for_display(relative))+ })?;} else {if let Some(parent) = dest_entry.parent() {fs::create_dir_all(parent).with_context(|| {@@ -288,8 +279,7 @@ pub fn step_copy_ignored()})?;}- if let Some((bytes, data)) =- copy_leaf(src_entry, &dest_entry, Some(&dest_path), force, stamp)?+ if let Some((bytes, data)) = copy_leaf(src_entry, &dest_entry, Some(&dest_path), force)?{progress.record(bytes, data);}diff --git a/src/commands/step/promote.rs b/src/commands/step/promote.rsindex 4a98a38b6..d4e054862 100644--- a/src/commands/step/promote.rs+++ b/src/commands/step/promote.rs@@ -42,9 +42,9 @@ fn move_entry(src: &Path, dest: &Path, is_dir: bool) -> anyhow::Result<()> {/// refusing over one would abort a promote after the branch exchange.fn copy_and_remove(src: &Path, dest: &Path, is_dir: bool) -> anyhow::Result<()> {let skipped = if is_dir {- copy_dir_recursive(src, dest, None, true, None, &Progress::disabled())?+ copy_dir_recursive(src, dest, None, true, &Progress::disabled())?} else {- usize::from(copy_leaf(src, dest, None, true, None)?.is_none())+ usize::from(copy_leaf(src, dest, None, true)?.is_none())};if skipped > 0 {bail!(diff --git a/src/copy.rs b/src/copy.rsindex 809134d22..20e173793 100644--- a/src/copy.rs+++ b/src/copy.rs@@ -26,24 +26,23 @@//! clone did not happen, never why — an unsupported filesystem and a//! cross-device copy are indistinguishable here.//!-//! A caller can pass a `stamp` to give every copied regular file the same-//! modification time. Without one, a file keeps whatever time its copy left it:-//! APFS clones and Windows' `CopyFileEx` carry the source's, while Linux's-//! `FICLONE` and `fs::copy` fallback write the time of the copy — and since-//! leaves copy in parallel, that time orders files by thread scheduling.-//! Build tools read that order: cargo marks a crate dirty when a dependency's-//! output is newer than its own, so a `target/` copied that way recompiles a-//! scattering of crates (#4248). Stamping with a time taken before the copy-//! makes files within the tree compare equal and keeps them no older than a-//! checkout that preceded it; preserving source times instead would make-//! outputs older than freshly checked-out sources.+//! A copied regular file gets its source's modification time, as `cp -a` and+//! `cp -p` give it, on every platform. APFS clones and Windows' `CopyFileEx`+//! carry it over already; Linux's `FICLONE` and the byte-copy fallback write the+//! time of the copy, and since leaves copy in parallel, that time orders files by+//! thread scheduling. Build tools compare these times. Cargo marks a crate dirty+//! when a dependency's output is newer than its own (#4248), and treats an output+//! newer than its sources as up to date. Source times keep both comparisons as+//! they were in the source tree, and they leave copied outputs older than sources+//! that `git worktree add` just checked out, so those rebuild rather than reuse+//! an artifact built from other sources. Symlinks and directories keep the time+//! of the copy.use std::fs;use std::io::{self, ErrorKind};use std::path::{Path, PathBuf};use std::sync::LazyLock;use std::sync::atomic::{AtomicUsize, Ordering};-use std::time::SystemTime;use anyhow::Context;use rayon::prelude::*;@@ -197,15 +196,13 @@ static COPY_POOL: LazyLock<rayon::ThreadPool> = LazyLock::new(|| {/// leaf-symlink behavior is preserved (without `force` the symlink is skipped;/// with `force` the symlink itself is replaced).///-/// When `stamp` is `Some`, a copied regular file gets it as its modification-/// time (see the module docs). Symlinks keep theirs: `std` can't set a link's-/// own times without following it.+/// A copied regular file gets the source's modification time (see the module+/// docs).pub fn copy_leaf(src: &Path,dest: &Path,root: Option<&Path>,force: bool,- stamp: Option<SystemTime>,) -> anyhow::Result<Option<(u64, DataCopy)>> {if let Some(root) = root {ensure_path_within_root(dest.parent().unwrap_or(dest), root)?;@@ -281,9 +278,7 @@ pub fn copy_leaf(fs::set_permissions(dest, src_meta.permissions()).context("setting destination file permissions")?;}- if let Some(stamp) = stamp {- set_modified(dest, stamp);- }+ preserve_modified(dest, &src_meta);FORCED_DATA_COPY.unwrap_or(classify_copy(fallback))}Err(e) if e.kind() == ErrorKind::AlreadyExists => {@@ -307,11 +302,11 @@ pub fn copy_leaf(Ok(Some((bytes, data)))}-/// Set a copied file's modification time, best-effort: the contents are already-/// in place, so a file we can't reopen keeps its copy-time mtime rather than-/// failing a batch that otherwise succeeded. Logged at debug, so `wt -vv` shows-/// it.-fn set_modified(path: &Path, stamp: SystemTime) {+/// Give a copied file the source's modification time, best-effort: the contents+/// are already in place, so a file we can't reopen keeps its copy-time mtime+/// rather than failing a batch that otherwise succeeded. Logged at debug, so+/// `wt -vv` shows it.+fn preserve_modified(path: &Path, src_meta: &fs::Metadata) {// Windows needs a handle with FILE_WRITE_ATTRIBUTES, which a read-only// open doesn't grant; Unix sets times on any descriptor the owner holds.#[cfg(windows)]@@ -325,7 +320,10 @@ fn set_modified(path: &Path, stamp: SystemTime) {#[cfg(not(windows))]let file = fs::File::open(path);- if let Err(e) = file.and_then(|file| file.set_modified(stamp)) {+ let result = src_meta+ .modified()+ .and_then(|mtime| file.and_then(|file| file.set_modified(mtime)));+ if let Err(e) = result {tracing::debug!(path = %path.display(), "leaving copy-time mtime on {}: {e}", path.display());}}@@ -386,15 +384,12 @@ struct CopyLeaf {/// When `root` is `Some`, refuses destination directory ancestry that resolves/// outside `root`. Leaves inherit the guarantee because `entry.file_name()` is/// a single basename and cannot escape the validated parent directory.-///-/// `stamp` is passed to each [`copy_leaf`].#[must_use = "a caller that deletes the source must refuse on a non-zero skip count"]pub fn copy_dir_recursive(src: &Path,dest: &Path,root: Option<&Path>,force: bool,- stamp: Option<SystemTime>,progress: &Progress,) -> anyhow::Result<usize> {// Phase 1: Walk directories iteratively, creating dest dirs and collecting leaves.@@ -469,7 +464,7 @@ pub fn copy_dir_recursive(leaves.par_iter().try_for_each(|leaf| -> anyhow::Result<()> {- match copy_leaf(&leaf.src, &leaf.dest, None, force, stamp)? {+ match copy_leaf(&leaf.src, &leaf.dest, None, force)? {Some((bytes, data)) => progress.record(bytes, data),None => {skipped_leaves.fetch_add(1, Ordering::Relaxed);@@ -580,7 +575,7 @@ mod tests {let src = dest_dir.path().join("does-not-exist");let dest = dest_dir.path().join("dest");- let result = copy_leaf(&src, &dest, None, false, None).unwrap();+ let result = copy_leaf(&src, &dest, None, false).unwrap();assert_eq!(result, None);assert!(!dest.exists());@@ -596,7 +591,7 @@ mod tests {let dest = dir.path().join("dest");fs::write(&dest, b"pre-existing content").unwrap();- let result = copy_leaf(&src, &dest, None, true, None).unwrap();+ let result = copy_leaf(&src, &dest, None, true).unwrap();assert_eq!(result, None);assert_eq!(fs::read(&dest).unwrap(), b"pre-existing content");@@ -612,7 +607,7 @@ mod tests {fs::write(&src, b"source content").unwrap();fs::write(&dest, b"pre-existing content").unwrap();- let result = copy_leaf(&src, &dest, None, false, None).unwrap();+ let result = copy_leaf(&src, &dest, None, false).unwrap();assert_eq!(result, None);assert_eq!(fs::read(&dest).unwrap(), b"pre-existing content");@@ -772,7 +767,7 @@ mod tests {fs::write(&src, b"source content").unwrap();fs::write(&dest, b"pre-existing content").unwrap();- let result = copy_leaf(&src, &dest, None, true, None).unwrap();+ let result = copy_leaf(&src, &dest, None, true).unwrap();// Only the byte half is asserted: whether the copy reflinked depends on// the filesystem under the temp dir, which varies across CI runners.@@ -797,8 +792,7 @@ mod tests {fs::write(src.join("b"), b"b").unwrap();fs::write(dest.join("a"), b"pre-existing").unwrap();- let skipped =- copy_dir_recursive(&src, &dest, None, false, None, &Progress::disabled()).unwrap();+ let skipped = copy_dir_recursive(&src, &dest, None, false, &Progress::disabled()).unwrap();assert_eq!(skipped, 1);assert_eq!(fs::read(dest.join("a")).unwrap(), b"pre-existing");@@ -822,8 +816,7 @@ mod tests {// classify. Asserted, so a zero count can't come from an empty fixture.assert!(src.join("sock").exists());- let skipped =- copy_dir_recursive(&src, &dest, None, true, None, &Progress::disabled()).unwrap();+ let skipped = copy_dir_recursive(&src, &dest, None, true, &Progress::disabled()).unwrap();assert_eq!(skipped, 0);assert_eq!(fs::read(dest.join("regular")).unwrap(), b"content");@@ -841,8 +834,7 @@ mod tests {fs::write(src.join("a"), b"a").unwrap();fs::write(src.join("nested").join("b"), b"b").unwrap();- let skipped =- copy_dir_recursive(&src, &dest, None, true, None, &Progress::disabled()).unwrap();+ let skipped = copy_dir_recursive(&src, &dest, None, true, &Progress::disabled()).unwrap();assert_eq!(skipped, 0);assert_eq!(fs::read(dest.join("nested").join("b")).unwrap(), b"b");@@ -875,7 +867,6 @@ mod tests {&dest_root.join("node_modules"),Some(&dest_root),false,- None,&Progress::disabled(),).expect("a tree inside the destination root should copy");@@ -887,43 +878,9 @@ mod tests {);}- #[test]- fn test_copy_dir_recursive_stamps_every_file_with_one_mtime() {- // A copy that leaves each file its own write time orders files by- // thread scheduling, which cargo reads as a dependency newer than its- // dependent (#4248). With a stamp, every copied file carries it —- // whatever the source's mtimes were.- let dir = tempfile::tempdir().unwrap();- let src = dir.path().join("src");- let dest = dir.path().join("dest");- fs::create_dir_all(src.join("nested")).unwrap();- let old = SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1_500_000_000);- for (name, offset) in [("a", 0), ("nested/b", 7), ("nested/c", 3)] {- let path = src.join(name);- fs::write(&path, name).unwrap();- fs::File::options()- .write(true)- .open(&path)- .unwrap()- .set_modified(old + std::time::Duration::from_secs(offset))- .unwrap();- }- let stamp = SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1_700_000_000);-- let skipped =- copy_dir_recursive(&src, &dest, None, false, Some(stamp), &Progress::disabled())- .unwrap();-- assert_eq!(skipped, 0);- for name in ["a", "nested/b", "nested/c"] {- let mtime = fs::metadata(dest.join(name)).unwrap().modified().unwrap();- assert_eq!(mtime, stamp, "{name} should carry the stamp");- }- }-#[cfg(unix)]#[test]- fn test_copy_leaf_stamps_a_read_only_file() {+ fn test_copy_leaf_preserves_mtime_of_a_read_only_file() {// A read-only source copies to a read-only destination; setting its// time must not need write access to it.use std::os::unix::fs::PermissionsExt;@@ -931,13 +888,19 @@ mod tests {let src = dir.path().join("src");let dest = dir.path().join("dest");fs::write(&src, b"content").unwrap();+ let old = std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1_500_000_000);+ fs::File::options()+ .write(true)+ .open(&src)+ .unwrap()+ .set_modified(old)+ .unwrap();fs::set_permissions(&src, fs::Permissions::from_mode(0o444)).unwrap();- let stamp = SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1_700_000_000);- copy_leaf(&src, &dest, None, false, Some(stamp)).unwrap();+ copy_leaf(&src, &dest, None, false).unwrap();let meta = fs::metadata(&dest).unwrap();- assert_eq!(meta.modified().unwrap(), stamp);+ assert_eq!(meta.modified().unwrap(), old);assert_eq!(meta.permissions().mode() & 0o777, 0o444);}@@ -950,8 +913,7 @@ mod tests {let src = dir.path().join("does-not-exist");let dest = dir.path().join("dest");- let err =- copy_dir_recursive(&src, &dest, None, false, None, &Progress::disabled()).unwrap_err();+ let err = copy_dir_recursive(&src, &dest, None, false, &Progress::disabled()).unwrap_err();assert!(err.to_string().contains("reading directory"),diff --git a/tests/integration_tests/step_copy_ignored.rs b/tests/integration_tests/step_copy_ignored.rsindex a1acb7c34..7f4692607 100644--- a/tests/integration_tests/step_copy_ignored.rs+++ b/tests/integration_tests/step_copy_ignored.rs@@ -508,6 +508,45 @@ fn test_copy_ignored_directory(mut repo: TestRepo) {);}+/// Copied files keep their source's mtime, as `cp -a` does. Cargo treats an+/// output newer than its sources as up to date, so a copied `target/` stamped+/// with the copy time would hide that the new worktree's sources changed.+#[rstest]+fn test_copy_ignored_preserves_source_mtimes(mut repo: TestRepo) {+ let feature_path = repo.add_worktree("feature");++ fs::create_dir_all(repo.root_path().join("target/debug")).unwrap();+ fs::write(repo.root_path().join(".gitignore"), "target/\n.env\n").unwrap();+ // One file reached through a directory entry, one as a top-level entry,+ // each with its own past mtime.+ let base = std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1_500_000_000);+ let files = [+ ("target/debug/output", base),+ (+ "target/CACHEDIR.TAG",+ base + std::time::Duration::from_secs(7),+ ),+ (".env", base + std::time::Duration::from_secs(3)),+ ];+ for (name, mtime) in files {+ let path = repo.root_path().join(name);+ fs::write(&path, name).unwrap();+ fs::File::options()+ .write(true)+ .open(&path)+ .unwrap()+ .set_modified(mtime)+ .unwrap();+ }++ run_copy_ignored(&repo, &feature_path);++ for (name, mtime) in files {+ let copied = fs::metadata(feature_path.join(name)).unwrap();+ assert_eq!(copied.modified().unwrap(), mtime, "{name} mtime");+ }+}+/// Test glob patterns: .env.*#[rstest]fn test_copy_ignored_glob_pattern(mut repo: TestRepo) {