Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
341c43e4c8 | ||
|
|
40f9998fcc |
@@ -48,6 +48,10 @@ lazy_static! {
|
||||
.collect();
|
||||
static ref ENV_PREFIX: Regex =
|
||||
Regex::new(r"^(?:sudo\s+|env\s+|[A-Z_][A-Z0-9_]*=[^\s]*\s+)+").unwrap();
|
||||
// Git global options that appear before the subcommand: -C <path>, -c <key=val>,
|
||||
// --git-dir <dir>, --work-tree <dir>, and flag-only options (#163)
|
||||
static ref GIT_GLOBAL_OPT: Regex =
|
||||
Regex::new(r"^(?:(?:-C\s+\S+|-c\s+\S+|--git-dir(?:=\S+|\s+\S+)|--work-tree(?:=\S+|\s+\S+)|--no-pager|--no-optional-locks|--bare|--literal-pathspecs)\s+)+").unwrap();
|
||||
}
|
||||
|
||||
/// Classify a single (already-split) command.
|
||||
@@ -76,6 +80,12 @@ pub fn classify_command(cmd: &str) -> Classification {
|
||||
return Classification::Ignored;
|
||||
}
|
||||
|
||||
// Normalize absolute binary paths: /usr/bin/grep → grep (#485)
|
||||
let cmd_normalized = strip_absolute_path(cmd_clean);
|
||||
// Strip git global options: git -C /tmp status → git status (#163)
|
||||
let cmd_normalized = strip_git_global_opts(&cmd_normalized);
|
||||
let cmd_clean = cmd_normalized.as_str();
|
||||
|
||||
// Exclude cat/head/tail with redirect operators — these are writes, not reads (#315)
|
||||
if cmd_clean.starts_with("cat ")
|
||||
|| cmd_clean.starts_with("head ")
|
||||
@@ -262,6 +272,42 @@ pub fn split_command_chain(cmd: &str) -> Vec<&str> {
|
||||
results
|
||||
}
|
||||
|
||||
/// Strip git global options before the subcommand (#163).
|
||||
/// `git -C /tmp status` → `git status`, preserving the rest.
|
||||
/// Returns the original string unchanged if not a git command.
|
||||
fn strip_git_global_opts(cmd: &str) -> String {
|
||||
// Only applies to commands starting with "git "
|
||||
if !cmd.starts_with("git ") {
|
||||
return cmd.to_string();
|
||||
}
|
||||
let after_git = &cmd[4..]; // skip "git "
|
||||
let stripped = GIT_GLOBAL_OPT.replace(after_git, "");
|
||||
format!("git {}", stripped.trim())
|
||||
}
|
||||
|
||||
/// Normalize absolute binary paths: `/usr/bin/grep -rn foo` → `grep -rn foo` (#485)
|
||||
/// Only strips if the first word contains a `/` (Unix path).
|
||||
fn strip_absolute_path(cmd: &str) -> String {
|
||||
let first_space = cmd.find(' ');
|
||||
let first_word = match first_space {
|
||||
Some(pos) => &cmd[..pos],
|
||||
None => cmd,
|
||||
};
|
||||
if first_word.contains('/') {
|
||||
// Extract basename
|
||||
let basename = first_word.rsplit('/').next().unwrap_or(first_word);
|
||||
if basename.is_empty() {
|
||||
return cmd.to_string();
|
||||
}
|
||||
match first_space {
|
||||
Some(pos) => format!("{}{}", basename, &cmd[pos..]),
|
||||
None => basename.to_string(),
|
||||
}
|
||||
} else {
|
||||
cmd.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if a command has RTK_DISABLED= prefix in its env prefix portion.
|
||||
pub fn has_rtk_disabled_prefix(cmd: &str) -> bool {
|
||||
let trimmed = cmd.trim();
|
||||
@@ -1941,4 +1987,132 @@ mod tests {
|
||||
);
|
||||
assert_eq!(strip_disabled_prefix("git status"), "git status");
|
||||
}
|
||||
|
||||
// --- #485: absolute path normalization ---
|
||||
|
||||
#[test]
|
||||
fn test_classify_absolute_path_grep() {
|
||||
assert_eq!(
|
||||
classify_command("/usr/bin/grep -rni pattern"),
|
||||
Classification::Supported {
|
||||
rtk_equivalent: "rtk grep",
|
||||
category: "Files",
|
||||
estimated_savings_pct: 75.0,
|
||||
status: RtkStatus::Existing,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_classify_absolute_path_ls() {
|
||||
assert_eq!(
|
||||
classify_command("/bin/ls -la"),
|
||||
Classification::Supported {
|
||||
rtk_equivalent: "rtk ls",
|
||||
category: "Files",
|
||||
estimated_savings_pct: 65.0,
|
||||
status: RtkStatus::Existing,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_classify_absolute_path_git() {
|
||||
assert_eq!(
|
||||
classify_command("/usr/local/bin/git status"),
|
||||
Classification::Supported {
|
||||
rtk_equivalent: "rtk git",
|
||||
category: "Git",
|
||||
estimated_savings_pct: 70.0,
|
||||
status: RtkStatus::Existing,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_classify_absolute_path_no_args() {
|
||||
// /usr/bin/find alone → still classified
|
||||
assert_eq!(
|
||||
classify_command("/usr/bin/find ."),
|
||||
Classification::Supported {
|
||||
rtk_equivalent: "rtk find",
|
||||
category: "Files",
|
||||
estimated_savings_pct: 70.0,
|
||||
status: RtkStatus::Existing,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_strip_absolute_path_helper() {
|
||||
assert_eq!(strip_absolute_path("/usr/bin/grep -rn foo"), "grep -rn foo");
|
||||
assert_eq!(strip_absolute_path("/bin/ls -la"), "ls -la");
|
||||
assert_eq!(strip_absolute_path("grep -rn foo"), "grep -rn foo");
|
||||
assert_eq!(strip_absolute_path("/usr/local/bin/git"), "git");
|
||||
}
|
||||
|
||||
// --- #163: git global options ---
|
||||
|
||||
#[test]
|
||||
fn test_classify_git_with_dash_c_path() {
|
||||
assert_eq!(
|
||||
classify_command("git -C /tmp status"),
|
||||
Classification::Supported {
|
||||
rtk_equivalent: "rtk git",
|
||||
category: "Git",
|
||||
estimated_savings_pct: 70.0,
|
||||
status: RtkStatus::Existing,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_classify_git_no_pager_log() {
|
||||
assert_eq!(
|
||||
classify_command("git --no-pager log -5"),
|
||||
Classification::Supported {
|
||||
rtk_equivalent: "rtk git",
|
||||
category: "Git",
|
||||
estimated_savings_pct: 70.0,
|
||||
status: RtkStatus::Existing,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_classify_git_git_dir() {
|
||||
assert_eq!(
|
||||
classify_command("git --git-dir /tmp/.git status"),
|
||||
Classification::Supported {
|
||||
rtk_equivalent: "rtk git",
|
||||
category: "Git",
|
||||
estimated_savings_pct: 70.0,
|
||||
status: RtkStatus::Existing,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rewrite_git_dash_c() {
|
||||
assert_eq!(
|
||||
rewrite_command("git -C /tmp status", &[]),
|
||||
Some("rtk git -C /tmp status".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rewrite_git_no_pager() {
|
||||
assert_eq!(
|
||||
rewrite_command("git --no-pager log -5", &[]),
|
||||
Some("rtk git --no-pager log -5".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_strip_git_global_opts_helper() {
|
||||
assert_eq!(strip_git_global_opts("git -C /tmp status"), "git status");
|
||||
assert_eq!(strip_git_global_opts("git --no-pager log"), "git log");
|
||||
assert_eq!(strip_git_global_opts("git status"), "git status");
|
||||
assert_eq!(strip_git_global_opts("cargo test"), "cargo test");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user