Skip to content

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

refactor: use shell_escape crate instead of manual quote escaping (#810)

Maximilian Roosa77de06c5d1b
diff --git a/src/diagnostic.rs b/src/diagnostic.rs
index 7838e8277..ac70f14f8 100644
--- a/src/diagnostic.rs
+++ b/src/diagnostic.rs
@@ -42,12 +42,14 @@
//! eprintln!("{}", hint_message(issue_hint()));
//! ```
//!
-use std::path::PathBuf;
+use std::borrow::Cow;
+use std::path::{Path, PathBuf};
use ansi_str::AnsiStr;
use anyhow::Context;
use color_print::cformat;
use minijinja::{Environment, context};
+use shell_escape::unix::escape as posix_escape;
use worktrunk::git::Repository;
use worktrunk::path::format_path_for_display;
use worktrunk::shell_exec::Cmd;
@@ -56,6 +58,27 @@ use worktrunk::styling::{eprintln, hint_message, info_message, warning_message};
use crate::cli::version_str;
use crate::output;
+/// Shell-escape a path for use in POSIX shell commands.
+///
+/// This is used for hints like `gh gist create --web <path>` that users copy/paste.
+/// We use POSIX escaping because:
+/// - `gh` is a cross-platform CLI that accepts POSIX-style paths on all platforms
+/// - Users on Windows typically use Git Bash or similar POSIX-compatible shells
+/// - The `shell_escape` crate's platform-specific escaping produces different output
+/// on Windows (cmd.exe style) vs Unix (POSIX style), causing snapshot test failures
+///
+/// We normalize Windows backslashes to forward slashes for consistent output.
+fn shell_escape_path(path: &Path) -> Cow<'_, str> {
+ let path_str = path.to_string_lossy();
+ // Normalize Windows backslashes to forward slashes for consistent POSIX-style paths
+ let normalized = if path_str.contains('\\') {
+ Cow::Owned(path_str.replace('\\', "/"))
+ } else {
+ path_str
+ };
+ posix_escape(normalized)
+}
+
/// Markdown template for the diagnostic report.
///
/// This template makes the report structure immediately visible.
@@ -245,14 +268,13 @@ pub(crate) fn write_if_verbose(verbose: u8, command_line: &str, error_msg: Optio
// Only show gh command if gh is installed
if is_gh_installed() {
- // Escape single quotes for shell: 'it'\''s' -> it's
- let path_str = path.to_string_lossy().replace('\'', "'\\''");
+ let path_str = shell_escape_path(&path);
// URL with prefilled body: ## Gist\n\n[Paste URL]\n\n## Description\n\n[Describe the issue]
let issue_url = "https://github.com/max-sixty/worktrunk/issues/new?body=%23%23%20Gist%0A%0A%5BPaste%20gist%20URL%5D%0A%0A%23%23%20Description%0A%0A%5BDescribe%20the%20issue%5D";
eprintln!(
"{}",
hint_message(cformat!(
- "To report a bug, create a secret gist with <bright-black>gh gist create --web '{path_str}'</> and reference it from an issue at <bright-black>{issue_url}</>"
+ "To report a bug, create a secret gist with <bright-black>gh gist create --web {path_str}</> and reference it from an issue at <bright-black>{issue_url}</>"
))
);
}
@@ -445,4 +467,30 @@ mod tests {
assert!(result.starts_with("(log truncated to last ~50KB)"));
assert!(result.len() < 55 * 1024);
}
+
+ #[test]
+ fn test_shell_escape_path_unix_style() {
+ // Unix paths with no special chars pass through unchanged
+ let path = Path::new("/home/user/project/.git/wt-logs/diagnostic.md");
+ let result = shell_escape_path(path);
+ assert_eq!(result, "/home/user/project/.git/wt-logs/diagnostic.md");
+ }
+
+ #[test]
+ fn test_shell_escape_path_windows_backslashes() {
+ // Windows backslashes are normalized to forward slashes
+ let path = Path::new(r"D:\a\worktrunk\.git\wt-logs\diagnostic.md");
+ let result = shell_escape_path(path);
+ // The colon in D: requires quoting in POSIX shells
+ assert!(result.contains("D:/a/worktrunk"));
+ assert!(!result.contains('\\'));
+ }
+
+ #[test]
+ fn test_shell_escape_path_special_chars() {
+ // Paths with spaces or special chars get quoted
+ let path = Path::new("/path/with spaces/file.md");
+ let result = shell_escape_path(path);
+ assert!(result.starts_with('\'') || result.contains("\\ "));
+ }
}
diff --git a/tests/integration_tests/diagnostic.rs b/tests/integration_tests/diagnostic.rs
index 339d0e558..523b9ea95 100644
--- a/tests/integration_tests/diagnostic.rs
+++ b/tests/integration_tests/diagnostic.rs
@@ -465,10 +465,12 @@ fn test_diagnostic_gh_hint_with_vv(mut repo: TestRepo) {
.find(|line| line.contains("report a bug"))
.expect("Should have hint about reporting a bug");
- // Normalize the path in the hint (handles both raw paths and _REPO_ filtered paths)
- let normalized = regex::Regex::new(r"'[^']*diagnostic\.md'")
+ // Normalize the path in the hint. The path may be:
+ // - Quoted on Windows (drive letter colon requires POSIX escaping): 'D:/a/.../diagnostic.md'
+ // - Unquoted on Unix (no special chars): _REPO_/.git/wt-logs/diagnostic.md
+ let normalized = regex::Regex::new(r"--web '?[^' \x1b]*diagnostic\.md'?")
.unwrap()
- .replace(hint_line, "'[DIAGNOSTIC_PATH]'");
+ .replace(hint_line, "--web [DIAGNOSTIC_PATH]");
let settings = setup_snapshot_settings(&repo);
settings.bind(|| {
diff --git a/tests/snapshots/integration__integration_tests__diagnostic__diagnostic_gh_hint.snap b/tests/snapshots/integration__integration_tests__diagnostic__diagnostic_gh_hint.snap
index ca2212811..8b427920b 100644
--- a/tests/snapshots/integration__integration_tests__diagnostic__diagnostic_gh_hint.snap
+++ b/tests/snapshots/integration__integration_tests__diagnostic__diagnostic_gh_hint.snap
@@ -2,4 +2,4 @@
source: tests/integration_tests/diagnostic.rs
expression: normalized
---
-↳ To report a bug, create a secret gist with gh gist create --web '[DIAGNOSTIC_PATH]' and reference it from an issue at https://github.com/max-sixty/worktrunk/issues/new?body=%23%23%20Gist%0A%0A%5BPaste%20gist%20URL%5D%0A%0A%23%23%20Description%0A%0A%5BDescribe%20the%20issue%5D
+↳ To report a bug, create a secret gist with gh gist create --web [DIAGNOSTIC_PATH] and reference it from an issue at https://github.com/max-sixty/worktrunk/issues/new?body=%23%23%20Gist%0A%0A%5BPaste%20gist%20URL%5D%0A%0A%23%23%20Description%0A%0A%5BDescribe%20the%20issue%5D