Skip to content

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

fix: use worktree root for hook CWD and template variables (#1097)

Maximilian Roosac2aaac439ed
diff --git a/src/commands/context.rs b/src/commands/context.rs
index 367b422dc..fb13bb309 100644
--- a/src/commands/context.rs
+++ b/src/commands/context.rs
@@ -20,6 +20,7 @@ pub struct CommandEnv {
/// Current branch name, if on a branch (None in detached HEAD state).
pub branch: Option<String>,
pub config: UserConfig,
+ /// Canonical absolute path to the worktree root (via `git rev-parse --show-toplevel`).
pub worktree_path: PathBuf,
}
@@ -30,7 +31,7 @@ impl CommandEnv {
/// Used in error messages when the environment can't be loaded.
pub fn for_action(action: &str, config: UserConfig) -> anyhow::Result<Self> {
let repo = Repository::current()?;
- let worktree_path = repo.current_worktree().path().to_path_buf();
+ let worktree_path = repo.current_worktree().root()?;
let branch = repo.require_current_branch(action)?;
Ok(Self {
@@ -48,7 +49,7 @@ impl CommandEnv {
pub fn for_action_branchless() -> anyhow::Result<Self> {
let repo = Repository::current()?;
let current_wt = repo.current_worktree();
- let worktree_path = current_wt.path().to_path_buf();
+ let worktree_path = current_wt.root()?;
// Propagate git errors (broken repo, missing git) but allow None for detached HEAD
let branch = current_wt
.branch()
diff --git a/tests/integration_tests/user_hooks.rs b/tests/integration_tests/user_hooks.rs
index 6f4c65a70..c39f02b9d 100644
--- a/tests/integration_tests/user_hooks.rs
+++ b/tests/integration_tests/user_hooks.rs
@@ -926,6 +926,68 @@ vars = "echo 'repo={{ repo }} branch={{ branch }}' > template_vars.txt"
);
}
+#[rstest]
+fn test_hook_template_variables_from_subdirectory(repo: TestRepo) {
+ // Hook that writes template variables and pwd to files so we can verify their values.
+ // This tests that running from a subdirectory still resolves worktree_path to the
+ // worktree root (not "." or the subdirectory) and sets hook CWD to the root.
+ repo.write_project_config(
+ r#"pre-merge = "echo '{{ worktree_path }}' > wt_path.txt && echo '{{ worktree_name }}' > wt_name.txt && pwd > hook_cwd.txt""#,
+ );
+ repo.commit("Add pre-merge hook");
+
+ // Create a subdirectory and run the hook from there
+ let subdir = repo.root_path().join("src").join("components");
+ fs::create_dir_all(&subdir).unwrap();
+
+ let output = repo
+ .wt_command()
+ .args(["hook", "pre-merge", "--yes"])
+ .current_dir(&subdir) // override: run from subdirectory
+ .output()
+ .expect("Failed to run wt hook pre-merge");
+
+ assert!(
+ output.status.success(),
+ "wt hook pre-merge failed: {}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+
+ // worktree_path should be the worktree root, not "." or the subdirectory.
+ // On Windows, to_posix_path() converts C:\... to /c/..., so check that the path
+ // is not relative rather than using is_absolute() (which rejects POSIX-style paths).
+ let wt_path = fs::read_to_string(repo.root_path().join("wt_path.txt"))
+ .expect("wt_path.txt should exist (hook should run from worktree root, not subdirectory)");
+ let wt_path = wt_path.trim();
+ assert_ne!(wt_path, ".", "worktree_path should not be relative '.'");
+ assert!(
+ wt_path.ends_with("repo"),
+ "worktree_path should end with repo dir name, got: {wt_path}"
+ );
+
+ // worktree_name should be the directory name, not "unknown"
+ let wt_name =
+ fs::read_to_string(repo.root_path().join("wt_name.txt")).expect("wt_name.txt should exist");
+ assert_eq!(
+ wt_name.trim(),
+ "repo",
+ "worktree_name should be the directory name, not 'unknown'"
+ );
+
+ // Hook CWD should be the worktree root, not the subdirectory
+ let hook_cwd = fs::read_to_string(repo.root_path().join("hook_cwd.txt"))
+ .expect("hook_cwd.txt should exist");
+ let hook_cwd = hook_cwd.trim();
+ assert!(
+ !hook_cwd.contains("src/components"),
+ "Hook should run from worktree root, not subdirectory. CWD was: {hook_cwd}"
+ );
+ assert!(
+ hook_cwd.ends_with("repo"),
+ "Hook CWD should be worktree root, got: {hook_cwd}"
+ );
+}
+
// ============================================================================
// Combined User and Project Hooks Tests
// ============================================================================