Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Add normalize-path dependency for lexical path normalization
Maximilian Roos479dd0b413aa
diff --git a/Cargo.lock b/Cargo.lockindex ec18a21f3..70826ccc6 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -1369,6 +1369,12 @@ dependencies = ["minimal-lexical",]+[[package]]+name = "normalize-path"+version = "0.2.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "f5438dd2b2ff4c6df6e1ce22d825ed2fa93ee2922235cc45186991717f0a892d"+[[package]]name = "num-conv"version = "0.1.0"@@ -2957,6 +2963,7 @@ dependencies = ["insta-cmd","log","minijinja",+ "normalize-path","osc8","portable-pty","rayon",diff --git a/Cargo.toml b/Cargo.tomlindex 8dc8ca086..73412e66b 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -73,6 +73,7 @@ unicode-width = "0.2"wrap-ansi = "0.1"osc8 = "0.1.0"home = "0.5.12"+normalize-path = "0.2.1"[target.'cfg(unix)'.dependencies]skim = "0.20"diff --git a/src/commands/worktree.rs b/src/commands/worktree.rsindex 8897dc31c..8a99dbbf6 100644--- a/src/commands/worktree.rs+++ b/src/commands/worktree.rs@@ -110,8 +110,8 @@//!//! The shell wrapper is generated by `wt config shell init <shell>` from templates in `templates/`.-use anyhow::Context;use color_print::cformat;+use normalize_path::NormalizePath;use std::path::PathBuf;use worktrunk::HookType;use worktrunk::config::{CommandPhase, WorktrunkConfig};@@ -227,7 +227,8 @@ fn compute_worktree_path(.format_path(repo_name, branch).map_err(|e| anyhow::anyhow!("Failed to format worktree path: {e}"))?;- Ok(repo_root.join(relative_path))+ // Normalize to resolve ".." from relative paths like "../repo.branch"+ Ok(repo_root.join(relative_path).normalize())}/// Flags indicating which merge operations occurred@@ -369,6 +370,11 @@ pub fn handle_switch(// Path-first lookup: check if a worktree exists at the expected pathif let Some((existing_path, path_branch)) = repo.worktree_at_path(&expected_path)? {+ // Check if directory actually exists (git might have stale metadata)+ if !existing_path.exists() {+ let branch = path_branch.unwrap_or_else(|| resolved_branch.clone());+ return Err(GitError::WorktreeMissing { branch }.into());+ }// Worktree exists at expected path - switch to it regardless of its branchlet _ = repo.record_switch_previous(new_previous.as_deref());let actual_branch = path_branch.unwrap_or_else(|| resolved_branch.clone());@@ -442,11 +448,8 @@ pub fn handle_switch(line.split('\'').nth(1).or_else(|| line.split('"').nth(1))}){- let path = std::path::PathBuf::from(path_str);- // Canonicalize if possible, otherwise use as-is- let normalized_path = path.canonicalize().unwrap_or(path);return Err(GitError::WorktreePathExists {- path: normalized_path,+ path: std::path::PathBuf::from(path_str),}.into());}@@ -468,11 +471,6 @@ pub fn handle_switch(.into());}- // Canonicalize the path to resolve any .. components- let worktree_path = worktree_path- .canonicalize()- .context("Failed to canonicalize worktree path")?;-// Check if git's DWIM created a tracking branch from a remote// This happens when we don't use --create and the branch exists on a remotelet from_remote = if !create {diff --git a/src/git/error.rs b/src/git/error.rsindex 397ce1df4..448a97ba5 100644--- a/src/git/error.rs+++ b/src/git/error.rs@@ -252,7 +252,9 @@ impl std::fmt::Display for GitError {error_message(cformat!("Directory already exists: <bold>{path_display}</>")),- hint_message("Remove the directory or use a different branch name")+ hint_message(cformat!(+ "Remove with <bright-black>rm -rf {path_display}</> or use a different branch name"+ )))}diff --git a/src/git/repository/mod.rs b/src/git/repository/mod.rsindex 413d5c805..2c928b50d 100644--- a/src/git/repository/mod.rs+++ b/src/git/repository/mod.rs@@ -3,6 +3,7 @@ use std::process::Command;use std::sync::OnceLock;use anyhow::{Context, bail};+use normalize_path::NormalizePath;// Import types and functions from parent module (mod.rs)use super::{@@ -1191,12 +1192,13 @@ impl Repository {path: &Path,) -> anyhow::Result<Option<(PathBuf, Option<String>)>> {let worktrees = self.list_worktrees()?;- let canonical_path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());+ // Use lexical normalization so comparison works even when path doesn't exist+ let normalized_path = path.normalize();Ok(worktrees.worktrees.iter()- .find(|wt| wt.path.canonicalize().unwrap_or_else(|_| wt.path.clone()) == canonical_path)+ .find(|wt| wt.path.normalize() == normalized_path).map(|wt| (wt.path.clone(), wt.branch.clone())))}diff --git a/src/path.rs b/src/path.rsindex c0155c6b7..7d421a385 100644--- a/src/path.rs+++ b/src/path.rs@@ -11,9 +11,8 @@ pub fn home_dir() -> Option<PathBuf> {/// Format a filesystem path for user-facing output.///-/// When the path lives under the user's home directory, it is shown with a-/// leading `~` (e.g., `/Users/alex/projects/wt` -> `~/projects/wt`). Paths-/// outside home are returned unchanged.+/// Replaces home directory prefix with `~` (e.g., `/Users/alex/projects/wt` -> `~/projects/wt`).+/// Paths outside home are returned unchanged.pub fn format_path_for_display(path: &Path) -> String {if let Some(home) = home_dir()&& let Ok(stripped) = path.strip_prefix(&home)diff --git a/tests/snapshots/git_error_display__worktree_path_exists.snap b/tests/snapshots/git_error_display__worktree_path_exists.snapindex 15db75629..829bea2e2 100644--- a/tests/snapshots/git_error_display__worktree_path_exists.snap+++ b/tests/snapshots/git_error_display__worktree_path_exists.snap@@ -1,7 +1,8 @@---source: tests/git_error_display.rs+assertion_line: 66expression: err.to_string()---β [31mDirectory already exists: [1m/tmp/repo.feature[22m[39m-π‘ [2mRemove the directory or use a different branch name[22m+π‘ [2mRemove with [90mrm -rf /tmp/repo.feature[39m or use a different branch name[22mdiff --git a/tests/snapshots/integration__integration_tests__switch__switch_error_path_occupied.snap b/tests/snapshots/integration__integration_tests__switch__switch_error_path_occupied.snapindex 4601c6d2d..242eae4ca 100644--- a/tests/snapshots/integration__integration_tests__switch__switch_error_path_occupied.snap+++ b/tests/snapshots/integration__integration_tests__switch__switch_error_path_occupied.snap@@ -1,5 +1,6 @@---source: tests/integration_tests/switch.rs+assertion_line: 48info:program: wtargs:@@ -25,6 +26,6 @@ exit_code: 1----- stdout ---------- stderr ------β [31mDirectory already exists: [1m[REPO]/../repo.occupied-branch[22m[39m+β [31mDirectory already exists: [1m[REPO].occupied-branch[22m[39m-π‘ [2mRemove the directory or use a different branch name[22m+π‘ [2mRemove with [90mrm -rf [REPO].occupied-branch[39m or use a different branch name[22m