Skip to content

Commit 2b80d84

Browse files
committed
Merge branch 'sh-on-windows'
2 parents 613f018 + a0cc80d commit 2b80d84

File tree

18 files changed

+153
-44
lines changed

18 files changed

+153
-44
lines changed

Cargo.lock

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gitoxide-core/src/pack/multi_index.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub fn entries(multi_index_path: PathBuf, format: OutputFormat, mut out: impl st
8282
if format != OutputFormat::Human {
8383
bail!("Only human format is supported right now");
8484
}
85-
let file = gix::odb::pack::multi_index::File::at(&multi_index_path)?;
85+
let file = gix::odb::pack::multi_index::File::at(multi_index_path)?;
8686
for entry in file.iter() {
8787
writeln!(out, "{} {} {}", entry.oid, entry.pack_index, entry.pack_offset)?;
8888
}

gitoxide-core/src/repository/exclude.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub fn query(
3838
let index = repo.index()?;
3939
let mut cache = repo.excludes(
4040
&index,
41-
Some(gix::ignore::Search::from_overrides(&mut overrides.into_iter())),
41+
Some(gix::ignore::Search::from_overrides(overrides.into_iter())),
4242
Default::default(),
4343
)?;
4444

gitoxide-core/src/repository/index/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn from_list(
4949
let object_hash = gix::hash::Kind::Sha1;
5050

5151
let mut index = gix::index::State::new(object_hash);
52-
for path in std::io::BufReader::new(std::fs::File::open(&entries_file)?).lines() {
52+
for path in std::io::BufReader::new(std::fs::File::open(entries_file)?).lines() {
5353
let path: PathBuf = path?.into();
5454
if !path.is_relative() {
5555
bail!("Input paths need to be relative, but {path:?} is not.")

gix-attributes/tests/state/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ mod value {
1515
}
1616

1717
#[test]
18+
#[allow(invalid_from_utf8)]
1819
fn from_value() {
1920
assert!(std::str::from_utf8(ILLFORMED_UTF8).is_err());
2021
assert!(

gix-command/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ doctest = false
1414

1515
[dependencies]
1616
gix-trace = { version = "^0.1.3", path = "../gix-trace" }
17+
gix-path = { version = "^0.10.0", path = "../gix-path" }
1718

1819
bstr = { version = "1.5.0", default-features = false, features = ["std"] }
20+
shell-words = "1.0"
1921

2022
[dev-dependencies]
2123
gix-testtools = { path = "../tests/tools" }

gix-command/src/lib.rs

+53-7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ pub struct Prepare {
2020
pub env: Vec<(OsString, OsString)>,
2121
/// If `true`, we will use `sh` to execute the `command`.
2222
pub use_shell: bool,
23+
/// If `true` (default `true` on windows and `false` everywhere else)
24+
/// we will see if it's safe to manually invoke `command` after splitting
25+
/// its arguments as a shell would do.
26+
/// Note that outside of windows, it's generally not advisable as this
27+
/// removes support for literal shell scripts with shell-builtins.
28+
///
29+
/// This mimics the behaviour we see with `git` on windows, which also
30+
/// won't invoke the shell there at all.
31+
///
32+
/// Only effective if `use_shell` is `true` as well, as the shell will
33+
/// be used as a fallback if it's not possible to split arguments as
34+
/// the command-line contains 'scripting'.
35+
pub allow_manual_arg_splitting: bool,
2336
}
2437

2538
mod prepare {
@@ -54,6 +67,14 @@ mod prepare {
5467
self
5568
}
5669

70+
/// Use a shell, but try to split arguments by hand if this be safely done without a shell.
71+
///
72+
/// If that's not the case, use a shell instead.
73+
pub fn with_shell_allow_argument_splitting(mut self) -> Self {
74+
self.allow_manual_arg_splitting = true;
75+
self.with_shell()
76+
}
77+
5778
/// Configure the process to use `stdio` for _stdin.
5879
pub fn stdin(mut self, stdio: Stdio) -> Self {
5980
self.stdin = stdio;
@@ -103,14 +124,38 @@ mod prepare {
103124
impl From<Prepare> for Command {
104125
fn from(mut prep: Prepare) -> Command {
105126
let mut cmd = if prep.use_shell {
106-
let mut cmd = Command::new(if cfg!(windows) { "sh" } else { "/bin/sh" });
107-
cmd.arg("-c");
108-
if !prep.args.is_empty() {
109-
prep.command.push(" \"$@\"")
127+
let split_args = prep
128+
.allow_manual_arg_splitting
129+
.then(|| {
130+
if gix_path::into_bstr(std::borrow::Cow::Borrowed(prep.command.as_ref()))
131+
.find_byteset(b"\\|&;<>()$`\n*?[#~%")
132+
.is_none()
133+
{
134+
prep.command
135+
.to_str()
136+
.and_then(|args| shell_words::split(args).ok().map(Vec::into_iter))
137+
} else {
138+
None
139+
}
140+
})
141+
.flatten();
142+
match split_args {
143+
Some(mut args) => {
144+
let mut cmd = Command::new(args.next().expect("non-empty input"));
145+
cmd.args(args);
146+
cmd
147+
}
148+
None => {
149+
let mut cmd = Command::new(if cfg!(windows) { "sh" } else { "/bin/sh" });
150+
cmd.arg("-c");
151+
if !prep.args.is_empty() {
152+
prep.command.push(" \"$@\"")
153+
}
154+
cmd.arg(prep.command);
155+
cmd.arg("--");
156+
cmd
157+
}
110158
}
111-
cmd.arg(prep.command);
112-
cmd.arg("--");
113-
cmd
114159
} else {
115160
Command::new(prep.command)
116161
};
@@ -140,5 +185,6 @@ pub fn prepare(cmd: impl Into<OsString>) -> Prepare {
140185
args: Vec::new(),
141186
env: Vec::new(),
142187
use_shell: false,
188+
allow_manual_arg_splitting: cfg!(windows),
143189
}
144190
}

gix-command/tests/command.rs

+46-3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,25 @@ mod prepare {
1515
let cmd = std::process::Command::from(gix_command::prepare(""));
1616
assert_eq!(format!("{cmd:?}"), "\"\"");
1717
}
18+
1819
#[test]
1920
fn single_and_multiple_arguments() {
2021
let cmd = std::process::Command::from(gix_command::prepare("ls").arg("first").args(["second", "third"]));
2122
assert_eq!(format!("{cmd:?}"), quoted(&["ls", "first", "second", "third"]));
2223
}
2324

25+
#[test]
26+
fn multiple_arguments_in_one_line_with_auto_split() {
27+
let cmd = std::process::Command::from(
28+
gix_command::prepare("echo first second third").with_shell_allow_argument_splitting(),
29+
);
30+
assert_eq!(
31+
format!("{cmd:?}"),
32+
quoted(&["echo", "first", "second", "third"]),
33+
"we split by hand which works unless one tries to rely on shell-builtins (which we can't detect)"
34+
);
35+
}
36+
2437
#[test]
2538
fn single_and_multiple_arguments_as_part_of_command() {
2639
let cmd = std::process::Command::from(gix_command::prepare("ls first second third"));
@@ -36,7 +49,11 @@ mod prepare {
3649
let cmd = std::process::Command::from(gix_command::prepare("ls first second third").with_shell());
3750
assert_eq!(
3851
format!("{cmd:?}"),
39-
quoted(&[SH, "-c", "ls first second third", "--"]),
52+
if cfg!(windows) {
53+
quoted(&["ls", "first", "second", "third"])
54+
} else {
55+
quoted(&[SH, "-c", "ls first second third", "--"])
56+
},
4057
"with shell, this works as it performs word splitting"
4158
);
4259
}
@@ -46,17 +63,43 @@ mod prepare {
4663
let cmd = std::process::Command::from(gix_command::prepare("ls --foo \"a b\"").arg("additional").with_shell());
4764
assert_eq!(
4865
format!("{cmd:?}"),
49-
format!("\"{SH}\" \"-c\" \"ls --foo \\\"a b\\\" \\\"$@\\\"\" \"--\" \"additional\""),
66+
if cfg!(windows) {
67+
quoted(&["ls", "--foo", "a b", "additional"])
68+
} else {
69+
format!(r#""{SH}" "-c" "ls --foo \"a b\" \"$@\"" "--" "additional""#)
70+
},
5071
"with shell, this works as it performs word splitting"
5172
);
5273
}
5374

75+
#[test]
76+
fn single_and_complex_arguments_with_auto_split() {
77+
let cmd =
78+
std::process::Command::from(gix_command::prepare("ls --foo=\"a b\"").with_shell_allow_argument_splitting());
79+
assert_eq!(
80+
format!("{cmd:?}"),
81+
format!(r#""ls" "--foo=a b""#),
82+
"splitting can also handle quotes"
83+
);
84+
}
85+
86+
#[test]
87+
fn single_and_complex_arguments_will_not_auto_split_on_special_characters() {
88+
let cmd =
89+
std::process::Command::from(gix_command::prepare("ls --foo=~/path").with_shell_allow_argument_splitting());
90+
assert_eq!(
91+
format!("{cmd:?}"),
92+
format!(r#""{SH}" "-c" "ls --foo=~/path" "--""#),
93+
"splitting can also handle quotes"
94+
);
95+
}
96+
5497
#[test]
5598
fn tilde_path_and_multiple_arguments_as_part_of_command_with_shell() {
5699
let cmd = std::process::Command::from(gix_command::prepare("~/bin/exe --foo \"a b\"").with_shell());
57100
assert_eq!(
58101
format!("{cmd:?}"),
59-
format!("\"{SH}\" \"-c\" \"~/bin/exe --foo \\\"a b\\\"\" \"--\""),
102+
format!(r#""{SH}" "-c" "~/bin/exe --foo \"a b\"" "--""#),
60103
"this always needs a shell as we need tilde expansion"
61104
);
62105
}

gix-credentials/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ gix-trace = { version = "^0.1.3", path = "../gix-trace" }
2828
thiserror = "1.0.32"
2929
serde = { version = "1.0.114", optional = true, default-features = false, features = ["derive"] }
3030
bstr = { version = "1.3.0", default-features = false, features = ["std"]}
31-
shell-words = "1.0"
3231

3332

3433

gix-credentials/src/program/mod.rs

+4-18
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,10 @@ impl Program {
8080
args.insert_str(0, "credential-");
8181
args.insert_str(0, " ");
8282
args.insert_str(0, git_program);
83-
let split_args = if args.find_byteset(b"\\|&;<>()$`\n*?[#~%").is_none() {
84-
args.to_str()
85-
.ok()
86-
.and_then(|args| shell_words::split(args).ok().map(Vec::into_iter))
87-
} else {
88-
None
89-
};
90-
match split_args {
91-
Some(mut args) => {
92-
let mut cmd = Command::new(args.next().expect("non-empty input"));
93-
cmd.args(args).arg(action.as_arg(true));
94-
cmd
95-
}
96-
None => gix_command::prepare(gix_path::from_bstr(args.as_ref()).into_owned())
97-
.arg(action.as_arg(true))
98-
.with_shell()
99-
.into(),
100-
}
83+
gix_command::prepare(gix_path::from_bstr(args.as_ref()).into_owned())
84+
.arg(action.as_arg(true))
85+
.with_shell_allow_argument_splitting()
86+
.into()
10187
}
10288
Kind::ExternalShellScript(for_shell)
10389
| Kind::ExternalPath {

gix-credentials/tests/program/from_custom_definition.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ fn path_with_args_that_definitely_need_shell() {
7373
assert!(matches!(&prog.kind, Kind::ExternalPath{path_and_args} if path_and_args == input));
7474
assert_eq!(
7575
format!("{:?}", prog.to_command(&helper::Action::Store("egal".into()))),
76-
format!(r#""{SH}" "-c" "/abs/name --arg --bar=\"a b\" \"$@\"" "--" "store""#)
76+
if cfg!(windows) {
77+
r#""/abs/name" "--arg" "--bar=a b" "store""#.to_owned()
78+
} else {
79+
format!(r#""{SH}" "-c" "/abs/name --arg --bar=\"a b\" \"$@\"" "--" "store""#)
80+
}
7781
);
7882
}
7983

@@ -96,7 +100,11 @@ fn path_with_simple_args() {
96100
assert!(matches!(&prog.kind, Kind::ExternalPath{path_and_args} if path_and_args == input));
97101
assert_eq!(
98102
format!("{:?}", prog.to_command(&helper::Action::Store("egal".into()))),
99-
format!(r#""{SH}" "-c" "/abs/name a b \"$@\"" "--" "store""#),
100-
"a shell is used as well because there are arguments, and we don't do splitting ourselves. On windows, this can be a problem."
103+
if cfg!(windows) {
104+
r#""/abs/name" "a" "b" "store""#.to_owned()
105+
} else {
106+
format!(r#""{SH}" "-c" "/abs/name a b \"$@\"" "--" "store""#)
107+
},
108+
"a shell is used as there are arguments, and it's generally more flexible, but on windows we split ourselves"
101109
);
102110
}

gix-index/src/extension/link.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl Link {
8181
.expect("split index file in .git folder")
8282
.join(format!("sharedindex.{}", self.shared_index_checksum));
8383
let mut shared_index = crate::File::at(
84-
&shared_index_path,
84+
shared_index_path,
8585
object_hash,
8686
skip_hash,
8787
crate::decode::Options {

gix-pack/tests/pack/index.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ mod version {
1919
#[test]
2020
fn lookup() -> Result<(), Box<dyn std::error::Error>> {
2121
let object_hash = gix_hash::Kind::Sha1;
22-
let file = index::File::at(&fixture_path(INDEX_V1), object_hash)?;
22+
let file = index::File::at(fixture_path(INDEX_V1), object_hash)?;
2323
for (id, desired_index, assertion) in &[
2424
(&b"036bd66fe9b6591e959e6df51160e636ab1a682e"[..], Some(0), "first"),
2525
(b"f7f791d96b9a34ef0f08db4b007c5309b9adc3d6", Some(65), "close to last"),
@@ -63,7 +63,7 @@ mod version {
6363
#[test]
6464
fn lookup() -> Result<(), Box<dyn std::error::Error>> {
6565
let object_hash = gix_hash::Kind::Sha1;
66-
let file = index::File::at(&fixture_path(INDEX_V2), object_hash)?;
66+
let file = index::File::at(fixture_path(INDEX_V2), object_hash)?;
6767
for (id, expected, assertion_message, hex_len) in [
6868
(&b"0ead45fc727edcf5cadca25ef922284f32bb6fc1"[..], Some(0), "first", 4),
6969
(b"e800b9c207e17f9b11e321cc1fba5dfe08af4222", Some(29), "last", 40),

gix-packetline/src/read/async_io.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ where
138138
}
139139
}
140140

141-
/// Peek the next packet line without consuming it.
141+
/// Peek the next packet line without consuming it. Returns `None` if a stop-packet or an error
142+
/// was encountered.
142143
///
143144
/// Multiple calls to peek will return the same packet line, if there is one.
144145
pub async fn peek_line(&mut self) -> Option<io::Result<Result<PacketLineRef<'_>, decode::Error>>> {

gix-packetline/src/read/blocking_io.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ where
132132
}
133133
}
134134

135-
/// Peek the next packet line without consuming it.
135+
/// Peek the next packet line without consuming it. Returns `None` if a stop-packet or an error
136+
/// was encountered.
136137
///
137138
/// Multiple calls to peek will return the same packet line, if there is one.
138139
pub fn peek_line(&mut self) -> Option<io::Result<Result<PacketLineRef<'_>, decode::Error>>> {

gix-packetline/tests/read/mod.rs

+22
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,28 @@ pub mod streaming_peek_iter {
7272
Ok(())
7373
}
7474

75+
#[maybe_async::test(feature = "blocking-io", async(feature = "async-io", async_std::test))]
76+
async fn peek_eof_is_none() -> crate::Result {
77+
let mut rd =
78+
gix_packetline::StreamingPeekableIter::new(&b"0005a0009ERR e0000"[..], &[PacketLineRef::Flush], false);
79+
rd.fail_on_err_lines(false);
80+
let res = rd.peek_line().await;
81+
assert_eq!(res.expect("line")??, PacketLineRef::Data(b"a"));
82+
rd.read_line().await;
83+
let res = rd.peek_line().await;
84+
assert_eq!(
85+
res.expect("line")??,
86+
PacketLineRef::Data(b"ERR e"),
87+
"we read the ERR but it's not interpreted as such"
88+
);
89+
rd.read_line().await;
90+
91+
let res = rd.peek_line().await;
92+
assert!(res.is_none(), "we peek into the flush packet, which is EOF");
93+
assert_eq!(rd.stopped_at(), Some(PacketLineRef::Flush));
94+
Ok(())
95+
}
96+
7597
#[maybe_async::test(feature = "blocking-io", async(feature = "async-io", async_std::test))]
7698
async fn peek_non_data() -> crate::Result {
7799
let mut rd =

gix/src/remote/connection/fetch/update_refs/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ mod update {
191191

192192
#[test]
193193
fn checked_out_branches_in_worktrees_are_rejected_with_additional_information() -> Result {
194-
let root = gix_path::realpath(&gix_testtools::scripted_fixture_read_only_with_args(
194+
let root = gix_path::realpath(gix_testtools::scripted_fixture_read_only_with_args(
195195
"make_fetch_repos.sh",
196196
[base_repo_path()],
197197
)?)?;

gix/tests/repository/shallow.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ mod traverse {
7171
#[test]
7272
#[parallel]
7373
fn complex_graphs_can_be_iterated_despite_multiple_shallow_boundaries() -> crate::Result {
74-
let base =
75-
gix_path::realpath(&gix_testtools::scripted_fixture_read_only("make_remote_repos.sh")?.join("base"))?;
74+
let base = gix_path::realpath(gix_testtools::scripted_fixture_read_only("make_remote_repos.sh")?.join("base"))?;
7675
let shallow_base = gix_testtools::scripted_fixture_read_only_with_args(
7776
"make_complex_shallow_repo.sh",
7877
Some(base.to_string_lossy()),

0 commit comments

Comments
 (0)