Skip to content

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

feat: add shell completion for step aliases (#1641)

Maximilian Roose43032ff5b7d
diff --git a/src/completion.rs b/src/completion.rs
index eab92db5e..92ccbd2ea 100644
--- a/src/completion.rs
+++ b/src/completion.rs
@@ -1,4 +1,5 @@
use std::cell::RefCell;
+use std::collections::BTreeMap;
use std::ffi::{OsStr, OsString};
use std::io::Write;
@@ -368,9 +369,90 @@ thread_local! {
fn completion_command() -> Command {
let cmd = cli::build_command();
+ let cmd = inject_alias_subcommands(cmd);
hide_non_positional_options_for_completion(cmd)
}
+/// Inject configured aliases as subcommands of `step` so they appear in completions.
+///
+/// Aliases are loaded from user config and project config (same merge order as
+/// `step_alias`). Aliases that shadow built-in step commands are skipped.
+fn inject_alias_subcommands(cmd: Command) -> Command {
+ let aliases = load_aliases_for_completion();
+ if aliases.is_empty() {
+ return cmd;
+ }
+
+ cmd.mut_subcommand("step", |mut step| {
+ for (name, template) in aliases {
+ // Skip aliases that shadow built-in step commands
+ if step
+ .get_subcommands()
+ .any(|s| s.get_name() == name.as_str())
+ {
+ continue;
+ }
+ let help = truncate_template(&template);
+ // clap::Command::new() requires Into<Str>, and Str only implements
+ // From<&'static str> (not From<String>). Leak is fine: completion is
+ // a short-lived subprocess that exits after printing candidates.
+ let name: &'static str = Box::leak(name.into_boxed_str());
+ let about: &'static str = Box::leak(format!("alias: {help}").into_boxed_str());
+ let sub = Command::new(name)
+ .about(about)
+ .arg(clap::Arg::new("dry-run").long("dry-run"))
+ .arg(clap::Arg::new("yes").short('y').long("yes"))
+ .arg(
+ clap::Arg::new("var")
+ .long("var")
+ .num_args(1)
+ .action(clap::ArgAction::Append),
+ );
+ step = step.subcommand(sub);
+ }
+ step
+ })
+}
+
+/// Load aliases from user and project config for completion.
+fn load_aliases_for_completion() -> BTreeMap<String, String> {
+ let mut aliases = BTreeMap::new();
+
+ if let Ok(repo) = Repository::current() {
+ // Project config aliases first
+ if let Ok(Some(project_config)) = ProjectConfig::load(&repo, false)
+ && let Some(project_aliases) = project_config.aliases
+ {
+ aliases.extend(project_aliases);
+ }
+ // User config aliases override
+ if let Ok(user_config) = UserConfig::load() {
+ let project_id = repo.project_identifier().ok();
+ aliases.extend(user_config.aliases(project_id.as_deref()));
+ }
+ } else if let Ok(user_config) = UserConfig::load() {
+ aliases.extend(user_config.aliases(None));
+ }
+
+ aliases
+}
+
+/// Truncate a template string for use as completion help text.
+fn truncate_template(template: &str) -> &str {
+ let s = template.trim();
+ let first_line = s.lines().next().unwrap_or(s);
+ if first_line.len() > 60 {
+ // Find the last char boundary at or before byte 57
+ let mut end = 57;
+ while end > 0 && !first_line.is_char_boundary(end) {
+ end -= 1;
+ }
+ &first_line[..end]
+ } else {
+ first_line
+ }
+}
+
/// Hide non-positional options so they're filtered out when positional/subcommand
/// completions exist, but still shown when completing `--<TAB>`.
///
@@ -416,3 +498,36 @@ fn hide_non_positional_options_for_completion(cmd: Command) -> Command {
process_command(cmd, true)
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_truncate_template() {
+ // Short template — returned as-is
+ assert_eq!(truncate_template("echo hello"), "echo hello");
+
+ // Multiline — only first line
+ assert_eq!(truncate_template("line one\nline two"), "line one");
+
+ // Leading/trailing whitespace trimmed
+ assert_eq!(truncate_template(" spaced \n"), "spaced");
+
+ // Exactly 60 chars — no truncation
+ let s60 = "a".repeat(60);
+ assert_eq!(truncate_template(&s60), s60.as_str());
+
+ // 61 chars — truncated to 57
+ let s61 = "b".repeat(61);
+ assert_eq!(truncate_template(&s61), &"b".repeat(57));
+
+ // Multi-byte chars where byte 57 falls mid-character.
+ // 'a' (1 byte) × 56 + '€' (3 bytes) × 2 = 62 bytes, > 60.
+ // Byte 57 is the second byte of the first '€', so the loop backs up to 56.
+ let multi = "a".repeat(56) + "€€";
+ let result = truncate_template(&multi);
+ assert_eq!(result.len(), 56);
+ assert_eq!(result, "a".repeat(56));
+ }
+}
diff --git a/tests/integration_tests/completion.rs b/tests/integration_tests/completion.rs
index b04f1c2e4..7faf9ed62 100644
--- a/tests/integration_tests/completion.rs
+++ b/tests/integration_tests/completion.rs
@@ -1582,3 +1582,106 @@ fn test_complete_switch_includes_remote_branches_when_under_threshold(mut repo:
"Remote branches should appear when total <= 100: {stdout}"
);
}
+
+#[rstest]
+fn test_complete_step_shows_aliases_from_project_config(repo: TestRepo) {
+ repo.commit("initial");
+ repo.write_project_config(
+ r#"
+[aliases]
+deploy = "make deploy"
+lint = "cargo clippy"
+"#,
+ );
+ repo.commit("add config");
+
+ let output = repo.completion_cmd(&["wt", "step", ""]).output().unwrap();
+ assert!(output.status.success());
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ let subcommands = value_suggestions(&stdout);
+
+ // Built-in commands still present
+ assert!(subcommands.contains(&"commit"), "Missing commit");
+ assert!(subcommands.contains(&"push"), "Missing push");
+ // Aliases appear
+ assert!(
+ subcommands.contains(&"deploy"),
+ "Missing alias 'deploy': {stdout}"
+ );
+ assert!(
+ subcommands.contains(&"lint"),
+ "Missing alias 'lint': {stdout}"
+ );
+}
+
+#[rstest]
+fn test_complete_step_shows_aliases_from_user_config(repo: TestRepo) {
+ repo.commit("initial");
+ repo.write_test_config(
+ r#"
+[aliases]
+update = "git pull --rebase"
+"#,
+ );
+
+ let output = repo.completion_cmd(&["wt", "step", ""]).output().unwrap();
+ assert!(output.status.success());
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ let subcommands = value_suggestions(&stdout);
+
+ assert!(
+ subcommands.contains(&"update"),
+ "Missing user alias 'update': {stdout}"
+ );
+}
+
+#[rstest]
+fn test_complete_step_alias_does_not_shadow_builtins(repo: TestRepo) {
+ repo.commit("initial");
+ repo.write_project_config(
+ r#"
+[aliases]
+commit = "echo 'shadowed'"
+deploy = "make deploy"
+"#,
+ );
+ repo.commit("add config");
+
+ let output = repo.completion_cmd(&["wt", "step", ""]).output().unwrap();
+ assert!(output.status.success());
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ let subcommands = value_suggestions(&stdout);
+
+ // 'commit' should appear exactly once (the built-in), not duplicated
+ let commit_count = subcommands.iter().filter(|&&s| s == "commit").count();
+ assert_eq!(commit_count, 1, "Built-in 'commit' should appear once");
+ // 'deploy' alias should appear
+ assert!(subcommands.contains(&"deploy"));
+}
+
+#[rstest]
+fn test_complete_step_alias_shows_flags(repo: TestRepo) {
+ repo.commit("initial");
+ repo.write_project_config(
+ r#"
+[aliases]
+deploy = "make deploy"
+"#,
+ );
+ repo.commit("add config");
+
+ // Complete flags for the alias subcommand
+ let output = repo
+ .completion_cmd(&["wt", "step", "deploy", "--"])
+ .output()
+ .unwrap();
+ assert!(output.status.success());
+ let stdout = String::from_utf8_lossy(&output.stdout);
+
+ assert!(
+ stdout.contains("--dry-run"),
+ "Missing --dry-run flag: {stdout}"
+ );
+ assert!(stdout.contains("--yes"), "Missing --yes flag: {stdout}");
+ assert!(stdout.contains("--var"), "Missing --var flag: {stdout}");
+}