Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix: use full canonical path for project identifier fallback (#747)
Maximilian Roos100e5de1c247
diff --git a/src/commands/command_approval.rs b/src/commands/command_approval.rsindex 094885378..520c7a720 100644--- a/src/commands/command_approval.rs+++ b/src/commands/command_approval.rs@@ -103,7 +103,8 @@ pub fn approve_command_batch(fn prompt_for_batch_approval(commands: &[&HookCommand], project_id: &str) -> anyhow::Result<bool> {use std::io::{self, IsTerminal, Write};- let project_name = project_id.split('/').next_back().unwrap_or(project_id);+ // Extract just the directory name for display (handles both Unix '/' and Windows '\')+ let project_name = project_id.rsplit(['/', '\\']).next().unwrap_or(project_id);let count = commands.len();let plural = if count == 1 { "" } else { "s" };diff --git a/src/git/repository/remotes.rs b/src/git/repository/remotes.rsindex 6594931be..681070c03 100644--- a/src/git/repository/remotes.rs+++ b/src/git/repository/remotes.rs@@ -121,7 +121,7 @@ impl Repository {/// Get a project identifier for approval tracking.////// Uses the git remote URL if available (e.g., "github.com/user/repo"),- /// otherwise falls back to the repository directory name.+ /// otherwise falls back to the full canonical path of the repository.////// This identifier is used to track which commands have been approved/// for execution in this project.@@ -150,14 +150,16 @@ impl Repository {return Ok(url.to_string());}- // Fall back to repository name (use worktree base for consistency across all worktrees)+ // Fall back to full canonical path (use worktree base for consistency across all worktrees)+ // Full path avoids collisions across unrelated repos with the same directory namelet repo_root = self.repo_path();- let repo_name = repo_root- .file_name()- .and_then(|name| name.to_str())- .context("Repository directory has no valid name")?;+ let canonical =+ dunce::canonicalize(repo_root).unwrap_or_else(|_| repo_root.to_path_buf());+ let path_str = canonical+ .to_str()+ .context("Repository path is not valid UTF-8")?;- Ok(repo_name.to_string())+ Ok(path_str.to_string())}).cloned()}diff --git a/tests/common/mod.rs b/tests/common/mod.rsindex 29dcf9e5d..43a71a238 100644--- a/tests/common/mod.rs+++ b/tests/common/mod.rs@@ -1301,17 +1301,20 @@ impl TestRepo {/// Get the project identifier for test configs.///- /// Returns the repository directory name. This works because the standard+ /// Returns the full canonical path of the repository. This works because the standard/// fixture uses a local path remote (`../origin_git`) which doesn't parse- /// as a proper git URL, causing worktrunk to fall back to the directory name.+ /// as a proper git URL, causing worktrunk to fall back to the full canonical path.////// Use this when writing test configs with `[projects."<id>"]` sections.+ /// Backslashes are escaped for use in TOML string literals (Windows paths).pub fn project_id(&self) -> String {- self.root- .file_name()- .and_then(|n| n.to_str())- .unwrap_or("repo")- .to_string()+ let path = dunce::canonicalize(&self.root)+ .unwrap_or_else(|_| self.root.clone())+ .to_str()+ .unwrap_or("")+ .to_string();+ // Escape backslashes for TOML string literals (Windows paths)+ path.replace('\\', "\\\\")}/// Get the path to the isolated test config filediff --git a/tests/integration_tests/hook_show.rs b/tests/integration_tests/hook_show.rsindex b871c5c31..75dce7d3a 100644--- a/tests/integration_tests/hook_show.rs+++ b/tests/integration_tests/hook_show.rs@@ -113,20 +113,25 @@ deploy = "scripts/deploy.sh"#[rstest]fn test_hook_show_approval_status(repo: TestRepo, temp_home: TempDir) {- // Remove origin so project_identifier is "repo" (directory name)+ // Remove origin so project_identifier uses full canonical pathrepo.run_git(&["remote", "remove", "origin"]);+ // Get the canonical path for the project identifier (escaped for TOML)+ let project_id_str = repo.project_id();+// Create user config at XDG path with one approved commandlet global_config_dir = temp_home.path().join(".config").join("worktrunk");fs::create_dir_all(&global_config_dir).unwrap();let config_path = global_config_dir.join("config.toml");fs::write(&config_path,- r#"worktree-path = "../{{ repo }}.{{ branch }}"+ format!(+ r#"worktree-path = "../{{{{ repo }}}}.{{{{ branch }}}}"-[projects."repo"]+[projects."{project_id_str}"]approved-commands = ["cargo build"]-"#,+"#+ ),).unwrap();@@ -230,7 +235,7 @@ lint = "pre-commit run"/// Test `wt hook clear` when no approvals exist for the project.#[rstest]fn test_hook_clear_no_approvals(repo: TestRepo, temp_home: TempDir) {- // Remove origin so project_identifier is "repo" (directory name)+ // Remove origin so project_identifier uses full canonical pathrepo.run_git(&["remote", "remove", "origin"]);// Create user config without any project approvals@@ -260,20 +265,25 @@ fn test_hook_clear_no_approvals(repo: TestRepo, temp_home: TempDir) {/// Test `wt hook clear` when project has approvals to clear.#[rstest]fn test_hook_clear_with_approvals(repo: TestRepo, temp_home: TempDir) {- // Remove origin so project_identifier is "repo" (directory name)+ // Remove origin so project_identifier uses full canonical pathrepo.run_git(&["remote", "remove", "origin"]);+ // Get the canonical path for the project identifier (escaped for TOML)+ let project_id_str = repo.project_id();+// Create user config with approved commands for this projectlet global_config_dir = temp_home.path().join(".config").join("worktrunk");fs::create_dir_all(&global_config_dir).unwrap();let config_path = global_config_dir.join("config.toml");fs::write(&config_path,- r#"worktree-path = "../{{ repo }}.{{ branch }}"+ format!(+ r#"worktree-path = "../{{{{ repo }}}}.{{{{ branch }}}}"-[projects."repo"]+[projects."{project_id_str}"]approved-commands = ["cargo build", "cargo test", "npm install"]-"#,+"#+ ),).unwrap();diff --git a/tests/integration_tests/repository.rs b/tests/integration_tests/repository.rsindex 0b858795a..ca33a89fd 100644--- a/tests/integration_tests/repository.rs+++ b/tests/integration_tests/repository.rs@@ -294,8 +294,9 @@ fn test_project_identifier_no_remote_fallback() {let repository = Repository::at(repo.root_path().to_path_buf()).unwrap();let id = repository.project_identifier().unwrap();- // Should be the repo directory name- assert_eq!(id, "repo");+ // Should be the full canonical path (security: avoids collisions across unrelated repos)+ let expected = dunce::canonicalize(repo.root_path()).unwrap();+ assert_eq!(id, expected.to_str().unwrap());}// =============================================================================