Skip to content

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

fix: shell-escape paths in -C flag hints (#1173)

Maximilian Roos1f36953b2b5a
diff --git a/src/commands/config/update.rs b/src/commands/config/update.rs
index 84e35ec18..63f9f2512 100644
--- a/src/commands/config/update.rs
+++ b/src/commands/config/update.rs
@@ -12,6 +12,7 @@ use worktrunk::config::{
use worktrunk::git::Repository;
use worktrunk::styling::{
eprintln, format_bash_with_gutter, hint_message, info_message, success_message,
+ suggest_command_in_dir,
};
use crate::output::prompt::{PromptResponse, prompt_yes_no_preview};
@@ -172,12 +173,9 @@ fn check_project_config() -> anyhow::Result<Option<UpdateCandidate>> {
// Linked worktrees can't apply the update — suggest -C to main worktree
if is_linked {
- let display_path = worktrunk::path::format_path_for_display(repo.repo_path());
+ let cmd = suggest_command_in_dir(repo.repo_path(), "config", &["update"], &[]);
eprintln!("{}", hint_message("To update project config:"));
- eprintln!(
- "{}",
- format_bash_with_gutter(&format!("wt -C {display_path} config update"))
- );
+ eprintln!("{}", format_bash_with_gutter(&cmd));
return Ok(None);
}
diff --git a/src/config/deprecation.rs b/src/config/deprecation.rs
index 3b66a0fb0..52b64d54c 100644
--- a/src/config/deprecation.rs
+++ b/src/config/deprecation.rs
@@ -30,7 +30,8 @@ use shell_escape::unix::escape;
use crate::config::WorktrunkConfig;
use crate::shell_exec::Cmd;
use crate::styling::{
- eprintln, format_bash_with_gutter, format_with_gutter, hint_message, warning_message,
+ eprintln, format_bash_with_gutter, format_with_gutter, hint_message, suggest_command_in_dir,
+ warning_message,
};
/// Tracks which config paths have already shown deprecation warnings this process.
@@ -980,13 +981,9 @@ pub fn format_deprecation_details(info: &DeprecationInfo) -> String {
}
} else if let Some(main_path) = &info.main_worktree_path {
// In linked worktree — include -C so the command works from here
- let display_path = crate::path::format_path_for_display(main_path);
+ let cmd = suggest_command_in_dir(main_path, "config", &["update"], &[]);
let _ = writeln!(out, "{}", hint_message("To apply:"));
- let _ = writeln!(
- out,
- "{}",
- format_bash_with_gutter(&format!("wt -C {display_path} config update"))
- );
+ let _ = writeln!(out, "{}", format_bash_with_gutter(&cmd));
}
out
diff --git a/src/styling/mod.rs b/src/styling/mod.rs
index d2969fd43..96493a09c 100644
--- a/src/styling/mod.rs
+++ b/src/styling/mod.rs
@@ -37,7 +37,7 @@ pub use format::{GUTTER_OVERHEAD, format_bash_with_gutter, format_with_gutter, w
pub use highlighting::format_toml;
pub use hyperlink::{Stream, hyperlink_stdout, strip_osc8_hyperlinks, supports_hyperlinks};
pub use line::{StyledLine, StyledString, truncate_visible};
-pub use suggest::suggest_command;
+pub use suggest::{suggest_command, suggest_command_in_dir};
// ============================================================================
// Verbosity
diff --git a/src/styling/suggest.rs b/src/styling/suggest.rs
index 2d1edfb1c..35b6fe6a9 100644
--- a/src/styling/suggest.rs
+++ b/src/styling/suggest.rs
@@ -25,9 +25,21 @@
//! let cmd = suggest_command("remove", &["my feature"], &[]);
//! assert_eq!(cmd, "wt remove 'my feature'");
//! ```
+//!
+//! For commands targeting a specific worktree directory, use
+//! [`suggest_command_in_dir`]:
+//!
+//! ```
+//! use std::path::Path;
+//! use worktrunk::styling::suggest_command_in_dir;
+//!
+//! let cmd = suggest_command_in_dir(Path::new("/tmp/repo"), "config", &["update"], &[]);
+//! assert_eq!(cmd, "wt -C /tmp/repo config update");
+//! ```
use shell_escape::escape;
use std::borrow::Cow;
+use std::path::Path;
/// Build a suggested command string for hints.
///
@@ -48,17 +60,38 @@ use std::borrow::Cow;
///
/// If any positional argument starts with `-`, a `--` separator is inserted
/// before it to prevent shell/clap from interpreting it as a flag.
-///
-/// # Design note: Global flags
-///
-/// Intentionally excludes global flags like `-C` (working directory).
-/// Suggestions show the operation, not the invocation context.
-/// Users may have changed directory since the error occurred.
-///
-/// If we later find users frequently need `-C` in suggestions (e.g., scripting
-/// contexts), we could add an optional `global_flags` parameter.
pub fn suggest_command(subcommand: &str, args: &[&str], flags: &[&str]) -> String {
- let mut parts = vec!["wt".to_string(), subcommand.to_string()];
+ format_command(None, subcommand, args, flags)
+}
+
+/// Like [`suggest_command`], but prepends `-C <path>` for commands targeting a
+/// specific worktree directory.
+///
+/// The path is formatted with tilde shortening when safe (no escaping needed)
+/// and falls back to a quoted absolute path otherwise.
+pub fn suggest_command_in_dir(
+ working_dir: &Path,
+ subcommand: &str,
+ args: &[&str],
+ flags: &[&str],
+) -> String {
+ format_command(Some(working_dir), subcommand, args, flags)
+}
+
+fn format_command(
+ working_dir: Option<&Path>,
+ subcommand: &str,
+ args: &[&str],
+ flags: &[&str],
+) -> String {
+ let mut parts = vec!["wt".to_string()];
+
+ if let Some(dir) = working_dir {
+ parts.push("-C".to_string());
+ parts.push(crate::path::format_path_for_display(dir));
+ }
+
+ parts.push(subcommand.to_string());
// Flags go before positional args (and before any -- separator)
// so they're always parsed as flags, not positional arguments.
@@ -183,4 +216,28 @@ mod tests {
fn test_flag_only() {
assert_eq!(suggest_command("list", &[], &["--full"]), "wt list --full");
}
+
+ #[test]
+ fn test_in_dir_simple_path() {
+ assert_eq!(
+ suggest_command_in_dir(Path::new("/tmp/repo"), "config", &["update"], &[]),
+ "wt -C /tmp/repo config update"
+ );
+ }
+
+ #[test]
+ fn test_in_dir_path_with_spaces() {
+ assert_eq!(
+ suggest_command_in_dir(Path::new("/tmp/my repo"), "config", &["update"], &[]),
+ "wt -C '/tmp/my repo' config update"
+ );
+ }
+
+ #[test]
+ fn test_in_dir_with_flags_and_args() {
+ assert_eq!(
+ suggest_command_in_dir(Path::new("/tmp/repo"), "remove", &["feature"], &["--force"]),
+ "wt -C /tmp/repo remove --force feature"
+ );
+ }
}