Skip to content

Accept ToComponents in gix_fs::Stack #1909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/msrv.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
env:
# dictated by `firefox` to support the `helix` editor, but now probably effectively be controlled by `jiff`, which also aligns with `regex`.
# IMPORTANT: adjust etc/msrv-badge.svg as well
rust_version: 1.74.0
rust_version: 1.75.0

steps:
- uses: actions/checkout@v4
Expand Down
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions etc/msrv-badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions gitoxide-core/src/repository/attributes/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ pub struct Options {
}

pub(crate) mod function {
use std::{borrow::Cow, io, path::Path};

use anyhow::bail;
use gix::bstr::BStr;
use std::borrow::Cow;
use std::{io, path::Path};

use crate::{
is_dir_to_mode,
Expand Down Expand Up @@ -44,7 +44,7 @@ pub(crate) mod function {
.ok()
.map(|m| is_dir_to_mode(m.is_dir()));

let entry = cache.at_entry(path.as_slice(), mode)?;
let entry = cache.at_entry(&path, mode)?;
if !entry.matching_attributes(&mut matches) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion gitoxide-core/src/repository/exclude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn query(
.metadata()
.ok()
.map(|m| is_dir_to_mode(m.is_dir()));
let entry = cache.at_entry(path.as_slice(), mode)?;
let entry = cache.at_entry(&path, mode)?;
let match_ = entry
.matching_exclude_pattern()
.and_then(|m| (show_ignore_patterns || !m.pattern.is_negative()).then_some(m));
Expand Down
5 changes: 4 additions & 1 deletion gix-fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
description = "A crate providing file system specific utilities to `gitoxide`"
authors = ["Sebastian Thiel <sebastian.thiel@icloud.com>"]
edition = "2021"
rust-version = "1.70"
rust-version = "1.75"
include = ["src/**/*", "LICENSE-*"]

[lib]
Expand All @@ -19,8 +19,11 @@ doctest = false
serde = ["dep:serde"]

[dependencies]
bstr = "1.5.0"
gix-path = { version = "^0.10.14", path = "../gix-path" }
gix-features = { version = "^0.40.0", path = "../gix-features", features = ["fs-read-dir"] }
gix-utils = { version = "^0.1.14", path = "../gix-utils" }
thiserror = "2.0.0"
serde = { version = "1.0.114", optional = true, default-features = false, features = ["std", "derive"] }

# For `Capabilities` to assure parallel operation works.
Expand Down
111 changes: 93 additions & 18 deletions gix-fs/src/stack.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,80 @@
use crate::Stack;
use bstr::{BStr, BString, ByteSlice};
use std::ffi::OsStr;
use std::path::{Component, Path, PathBuf};

use crate::Stack;
///
pub mod to_normal_path_components {
use std::ffi::OsString;

/// The error used in [`ToNormalPathComponents::to_normal_path_components()`](super::ToNormalPathComponents::to_normal_path_components()).
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error("Input path \"{path}\" contains relative or absolute components", path = std::path::Path::new(.0.as_os_str()).display())]
NotANormalComponent(OsString),
#[error("Could not convert to UTF8 or from UTF8 due to ill-formed input")]
IllegalUtf8,
}
}

/// Obtain an iterator over `OsStr`-components which are normal, none-relative and not absolute.
pub trait ToNormalPathComponents {
/// Return an iterator over the normal components of a path, without the separator.
fn to_normal_path_components(&self) -> impl Iterator<Item = Result<&OsStr, to_normal_path_components::Error>>;
}

impl ToNormalPathComponents for &Path {
fn to_normal_path_components(&self) -> impl Iterator<Item = Result<&OsStr, to_normal_path_components::Error>> {
self.components().map(|component| match component {
Component::Normal(os_str) => Ok(os_str),
_ => Err(to_normal_path_components::Error::NotANormalComponent(
self.as_os_str().to_owned(),
)),
})
}
}

impl ToNormalPathComponents for PathBuf {
fn to_normal_path_components(&self) -> impl Iterator<Item = Result<&OsStr, to_normal_path_components::Error>> {
self.components().map(|component| match component {
Component::Normal(os_str) => Ok(os_str),
_ => Err(to_normal_path_components::Error::NotANormalComponent(
self.as_os_str().to_owned(),
)),
})
}
}

impl ToNormalPathComponents for &BStr {
fn to_normal_path_components(&self) -> impl Iterator<Item = Result<&OsStr, to_normal_path_components::Error>> {
self.split(|b| *b == b'/').filter(|c| !c.is_empty()).map(|component| {
gix_path::try_from_byte_slice(component.as_bstr())
.map_err(|_| to_normal_path_components::Error::IllegalUtf8)
.map(Path::as_os_str)
})
}
}

impl ToNormalPathComponents for &str {
fn to_normal_path_components(&self) -> impl Iterator<Item = Result<&OsStr, to_normal_path_components::Error>> {
self.split('/').filter(|c| !c.is_empty()).map(|component| {
gix_path::try_from_byte_slice(component.as_bytes())
.map_err(|_| to_normal_path_components::Error::IllegalUtf8)
.map(Path::as_os_str)
})
}
}

impl ToNormalPathComponents for &BString {
fn to_normal_path_components(&self) -> impl Iterator<Item = Result<&OsStr, to_normal_path_components::Error>> {
self.split(|b| *b == b'/').filter(|c| !c.is_empty()).map(|component| {
gix_path::try_from_byte_slice(component.as_bstr())
.map_err(|_| to_normal_path_components::Error::IllegalUtf8)
.map(Path::as_os_str)
})
}
}

/// Access
impl Stack {
Expand Down Expand Up @@ -62,8 +136,13 @@ impl Stack {
/// `relative` paths are terminal, so point to their designated file or directory.
/// The path is also expected to be normalized, and should not contain extra separators, and must not contain `..`
/// or have leading or trailing slashes (or additionally backslashes on Windows).
pub fn make_relative_path_current(&mut self, relative: &Path, delegate: &mut dyn Delegate) -> std::io::Result<()> {
if self.valid_components != 0 && relative.as_os_str().is_empty() {
pub fn make_relative_path_current(
&mut self,
relative: impl ToNormalPathComponents,
delegate: &mut dyn Delegate,
) -> std::io::Result<()> {
let mut components = relative.to_normal_path_components().peekable();
if self.valid_components != 0 && components.peek().is_none() {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
"empty inputs are not allowed",
Expand All @@ -73,15 +152,19 @@ impl Stack {
delegate.push_directory(self)?;
}

let mut components = relative.components().peekable();
let mut existing_components = self.current_relative.components();
let mut matching_components = 0;
while let (Some(existing_comp), Some(new_comp)) = (existing_components.next(), components.peek()) {
if existing_comp == *new_comp {
components.next();
matching_components += 1;
} else {
break;
match new_comp {
Ok(new_comp) => {
if existing_comp.as_os_str() == *new_comp {
components.next();
matching_components += 1;
} else {
break;
}
}
Err(err) => return Err(std::io::Error::other(format!("{err}"))),
}
}

Expand All @@ -100,15 +183,7 @@ impl Stack {
}

while let Some(comp) = components.next() {
if !matches!(comp, Component::Normal(_)) {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"Input path \"{}\" contains relative or absolute components",
relative.display()
),
));
}
let comp = comp.map_err(std::io::Error::other)?;
let is_last_component = components.peek().is_none();
self.current_is_directory = !is_last_component;
self.current.push(comp);
Expand Down
Loading
Loading