Accept `ToComponents` in `gix_fs::Stack`
Unified · Split
| @@ -31,7 +31,7 @@ jobs: · 30 unmodified lines | ||
| 31 | 31 | env: |
| 32 | 32 | # dictated by `firefox` to support the `helix` editor, but now probably effectively be controlled by `jiff`, which also aligns with `regex`. |
| 33 | 33 | # IMPORTANT: adjust etc/msrv-badge.svg as well |
| 34 | − rust_version: 1.74.0 | |
| 34 | + rust_version: 1.75.0 | |
| 35 | 35 | |
| 36 | 36 | steps: |
| 37 | 37 | - uses: actions/checkout@v4 |
| @@ -1901,13 +1901,16 @@ dependencies = [ · 1900 unmodified lines | ||
| 1901 | 1901 | name = "gix-fs" |
| 1902 | 1902 | version = "0.13.0" |
| 1903 | 1903 | dependencies = [ |
| 1904 | + "bstr", | |
| 1904 | 1905 | "crossbeam-channel", |
| 1905 | 1906 | "fastrand", |
| 1906 | 1907 | "gix-features 0.40.0", |
| 1908 | + "gix-path 0.10.14", | |
| 1907 | 1909 | "gix-utils 0.1.14", |
| 1908 | 1910 | "is_ci", |
| 1909 | 1911 | "serde", |
| 1910 | 1912 | "tempfile", |
| 1913 | + "thiserror 2.0.12", | |
| 1911 | 1914 | ] |
| 1912 | 1915 | |
| 1913 | 1916 | [[package]] |
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="92" height="20" role="img" aria-label="rustc: 1.70.0+"> |
| 2 | − <title>rustc: 1.74.0+</title> | |
| 2 | + <title>rustc: 1.75.0+</title> | |
| 3 | 3 | <linearGradient id="s" x2="0" y2="100%"> |
| 4 | 4 | <stop offset="0" stop-color="#bbb" stop-opacity=".1"/> |
| 5 | 5 | <stop offset="1" stop-opacity=".1"/> |
| @@ -15,7 +15,7 @@ · 9 unmodified lines | ||
| 15 | 15 | <g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"> |
| 16 | 16 | <text aria-hidden="true" x="195" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="270">rustc</text> |
| 17 | 17 | <text x="195" y="140" transform="scale(.1)" fill="#fff" textLength="270">rustc</text> |
| 18 | − <text aria-hidden="true" x="635" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="450">1.74.0+</text> | |
| 19 | − <text x="635" y="140" transform="scale(.1)" fill="#fff" textLength="450">1.74.0+</text> | |
| 18 | + <text aria-hidden="true" x="635" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="450">1.75.0+</text> | |
| 19 | + <text x="635" y="140" transform="scale(.1)" fill="#fff" textLength="450">1.75.0+</text> | |
| 20 | 20 | </g> |
| 21 | 21 | </svg> |
| @@ -8,10 +8,10 @@ pub struct Options { · 7 unmodified lines | ||
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | pub(crate) mod function { |
| 11 | − use std::{borrow::Cow, io, path::Path}; | |
| 12 | − | |
| 13 | 11 | use anyhow::bail; |
| 14 | 12 | use gix::bstr::BStr; |
| 13 | + use std::borrow::Cow; | |
| 14 | + use std::{io, path::Path}; | |
| 15 | 15 | |
| 16 | 16 | use crate::{ |
| 17 | 17 | is_dir_to_mode, |
| @@ -44,7 +44,7 @@ pub(crate) mod function { · 26 unmodified lines | ||
| 44 | 44 | .ok() |
| 45 | 45 | .map(|m| is_dir_to_mode(m.is_dir())); |
| 46 | 46 | |
| 47 | − let entry = cache.at_entry(path.as_slice(), mode)?; | |
| 47 | + let entry = cache.at_entry(&path, mode)?; | |
| 48 | 48 | if !entry.matching_attributes(&mut matches) { |
| 49 | 49 | continue; |
| 50 | 50 | } |
| @@ -48,7 +48,7 @@ pub fn query( · 47 unmodified lines | ||
| 48 | 48 | .metadata() |
| 49 | 49 | .ok() |
| 50 | 50 | .map(|m| is_dir_to_mode(m.is_dir())); |
| 51 | − let entry = cache.at_entry(path.as_slice(), mode)?; | |
| 51 | + let entry = cache.at_entry(&path, mode)?; | |
| 52 | 52 | let match_ = entry |
| 53 | 53 | .matching_exclude_pattern() |
| 54 | 54 | .and_then(|m| (show_ignore_patterns || !m.pattern.is_negative()).then_some(m)); |
| @@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0" · 7 unmodified lines | ||
| 8 | 8 | description = "A crate providing file system specific utilities to `gitoxide`" |
| 9 | 9 | authors = ["Sebastian Thiel <sebastian.thiel@icloud.com>"] |
| 10 | 10 | edition = "2021" |
| 11 | −rust-version = "1.70" | |
| 11 | +rust-version = "1.75" | |
| 12 | 12 | include = ["src/**/*", "LICENSE-*"] |
| 13 | 13 | |
| 14 | 14 | [lib] |
| @@ -19,8 +19,11 @@ doctest = false · 4 unmodified lines | ||
| 19 | 19 | serde = ["dep:serde"] |
| 20 | 20 | |
| 21 | 21 | [dependencies] |
| 22 | +bstr = "1.5.0" | |
| 23 | +gix-path = { version = "^0.10.14", path = "../gix-path" } | |
| 22 | 24 | gix-features = { version = "^0.40.0", path = "../gix-features", features = ["fs-read-dir"] } |
| 23 | 25 | gix-utils = { version = "^0.1.14", path = "../gix-utils" } |
| 26 | +thiserror = "2.0.0" | |
| 24 | 27 | serde = { version = "1.0.114", optional = true, default-features = false, features = ["std", "derive"] } |
| 25 | 28 | |
| 26 | 29 | # For `Capabilities` to assure parallel operation works. |
| @@ -1,6 +1,80 @@ | ||
| 1 | +use crate::Stack; | |
| 2 | +use bstr::{BStr, BString, ByteSlice}; | |
| 3 | +use std::ffi::OsStr; | |
| 1 | 4 | use std::path::{Component, Path, PathBuf}; |
| 2 | 5 | |
| 3 | −use crate::Stack; | |
| 6 | +/// | |
| 7 | +pub mod to_normal_path_components { | |
| 8 | + use std::ffi::OsString; | |
| 9 | + | |
| 10 | + /// The error used in [`ToNormalPathComponents::to_normal_path_components()`](super::ToNormalPathComponents::to_normal_path_components()). | |
| 11 | + #[derive(Debug, thiserror::Error)] | |
| 12 | + #[allow(missing_docs)] | |
| 13 | + pub enum Error { | |
| 14 | + #[error("Input path \"{path}\" contains relative or absolute components", path = std::path::Path::new(.0.as_os_str()).display())] | |
| 15 | + NotANormalComponent(OsString), | |
| 16 | + #[error("Could not convert to UTF8 or from UTF8 due to ill-formed input")] | |
| 17 | + IllegalUtf8, | |
| 18 | + } | |
| 19 | +} | |
| 20 | + | |
| 21 | +/// Obtain an iterator over `OsStr`-components which are normal, none-relative and not absolute. | |
| 22 | +pub trait ToNormalPathComponents { | |
| 23 | + /// Return an iterator over the normal components of a path, without the separator. | |
| 24 | + fn to_normal_path_components(&self) -> impl Iterator<Item = Result<&OsStr, to_normal_path_components::Error>>; | |
| 25 | +} | |
| 26 | + | |
| 27 | +impl ToNormalPathComponents for &Path { | |
| 28 | + fn to_normal_path_components(&self) -> impl Iterator<Item = Result<&OsStr, to_normal_path_components::Error>> { | |
| 29 | + self.components().map(|component| match component { | |
| 30 | + Component::Normal(os_str) => Ok(os_str), | |
| 31 | + _ => Err(to_normal_path_components::Error::NotANormalComponent( | |
| 32 | + self.as_os_str().to_owned(), | |
| 33 | + )), | |
| 34 | + }) | |
| 35 | + } | |
| 36 | +} | |
| 37 | + | |
| 38 | +impl ToNormalPathComponents for PathBuf { | |
| 39 | + fn to_normal_path_components(&self) -> impl Iterator<Item = Result<&OsStr, to_normal_path_components::Error>> { | |
| 40 | + self.components().map(|component| match component { | |
| 41 | + Component::Normal(os_str) => Ok(os_str), | |
| 42 | + _ => Err(to_normal_path_components::Error::NotANormalComponent( | |
| 43 | + self.as_os_str().to_owned(), | |
| 44 | + )), | |
| 45 | + }) | |
| 46 | + } | |
| 47 | +} | |
| 48 | + | |
| 49 | +impl ToNormalPathComponents for &BStr { | |
| 50 | + fn to_normal_path_components(&self) -> impl Iterator<Item = Result<&OsStr, to_normal_path_components::Error>> { | |
| 51 | + self.split(|b| *b == b'/').filter(|c| !c.is_empty()).map(|component| { | |
| 52 | + gix_path::try_from_byte_slice(component.as_bstr()) | |
| 53 | + .map_err(|_| to_normal_path_components::Error::IllegalUtf8) | |
| 54 | + .map(Path::as_os_str) | |
| 55 | + }) | |
| 56 | + } | |
| 57 | +} | |
| 58 | + | |
| 59 | +impl ToNormalPathComponents for &str { | |
| 60 | + fn to_normal_path_components(&self) -> impl Iterator<Item = Result<&OsStr, to_normal_path_components::Error>> { | |
| 61 | + self.split('/').filter(|c| !c.is_empty()).map(|component| { | |
| 62 | + gix_path::try_from_byte_slice(component.as_bytes()) | |
| 63 | + .map_err(|_| to_normal_path_components::Error::IllegalUtf8) | |
| 64 | + .map(Path::as_os_str) | |
| 65 | + }) | |
| 66 | + } | |
| 67 | +} | |
| 68 | + | |
| 69 | +impl ToNormalPathComponents for &BString { | |
| 70 | + fn to_normal_path_components(&self) -> impl Iterator<Item = Result<&OsStr, to_normal_path_components::Error>> { | |
| 71 | + self.split(|b| *b == b'/').filter(|c| !c.is_empty()).map(|component| { | |
| 72 | + gix_path::try_from_byte_slice(component.as_bstr()) | |
| 73 | + .map_err(|_| to_normal_path_components::Error::IllegalUtf8) | |
| 74 | + .map(Path::as_os_str) | |
| 75 | + }) | |
| 76 | + } | |
| 77 | +} | |
| 4 | 78 | |
| 5 | 79 | /// Access |
| 6 | 80 | impl Stack { |
| @@ -62,8 +136,13 @@ impl Stack { · 55 unmodified lines | ||
| 62 | 136 | /// `relative` paths are terminal, so point to their designated file or directory. |
| 63 | 137 | /// The path is also expected to be normalized, and should not contain extra separators, and must not contain `..` |
| 64 | 138 | /// 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() { | |
| 139 | + pub fn make_relative_path_current( | |
| 140 | + &mut self, | |
| 141 | + relative: impl ToNormalPathComponents, | |
| 142 | + delegate: &mut dyn Delegate, | |
| 143 | + ) -> std::io::Result<()> { | |
| 144 | + let mut components = relative.to_normal_path_components().peekable(); | |
| 145 | + if self.valid_components != 0 && components.peek().is_none() { | |
| 67 | 146 | return Err(std::io::Error::new( |
| 68 | 147 | std::io::ErrorKind::Other, |
| 69 | 148 | "empty inputs are not allowed", |
| @@ -73,15 +152,19 @@ impl Stack { · 3 unmodified lines | ||
| 73 | 152 | delegate.push_directory(self)?; |
| 74 | 153 | } |
| 75 | 154 | |
| 76 | − let mut components = relative.components().peekable(); | |
| 77 | 155 | let mut existing_components = self.current_relative.components(); |
| 78 | 156 | let mut matching_components = 0; |
| 79 | 157 | 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; | |
| 158 | + match new_comp { | |
| 159 | + Ok(new_comp) => { | |
| 160 | + if existing_comp.as_os_str() == *new_comp { | |
| 161 | + components.next(); | |
| 162 | + matching_components += 1; | |
| 163 | + } else { | |
| 164 | + break; | |
| 165 | + } | |
| 166 | + } | |
| 167 | + Err(err) => return Err(std::io::Error::other(format!("{err}"))), | |
| 85 | 168 | } |
| 86 | 169 | } |
| 87 | 170 | |
| @@ -100,15 +183,7 @@ impl Stack { · 12 unmodified lines | ||
| 100 | 183 | } |
| 101 | 184 | |
| 102 | 185 | 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 | − } | |
| 186 | + let comp = comp.map_err(std::io::Error::other)?; | |
| 112 | 187 | let is_last_component = components.peek().is_none(); |
| 113 | 188 | self.current_is_directory = !is_last_component; |
| 114 | 189 | self.current.push(comp); |
| @@ -182,7 +182,7 @@ fn empty_paths_are_noop_if_no_path_was_pushed_before() { · 181 unmodified lines | ||
| 182 | 182 | let mut s = Stack::new(root.clone()); |
| 183 | 183 | |
| 184 | 184 | let mut r = Record::default(); |
| 185 | − s.make_relative_path_current("".as_ref(), &mut r).unwrap(); | |
| 185 | + s.make_relative_path_current("", &mut r).unwrap(); | |
| 186 | 186 | assert_eq!( |
| 187 | 187 | s.current_relative().to_string_lossy(), |
| 188 | 188 | "", |
| @@ -196,7 +196,7 @@ fn relative_components_are_invalid() { · 7 unmodified lines | ||
| 196 | 196 | let mut s = Stack::new(root.clone()); |
| 197 | 197 | |
| 198 | 198 | let mut r = Record::default(); |
| 199 | − let err = s.make_relative_path_current("a/..".as_ref(), &mut r).unwrap_err(); | |
| 199 | + let err = s.make_relative_path_current(p("a/.."), &mut r).unwrap_err(); | |
| 200 | 200 | assert_eq!( |
| 201 | 201 | err.to_string(), |
| 202 | 202 | format!( |
| @@ -205,7 +205,7 @@ fn relative_components_are_invalid() { · 2 unmodified lines | ||
| 205 | 205 | ) |
| 206 | 206 | ); |
| 207 | 207 | |
| 208 | − s.make_relative_path_current("a/./b".as_ref(), &mut r) | |
| 208 | + s.make_relative_path_current(p("a/./b"), &mut r) | |
| 209 | 209 | .expect("dot is ignored"); |
| 210 | 210 | assert_eq!( |
| 211 | 211 | r, |
| @@ -221,7 +221,7 @@ fn relative_components_are_invalid() { · 9 unmodified lines | ||
| 221 | 221 | if cfg!(windows) { r".\a\b" } else { "./a/b" }, |
| 222 | 222 | "dot is silently ignored" |
| 223 | 223 | ); |
| 224 | − s.make_relative_path_current("a//b/".as_ref(), &mut r) | |
| 224 | + s.make_relative_path_current(p("a//b/"), &mut r) | |
| 225 | 225 | .expect("multiple-slashes are ignored"); |
| 226 | 226 | assert_eq!( |
| 227 | 227 | r, |
| @@ -240,19 +240,19 @@ fn absolute_paths_are_invalid() -> crate::Result { · 12 unmodified lines | ||
| 240 | 240 | let mut s = Stack::new(root.clone()); |
| 241 | 241 | |
| 242 | 242 | let mut r = Record::default(); |
| 243 | − let err = s.make_relative_path_current("/".as_ref(), &mut r).unwrap_err(); | |
| 243 | + let err = s.make_relative_path_current(p("/"), &mut r).unwrap_err(); | |
| 244 | 244 | assert_eq!( |
| 245 | 245 | err.to_string(), |
| 246 | 246 | r#"Input path "/" contains relative or absolute components"#, |
| 247 | 247 | "a leading slash is always considered absolute" |
| 248 | 248 | ); |
| 249 | − s.make_relative_path_current("a/".as_ref(), &mut r)?; | |
| 249 | + s.make_relative_path_current(p("a/"), &mut r)?; | |
| 250 | 250 | assert_eq!( |
| 251 | 251 | s.current(), |
| 252 | 252 | p("./a/"), |
| 253 | 253 | "trailing slashes aren't a problem at this stage, as they cannot cause a 'breakout'" |
| 254 | 254 | ); |
| 255 | − s.make_relative_path_current(r"b\".as_ref(), &mut r)?; | |
| 255 | + s.make_relative_path_current(p(r"b\"), &mut r)?; | |
| 256 | 256 | assert_eq!( |
| 257 | 257 | s.current(), |
| 258 | 258 | p(r"./b\"), |
| @@ -261,7 +261,7 @@ fn absolute_paths_are_invalid() -> crate::Result { · 2 unmodified lines | ||
| 261 | 261 | |
| 262 | 262 | #[cfg(windows)] |
| 263 | 263 | { |
| 264 | − let err = s.make_relative_path_current(r"\".as_ref(), &mut r).unwrap_err(); | |
| 264 | + let err = s.make_relative_path_current(Path::new(r"\"), &mut r).unwrap_err(); | |
| 265 | 265 | assert_eq!( |
| 266 | 266 | err.to_string(), |
| 267 | 267 | r#"Input path "\" contains relative or absolute components"#, |
| @@ -269,20 +269,20 @@ fn absolute_paths_are_invalid() -> crate::Result { · 1 unmodified line | ||
| 269 | 269 | hence they are forbidden." |
| 270 | 270 | ); |
| 271 | 271 | |
| 272 | − let err = s.make_relative_path_current("c:".as_ref(), &mut r).unwrap_err(); | |
| 272 | + let err = s.make_relative_path_current(Path::new("c:"), &mut r).unwrap_err(); | |
| 273 | 273 | assert_eq!( |
| 274 | 274 | err.to_string(), |
| 275 | 275 | r#"Input path "c:" contains relative or absolute components"#, |
| 276 | 276 | "on Windows, drive-letters without trailing backslash or slash are also absolute (even though they ought to be relative)" |
| 277 | 277 | ); |
| 278 | − let err = s.make_relative_path_current(r"c:\".as_ref(), &mut r).unwrap_err(); | |
| 278 | + let err = s.make_relative_path_current(Path::new(r"c:\"), &mut r).unwrap_err(); | |
| 279 | 279 | assert_eq!( |
| 280 | 280 | err.to_string(), |
| 281 | 281 | r#"Input path "c:\" contains relative or absolute components"#, |
| 282 | 282 | "on Windows, drive-letters are absolute, which is expected" |
| 283 | 283 | ); |
| 284 | 284 | |
| 285 | − s.make_relative_path_current("֍:".as_ref(), &mut r)?; | |
| 285 | + s.make_relative_path_current(Path::new("֍:"), &mut r)?; | |
| 286 | 286 | assert_eq!( |
| 287 | 287 | s.current().to_string_lossy(), |
| 288 | 288 | ".\\֍:", |
| @@ -290,7 +290,7 @@ fn absolute_paths_are_invalid() -> crate::Result { · 1 unmodified line | ||
| 290 | 290 | but we just turn it into a presumably invalid path which is fine, i.e. we get a joined path" |
| 291 | 291 | ); |
| 292 | 292 | let err = s |
| 293 | − .make_relative_path_current(r"\\localhost\hello".as_ref(), &mut r) | |
| 293 | + .make_relative_path_current(Path::new(r"\\localhost\hello"), &mut r) | |
| 294 | 294 | .unwrap_err(); |
| 295 | 295 | assert_eq!( |
| 296 | 296 | err.to_string(), |
| @@ -298,7 +298,9 @@ fn absolute_paths_are_invalid() -> crate::Result { · 1 unmodified line | ||
| 298 | 298 | "there is UNC paths as well" |
| 299 | 299 | ); |
| 300 | 300 | |
| 301 | − let err = s.make_relative_path_current(r#"\\?\C:"#.as_ref(), &mut r).unwrap_err(); | |
| 301 | + let err = s | |
| 302 | + .make_relative_path_current(Path::new(r#"\\?\C:"#), &mut r) | |
| 303 | + .unwrap_err(); | |
| 302 | 304 | assert_eq!( |
| 303 | 305 | err.to_string(), |
| 304 | 306 | r#"Input path "\\?\C:" contains relative or absolute components"#, |
| @@ -314,10 +316,10 @@ fn delegate_calls_are_consistent() -> crate::Result { · 9 unmodified lines | ||
| 314 | 316 | let mut s = Stack::new(root.clone()); |
| 315 | 317 | |
| 316 | 318 | assert_eq!(s.current(), root); |
| 317 | − assert_eq!(s.current_relative(), Path::new("")); | |
| 319 | + assert_eq!(s.current_relative(), p("")); | |
| 318 | 320 | |
| 319 | 321 | let mut r = Record::default(); |
| 320 | − s.make_relative_path_current("a/b".as_ref(), &mut r)?; | |
| 322 | + s.make_relative_path_current("a/b", &mut r)?; | |
| 321 | 323 | let mut dirs = vec![root.clone(), root.join("a")]; |
| 322 | 324 | assert_eq!( |
| 323 | 325 | r, |
| @@ -329,7 +331,7 @@ fn delegate_calls_are_consistent() -> crate::Result { · 5 unmodified lines | ||
| 329 | 331 | "it pushes the root-directory first, then the intermediate one" |
| 330 | 332 | ); |
| 331 | 333 | |
| 332 | − s.make_relative_path_current("a/b2".as_ref(), &mut r)?; | |
| 334 | + s.make_relative_path_current("a/b2", &mut r)?; | |
| 333 | 335 | assert_eq!( |
| 334 | 336 | r, |
| 335 | 337 | Record { |
| @@ -340,7 +342,7 @@ fn delegate_calls_are_consistent() -> crate::Result { · 4 unmodified lines | ||
| 340 | 342 | "dirs remain the same as b2 is a leaf/file, hence the new `push`" |
| 341 | 343 | ); |
| 342 | 344 | |
| 343 | − s.make_relative_path_current("c/d/e".as_ref(), &mut r)?; | |
| 345 | + s.make_relative_path_current("c/d/e", &mut r)?; | |
| 344 | 346 | dirs.pop(); |
| 345 | 347 | dirs.extend([root.join("c"), root.join("c").join("d")]); |
| 346 | 348 | assert_eq!( |
| @@ -354,7 +356,7 @@ fn delegate_calls_are_consistent() -> crate::Result { · 7 unmodified lines | ||
| 354 | 356 | ); |
| 355 | 357 | |
| 356 | 358 | dirs.push(root.join("c").join("d").join("x")); |
| 357 | − s.make_relative_path_current("c/d/x/z".as_ref(), &mut r)?; | |
| 359 | + s.make_relative_path_current("c/d/x/z", &mut r)?; | |
| 358 | 360 | assert_eq!( |
| 359 | 361 | r, |
| 360 | 362 | Record { |
| @@ -366,8 +368,8 @@ fn delegate_calls_are_consistent() -> crate::Result { · 5 unmodified lines | ||
| 366 | 368 | ); |
| 367 | 369 | |
| 368 | 370 | dirs.drain(1..).count(); |
| 369 | − s.make_relative_path_current("f".as_ref(), &mut r)?; | |
| 370 | − assert_eq!(s.current_relative(), Path::new("f")); | |
| 371 | + s.make_relative_path_current("f", &mut r)?; | |
| 372 | + assert_eq!(s.current_relative(), p("f")); | |
| 371 | 373 | assert_eq!( |
| 372 | 374 | r, |
| 373 | 375 | Record { |
| @@ -379,7 +381,7 @@ fn delegate_calls_are_consistent() -> crate::Result { · 5 unmodified lines | ||
| 379 | 381 | ); |
| 380 | 382 | |
| 381 | 383 | dirs.push(root.join("x")); |
| 382 | − s.make_relative_path_current("x/z".as_ref(), &mut r)?; | |
| 384 | + s.make_relative_path_current("x/z", &mut r)?; | |
| 383 | 385 | assert_eq!( |
| 384 | 386 | r, |
| 385 | 387 | Record { |
| @@ -391,7 +393,7 @@ fn delegate_calls_are_consistent() -> crate::Result { · 5 unmodified lines | ||
| 391 | 393 | ); |
| 392 | 394 | |
| 393 | 395 | dirs.push(root.join("x").join("z")); |
| 394 | − s.make_relative_path_current("x/z/a".as_ref(), &mut r)?; | |
| 396 | + s.make_relative_path_current("x/z/a", &mut r)?; | |
| 395 | 397 | assert_eq!( |
| 396 | 398 | r, |
| 397 | 399 | Record { |
| @@ -404,7 +406,7 @@ fn delegate_calls_are_consistent() -> crate::Result { · 6 unmodified lines | ||
| 404 | 406 | |
| 405 | 407 | dirs.push(root.join("x").join("z").join("a")); |
| 406 | 408 | dirs.push(root.join("x").join("z").join("a").join("b")); |
| 407 | − s.make_relative_path_current("x/z/a/b/c".as_ref(), &mut r)?; | |
| 409 | + s.make_relative_path_current("x/z/a/b/c", &mut r)?; | |
| 408 | 410 | assert_eq!( |
| 409 | 411 | r, |
| 410 | 412 | Record { |
| @@ -416,7 +418,7 @@ fn delegate_calls_are_consistent() -> crate::Result { · 5 unmodified lines | ||
| 416 | 418 | ); |
| 417 | 419 | |
| 418 | 420 | dirs.drain(1 /*root*/ + 1 /*x*/ + 1 /*x/z*/ ..).count(); |
| 419 | − s.make_relative_path_current("x/z".as_ref(), &mut r)?; | |
| 421 | + s.make_relative_path_current("x/z", &mut r)?; | |
| 420 | 422 | assert_eq!( |
| 421 | 423 | r, |
| 422 | 424 | Record { |
| @@ -432,7 +434,7 @@ fn delegate_calls_are_consistent() -> crate::Result { · 9 unmodified lines | ||
| 432 | 434 | "the stack is state so keeps thinking it's a directory which is consistent. Git does it differently though." |
| 433 | 435 | ); |
| 434 | 436 | |
| 435 | − let err = s.make_relative_path_current("".as_ref(), &mut r).unwrap_err(); | |
| 437 | + let err = s.make_relative_path_current(p(""), &mut r).unwrap_err(); | |
| 436 | 438 | assert_eq!( |
| 437 | 439 | err.to_string(), |
| 438 | 440 | "empty inputs are not allowed", |
| @@ -440,7 +442,7 @@ fn delegate_calls_are_consistent() -> crate::Result { · 1 unmodified line | ||
| 440 | 442 | and besides that really shouldn't happen" |
| 441 | 443 | ); |
| 442 | 444 | |
| 443 | − s.make_relative_path_current("leaf".as_ref(), &mut r)?; | |
| 445 | + s.make_relative_path_current("leaf", &mut r)?; | |
| 444 | 446 | dirs.drain(1..).count(); |
| 445 | 447 | assert_eq!( |
| 446 | 448 | r, |
| @@ -452,7 +454,7 @@ fn delegate_calls_are_consistent() -> crate::Result { · 5 unmodified lines | ||
| 452 | 454 | "reset as much as possible, with just a leaf-component and the root directory" |
| 453 | 455 | ); |
| 454 | 456 | |
| 455 | − s.make_relative_path_current("a//b".as_ref(), &mut r)?; | |
| 457 | + s.make_relative_path_current(p("a//b"), &mut r)?; | |
| 456 | 458 | dirs.push(root.join("a")); |
| 457 | 459 | assert_eq!( |
| 458 | 460 | r, |
| @@ -466,7 +468,7 @@ fn delegate_calls_are_consistent() -> crate::Result { · 7 unmodified lines | ||
| 466 | 468 | |
| 467 | 469 | #[cfg(not(windows))] |
| 468 | 470 | { |
| 469 | − s.make_relative_path_current(r"\/b".as_ref(), &mut r)?; | |
| 471 | + s.make_relative_path_current(r"\/b", &mut r)?; | |
| 470 | 472 | dirs.pop(); |
| 471 | 473 | dirs.push(root.join(r"\")); |
| 472 | 474 | assert_eq!( |
| @@ -479,7 +481,7 @@ fn delegate_calls_are_consistent() -> crate::Result { · 6 unmodified lines | ||
| 479 | 481 | "a backslash is a normal character outside of Windows, so it's fine to have it as component" |
| 480 | 482 | ); |
| 481 | 483 | |
| 482 | − s.make_relative_path_current(r"\".as_ref(), &mut r)?; | |
| 484 | + s.make_relative_path_current(r"\", &mut r)?; | |
| 483 | 485 | assert_eq!( |
| 484 | 486 | r, |
| 485 | 487 | Record { |
| @@ -494,7 +496,7 @@ fn delegate_calls_are_consistent() -> crate::Result { · 8 unmodified lines | ||
| 494 | 496 | r"a backslash can also be a valid leaf component - here we only popped the 'b', leaving the \ 'directory'" |
| 495 | 497 | ); |
| 496 | 498 | |
| 497 | − s.make_relative_path_current(r"\\".as_ref(), &mut r)?; | |
| 499 | + s.make_relative_path_current(r"\\", &mut r)?; | |
| 498 | 500 | dirs.pop(); |
| 499 | 501 | assert_eq!( |
| 500 | 502 | r, |
| @@ -513,7 +515,7 @@ fn delegate_calls_are_consistent() -> crate::Result { · 12 unmodified lines | ||
| 513 | 515 | |
| 514 | 516 | #[cfg(windows)] |
| 515 | 517 | { |
| 516 | − s.make_relative_path_current(r"c\/d".as_ref(), &mut r)?; | |
| 518 | + s.make_relative_path_current(Path::new(r"c\/d"), &mut r)?; | |
| 517 | 519 | dirs.pop(); |
| 518 | 520 | dirs.push(root.join("c")); |
| 519 | 521 | assert_eq!( |
| @@ -124,7 +124,7 @@ impl Response { · 123 unmodified lines | ||
| 124 | 124 | io::ErrorKind::UnexpectedEof, |
| 125 | 125 | "Could not read message headline", |
| 126 | 126 | ))); |
| 127 | − }; | |
| 127 | + } | |
| 128 | 128 | |
| 129 | 129 | match line.trim_end() { |
| 130 | 130 | "acknowledgments" => { |
| @@ -1,10 +1,10 @@ | ||
| 1 | +use crate::SymlinkCheck; | |
| 1 | 2 | use bstr::BStr; |
| 3 | +use gix_fs::stack::ToNormalPathComponents; | |
| 2 | 4 | use gix_fs::Stack; |
| 3 | 5 | use std::borrow::Cow; |
| 4 | 6 | use std::path::{Path, PathBuf}; |
| 5 | 7 | |
| 6 | −use crate::SymlinkCheck; | |
| 7 | − | |
| 8 | 8 | impl SymlinkCheck { |
| 9 | 9 | /// Create a new stack that starts operating at `root`. |
| 10 | 10 | pub fn new(root: PathBuf) -> Self { |
| @@ -24,7 +24,7 @@ impl SymlinkCheck { · 13 unmodified lines | ||
| 24 | 24 | /// ### Note |
| 25 | 25 | /// |
| 26 | 26 | /// On windows, no verification is performed, instead only the combined path is provided as usual. |
| 27 | − pub fn verified_path(&mut self, relative_path: &Path) -> std::io::Result<&Path> { | |
| 27 | + pub fn verified_path(&mut self, relative_path: impl ToNormalPathComponents) -> std::io::Result<&Path> { | |
| 28 | 28 | self.inner.make_relative_path_current(relative_path, &mut Delegate)?; |
| 29 | 29 | Ok(self.inner.current()) |
| 30 | 30 | } |
| @@ -34,7 +34,7 @@ impl SymlinkCheck { · 3 unmodified lines | ||
| 34 | 34 | /// For convenience, this incarnation is tuned to be easy to use with Git paths, i.e. slash-separated `BString` path. |
| 35 | 35 | pub fn verified_path_allow_nonexisting(&mut self, relative_path: &BStr) -> std::io::Result<Cow<'_, Path>> { |
| 36 | 36 | let rela_path = gix_path::try_from_bstr(relative_path).map_err(std::io::Error::other)?; |
| 37 | − if let Err(err) = self.verified_path(&rela_path) { | |
| 37 | + if let Err(err) = self.verified_path(rela_path.as_ref()) { | |
| 38 | 38 | if err.kind() == std::io::ErrorKind::NotFound { |
| 39 | 39 | Ok(Cow::Owned(self.inner.root().join(rela_path))) |
| 40 | 40 | } else { |
| @@ -25,7 +25,7 @@ fn paths_not_going_through_symlink_directories_are_ok_and_point_to_correct_item( · 24 unmodified lines | ||
| 25 | 25 | ("dir/dirlink", is_symlinked_dir), |
| 26 | 26 | ] { |
| 27 | 27 | assert!( |
| 28 | − expectation(&stack.verified_path(rela_path.as_ref())?.symlink_metadata()?), | |
| 28 | + expectation(&stack.verified_path(rela_path)?.symlink_metadata()?), | |
| 29 | 29 | "{rela_path:?} expectation failed" |
| 30 | 30 | ); |
| 31 | 31 | } |
| @@ -35,7 +35,7 @@ fn paths_not_going_through_symlink_directories_are_ok_and_point_to_correct_item( · 3 unmodified lines | ||
| 35 | 35 | |
| 36 | 36 | #[test] |
| 37 | 37 | fn leaf_file_does_not_have_to_exist() -> crate::Result { |
| 38 | − assert!(!stack().verified_path("dir/does-not-exist".as_ref())?.exists()); | |
| 38 | + assert!(!stack().verified_path("dir/does-not-exist")?.exists()); | |
| 39 | 39 | Ok(()) |
| 40 | 40 | } |
| 41 | 41 | |
| @@ -43,10 +43,7 @@ fn leaf_file_does_not_have_to_exist() -> crate::Result { · 1 unmodified line | ||
| 43 | 43 | #[cfg(not(windows))] |
| 44 | 44 | fn intermediate_directories_have_to_exist_or_not_found_error() -> crate::Result { |
| 45 | 45 | assert_eq!( |
| 46 | − stack() | |
| 47 | − .verified_path("nonexisting-dir/file".as_ref()) | |
| 48 | − .unwrap_err() | |
| 49 | − .kind(), | |
| 46 | + stack().verified_path("nonexisting-dir/file").unwrap_err().kind(), | |
| 50 | 47 | std::io::ErrorKind::NotFound |
| 51 | 48 | ); |
| 52 | 49 | Ok(()) |
| @@ -55,7 +52,7 @@ fn intermediate_directories_have_to_exist_or_not_found_error() -> crate::Result · 2 unmodified lines | ||
| 55 | 52 | #[test] |
| 56 | 53 | #[cfg(windows)] |
| 57 | 54 | fn intermediate_directories_do_not_have_exist_for_success() -> crate::Result { |
| 58 | − assert!(stack().verified_path("nonexisting-dir/file".as_ref()).is_ok()); | |
| 55 | + assert!(stack().verified_path("nonexisting-dir/file").is_ok()); | |
| 59 | 56 | Ok(()) |
| 60 | 57 | } |
| 61 | 58 | |
| @@ -67,16 +64,13 @@ fn intermediate_directories_do_not_have_exist_for_success() -> crate::Result { · 5 unmodified lines | ||
| 67 | 64 | fn paths_leading_through_symlinks_are_rejected() { |
| 68 | 65 | let mut stack = stack(); |
| 69 | 66 | assert_eq!( |
| 70 | − stack | |
| 71 | − .verified_path("root-dirlink/file-in-dir".as_ref()) | |
| 72 | − .unwrap_err() | |
| 73 | − .kind(), | |
| 67 | + stack.verified_path("root-dirlink/file-in-dir").unwrap_err().kind(), | |
| 74 | 68 | std::io::ErrorKind::Other, |
| 75 | 69 | "root-dirlink is a symlink to a directory" |
| 76 | 70 | ); |
| 77 | 71 | |
| 78 | 72 | assert_eq!( |
| 79 | − stack.verified_path("dir/dirlink/nothing".as_ref()).unwrap_err().kind(), | |
| 73 | + stack.verified_path("dir/dirlink/nothing").unwrap_err().kind(), | |
| 80 | 74 | std::io::ErrorKind::Other, |
| 81 | 75 | "root-dirlink is a symlink to a directory" |
| 82 | 76 | ); |
| @@ -80,7 +80,7 @@ where · 79 unmodified lines | ||
| 80 | 80 | let dest_relative = gix_path::try_from_bstr(entry_path).map_err(|_| crate::checkout::Error::IllformedUtf8 { |
| 81 | 81 | path: entry_path.to_owned(), |
| 82 | 82 | })?; |
| 83 | − let path_cache = path_cache.at_path(dest_relative, Some(entry.mode), &*objects)?; | |
| 83 | + let path_cache = path_cache.at_path(dest_relative.as_ref(), Some(entry.mode), &*objects)?; | |
| 84 | 84 | let dest = path_cache.path(); |
| 85 | 85 | |
| 86 | 86 | let object_size = match entry.mode { |
| @@ -9,7 +9,7 @@ description = "A crate of the gitoxide project for shared worktree related types · 8 unmodified lines | ||
| 9 | 9 | authors = ["Sebastian Thiel <sebastian.thiel@icloud.com>"] |
| 10 | 10 | edition = "2021" |
| 11 | 11 | include = ["src/**/*", "LICENSE-*"] |
| 12 | −rust-version = "1.70" | |
| 12 | +rust-version = "1.74" | |
| 13 | 13 | autotests = false |
| 14 | 14 | |
| 15 | 15 | [lib] |
| @@ -113,7 +113,7 @@ impl Stack { · 112 unmodified lines | ||
| 113 | 113 | /// Provide access to cached information for that `relative` path via the returned platform. |
| 114 | 114 | pub fn at_path( |
| 115 | 115 | &mut self, |
| 116 | − relative: impl AsRef<Path>, | |
| 116 | + relative: impl ToNormalPathComponents, | |
| 117 | 117 | mode: Option<gix_index::entry::Mode>, |
| 118 | 118 | objects: &dyn gix_object::Find, |
| 119 | 119 | ) -> std::io::Result<Platform<'_>> { |
| @@ -127,8 +127,7 @@ impl Stack { · 7 unmodified lines | ||
| 127 | 127 | case: self.case, |
| 128 | 128 | statistics: &mut self.statistics, |
| 129 | 129 | }; |
| 130 | − self.stack | |
| 131 | − .make_relative_path_current(relative.as_ref(), &mut delegate)?; | |
| 130 | + self.stack.make_relative_path_current(relative, &mut delegate)?; | |
| 132 | 131 | Ok(Platform { |
| 133 | 132 | parent: self, |
| 134 | 133 | is_dir: mode_is_dir(mode), |
| @@ -148,15 +147,8 @@ impl Stack { · 13 unmodified lines | ||
| 148 | 147 | objects: &dyn gix_object::Find, |
| 149 | 148 | ) -> std::io::Result<Platform<'_>> { |
| 150 | 149 | let relative = relative.into(); |
| 151 | − let relative_path = gix_path::try_from_bstr(relative).map_err(|_err| { | |
| 152 | − std::io::Error::new( | |
| 153 | − std::io::ErrorKind::Other, | |
| 154 | − format!("The path \"{relative}\" contained invalid UTF-8 and could not be turned into a path"), | |
| 155 | − ) | |
| 156 | − })?; | |
| 157 | − | |
| 158 | 150 | self.at_path( |
| 159 | − relative_path, | |
| 151 | + relative, | |
| 160 | 152 | mode.or_else(|| relative.ends_with_str("/").then_some(gix_index::entry::Mode::DIR)), |
| 161 | 153 | objects, |
| 162 | 154 | ) |
| @@ -210,6 +202,7 @@ impl Stack { · 47 unmodified lines | ||
| 210 | 202 | /// |
| 211 | 203 | pub mod delegate; |
| 212 | 204 | use delegate::StackDelegate; |
| 205 | +use gix_fs::stack::ToNormalPathComponents; | |
| 213 | 206 | |
| 214 | 207 | mod platform; |
| 215 | 208 | /// |
| @@ -92,7 +92,7 @@ fn symlinks_or_files_in_path_are_forbidden_or_unlinked_when_forced() -> crate::R · 91 unmodified lines | ||
| 92 | 92 | let relative_path = format!("{dirname}/file"); |
| 93 | 93 | assert_eq!( |
| 94 | 94 | cache |
| 95 | − .at_path(&relative_path, IS_FILE, &gix_object::find::Never) | |
| 95 | + .at_path(&*relative_path, IS_FILE, &gix_object::find::Never) | |
| 96 | 96 | .unwrap_err() |
| 97 | 97 | .kind(), |
| 98 | 98 | std::io::ErrorKind::AlreadyExists |
| @@ -112,7 +112,9 @@ fn symlinks_or_files_in_path_are_forbidden_or_unlinked_when_forced() -> crate::R · 13 unmodified lines | ||
| 112 | 112 | *unlink_on_collision = true; |
| 113 | 113 | } |
| 114 | 114 | let relative_path = format!("{dirname}/file"); |
| 115 | − let path = cache.at_path(&relative_path, IS_FILE, &gix_object::find::Never)?.path(); | |
| 115 | + let path = cache | |
| 116 | + .at_path(&*relative_path, IS_FILE, &gix_object::find::Never)? | |
| 117 | + .path(); | |
| 116 | 118 | assert!(path.parent().unwrap().is_dir(), "directory was forcefully created"); |
| 117 | 119 | assert!(!path.exists()); |
| 118 | 120 | } |
| @@ -1,10 +1,10 @@ | ||
| 1 | +use crate::{hex_to_id, worktree::stack::probe_case}; | |
| 1 | 2 | use bstr::{BStr, ByteSlice}; |
| 3 | +use gix_fs::stack::ToNormalPathComponents; | |
| 2 | 4 | use gix_index::entry::Mode; |
| 3 | 5 | use gix_worktree::{stack::state::ignore::Source, Stack}; |
| 4 | 6 | use std::fs::Metadata; |
| 5 | 7 | |
| 6 | −use crate::{hex_to_id, worktree::stack::probe_case}; | |
| 7 | − | |
| 8 | 8 | struct IgnoreExpectations<'a> { |
| 9 | 9 | lines: bstr::Lines<'a>, |
| 10 | 10 | } |
| @@ -170,7 +170,10 @@ fn check_against_baseline() -> crate::Result { · 159 unmodified lines | ||
| 170 | 170 | // OK: we provide negative patterns that matched on paths if there was no other match, while git doesn't. |
| 171 | 171 | } |
| 172 | 172 | (actual, expected) => { |
| 173 | − panic!("actual {actual:?} didn't match {expected:?} at '{relative_entry}'"); | |
| 173 | + panic!( | |
| 174 | + "actual {actual:?} didn't match {expected:?} at '{relative_entry}': {components:?}", | |
| 175 | + components = relative_entry.to_normal_path_components().collect::<Vec<_>>() | |
| 176 | + ); | |
| 174 | 177 | } |
| 175 | 178 | } |
| 176 | 179 | } |
| @@ -9,7 +9,7 @@ version = "0.70.0" · 8 unmodified lines | ||
| 9 | 9 | authors = ["Sebastian Thiel <sebastian.thiel@icloud.com>"] |
| 10 | 10 | edition = "2021" |
| 11 | 11 | include = ["src/**/*", "LICENSE-*"] |
| 12 | −rust-version = "1.70" | |
| 12 | +rust-version = "1.75" | |
| 13 | 13 | |
| 14 | 14 | [lib] |
| 15 | 15 | doctest = false |
| @@ -1,7 +1,7 @@ | ||
| 1 | +use crate::{types::AttributeStack, Repository}; | |
| 2 | +use gix_fs::stack::ToNormalPathComponents; | |
| 1 | 3 | use std::ops::{Deref, DerefMut}; |
| 2 | 4 | |
| 3 | −use crate::{bstr::BStr, types::AttributeStack, Repository}; | |
| 4 | − | |
| 5 | 5 | /// Lifecycle |
| 6 | 6 | impl<'repo> AttributeStack<'repo> { |
| 7 | 7 | /// Create a new instance from a `repo` and the underlying pre-configured `stack`. |
| @@ -44,18 +44,18 @@ impl AttributeStack<'_> { · 36 unmodified lines | ||
| 44 | 44 | relative: impl AsRef<std::path::Path>, |
| 45 | 45 | mode: Option<gix_index::entry::Mode>, |
| 46 | 46 | ) -> std::io::Result<gix_worktree::stack::Platform<'_>> { |
| 47 | − self.inner.at_path(relative, mode, &self.repo.objects) | |
| 47 | + self.inner.at_path(relative.as_ref(), mode, &self.repo.objects) | |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | /// Obtain a platform for attribute or ignore lookups from a repo-`relative` path, typically obtained from an index entry. |
| 51 | 51 | /// `mode` should reflect whether it's a directory or not, or left at `None` if unknown. |
| 52 | 52 | /// |
| 53 | 53 | /// If `relative` ends with `/` and `mode` is `None`, it is automatically assumed to be a directory. |
| 54 | − pub fn at_entry<'r>( | |
| 54 | + pub fn at_entry( | |
| 55 | 55 | &mut self, |
| 56 | − relative: impl Into<&'r BStr>, | |
| 56 | + relative: impl ToNormalPathComponents, | |
| 57 | 57 | mode: Option<gix_index::entry::Mode>, |
| 58 | 58 | ) -> std::io::Result<gix_worktree::stack::Platform<'_>> { |
| 59 | − self.inner.at_entry(relative, mode, &self.repo.objects) | |
| 59 | + self.inner.at_path(relative, mode, &self.repo.objects) | |
| 60 | 60 | } |
| 61 | 61 | } |
| @@ -64,7 +64,7 @@ impl<'repo> Id<'repo> { · 63 unmodified lines | ||
| 64 | 64 | |
| 65 | 65 | fn calculate_auto_hex_len(num_packed_objects: u64) -> usize { |
| 66 | 66 | let mut len = 64 - num_packed_objects.leading_zeros(); |
| 67 | − len = (len + 1) / 2; | |
| 67 | + len = len.div_ceil(2); | |
| 68 | 68 | len.max(7) as usize |
| 69 | 69 | } |
| 70 | 70 | |
| @@ -29,7 +29,7 @@ pub(super) mod function { · 28 unmodified lines | ||
| 29 | 29 | { |
| 30 | 30 | let rela_path = gix::path::from_bstr(rela_path); |
| 31 | 31 | let src = worktree_dir.join(&rela_path); |
| 32 | − stack.make_relative_path_current(&rela_path, &mut create_dir)?; | |
| 32 | + stack.make_relative_path_current(&*rela_path, &mut create_dir)?; | |
| 33 | 33 | let dst = stack.current(); |
| 34 | 34 | |
| 35 | 35 | eprintln!( |