Skip to content

Commit 40e8e51

Browse files
committed
feat!: use gix_path::convert::ToPathComponents
1 parent 0d3e74f commit 40e8e51

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gix-fs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ serde = ["dep:serde"]
2020

2121
[dependencies]
2222
gix-features = { version = "^0.40.0", path = "../gix-features", features = ["fs-read-dir"] }
23+
gix-path = { version = "^0.10.14", path = "../gix-path" }
2324
gix-utils = { version = "^0.1.14", path = "../gix-utils" }
2425
serde = { version = "1.0.114", optional = true, default-features = false, features = ["std", "derive"] }
2526

gix-fs/src/stack.rs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::path::{Component, Path, PathBuf};
1+
use std::path::{Path, PathBuf};
22

33
use crate::Stack;
44

@@ -62,8 +62,14 @@ impl Stack {
6262
/// `relative` paths are terminal, so point to their designated file or directory.
6363
/// The path is also expected to be normalized, and should not contain extra separators, and must not contain `..`
6464
/// or have leading or trailing slashes (or additionally backslashes on Windows).
65-
pub fn make_relative_path_current(&mut self, relative: &Path, delegate: &mut dyn Delegate) -> std::io::Result<()> {
66-
if self.valid_components != 0 && relative.as_os_str().is_empty() {
65+
pub fn make_relative_path_current(
66+
&mut self,
67+
relative: impl gix_path::ToPathComponents,
68+
delegate: &mut dyn Delegate,
69+
) -> std::io::Result<()> {
70+
let components: Vec<_> = relative.to_components().collect();
71+
72+
if self.valid_components != 0 && components.is_empty() {
6773
return Err(std::io::Error::new(
6874
std::io::ErrorKind::Other,
6975
"empty inputs are not allowed",
@@ -73,16 +79,21 @@ impl Stack {
7379
delegate.push_directory(self)?;
7480
}
7581

76-
let mut components = relative.components().peekable();
82+
let mut components = components.into_iter().peekable();
7783
let mut existing_components = self.current_relative.components();
7884
let mut matching_components = 0;
7985
while let (Some(existing_comp), Some(new_comp)) = (existing_components.next(), components.peek()) {
80-
if existing_comp == *new_comp {
81-
components.next();
82-
matching_components += 1;
83-
} else {
84-
break;
85-
}
86+
match new_comp {
87+
Ok(new_comp) => {
88+
if existing_comp.as_os_str() == *new_comp {
89+
components.next();
90+
matching_components += 1;
91+
} else {
92+
break;
93+
}
94+
}
95+
Err(err) => return Err(std::io::Error::new(std::io::ErrorKind::Other, format!("{err}"))),
96+
};
8697
}
8798

8899
for _ in 0..self.valid_components - matching_components {
@@ -100,15 +111,12 @@ impl Stack {
100111
}
101112

102113
while let Some(comp) = components.next() {
103-
if !matches!(comp, Component::Normal(_)) {
104-
return Err(std::io::Error::new(
105-
std::io::ErrorKind::Other,
106-
format!(
107-
"Input path \"{}\" contains relative or absolute components",
108-
relative.display()
109-
),
110-
));
111-
}
114+
let comp = match comp {
115+
Ok(comp) => comp,
116+
Err(err) => {
117+
return Err(std::io::Error::new(std::io::ErrorKind::Other, format!("{err}")));
118+
}
119+
};
112120
let is_last_component = components.peek().is_none();
113121
self.current_is_directory = !is_last_component;
114122
self.current.push(comp);

0 commit comments

Comments
 (0)