Skip to content

Commit 7b027e7

Browse files
authored
Revert "Revert "Overhaul shell detection and centralize command generation for unified exec"" (#6607)
Reverts #6606
1 parent db2aa57 commit 7b027e7

File tree

11 files changed

+297
-409
lines changed

11 files changed

+297
-409
lines changed

codex-rs/core/src/bash.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
use std::path::PathBuf;
2+
13
use tree_sitter::Node;
24
use tree_sitter::Parser;
35
use tree_sitter::Tree;
46
use tree_sitter_bash::LANGUAGE as BASH;
57

8+
use crate::shell::ShellType;
9+
use crate::shell::detect_shell_type;
10+
611
/// Parse the provided bash source using tree-sitter-bash, returning a Tree on
712
/// success or None if parsing failed.
813
pub fn try_parse_shell(shell_lc_arg: &str) -> Option<Tree> {
@@ -88,23 +93,16 @@ pub fn try_parse_word_only_commands_sequence(tree: &Tree, src: &str) -> Option<V
8893
Some(commands)
8994
}
9095

91-
pub fn is_well_known_sh_shell(shell: &str) -> bool {
92-
if shell == "bash" || shell == "zsh" {
93-
return true;
94-
}
95-
96-
let shell_name = std::path::Path::new(shell)
97-
.file_name()
98-
.and_then(|s| s.to_str())
99-
.unwrap_or(shell);
100-
matches!(shell_name, "bash" | "zsh")
101-
}
102-
10396
pub fn extract_bash_command(command: &[String]) -> Option<(&str, &str)> {
10497
let [shell, flag, script] = command else {
10598
return None;
10699
};
107-
if !matches!(flag.as_str(), "-lc" | "-c") || !is_well_known_sh_shell(shell) {
100+
if !matches!(flag.as_str(), "-lc" | "-c")
101+
|| !matches!(
102+
detect_shell_type(&PathBuf::from(shell)),
103+
Some(ShellType::Zsh) | Some(ShellType::Bash)
104+
)
105+
{
108106
return None;
109107
}
110108
Some((shell, script))

codex-rs/core/src/command_safety/is_safe_command.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::bash::parse_shell_lc_plain_commands;
2+
use crate::command_safety::windows_safe_commands::is_safe_command_windows;
23

34
pub fn is_known_safe_command(command: &[String]) -> bool {
45
let command: Vec<String> = command
@@ -11,12 +12,9 @@ pub fn is_known_safe_command(command: &[String]) -> bool {
1112
}
1213
})
1314
.collect();
14-
#[cfg(target_os = "windows")]
15-
{
16-
use super::windows_safe_commands::is_safe_command_windows;
17-
if is_safe_command_windows(&command) {
18-
return true;
19-
}
15+
16+
if is_safe_command_windows(&command) {
17+
return true;
2018
}
2119

2220
if is_safe_to_call_with_exec(&command) {
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
pub mod is_dangerous_command;
22
pub mod is_safe_command;
3-
#[cfg(target_os = "windows")]
43
pub mod windows_safe_commands;

codex-rs/core/src/environment_context.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,6 @@ mod tests {
329329
Some(workspace_write_policy(vec!["/repo"], false)),
330330
Some(Shell::Bash(BashShell {
331331
shell_path: "/bin/bash".into(),
332-
bashrc_path: "/home/user/.bashrc".into(),
333332
})),
334333
);
335334
let context2 = EnvironmentContext::new(
@@ -338,7 +337,6 @@ mod tests {
338337
Some(workspace_write_policy(vec!["/repo"], false)),
339338
Some(Shell::Zsh(ZshShell {
340339
shell_path: "/bin/zsh".into(),
341-
zshrc_path: "/home/user/.zshrc".into(),
342340
})),
343341
);
344342

0 commit comments

Comments
 (0)