Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Preserve non-UTF-8 ignored paths (#4257)
DDL2710f7a8253c
diff --git a/src/commands/step/copy_ignored.rs b/src/commands/step/copy_ignored.rsindex ba216759d..b7417170d 100644--- a/src/commands/step/copy_ignored.rs+++ b/src/commands/step/copy_ignored.rs@@ -185,7 +185,7 @@ pub fn step_copy_ignored(.strip_prefix(&source_path).unwrap_or(src_entry.as_path());serde_json::json!({- "path": relative,+ "path": relative.to_string_lossy(),"kind": if *is_dir { "dir" } else { "file" },})})@@ -303,7 +303,7 @@ pub fn step_copy_ignored(.strip_prefix(&source_path).unwrap_or(src_entry.as_path());serde_json::json!({- "path": relative,+ "path": relative.to_string_lossy(),"kind": if *is_dir { "dir" } else { "file" },})})diff --git a/src/commands/step/shared.rs b/src/commands/step/shared.rsindex 26097cfc5..5acb92c8c 100644--- a/src/commands/step/shared.rs+++ b/src/commands/step/shared.rs@@ -225,20 +225,36 @@ fn list_ignored_entries(return Err(worktrunk::git::CommandError::from_failed_output("git", &args, &output).into());}- // Parse output: NUL-separated entries; directories end with /- let entries = String::from_utf8_lossy(&output.stdout)- .split('\0')+ // Git's -z output contains path bytes, which need not be UTF-8 on Unix.+ let entries = output+ .stdout+ .split(|&byte| byte == 0).filter(|entry| !entry.is_empty()).map(|entry| {- let is_dir = entry.ends_with('/');- let path = worktree_path.join(entry.trim_end_matches('/'));- (path, is_dir)+ let (relative, is_dir) = match entry.strip_suffix(b"/") {+ Some(relative) => (relative, true),+ None => (entry, false),+ };+ (worktree_path.join(path_from_git_bytes(relative)), is_dir)}).collect();Ok(entries)}+#[cfg(unix)]+fn path_from_git_bytes(bytes: &[u8]) -> PathBuf {+ use std::ffi::OsString;+ use std::os::unix::ffi::OsStringExt;++ OsString::from_vec(bytes.to_vec()).into()+}++#[cfg(not(unix))]+fn path_from_git_bytes(bytes: &[u8]) -> PathBuf {+ String::from_utf8_lossy(bytes).into_owned().into()+}+#[cfg(test)]mod tests {use worktrunk::git::CommandError;diff --git a/tests/integration_tests/step_copy_ignored.rs b/tests/integration_tests/step_copy_ignored.rsindex a18038df1..ffe792919 100644--- a/tests/integration_tests/step_copy_ignored.rs+++ b/tests/integration_tests/step_copy_ignored.rs@@ -298,6 +298,46 @@ fn test_copy_ignored_basic(mut repo: TestRepo) {);}+/// Git paths are byte strings on Linux. The `-z` discovery output must reach+/// the filesystem unchanged instead of replacing invalid UTF-8 with U+FFFD.+#[cfg(target_os = "linux")]+#[rstest]+fn test_copy_ignored_preserves_non_utf8_filename(mut repo: TestRepo) {+ use std::ffi::OsString;+ use std::os::unix::ffi::OsStringExt;++ let feature_path = repo.add_worktree("feature");+ let name = OsString::from_vec(b"cache-\xff.bin".to_vec());+ fs::write(repo.root_path().join(&name), "payload").unwrap();+ fs::write(repo.root_path().join(".gitignore"), "cache-*.bin\n").unwrap();++ for (dry_run, outcome) in [(true, "planned"), (false, "copied")] {+ let mut command = repo.wt_command();+ command+ .args(["step", "copy-ignored", "--format=json"])+ .current_dir(&feature_path);+ if dry_run {+ command.arg("--dry-run");+ }+ let output = command.output().unwrap();+ assert!(+ output.status.success(),+ "copy-ignored JSON failed: {}",+ String::from_utf8_lossy(&output.stderr)+ );+ let payload: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();+ assert_eq!(payload["outcome"], outcome);+ assert_eq!(payload["entries"][0]["path"], "cache-\u{fffd}.bin");+ assert_eq!(feature_path.join(&name).exists(), !dry_run);+ }++ assert_eq!(+ fs::read(feature_path.join(name)).unwrap(),+ b"payload",+ "copy-ignored must preserve the filename's original bytes"+ );+}+/// Test idempotent behavior: running twice should succeed (skips existing files)#[rstest]fn test_copy_ignored_idempotent(mut repo: TestRepo) {