Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

Fix push conflicts for non-UTF-8 paths (#4259)

DDL98533f3774b4
diff --git a/src/commands/repository_ext.rs b/src/commands/repository_ext.rs
index 43672efdc..577b8a134 100644
--- a/src/commands/repository_ext.rs
+++ b/src/commands/repository_ext.rs
@@ -451,18 +451,18 @@ impl RepositoryCliExt for Repository {
// filenames with spaces and renames ("XY path\0" for normal files,
// "XY new_path\0old_path\0" for renames/copies).
let wt = self.worktree_at(wt_path);
- let wt_status_output = wt.run_command(&["status", "--porcelain", "-z", "-uall"])?;
- if wt_status_output.trim().is_empty() {
+ let wt_status_output = wt.run_command_bytes(&["status", "--porcelain", "-z", "-uall"])?;
+ if wt_status_output.is_empty() {
return Ok(());
}
let push_files = self.changed_files(target_branch, "HEAD")?;
- let wt_files: Vec<String> = parse_porcelain_z(&wt_status_output);
+ let wt_files = parse_porcelain_z(&wt_status_output);
let overlapping: Vec<String> = push_files
.iter()
.filter(|f| wt_files.contains(f))
- .cloned()
+ .map(|path| String::from_utf8_lossy(path).into_owned())
.collect();
if !overlapping.is_empty() {
@@ -732,95 +732,6 @@ mod tests {
);
}
- #[test]
- fn test_parse_porcelain_z_modified_staged() {
- // "M file.txt\0" - staged modification
- let output = "M file.txt\0";
- assert_eq!(parse_porcelain_z(output), vec!["file.txt"]);
- }
-
- #[test]
- fn test_parse_porcelain_z_modified_unstaged() {
- // " M file.txt\0" - unstaged modification (this was the bug case)
- let output = " M file.txt\0";
- assert_eq!(parse_porcelain_z(output), vec!["file.txt"]);
- }
-
- #[test]
- fn test_parse_porcelain_z_modified_both() {
- // "MM file.txt\0" - both staged and unstaged
- let output = "MM file.txt\0";
- assert_eq!(parse_porcelain_z(output), vec!["file.txt"]);
- }
-
- #[test]
- fn test_parse_porcelain_z_untracked() {
- // "?? new.txt\0" - untracked file
- let output = "?? new.txt\0";
- assert_eq!(parse_porcelain_z(output), vec!["new.txt"]);
- }
-
- #[test]
- fn test_parse_porcelain_z_rename() {
- // "R new.txt\0old.txt\0" - rename includes both paths
- let output = "R new.txt\0old.txt\0";
- let result = parse_porcelain_z(output);
- assert_eq!(result, vec!["new.txt", "old.txt"]);
- }
-
- #[test]
- fn test_parse_porcelain_z_copy() {
- // "C copy.txt\0original.txt\0" - copy includes both paths
- let output = "C copy.txt\0original.txt\0";
- let result = parse_porcelain_z(output);
- assert_eq!(result, vec!["copy.txt", "original.txt"]);
- }
-
- #[test]
- fn test_parse_porcelain_z_multiple_files() {
- // Multiple files with different statuses
- let output = " M file1.txt\0M file2.txt\0?? untracked.txt\0R new.txt\0old.txt\0";
- let result = parse_porcelain_z(output);
- assert_eq!(
- result,
- vec![
- "file1.txt",
- "file2.txt",
- "untracked.txt",
- "new.txt",
- "old.txt"
- ]
- );
- }
-
- #[test]
- fn test_parse_porcelain_z_filename_with_spaces() {
- // "M file with spaces.txt\0"
- let output = "M file with spaces.txt\0";
- assert_eq!(parse_porcelain_z(output), vec!["file with spaces.txt"]);
- }
-
- #[test]
- fn test_parse_porcelain_z_empty() {
- assert_eq!(parse_porcelain_z(""), Vec::<String>::new());
- }
-
- #[test]
- fn test_parse_porcelain_z_short_entry_skipped() {
- // Entry too short to have path (malformed, shouldn't happen in practice)
- let output = "M\0";
- assert_eq!(parse_porcelain_z(output), Vec::<String>::new());
- }
-
- #[test]
- fn test_parse_porcelain_z_rename_missing_old_path() {
- // Rename without old path (malformed, but should handle gracefully)
- let output = "R new.txt\0";
- let result = parse_porcelain_z(output);
- // Should include new.txt, old path is simply not added
- assert_eq!(result, vec!["new.txt"]);
- }
-
#[test]
fn test_parse_untracked_files_single() {
assert_eq!(parse_untracked_files("?? new.txt\0"), vec!["new.txt"]);
diff --git a/src/git/parse.rs b/src/git/parse.rs
index 485408537..ccff00d83 100644
--- a/src/git/parse.rs
+++ b/src/git/parse.rs
@@ -145,9 +145,12 @@ impl DefaultBranchName {
///
/// This correctly handles filenames with spaces and ensures both old and new
/// paths are included for renames/copies (important for overlap detection).
-pub fn parse_porcelain_z(output: &str) -> Vec<String> {
+/// Raw bytes keep distinct non-UTF-8 paths distinct during comparison.
+pub fn parse_porcelain_z(output: &[u8]) -> Vec<Vec<u8>> {
let mut files = Vec::new();
- let mut entries = output.split('\0').filter(|s| !s.is_empty());
+ let mut entries = output
+ .split(|byte| *byte == 0)
+ .filter(|entry| !entry.is_empty());
while let Some(entry) = entries.next() {
// Each entry is "XY path" where XY is exactly 2 status chars
@@ -157,13 +160,12 @@ pub fn parse_porcelain_z(output: &str) -> Vec<String> {
let status = &entry[0..2];
let path = &entry[3..];
- files.push(path.to_string());
+ files.push(path.to_vec());
// For renames (R) and copies (C), the next NUL-separated field is the old path
- if status.contains(['R', 'C'])
- && let Some(old_path) = entries.next()
- {
- files.push(old_path.to_string());
+ let has_source_path = status.contains(&b'R') || status.contains(&b'C');
+ if has_source_path && let Some(old_path) = entries.next() {
+ files.push(old_path.to_vec());
}
}
@@ -394,66 +396,97 @@ mod tests {
#[test]
fn test_parse_porcelain_z_empty() {
- assert!(parse_porcelain_z("").is_empty());
+ assert!(parse_porcelain_z(b"").is_empty());
}
#[test]
fn test_parse_porcelain_z_modified_file() {
// "M src/main.rs\0"
let output = " M src/main.rs\0";
- let files = parse_porcelain_z(output);
- assert_eq!(files, vec!["src/main.rs"]);
+ let files = parse_porcelain_z(output.as_bytes());
+ assert_eq!(files, vec![b"src/main.rs".to_vec()]);
}
#[test]
fn test_parse_porcelain_z_multiple_files() {
let output = " M src/main.rs\0?? new_file.txt\0";
- let files = parse_porcelain_z(output);
- assert_eq!(files, vec!["src/main.rs", "new_file.txt"]);
+ let files = parse_porcelain_z(output.as_bytes());
+ assert_eq!(
+ files,
+ vec![b"src/main.rs".to_vec(), b"new_file.txt".to_vec()]
+ );
}
#[test]
fn test_parse_porcelain_z_rename() {
// Renames: "R new_name\0old_name\0"
let output = "R new_name.rs\0old_name.rs\0";
- let files = parse_porcelain_z(output);
- assert_eq!(files, vec!["new_name.rs", "old_name.rs"]);
+ let files = parse_porcelain_z(output.as_bytes());
+ assert_eq!(
+ files,
+ vec![b"new_name.rs".to_vec(), b"old_name.rs".to_vec()]
+ );
}
#[test]
fn test_parse_porcelain_z_copy() {
let output = "C copy.rs\0original.rs\0";
- let files = parse_porcelain_z(output);
- assert_eq!(files, vec!["copy.rs", "original.rs"]);
+ let files = parse_porcelain_z(output.as_bytes());
+ assert_eq!(files, vec![b"copy.rs".to_vec(), b"original.rs".to_vec()]);
}
#[test]
fn test_parse_porcelain_z_rename_among_others() {
let output = " M keep.rs\0R new.rs\0old.rs\0?? untracked.txt\0";
- let files = parse_porcelain_z(output);
- assert_eq!(files, vec!["keep.rs", "new.rs", "old.rs", "untracked.txt"]);
+ let files = parse_porcelain_z(output.as_bytes());
+ assert_eq!(
+ files,
+ vec![
+ b"keep.rs".to_vec(),
+ b"new.rs".to_vec(),
+ b"old.rs".to_vec(),
+ b"untracked.txt".to_vec()
+ ]
+ );
}
#[test]
fn test_parse_porcelain_z_worktree_rename() {
let output = " R new.rs\0old.rs\0?? untracked.txt\0";
- let files = parse_porcelain_z(output);
- assert_eq!(files, vec!["new.rs", "old.rs", "untracked.txt"]);
+ let files = parse_porcelain_z(output.as_bytes());
+ assert_eq!(
+ files,
+ vec![
+ b"new.rs".to_vec(),
+ b"old.rs".to_vec(),
+ b"untracked.txt".to_vec()
+ ]
+ );
}
#[test]
fn test_parse_porcelain_z_spaces_in_path() {
let output = " M path with spaces/file name.rs\0";
- let files = parse_porcelain_z(output);
- assert_eq!(files, vec!["path with spaces/file name.rs"]);
+ let files = parse_porcelain_z(output.as_bytes());
+ assert_eq!(files, vec![b"path with spaces/file name.rs".to_vec()]);
}
#[test]
fn test_parse_porcelain_z_skips_short_entries() {
// Entries shorter than 3 chars (status + space + path) are skipped
let output = " M valid.rs\0ab\0";
+ let files = parse_porcelain_z(output.as_bytes());
+ assert_eq!(files, vec![b"valid.rs".to_vec()]);
+ }
+
+ #[test]
+ fn test_parse_porcelain_z_preserves_distinct_non_utf8_paths() {
+ let output = b" D collision-\xfe\0 D collision-\xff\0";
let files = parse_porcelain_z(output);
- assert_eq!(files, vec!["valid.rs"]);
+ assert_eq!(
+ files,
+ vec![b"collision-\xfe".to_vec(), b"collision-\xff".to_vec()]
+ );
}
// ============================================================================
diff --git a/src/git/repository/diff.rs b/src/git/repository/diff.rs
index 5da204010..7b6415c50 100644
--- a/src/git/repository/diff.rs
+++ b/src/git/repository/diff.rs
@@ -314,14 +314,14 @@ impl Repository {
}
/// Get files changed between base and head, as repository-root-relative
- /// paths.
+ /// raw path bytes.
///
/// `diff-tree` detects no renames, so a moved file lists both its old and
/// new path — what overlap detection needs (e.g., detecting conflicts when
/// a file is renamed in one branch but has uncommitted changes under the
/// old name).
- pub fn changed_files(&self, base: &str, head: &str) -> anyhow::Result<Vec<String>> {
- let stdout = self.run_command(&PlumbingDiff::Tree.args(&[
+ pub fn changed_files(&self, base: &str, head: &str) -> anyhow::Result<Vec<Vec<u8>>> {
+ let args = PlumbingDiff::Tree.args(&[
"-r",
"--name-only",
"-z",
@@ -329,11 +329,12 @@ impl Repository {
base,
head,
"--",
- ]))?;
- Ok(stdout
- .split('\0')
+ ]);
+ Ok(self
+ .run_command_bytes(&args)?
+ .split(|byte| *byte == 0)
.filter(|path| !path.is_empty())
- .map(str::to_string)
+ .map(<[u8]>::to_vec)
.collect())
}
diff --git a/src/git/repository/mod.rs b/src/git/repository/mod.rs
index 1c6bfbb3c..9566e1e93 100644
--- a/src/git/repository/mod.rs
+++ b/src/git/repository/mod.rs
@@ -1975,6 +1975,19 @@ impl Repository {
args: &[&str],
timeout: Option<std::time::Duration>,
) -> anyhow::Result<String> {
+ Ok(String::from_utf8_lossy(&self.run_command_bytes_bounded(args, timeout)?).into_owned())
+ }
+
+ /// Run a git command and return stdout without decoding paths.
+ pub fn run_command_bytes(&self, args: &[&str]) -> anyhow::Result<Vec<u8>> {
+ self.run_command_bytes_bounded(args, None)
+ }
+
+ fn run_command_bytes_bounded(
+ &self,
+ args: &[&str],
+ timeout: Option<std::time::Duration>,
+ ) -> anyhow::Result<Vec<u8>> {
let mut cmd = self.with_object_store_env(
Cmd::new("git")
.args(args.iter().copied())
@@ -1995,8 +2008,7 @@ impl Repository {
);
}
- let stdout = String::from_utf8_lossy(&output.stdout).into_owned();
- Ok(stdout)
+ Ok(output.stdout)
}
/// Run a git command and return whether it succeeded (exit code 0).
diff --git a/src/git/repository/working_tree.rs b/src/git/repository/working_tree.rs
index f89f65f75..f6a7f0629 100644
--- a/src/git/repository/working_tree.rs
+++ b/src/git/repository/working_tree.rs
@@ -330,14 +330,18 @@ impl<'a> WorkingTree<'a> {
/// Run a git command in this worktree and return stdout.
pub fn run_command(&self, args: &[&str]) -> anyhow::Result<String> {
+ Ok(String::from_utf8_lossy(&self.run_command_bytes(args)?).into_owned())
+ }
+
+ /// Run a git command in this worktree and return stdout without decoding paths.
+ pub fn run_command_bytes(&self, args: &[&str]) -> anyhow::Result<Vec<u8>> {
let output = self.run_command_output(args)?;
if !output.status.success() {
return Err(CommandError::from_failed_output("git", args, &output).into());
}
- let stdout = String::from_utf8_lossy(&output.stdout).into_owned();
- Ok(stdout)
+ Ok(output.stdout)
}
/// Run a git command in this worktree and return the raw Output.
diff --git a/tests/integration_tests/push.rs b/tests/integration_tests/push.rs
index bcf976bd1..9aa064144 100644
--- a/tests/integration_tests/push.rs
+++ b/tests/integration_tests/push.rs
@@ -160,6 +160,58 @@ fn test_push_dirty_target_changes_stay_in_place(mut repo: TestRepo) {
);
}
+/// Distinct non-UTF-8 paths must not collapse to the same replacement string
+/// during target-conflict detection.
+#[cfg(target_os = "linux")]
+#[rstest]
+fn test_push_distinguishes_non_utf8_target_paths(mut repo: TestRepo) {
+ use std::ffi::OsString;
+ use std::os::unix::ffi::OsStringExt;
+
+ let target_path = OsString::from_vec(b"collision-\xfe".to_vec());
+ let pushed_path = OsString::from_vec(b"collision-\xff".to_vec());
+ let root = repo.root_path().to_path_buf();
+ std::fs::write(root.join(&target_path), "target").unwrap();
+ std::fs::write(root.join(&pushed_path), "pushed").unwrap();
+ repo.run_git(&["add", "-A"]);
+ repo.run_git(&["commit", "-m", "Add raw paths"]);
+
+ let feature_wt = repo.add_worktree("feature");
+ std::fs::remove_file(feature_wt.join(&pushed_path)).unwrap();
+ repo.run_git_in(&feature_wt, &["add", "-A"]);
+ repo.run_git_in(&feature_wt, &["commit", "-m", "Delete pushed path"]);
+ std::fs::remove_file(root.join(&target_path)).unwrap();
+
+ let target_status = repo
+ .git_command()
+ .args(["status", "--porcelain", "-z"])
+ .run()
+ .unwrap();
+ assert_eq!(target_status.stdout, b" D collision-\xfe\0");
+ let repository = worktrunk::git::Repository::at(&root).unwrap();
+ assert_eq!(
+ repository.changed_files("main", "feature").unwrap(),
+ [b"collision-\xff".to_vec()]
+ );
+
+ let output = repo
+ .wt_command()
+ .current_dir(&feature_wt)
+ .args(["step", "push", "main"])
+ .output()
+ .unwrap();
+ assert!(
+ output.status.success(),
+ "distinct raw paths must not conflict: {}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+ assert_eq!(
+ repo.git_output(&["rev-parse", "main"]),
+ repo.git_output(&["rev-parse", "feature"])
+ );
+ assert!(!root.join(target_path).exists());
+}
+
#[rstest]
fn test_push_dirty_target_overlap_renamed_file(mut repo: TestRepo) {
// Regression test: overlap detection must detect conflicts when a file is renamed
diff --git a/tests/integration_tests/repository.rs b/tests/integration_tests/repository.rs
index 5b526ed87..026cf1a5b 100644
--- a/tests/integration_tests/repository.rs
+++ b/tests/integration_tests/repository.rs
@@ -1242,7 +1242,7 @@ fn test_submodule_bump_survives_submodule_ignore() {
);
assert_eq!(
repository.changed_files(&main_sha, &feature_sha).unwrap(),
- ["sub"]
+ [b"sub".to_vec()]
);
let stats = repository.branch_diff_stats("main", "feature").unwrap();
assert_eq!((stats.added, stats.deleted), (1, 1));