Skip to content

Commit d8e8b54

Browse files
committed
support for multiple arguments with shell-script support (#450)
1 parent 1cb2e96 commit d8e8b54

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

Diff for: git-command/src/lib.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub struct Prepare {
1010
stdin: std::process::Stdio,
1111
stdout: std::process::Stdio,
1212
stderr: std::process::Stdio,
13+
args: Vec<OsString>,
1314
use_shell: bool,
1415
}
1516

@@ -43,6 +44,12 @@ mod prepare {
4344
self.stderr = stdio;
4445
self
4546
}
47+
48+
/// Add `arg` to the list of arguments to call the command with.
49+
pub fn arg(mut self, arg: impl Into<std::ffi::OsString>) -> Self {
50+
self.args.push(arg.into());
51+
self
52+
}
4653
}
4754

4855
/// Finalization
@@ -55,16 +62,23 @@ mod prepare {
5562
}
5663

5764
impl Into<Command> for Prepare {
58-
fn into(self) -> Command {
65+
fn into(mut self) -> Command {
5966
let mut cmd = if self.use_shell {
6067
let mut cmd = Command::new(if cfg!(windows) { "sh" } else { "/bin/sh" });
6168
cmd.arg("-c");
69+
if !self.args.is_empty() {
70+
self.command.push(" \"$@\"")
71+
}
6272
cmd.arg(self.command);
73+
cmd.arg("--");
6374
cmd
6475
} else {
6576
Command::new(self.command)
6677
};
67-
cmd.stdin(self.stdin).stdout(self.stdout).stderr(self.stderr);
78+
cmd.stdin(self.stdin)
79+
.stdout(self.stdout)
80+
.stderr(self.stderr)
81+
.args(self.args);
6882
cmd
6983
}
7084
}
@@ -83,6 +97,7 @@ pub fn prepare(cmd: impl Into<OsString>) -> Prepare {
8397
stdin: std::process::Stdio::null(),
8498
stdout: std::process::Stdio::piped(),
8599
stderr: std::process::Stdio::null(),
100+
args: Vec::new(),
86101
use_shell: false,
87102
}
88103
}

Diff for: git-command/tests/command.rs

+27
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ mod spawn {
1818
}
1919

2020
mod with_shell {
21+
use git_testtools::bstr::ByteSlice;
22+
2123
#[test]
2224
fn command_in_path_with_args() -> crate::Result {
2325
assert!(git_command::prepare(if cfg!(unix) { "ls -l" } else { "dir.exe -a" })
@@ -33,5 +35,30 @@ mod spawn {
3335
assert!(git_command::prepare(":;:;:").with_shell().spawn()?.wait()?.success());
3436
Ok(())
3537
}
38+
39+
#[test]
40+
fn sh_shell_specific_script_code_with_single_extra_arg() -> crate::Result {
41+
let out = git_command::prepare("echo")
42+
.with_shell()
43+
.arg("1")
44+
.spawn()?
45+
.wait_with_output()?;
46+
assert!(out.status.success());
47+
assert_eq!(out.stdout.as_bstr(), "1\n");
48+
Ok(())
49+
}
50+
51+
#[test]
52+
fn sh_shell_specific_script_code_with_multiple_extra_args() -> crate::Result {
53+
let out = git_command::prepare("echo")
54+
.with_shell()
55+
.arg("1")
56+
.arg("2")
57+
.spawn()?
58+
.wait_with_output()?;
59+
assert!(out.status.success());
60+
assert_eq!(out.stdout.as_bstr(), "1 2\n");
61+
Ok(())
62+
}
3663
}
3764
}

0 commit comments

Comments
 (0)