Skip to content

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

Escape terminal controls in commit subjects and PR context (#4605)

Maximilian Roos215507b8f894
diff --git a/src/commands/list/render.rs b/src/commands/list/render.rs
index 55c0454d1..6a4cfe24c 100644
--- a/src/commands/list/render.rs
+++ b/src/commands/list/render.rs
@@ -2,6 +2,7 @@ use crate::display::{format_relative_time_short, shorten_path};
use anstyle::{Effects, Style};
use unicode_width::UnicodeWidthStr;
use worktrunk::styling::{DETACHED, StyledLine, truncate_visible};
+use worktrunk::utils::escape_controls;
use super::columns::{ColumnKind, DiffVariant};
use super::layout::{
@@ -653,7 +654,8 @@ impl ColumnLayout {
return self.placeholder_cell(placeholder);
};
let mut cell = StyledLine::new();
- let msg = truncate_visible(&commit.commit_message, max_message_len);
+ let message = escape_controls(&commit.commit_message);
+ let msg = truncate_visible(&message, max_message_len);
cell.push_styled(msg, Style::new().dimmed());
cell
}
diff --git a/src/commands/worktree/switch.rs b/src/commands/worktree/switch.rs
index 3f3db6a7d..9cd947260 100644
--- a/src/commands/worktree/switch.rs
+++ b/src/commands/worktree/switch.rs
@@ -28,6 +28,7 @@ use worktrunk::styling::{
eprintln, format_with_gutter, hint_message, info_message, progress_message, suggest_command,
warning_message,
};
+use worktrunk::utils::escape_controls;
use super::resolve::{compute_worktree_path, offer_bare_repo_worktree_path_fix};
use super::types::{CreationMethod, RefIdentity, SwitchBranchInfo, SwitchPlan, SwitchResult};
@@ -192,13 +193,14 @@ fn format_ref_context(info: &RemoteRefInfo) -> String {
}
status_parts.push(info.source_ref());
let status_line = status_parts.join(" · ");
+ let status_line = escape_controls(&status_line);
cformat!(
"<bold>{}</> ({}{})\n{status_line} · <bright-black>{}</>",
- info.title,
+ escape_controls(&info.title),
info.ref_type().symbol(),
info.number,
- info.url
+ escape_controls(&info.url)
)
}
diff --git a/tests/integration_tests/list.rs b/tests/integration_tests/list.rs
index 6456bf00d..445e92b31 100644
--- a/tests/integration_tests/list.rs
+++ b/tests/integration_tests/list.rs
@@ -317,6 +317,36 @@ fn test_list_long_commit_message(mut repo: TestRepo) {
assert_cmd_snapshot!(list_snapshots::command(&repo, repo.root_path()));
}
+#[rstest]
+fn test_list_escapes_commit_controls(repo: TestRepo) {
+ let subject = "hello\x1b]52;c;YQ==\x07 café";
+ repo.commit(subject);
+ let output = repo
+ .wt_command()
+ .args(["list", "--format=table"])
+ .env("CLICOLOR_FORCE", "1")
+ .env("COLUMNS", "200")
+ .output()
+ .unwrap();
+ assert!(output.status.success());
+ let stdout = String::from_utf8(output.stdout).unwrap();
+ assert!(
+ stdout.contains(r"hello\u{1b}]52;c;YQ==\u{7} café"),
+ "{stdout:?}"
+ );
+ assert!(!stdout.contains("\x1b]52;"), "{stdout:?}");
+ assert!(stdout.contains("\x1b["), "test must exercise styled output");
+
+ let output = repo
+ .wt_command()
+ .args(["list", "--format=json"])
+ .output()
+ .unwrap();
+ assert!(output.status.success());
+ let json: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
+ assert_eq!(json["items"][0]["head"]["subject"], subject);
+}
+
#[rstest]
fn test_list_unicode_commit_message(mut repo: TestRepo) {
// Create commit with Unicode message
diff --git a/tests/integration_tests/switch.rs b/tests/integration_tests/switch.rs
index 4245dacac..65d9f11d0 100644
--- a/tests/integration_tests/switch.rs
+++ b/tests/integration_tests/switch.rs
@@ -3587,6 +3587,38 @@ fn configure_mock_cli_env(cmd: &mut std::process::Command, mock_bin: &Path) {
cmd.env(path_var_name, new_path);
}
+#[rstest]
+fn test_switch_pr_escapes_context_controls(#[from(repo_with_remote)] repo: TestRepo) {
+ repo.run_git(&[
+ "remote",
+ "set-url",
+ "origin",
+ "https://github.com/owner/test-repo.git",
+ ]);
+ let response = serde_json::json!({
+ "title": "hello\n\u{1b}]52;c;YQ==\u{7} café",
+ "user": {"login": "alice\u{1b}[31m"},
+ "state": "open\r",
+ "draft": false,
+ "head": {"ref": "feature-auth", "repo": {"name": "test-repo", "owner": {"login": "owner"}}},
+ "base": {"ref": "main", "repo": {"name": "test-repo", "owner": {"login": "owner"}}},
+ "html_url": "https://github.com/owner/test-repo/pull/101"
+ });
+ let mock_bin = setup_mock_gh_for_pr(&repo, &response.to_string());
+ let mut cmd = repo.wt_command();
+ cmd.args(["switch", "--create", "pr:101", "--no-hooks"])
+ .env("CLICOLOR_FORCE", "1");
+ configure_mock_cli_env(&mut cmd, &mock_bin);
+ let output = cmd.output().unwrap();
+ assert!(!output.status.success()); // --create conflicts after displaying the PR.
+ let stderr = String::from_utf8(output.stderr).unwrap();
+ assert!(stderr.contains(r"\u{1b}]52;c;YQ==\u{7} café"), "{stderr:?}");
+ assert!(stderr.contains(r"alice\u{1b}[31m"), "{stderr:?}");
+ assert!(stderr.contains(r"open\r"), "{stderr:?}");
+ assert!(!stderr.contains("\x1b]52;"), "{stderr:?}");
+ assert!(stderr.contains("\x1b["), "test must exercise styled output");
+}
+
/// Test that --create flag conflicts with pr: syntax
#[rstest]
fn test_switch_pr_create_conflict(#[from(repo_with_remote)] repo: TestRepo) {