Skip to content

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

feat(config): add parent and name path-traversal template filters (#2592)

Maximilian Roos670cb2a7b0f9
diff --git a/docs/content/hook.md b/docs/content/hook.md
index a1c1dc8b1..8984d731c 100644
--- a/docs/content/hook.md
+++ b/docs/content/hook.md
@@ -186,6 +186,8 @@ Templates support Jinja2 filters for transforming values:
| `sanitize_hash` | `{{ branch \| sanitize_hash }}` | Filesystem-safe name with hash suffix for uniqueness |
| `hash` | `{{ branch \| hash }}` | 3-character base36 digest of the input |
| `hash_port` | `{{ branch \| hash_port }}` | Hash to port 10000-19999 |
+| `parent` | `{{ repo_path \| parent }}` | Strip the last path component (`/a/b/c` → `/a/b`) |
+| `name` | `{{ repo_path \| name }}` | Keep only the last path component (`/a/b/c` → `c`) |
The `sanitize` filter makes branch names safe for filesystem paths. The `sanitize_db` filter produces database-safe identifiers — lowercase alphanumeric and underscores, no leading digits, with a 3-character hash suffix to avoid collisions and reserved words. The `sanitize_hash` filter produces a filesystem-safe name and appends a 3-character hash suffix when sanitization changed the input, so distinct originals never collide — already-safe names pass through unchanged. The `hash` filter is the bare 3-character base36 digest, useful for composing your own truncate-with-collision-avoidance recipes when an output budget is tight (e.g., Unix socket paths capped at 107 bytes):
@@ -194,6 +196,13 @@ The `sanitize` filter makes branch names safe for filesystem paths. The `sanitiz
worktree-path = "/tmp/{{ (branch | sanitize)[:20] }}_{{ branch | sanitize | hash }}"
```
+The `parent` and `name` filters traverse paths. They're useful for bare repos in a hidden directory like `myproject/.git`, where `{{ repo }}` resolves to `.git`:
+
+```toml
+# Place worktrees as siblings of the bare repo, named `<wrapper>.<branch>`
+worktree-path = "{{ repo_path }}/../{{ repo_path | parent | name }}.{{ branch | sanitize }}"
+```
+
The `hash_port` filter is useful for running dev servers on unique ports per worktree:
```toml
diff --git a/skills/worktrunk/reference/hook.md b/skills/worktrunk/reference/hook.md
index d56113056..409df4714 100644
--- a/skills/worktrunk/reference/hook.md
+++ b/skills/worktrunk/reference/hook.md
@@ -177,6 +177,8 @@ Templates support Jinja2 filters for transforming values:
| `sanitize_hash` | `{{ branch \| sanitize_hash }}` | Filesystem-safe name with hash suffix for uniqueness |
| `hash` | `{{ branch \| hash }}` | 3-character base36 digest of the input |
| `hash_port` | `{{ branch \| hash_port }}` | Hash to port 10000-19999 |
+| `parent` | `{{ repo_path \| parent }}` | Strip the last path component (`/a/b/c` → `/a/b`) |
+| `name` | `{{ repo_path \| name }}` | Keep only the last path component (`/a/b/c` → `c`) |
The `sanitize` filter makes branch names safe for filesystem paths. The `sanitize_db` filter produces database-safe identifiers — lowercase alphanumeric and underscores, no leading digits, with a 3-character hash suffix to avoid collisions and reserved words. The `sanitize_hash` filter produces a filesystem-safe name and appends a 3-character hash suffix when sanitization changed the input, so distinct originals never collide — already-safe names pass through unchanged. The `hash` filter is the bare 3-character base36 digest, useful for composing your own truncate-with-collision-avoidance recipes when an output budget is tight (e.g., Unix socket paths capped at 107 bytes):
@@ -185,6 +187,13 @@ The `sanitize` filter makes branch names safe for filesystem paths. The `sanitiz
worktree-path = "/tmp/{{ (branch | sanitize)[:20] }}_{{ branch | sanitize | hash }}"
```
+The `parent` and `name` filters traverse paths. They're useful for bare repos in a hidden directory like `myproject/.git`, where `{{ repo }}` resolves to `.git`:
+
+```toml
+# Place worktrees as siblings of the bare repo, named `<wrapper>.<branch>`
+worktree-path = "{{ repo_path }}/../{{ repo_path | parent | name }}.{{ branch | sanitize }}"
+```
+
The `hash_port` filter is useful for running dev servers on unique ports per worktree:
```toml
diff --git a/src/cli/mod.rs b/src/cli/mod.rs
index 42d99b82c..d43372a67 100644
--- a/src/cli/mod.rs
+++ b/src/cli/mod.rs
@@ -1371,6 +1371,8 @@ Templates support Jinja2 filters for transforming values:
| `sanitize_hash` | `{{ branch \| sanitize_hash }}` | Filesystem-safe name with hash suffix for uniqueness |
| `hash` | `{{ branch \| hash }}` | 3-character base36 digest of the input |
| `hash_port` | `{{ branch \| hash_port }}` | Hash to port 10000-19999 |
+| `parent` | `{{ repo_path \| parent }}` | Strip the last path component (`/a/b/c` → `/a/b`) |
+| `name` | `{{ repo_path \| name }}` | Keep only the last path component (`/a/b/c` → `c`) |
The `sanitize` filter makes branch names safe for filesystem paths. The `sanitize_db` filter produces database-safe identifiers — lowercase alphanumeric and underscores, no leading digits, with a 3-character hash suffix to avoid collisions and reserved words. The `sanitize_hash` filter produces a filesystem-safe name and appends a 3-character hash suffix when sanitization changed the input, so distinct originals never collide — already-safe names pass through unchanged. The `hash` filter is the bare 3-character base36 digest, useful for composing your own truncate-with-collision-avoidance recipes when an output budget is tight (e.g., Unix socket paths capped at 107 bytes):
@@ -1379,6 +1381,13 @@ The `sanitize` filter makes branch names safe for filesystem paths. The `sanitiz
worktree-path = "/tmp/{{ (branch | sanitize)[:20] }}_{{ branch | sanitize | hash }}"
```
+The `parent` and `name` filters traverse paths. They're useful for bare repos in a hidden directory like `myproject/.git`, where `{{ repo }}` resolves to `.git`:
+
+```toml
+# Place worktrees as siblings of the bare repo, named `<wrapper>.<branch>`
+worktree-path = "{{ repo_path }}/../{{ repo_path | parent | name }}.{{ branch | sanitize }}"
+```
+
The `hash_port` filter is useful for running dev servers on unique ports per worktree:
```toml
diff --git a/src/config/expansion.rs b/src/config/expansion.rs
index cbabdd44e..db8dda5fd 100644
--- a/src/config/expansion.rs
+++ b/src/config/expansion.rs
@@ -613,6 +613,18 @@ fn setup_template_env(repo: &Repository) -> Environment<'static> {
short_hash(value.as_str().unwrap_or_default())
});
env.add_filter("hash_port", |value: String| string_to_port(&value));
+ env.add_filter("parent", |value: Value| -> String {
+ std::path::Path::new(value.as_str().unwrap_or_default())
+ .parent()
+ .map(|p| to_posix_path(&p.to_string_lossy()))
+ .unwrap_or_default()
+ });
+ env.add_filter("name", |value: Value| -> String {
+ std::path::Path::new(value.as_str().unwrap_or_default())
+ .file_name()
+ .map(|n| n.to_string_lossy().into_owned())
+ .unwrap_or_default()
+ });
// Register worktree_path_of_branch function for looking up branch worktree paths.
// Returns raw paths — shell escaping is applied by the formatter at output time.
@@ -751,6 +763,8 @@ pub fn validate_template(
/// - `sanitize_hash` — Filesystem-safe name with hash suffix so distinct inputs never collide
/// - `hash` — 3-character base36 hash digest of the input
/// - `hash_port` — Hash to deterministic port number (10000-19999)
+/// - `parent` — Strip the last path component (e.g., `/a/b/c` → `/a/b`)
+/// - `name` — Keep only the last path component (e.g., `/a/b/c` → `c`)
///
/// # Functions
/// - `worktree_path_of_branch(branch)` — Look up the filesystem path of a branch's worktree
@@ -1485,6 +1499,59 @@ mod tests {
assert_eq!(r1, r2);
}
+ #[test]
+ fn test_path_parent_and_name_filters() {
+ let test = test_repo();
+ let mut vars = HashMap::new();
+
+ // Bare repo wrapped in a hidden dir: `parent | name` recovers the wrapper name
+ // (the case from #1279 — `{{ repo }}` resolves to `.git`, but the user wants `myrepo`)
+ vars.insert("repo_path", "/projects/myrepo/.git");
+ let result = expand_template(
+ "{{ repo_path | parent | name }}",
+ &vars,
+ false,
+ &test.repo,
+ "test",
+ )
+ .unwrap();
+ assert_eq!(result, "myrepo");
+
+ // Composing into a worktree-path template
+ vars.insert("branch", "feature-auth");
+ let result = expand_template(
+ "{{ repo_path }}/../{{ repo_path | parent | name }}.{{ branch | sanitize }}",
+ &vars,
+ false,
+ &test.repo,
+ "test",
+ )
+ .unwrap();
+ assert_eq!(result, "/projects/myrepo/.git/../myrepo.feature-auth");
+
+ // `parent` strips the last component
+ vars.insert("repo_path", "/a/b/c");
+ let parent =
+ expand_template("{{ repo_path | parent }}", &vars, false, &test.repo, "test").unwrap();
+ assert_eq!(parent, "/a/b");
+
+ // `name` keeps only the last component
+ let name =
+ expand_template("{{ repo_path | name }}", &vars, false, &test.repo, "test").unwrap();
+ assert_eq!(name, "c");
+
+ // No separator: parent is empty, name is the whole input
+ vars.insert("repo_path", "myrepo");
+ assert_eq!(
+ expand_template("{{ repo_path | parent }}", &vars, false, &test.repo, "test").unwrap(),
+ ""
+ );
+ assert_eq!(
+ expand_template("{{ repo_path | name }}", &vars, false, &test.repo, "test").unwrap(),
+ "myrepo"
+ );
+ }
+
#[test]
fn test_redact_credentials_https_token() {
// GitHub-style personal access token