mirror of
https://github.com/openai/codex.git
synced 2026-11-01 19:33:35 +00:00
Codex created this PR from the following prompt: > upgrade this entire repo to Rust 1.89. Note that this requires updating codex-rs/rust-toolchain.toml as well as the workflows in .github/. Make sure that things are "clippy clean" as this change will likely uncover new Clippy errors. `just fmt` and `cargo clippy --tests` are sufficient to check for correctness Note this modifies a lot of lines because it folds nested `if` statements using `&&`. --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/openai/codex/pull/2465). * #2467 * __->__ #2465
63 lines
1.7 KiB
Rust
63 lines
1.7 KiB
Rust
use std::path::Path;
|
||
use std::path::PathBuf;
|
||
|
||
use shlex::try_join;
|
||
|
||
pub(crate) fn escape_command(command: &[String]) -> String {
|
||
try_join(command.iter().map(|s| s.as_str())).unwrap_or_else(|_| command.join(" "))
|
||
}
|
||
|
||
pub(crate) fn strip_bash_lc_and_escape(command: &[String]) -> String {
|
||
match command {
|
||
// exactly three items
|
||
[first, second, third]
|
||
// first two must be "bash", "-lc"
|
||
if first == "bash" && second == "-lc" =>
|
||
{
|
||
third.clone() // borrow `third`
|
||
}
|
||
_ => escape_command(command),
|
||
}
|
||
}
|
||
|
||
/// If `path` is absolute and inside $HOME, return the part *after* the home
|
||
/// directory; otherwise, return the path as-is. Note if `path` is the homedir,
|
||
/// this will return and empty path.
|
||
pub(crate) fn relativize_to_home<P>(path: P) -> Option<PathBuf>
|
||
where
|
||
P: AsRef<Path>,
|
||
{
|
||
let path = path.as_ref();
|
||
if !path.is_absolute() {
|
||
// If the path is not absolute, we can’t do anything with it.
|
||
return None;
|
||
}
|
||
|
||
if let Some(home_dir) = std::env::var_os("HOME").map(PathBuf::from)
|
||
&& let Ok(rel) = path.strip_prefix(&home_dir)
|
||
{
|
||
return Some(rel.to_path_buf());
|
||
}
|
||
|
||
None
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn test_escape_command() {
|
||
let args = vec!["foo".into(), "bar baz".into(), "weird&stuff".into()];
|
||
let cmdline = escape_command(&args);
|
||
assert_eq!(cmdline, "foo 'bar baz' 'weird&stuff'");
|
||
}
|
||
|
||
#[test]
|
||
fn test_strip_bash_lc_and_escape() {
|
||
let args = vec!["bash".into(), "-lc".into(), "echo hello".into()];
|
||
let cmdline = strip_bash_lc_and_escape(&args);
|
||
assert_eq!(cmdline, "echo hello");
|
||
}
|
||
}
|