Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Refactor branch name sanitization for consistency
Maximilian Roos40e4a2c5cf0e
diff --git a/src/commands/statusline.rs b/src/commands/statusline.rsindex 173f64977..850346d80 100644--- a/src/commands/statusline.rs+++ b/src/commands/statusline.rs@@ -166,7 +166,9 @@ pub fn run(claude_code: bool) -> Result<()> {let status_to_show = if let Some(ref dir) = dir_str {// status_line format: "branch rest..." - check if dir ends with .branchif let Some((branch, rest)) = status_line.split_once(" ") {- let pattern = format!(".{branch}");+ // Normalize branch name for comparison (slashes become dashes in paths)+ let normalized_branch = worktrunk::config::sanitize_branch_name(branch);+ let pattern = format!(".{normalized_branch}");if dir.ends_with(&pattern) {// Directory already shows branch via worktrunk template, skip itrest.to_string()@@ -352,4 +354,24 @@ mod tests {assert!(ClaudeCodeContext::parse("not json").is_none());assert!(ClaudeCodeContext::parse("{invalid}").is_none());}++ #[test]+ fn test_branch_deduplication_with_slashes() {+ // Simulate the actual scenario:+ // - Directory: ~/w/insta.claude-fix-snapshot-merge-conflicts-xyz+ // - Branch: claude/fix-snapshot-merge-conflicts-xyz+ let dir = "~/w/insta.claude-fix-snapshot-merge-conflicts-xyz";+ let branch = "claude/fix-snapshot-merge-conflicts-xyz";++ let normalized_branch = worktrunk::config::sanitize_branch_name(branch);+ let pattern = format!(".{normalized_branch}");++ assert!(+ dir.ends_with(&pattern),+ "Directory '{}' should end with pattern '{}' (normalized from branch '{}')",+ dir,+ pattern,+ branch+ );+ }}diff --git a/src/config/expansion.rs b/src/config/expansion.rsindex e337472a9..70a8eee39 100644--- a/src/config/expansion.rs+++ b/src/config/expansion.rs@@ -5,6 +5,23 @@use minijinja::Environment;+/// Sanitize a branch name for use in filesystem paths.+///+/// Replaces path separators (`/` and `\`) with dashes to prevent directory traversal+/// and ensure the branch name is a single path component.+///+/// # Examples+/// ```+/// use worktrunk::config::sanitize_branch_name;+///+/// assert_eq!(sanitize_branch_name("feature/foo"), "feature-foo");+/// assert_eq!(sanitize_branch_name("user\\task"), "user-task");+/// assert_eq!(sanitize_branch_name("simple-branch"), "simple-branch");+/// ```+pub fn sanitize_branch_name(branch: &str) -> String {+ branch.replace(['/', '\\'], "-")+}+/// Expand template variables using minijinja////// All templates support:@@ -40,7 +57,7 @@ pub fn expand_template(use std::borrow::Cow;// Sanitize branch name by replacing path separators- let safe_branch = branch.replace(['/', '\\'], "-");+ let safe_branch = sanitize_branch_name(branch);// Shell-escape all variableslet escaped_worktree = escape(Cow::Borrowed(main_worktree)).to_string();diff --git a/src/config/mod.rs b/src/config/mod.rsindex f3d3884b4..a27206f2d 100644--- a/src/config/mod.rs+++ b/src/config/mod.rs@@ -45,7 +45,7 @@ mod user;// Re-export public typespub use commands::{Command, CommandConfig, CommandPhase};-pub use expansion::{expand_command_template, expand_template};+pub use expansion::{expand_command_template, expand_template, sanitize_branch_name};pub use project::{ProjectConfig, find_unknown_keys as find_unknown_project_keys};pub use user::{CommitGenerationConfig, StageMode, UserProjectConfig, WorktrunkConfig,