Skip to content

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

fix: normalize deprecated template vars in approval matching (#388)

Maximilian Roos473654857cdf
diff --git a/src/config/deprecation.rs b/src/config/deprecation.rs
index 62f070996..139eac160 100644
--- a/src/config/deprecation.rs
+++ b/src/config/deprecation.rs
@@ -24,6 +24,32 @@ const DEPRECATED_VARS: &[(&str, &str)] = &[
("main_worktree", "repo"),
];
+/// Normalize a template string by replacing deprecated variables with their canonical names.
+///
+/// This allows approval matching to work regardless of whether the command was saved
+/// with old or new variable names. For example, `{{ repo_root }}` and `{{ repo_path }}`
+/// will both normalize to `{{ repo_path }}`.
+///
+/// Returns `Cow::Borrowed` if no replacements needed, avoiding allocation.
+pub fn normalize_template_vars(template: &str) -> Cow<'_, str> {
+ use regex::Regex;
+
+ // Quick check: if none of the deprecated vars appear, return borrowed
+ if !DEPRECATED_VARS
+ .iter()
+ .any(|(old, _)| template.contains(old))
+ {
+ return Cow::Borrowed(template);
+ }
+
+ let mut result = template.to_string();
+ for &(old, new) in DEPRECATED_VARS {
+ let re = Regex::new(&format!(r"\b{}\b", regex::escape(old))).unwrap();
+ result = re.replace_all(&result, new).into_owned();
+ }
+ Cow::Owned(result)
+}
+
/// Find all deprecated variables used in the content
///
/// Parses TOML to extract string values, then uses minijinja to detect
@@ -379,4 +405,101 @@ post-create = "echo hello"
r#"cmd = "{% if repo_path %}echo {{ repo_path }}{% endif %}""#
);
}
+
+ // Tests for normalize_template_vars (single template string normalization)
+
+ #[test]
+ fn test_normalize_no_deprecated_vars() {
+ let template = "ln -sf {{ repo_path }}/node_modules";
+ let result = normalize_template_vars(template);
+ assert!(matches!(result, Cow::Borrowed(_)), "Should not allocate");
+ assert_eq!(result, template);
+ }
+
+ #[test]
+ fn test_normalize_repo_root() {
+ let template = "ln -sf {{ repo_root }}/node_modules";
+ let result = normalize_template_vars(template);
+ assert_eq!(result, "ln -sf {{ repo_path }}/node_modules");
+ }
+
+ #[test]
+ fn test_normalize_worktree() {
+ let template = "cd {{ worktree }} && npm install";
+ let result = normalize_template_vars(template);
+ assert_eq!(result, "cd {{ worktree_path }} && npm install");
+ }
+
+ #[test]
+ fn test_normalize_main_worktree() {
+ let template = "../{{ main_worktree }}.{{ branch }}";
+ let result = normalize_template_vars(template);
+ assert_eq!(result, "../{{ repo }}.{{ branch }}");
+ }
+
+ #[test]
+ fn test_normalize_multiple_vars() {
+ let template = "ln -sf {{ repo_root }}/node_modules {{ worktree }}/node_modules";
+ let result = normalize_template_vars(template);
+ assert_eq!(
+ result,
+ "ln -sf {{ repo_path }}/node_modules {{ worktree_path }}/node_modules"
+ );
+ }
+
+ #[test]
+ fn test_normalize_does_not_match_suffix() {
+ // Should NOT replace "worktree_path" when looking for "worktree"
+ let template = "cd {{ worktree_path }}";
+ let result = normalize_template_vars(template);
+ // Note: may allocate due to coarse quick check, but result is unchanged
+ assert_eq!(result, template);
+ }
+
+ #[test]
+ fn test_normalize_with_filter() {
+ let template = "{{ repo_root | sanitize }}";
+ let result = normalize_template_vars(template);
+ assert_eq!(result, "{{ repo_path | sanitize }}");
+ }
+
+ // Tests for approved-commands array handling
+
+ #[test]
+ fn test_find_deprecated_vars_in_approved_commands() {
+ let content = r#"
+[projects."github.com/user/repo"]
+approved-commands = [
+ "ln -sf {{ repo_root }}/node_modules",
+ "cd {{ worktree }} && npm install",
+]
+"#;
+ let found = find_deprecated_vars(content);
+ assert_eq!(
+ found,
+ vec![("repo_root", "repo_path"), ("worktree", "worktree_path"),]
+ );
+ }
+
+ #[test]
+ fn test_replace_deprecated_vars_in_approved_commands() {
+ let content = r#"
+[projects."github.com/user/repo"]
+approved-commands = [
+ "ln -sf {{ repo_root }}/node_modules",
+ "cd {{ worktree }} && npm install",
+]
+"#;
+ let result = replace_deprecated_vars(content);
+ assert_eq!(
+ result,
+ r#"
+[projects."github.com/user/repo"]
+approved-commands = [
+ "ln -sf {{ repo_path }}/node_modules",
+ "cd {{ worktree_path }} && npm install",
+]
+"#
+ );
+ }
}
diff --git a/src/config/mod.rs b/src/config/mod.rs
index d0cde036c..2ad661a75 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -27,6 +27,7 @@ mod user;
// Re-export public types
pub use commands::{Command, CommandConfig};
pub use deprecation::check_and_migrate as check_deprecated_vars;
+pub use deprecation::normalize_template_vars;
pub use expansion::{expand_template, sanitize_branch_name};
pub use hooks::HooksConfig;
pub use project::{
diff --git a/src/config/user.rs b/src/config/user.rs
index 477dfca5a..b566b9927 100644
--- a/src/config/user.rs
+++ b/src/config/user.rs
@@ -417,11 +417,21 @@ impl WorktrunkConfig {
expand_template(&self.worktree_path(), &vars, false)
}
- /// Check if a command is approved for the given project
+ /// Check if a command is approved for the given project.
+ ///
+ /// Normalizes both the stored approvals and the incoming command to canonical
+ /// variable names before comparing. This allows approvals to match regardless
+ /// of whether they were saved with deprecated variable names (e.g., `repo_root`)
+ /// or current names (e.g., `repo_path`).
pub fn is_command_approved(&self, project: &str, command: &str) -> bool {
+ let normalized_command = super::deprecation::normalize_template_vars(command);
self.projects
.get(project)
- .map(|p| p.approved_commands.iter().any(|c| c == command))
+ .map(|p| {
+ p.approved_commands
+ .iter()
+ .any(|c| super::deprecation::normalize_template_vars(c) == normalized_command)
+ })
.unwrap_or(false)
}
@@ -929,6 +939,77 @@ rename-tab = "echo 'switched'"
assert!(!config.is_command_approved("other/project", "npm install"));
}
+ #[test]
+ fn test_is_command_approved_normalizes_deprecated_vars() {
+ // Approval saved with deprecated variable should match command with new variable
+ let mut config = WorktrunkConfig::default();
+ config.projects.insert(
+ "github.com/user/repo".to_string(),
+ UserProjectConfig {
+ approved_commands: vec![
+ "ln -sf {{ repo_root }}/node_modules".to_string(), // old var
+ ],
+ },
+ );
+
+ // Should match when checking with new variable name
+ assert!(config.is_command_approved(
+ "github.com/user/repo",
+ "ln -sf {{ repo_path }}/node_modules" // new var
+ ));
+
+ // Should still match exact old name too
+ assert!(config.is_command_approved(
+ "github.com/user/repo",
+ "ln -sf {{ repo_root }}/node_modules" // old var
+ ));
+ }
+
+ #[test]
+ fn test_is_command_approved_normalizes_new_approval_matches_old_command() {
+ // Approval saved with new variable should match command with deprecated variable
+ let mut config = WorktrunkConfig::default();
+ config.projects.insert(
+ "github.com/user/repo".to_string(),
+ UserProjectConfig {
+ approved_commands: vec![
+ "cd {{ worktree_path }} && npm install".to_string(), // new var
+ ],
+ },
+ );
+
+ // Should match when checking with old variable name
+ assert!(config.is_command_approved(
+ "github.com/user/repo",
+ "cd {{ worktree }} && npm install" // old var
+ ));
+ }
+
+ #[test]
+ fn test_is_command_approved_normalizes_multiple_vars() {
+ let mut config = WorktrunkConfig::default();
+ config.projects.insert(
+ "github.com/user/repo".to_string(),
+ UserProjectConfig {
+ approved_commands: vec![
+ "ln -sf {{ repo_root }}/modules {{ worktree }}/modules".to_string(),
+ ],
+ },
+ );
+
+ // Should match with all new variable names
+ assert!(config.is_command_approved(
+ "github.com/user/repo",
+ "ln -sf {{ repo_path }}/modules {{ worktree_path }}/modules"
+ ));
+
+ // Should match with mixed old/new (both normalize to same canonical form)
+ assert!(config.is_command_approved(
+ "github.com/user/repo",
+ "ln -sf {{ repo_path }}/modules {{ worktree }}/modules"
+ ));
+ }
+
#[test]
fn test_worktrunk_config_format_path() {
let config = WorktrunkConfig::default();