Skip to content

Commit

Permalink
Merge pull request GitoxideLabs#1724 from GitoxideLabs/gix-command-api
Browse files Browse the repository at this point in the history
gix command api
  • Loading branch information
Byron authored Dec 16, 2024
2 parents cd9060a + c67770f commit faa0cde
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
8 changes: 7 additions & 1 deletion gix-command/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,17 @@ mod prepare {
/// Use a shell, but try to split arguments by hand if this can be safely done without a shell.
///
/// If that's not the case, use a shell instead.
pub fn with_shell_allow_argument_splitting(mut self) -> Self {
pub fn with_shell_allow_manual_argument_splitting(mut self) -> Self {
self.allow_manual_arg_splitting = true;
self.with_shell()
}

/// Use a shell, but prohibit splitting arguments by hand even if this could be safely done without a shell.
pub fn with_shell_disallow_manual_argument_splitting(mut self) -> Self {
self.allow_manual_arg_splitting = false;
self.with_shell()
}

/// Configure the process to use `stdio` for _stdin.
pub fn stdin(mut self, stdio: Stdio) -> Self {
self.stdin = stdio;
Expand Down
20 changes: 15 additions & 5 deletions gix-command/tests/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ mod prepare {
#[test]
fn multiple_arguments_in_one_line_with_auto_split() {
let cmd = std::process::Command::from(
gix_command::prepare("echo first second third").with_shell_allow_argument_splitting(),
gix_command::prepare("echo first second third").with_shell_allow_manual_argument_splitting(),
);
assert_eq!(
format!("{cmd:?}"),
Expand Down Expand Up @@ -313,19 +313,29 @@ mod prepare {

#[test]
fn single_and_complex_arguments_with_auto_split() {
let cmd =
std::process::Command::from(gix_command::prepare("ls --foo=\"a b\"").with_shell_allow_argument_splitting());
let cmd = std::process::Command::from(
gix_command::prepare("ls --foo=\"a b\"").with_shell_allow_manual_argument_splitting(),
);
assert_eq!(
format!("{cmd:?}"),
format!(r#""ls" "--foo=a b""#),
"splitting can also handle quotes"
);
}

#[test]
fn single_and_complex_arguments_without_auto_split() {
let cmd = std::process::Command::from(
gix_command::prepare("ls --foo=\"a b\"").with_shell_disallow_manual_argument_splitting(),
);
assert_eq!(format!("{cmd:?}"), quoted(&[SH, "-c", r#"ls --foo=\"a b\""#, "--"]));
}

#[test]
fn single_and_complex_arguments_will_not_auto_split_on_special_characters() {
let cmd =
std::process::Command::from(gix_command::prepare("ls --foo=~/path").with_shell_allow_argument_splitting());
let cmd = std::process::Command::from(
gix_command::prepare("ls --foo=~/path").with_shell_allow_manual_argument_splitting(),
);
assert_eq!(
format!("{cmd:?}"),
format!(r#""{SH}" "-c" "ls --foo=~/path" "--""#),
Expand Down
2 changes: 1 addition & 1 deletion gix-credentials/src/program/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl Program {
args.insert_str(0, git_program.to_string_lossy().as_ref());
gix_command::prepare(gix_path::from_bstr(args.as_bstr()).into_owned())
.arg(action.as_arg(true))
.with_shell_allow_argument_splitting()
.with_shell_allow_manual_argument_splitting()
.into()
}
Kind::ExternalShellScript(for_shell)
Expand Down

0 comments on commit faa0cde

Please sign in to comment.