Skip to content

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

refactor(eval): render -v variables through the shared formatter (#3536)

Maximilian Roos6a85da8f468d
diff --git a/src/commands/eval.rs b/src/commands/eval.rs
index 1fe01fb82..fb3fbd8c8 100644
--- a/src/commands/eval.rs
+++ b/src/commands/eval.rs
@@ -6,7 +6,7 @@
use std::collections::HashMap;
use color_print::cformat;
-use worktrunk::config::{UserConfig, expand_template};
+use worktrunk::config::{UserConfig, expand_template, format_base_variables};
use worktrunk::git::Repository;
use worktrunk::shell_exec::ShellEscapeMode;
use worktrunk::styling::{eprintln, format_with_gutter, info_message, println, verbosity};
@@ -40,22 +40,14 @@ pub fn step_eval(template: &str, format: SwitchFormat) -> anyhow::Result<()> {
let context_map = build_hook_context(&ctx, &[], None)?;
if verbosity() >= 1 {
- let width = context_map.keys().map(String::len).max().unwrap_or(0);
- let mut keys: Vec<&str> = context_map.keys().map(String::as_str).collect();
- keys.sort();
- let listing = keys
- .iter()
- .map(|key| {
- let pad = " ".repeat(width - key.len());
- cformat!("<bold>{key}</>{pad} = {}", context_map[*key])
- })
- .collect::<Vec<_>>()
- .join("\n");
eprintln!(
"{}",
info_message(cformat!("<bold>{EVAL_NAME}</> template variables:"))
);
- eprintln!("{}", format_with_gutter(&listing, None));
+ eprintln!(
+ "{}",
+ format_with_gutter(&format_base_variables(&context_map), None)
+ );
}
let vars: HashMap<&str, &str> = context_map
diff --git a/src/config/expansion.rs b/src/config/expansion.rs
index e0ce6f5e1..056f777b3 100644
--- a/src/config/expansion.rs
+++ b/src/config/expansion.rs
@@ -287,6 +287,17 @@ pub fn format_alias_variables(
format_variables_table(&vars, &display_ctx, referenced)
}
+/// Format the resolved template variables for a bare template expansion
+/// (`wt step eval`), whose scope is exactly [`base_vars`] — no hook operation
+/// or infrastructure vars, no alias `args`.
+///
+/// Same curated ordering and `(unset)` treatment as [`format_hook_variables`],
+/// so the four `-v` variable listings (hook foreground, hook background, alias,
+/// eval) render one family.
+pub fn format_base_variables(ctx: &HashMap<String, String>) -> String {
+ format_variables_table(&base_vars(), ctx, None)
+}
+
/// Extend `referenced` with the implicit context-map keys an alias dispatch
/// needs:
///
diff --git a/src/config/mod.rs b/src/config/mod.rs
index 3d5de9ef4..4742f21b9 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -162,10 +162,10 @@ pub use deprecation::{DeprecationKind, Deprecations};
pub use expansion::{
ACTIVE_VARS, ALIAS_ARGS_KEY, DEPRECATED_TEMPLATE_VARS, EXEC_BASE_VARS, REPO_VARS,
TemplateExpandError, ValidationScope, alias_context_filter, base_vars, expand_template,
- format_alias_variables, format_hook_variables, redact_credentials, referenced_vars_for_config,
- sanitize_branch_name, sanitize_db, short_hash, template_environment, template_references_var,
- validate_list_column_template, validate_template, validate_template_syntax, vars_available_in,
- vars_map_to_value,
+ format_alias_variables, format_base_variables, format_hook_variables, redact_credentials,
+ referenced_vars_for_config, sanitize_branch_name, sanitize_db, short_hash,
+ template_environment, template_references_var, validate_list_column_template,
+ validate_template, validate_template_syntax, vars_available_in, vars_map_to_value,
};
pub use hooks::HooksConfig;
pub use project::{
diff --git a/tests/snapshots/integration__integration_tests__eval__eval_format_json_verbose.snap b/tests/snapshots/integration__integration_tests__eval__eval_format_json_verbose.snap
index 18a1cf9bf..f97944606 100644
--- a/tests/snapshots/integration__integration_tests__eval__eval_format_json_verbose.snap
+++ b/tests/snapshots/integration__integration_tests__eval__eval_format_json_verbose.snap
@@ -57,23 +57,20 @@ exit_code: 0
----- stderr -----
○ eval template variables:
-  branch = main
-  commit = 05a4a45d0b981dad5c27db59dca482836d59f89e
-  cwd = _REPO_
-  default_branch = main
-  main_worktree = repo
-  main_worktree_path = _REPO_
-  primary_worktree_path = _REPO_
-  remote = origin
-  remote_url = ../origin.git
-  repo = repo
-  repo_path = _REPO_
-  repo_root = _REPO_
-  short_commit = 05a4a45
-  upstream = origin/main
-  worktree = _REPO_
-  worktree_name = repo
-  worktree_path = _REPO_
+  branch = main
+  worktree_path = _REPO_
+  worktree_name = repo
+  commit = 05a4a45d0b981dad5c27db59dca482836d59f89e
+  short_commit = 05a4a45
+  upstream = origin/main
+  repo = repo
+  repo_path = _REPO_
+  owner = (unset)
+  primary_worktree_path = _REPO_
+  default_branch = main
+  remote = origin
+  remote_url = ../origin.git
+  cwd = _REPO_
○ eval source
  {{ branch | hash_port }}
○ eval result
diff --git a/tests/snapshots/integration__integration_tests__eval__eval_verbose.snap b/tests/snapshots/integration__integration_tests__eval__eval_verbose.snap
index 031080134..77908ff36 100644
--- a/tests/snapshots/integration__integration_tests__eval__eval_verbose.snap
+++ b/tests/snapshots/integration__integration_tests__eval__eval_verbose.snap
@@ -52,23 +52,20 @@ exit_code: 0
----- stderr -----
○ eval template variables:
-  branch = main
-  commit = 05a4a45d0b981dad5c27db59dca482836d59f89e
-  cwd = _REPO_
-  default_branch = main
-  main_worktree = repo
-  main_worktree_path = _REPO_
-  primary_worktree_path = _REPO_
-  remote = origin
-  remote_url = ../origin.git
-  repo = repo
-  repo_path = _REPO_
-  repo_root = _REPO_
-  short_commit = 05a4a45
-  upstream = origin/main
-  worktree = _REPO_
-  worktree_name = repo
-  worktree_path = _REPO_
+  branch = main
+  worktree_path = _REPO_
+  worktree_name = repo
+  commit = 05a4a45d0b981dad5c27db59dca482836d59f89e
+  short_commit = 05a4a45
+  upstream = origin/main
+  repo = repo
+  repo_path = _REPO_
+  owner = (unset)
+  primary_worktree_path = _REPO_
+  default_branch = main
+  remote = origin
+  remote_url = ../origin.git
+  cwd = _REPO_
○ eval source
  {{ branch | hash_port }}
○ eval result