From 2fce6bfee7cd31fcd78282e696f586a3eeb1f810 Mon Sep 17 00:00:00 2001 From: Mauro Deryckere Date: Thu, 26 Mar 2026 09:03:56 +0100 Subject: [PATCH] fix(rewrite): skip cat rewrite when incompatible flags are present MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit cat flags like -v, -A, -e, -t, -s have different semantics than rtk read flags (-v means verbose, -n means line numbers). Blindly forwarding flags caused incorrect behavior (e.g. cat -A file → rtk read -A file which fails). Only skip rewrite for incompatible flags. cat -n (line numbers) maps correctly to rtk read -n, so it is still rewritten for token savings. Plain `cat file` continues to rewrite to `rtk read file` as before. --- src/discover/registry.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/discover/registry.rs b/src/discover/registry.rs index cc975b3b..f8d37c31 100644 --- a/src/discover/registry.rs +++ b/src/discover/registry.rs @@ -615,6 +615,16 @@ fn rewrite_segment(seg: &str, excluded: &[String]) -> Option { return rewrite_tail_lines(cmd_part).map(|r| format!("{}{}", r, redirect_suffix)); } + // Most cat flags (-v, -A, -e, -t, -s, -b, --show-all, etc.) have different + // semantics than rtk read or no equivalent at all. Only `-n` (line numbers) + // maps correctly to `rtk read -n`. Skip rewrite for any other flag. + if cmd_part.starts_with("cat ") { + let args = cmd_part["cat ".len()..].trim_start(); + if args.starts_with('-') && !args.starts_with("-n ") && !args.starts_with("-n\t") { + return None; + } + } + // Use classify_command for correct ignore/prefix handling let rtk_equivalent = match classify_command(cmd_part) { Classification::Supported { rtk_equivalent, .. } => { @@ -1165,6 +1175,26 @@ mod tests { ); } + #[test] + fn test_rewrite_cat_with_incompatible_flags_skipped() { + // cat flags with different semantics than rtk read — skip rewrite + assert_eq!(rewrite_command("cat -A file.cpp", &[]), None); + assert_eq!(rewrite_command("cat -v file.txt", &[]), None); + assert_eq!(rewrite_command("cat -e file.txt", &[]), None); + assert_eq!(rewrite_command("cat -t file.txt", &[]), None); + assert_eq!(rewrite_command("cat -s file.txt", &[]), None); + assert_eq!(rewrite_command("cat --show-all file.txt", &[]), None); + } + + #[test] + fn test_rewrite_cat_with_compatible_flags() { + // cat -n (line numbers) maps to rtk read -n — allow rewrite + assert_eq!( + rewrite_command("cat -n file.txt", &[]), + Some("rtk read -n file.txt".into()) + ); + } + #[test] fn test_rewrite_rg_pattern() { assert_eq!(