Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Fix branch variable expansion in detached HEAD worktrees
Maximilian Roosa74c1f3e0a48
diff --git a/src/commands/for_each.rs b/src/commands/for_each.rsindex 3efb9d3ae..c5d98c18d 100644--- a/src/commands/for_each.rs+++ b/src/commands/for_each.rs@@ -67,7 +67,15 @@ pub fn step_for_each(args: Vec<String>) -> anyhow::Result<()> {let wt_repo = Repository::at(&wt.path);// Build full hook context for this worktree- let ctx = CommandContext::new(&wt_repo, &config, Some(branch), &wt.path, &repo_root, false);+ // Pass wt.branch directly (not the display string) so detached HEAD maps to None -> "HEAD"+ let ctx = CommandContext::new(+ &wt_repo,+ &config,+ wt.branch.as_deref(),+ &wt.path,+ &repo_root,+ false,+ );let context_map = build_hook_context(&ctx, &[]);// Convert to &str references for expand_templatediff --git a/tests/integration_tests/for_each.rs b/tests/integration_tests/for_each.rsindex fc890b1d5..1d4acc1df 100644--- a/tests/integration_tests/for_each.rs+++ b/tests/integration_tests/for_each.rs@@ -76,3 +76,18 @@ fn test_for_each_with_template(repo: TestRepo) {&["for-each", "--", "echo", "Branch: {{ branch }}"],);}++#[rstest]+fn test_for_each_detached_branch_variable(mut repo: TestRepo) {+ // Regression test: {{ branch }} should expand to "HEAD" in detached worktrees+ // Previously, it incorrectly expanded to "(detached)" because the display+ // string was passed to CommandContext instead of None+ repo.add_worktree("detached-test");+ repo.detach_head_in_worktree("detached-test");++ snapshot_for_each(+ "for_each_detached_branch_variable",+ &repo,+ &["for-each", "--", "echo", "Branch: {{ branch }}"],+ );+}diff --git a/tests/snapshots/integration__integration_tests__for_each__for_each_detached_branch_variable.snap b/tests/snapshots/integration__integration_tests__for_each__for_each_detached_branch_variable.snapnew file mode 100644index 000000000..bedfc8785--- /dev/null+++ b/tests/snapshots/integration__integration_tests__for_each__for_each_detached_branch_variable.snap@@ -0,0 +1,39 @@+---+source: tests/integration_tests/for_each.rs+info:+ program: wt+ args:+ - step+ - for-each+ - "--"+ - echo+ - "Branch: {{ branch }}"+ env:+ APPDATA: "[TEST_CONFIG_HOME]"+ CLICOLOR_FORCE: "1"+ COLUMNS: "150"+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"+ GIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""+ HOME: "[TEST_HOME]"+ LANG: C+ LC_ALL: C+ PATH: "[PATH]"+ SOURCE_DATE_EPOCH: "1735776000"+ USERPROFILE: "[TEST_HOME]"+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"+---+success: true+exit_code: 0+----- stdout -----++----- stderr -----+π [36mRunning in [1mmain[22m...[39m+Branch: main+π [36mRunning in [1m(detached)[22m...[39m+Branch: HEAD++β [32mCompleted in 2 worktrees[39m