@@ -45,9 +45,9 @@ pub fn try_os_str_into_bstr(path: Cow<'_, OsStr>) -> Result<Cow<'_, BStr>, Utf8E
45
45
}
46
46
}
47
47
48
- /// Convert the given path either into its raw bytes on unix or its UTF8 encoded counterpart on windows .
48
+ /// Convert the given path either into its raw bytes on Unix or its UTF-8 encoded counterpart on Windows .
49
49
///
50
- /// On windows , if the source Path contains ill-formed, lone surrogates, the UTF-8 conversion will fail
50
+ /// On Windows , if the source ` Path`` contains ill-formed, lone surrogates, the UTF-8 conversion will fail
51
51
/// causing `Utf8Error` to be returned.
52
52
pub fn try_into_bstr < ' a > ( path : impl Into < Cow < ' a , Path > > ) -> Result < Cow < ' a , BStr > , Utf8Error > {
53
53
let path = path. into ( ) ;
@@ -86,7 +86,7 @@ pub fn try_into_bstr<'a>(path: impl Into<Cow<'a, Path>>) -> Result<Cow<'a, BStr>
86
86
Ok ( path_str)
87
87
}
88
88
89
- /// Similar to [`try_into_bstr()`] but **panics** if malformed surrogates are encountered on windows .
89
+ /// Similar to [`try_into_bstr()`] but **panics** if malformed surrogates are encountered on Windows .
90
90
pub fn into_bstr < ' a > ( path : impl Into < Cow < ' a , Path > > ) -> Cow < ' a , BStr > {
91
91
try_into_bstr ( path) . expect ( "prefix path doesn't contain ill-formed UTF-8" )
92
92
}
@@ -101,11 +101,11 @@ pub fn join_bstr_unix_pathsep<'a, 'b>(base: impl Into<Cow<'a, BStr>>, path: impl
101
101
base
102
102
}
103
103
104
- /// Given `input` bytes, produce a `Path` from them ignoring encoding entirely if on unix .
104
+ /// Given `input` bytes, produce a `Path` from them ignoring encoding entirely if on Unix .
105
105
///
106
- /// On windows , the input is required to be valid UTF-8, which is guaranteed if we wrote it before. There are some potential
107
- /// git versions and windows installation which produce mal-formed UTF-16 if certain emojies are in the path. It's as rare as
108
- /// it sounds, but possible.
106
+ /// On Windows , the input is required to be valid UTF-8, which is guaranteed if we wrote it before.
107
+ /// There are some potential Git versions and Windows installations which produce malformed UTF-16
108
+ /// if certain emojis are in the path. It's as rare as it sounds, but possible.
109
109
pub fn try_from_byte_slice ( input : & [ u8 ] ) -> Result < & Path , Utf8Error > {
110
110
#[ cfg( unix) ]
111
111
let p = {
@@ -131,7 +131,7 @@ pub fn try_from_bstr<'a>(input: impl Into<Cow<'a, BStr>>) -> Result<Cow<'a, Path
131
131
}
132
132
}
133
133
134
- /// Similar to [`try_from_bstr()`], but **panics** if malformed surrogates are encountered on windows .
134
+ /// Similar to [`try_from_bstr()`], but **panics** if malformed surrogates are encountered on Windows .
135
135
pub fn from_bstr < ' a > ( input : impl Into < Cow < ' a , BStr > > ) -> Cow < ' a , Path > {
136
136
try_from_bstr ( input) . expect ( "prefix path doesn't contain ill-formed UTF-8" )
137
137
}
@@ -205,7 +205,8 @@ pub fn to_native_separators<'a>(path: impl Into<Cow<'a, BStr>>) -> Cow<'a, BStr>
205
205
p
206
206
}
207
207
208
- /// Convert paths with slashes to backslashes on windows and do nothing on unix, but **panics** if malformed surrogates are encountered on windows.
208
+ /// Convert paths with slashes to backslashes on Windows and do nothing on Unix,
209
+ /// but **panic** if unpaired surrogates are encountered on Windows.
209
210
pub fn to_native_path_on_windows < ' a > ( path : impl Into < Cow < ' a , BStr > > ) -> Cow < ' a , std:: path:: Path > {
210
211
#[ cfg( not( windows) ) ]
211
212
{
@@ -217,47 +218,52 @@ pub fn to_native_path_on_windows<'a>(path: impl Into<Cow<'a, BStr>>) -> Cow<'a,
217
218
}
218
219
}
219
220
220
- /// Replaces windows path separators with slashes, but only do so on windows .
221
+ /// Replace Windows path separators with slashes, but only do so on Windows .
221
222
pub fn to_unix_separators_on_windows < ' a > ( path : impl Into < Cow < ' a , BStr > > ) -> Cow < ' a , BStr > {
222
223
#[ cfg( windows) ]
223
224
{
224
- replace ( path, b'\\' , b'/' )
225
+ to_unix_separators ( path)
225
226
}
226
227
#[ cfg( not( windows) ) ]
227
228
{
228
229
path. into ( )
229
230
}
230
231
}
231
232
232
- /// Replaces windows path separators with slashes, unconditionally.
233
+ /// Replace Windows path separators with slashes, which typically resembles a Unix path , unconditionally.
233
234
///
234
235
/// **Note** Do not use these and prefer the conditional versions of this method.
235
236
// TODO: use https://lib.rs/crates/path-slash to handle escapes
236
237
pub fn to_unix_separators < ' a > ( path : impl Into < Cow < ' a , BStr > > ) -> Cow < ' a , BStr > {
237
238
replace ( path, b'\\' , b'/' )
238
239
}
239
240
240
- /// Find backslashes and replace them with slashes, which typically resembles a unix path , unconditionally.
241
+ /// Find slashes and replace them with backslashes , unconditionally.
241
242
///
242
243
/// **Note** Do not use these and prefer the conditional versions of this method.
243
244
// TODO: use https://lib.rs/crates/path-slash to handle escapes
244
245
pub fn to_windows_separators < ' a > ( path : impl Into < Cow < ' a , BStr > > ) -> Cow < ' a , BStr > {
245
246
replace ( path, b'/' , b'\\' )
246
247
}
247
248
248
- /// Resolve relative components virtually without accessing the file system, e.g. turn `a/./b/c/.././..` into `a`,
249
- /// without keeping intermediate `..` and `/a/../b/..` becomes `/`.
250
- /// If the input path was relative and ends up being the `current_dir`, `.` is returned instead of the full path to `current_dir`.
251
- /// Note that single `.` components as well as duplicate separators are left untouched.
249
+ /// Resolve relative components virtually, eliminating intermediate `..` without accessing the filesystem.
252
250
///
253
- /// This is particularly useful when manipulating paths that are based on user input, and not resolving intermediate
254
- /// symlinks keeps the path similar to what the user provided. If that's not desirable, use `[realpath()][crate::realpath()`
255
- /// instead.
251
+ /// For example, this turns `a/./b/c/.././..` into `a`, and turns `/a/../b/..` into `/`.
256
252
///
257
- /// Note that we might access the `current_dir` if we run out of path components to pop off, which is expected to be absolute
258
- /// as typical return value of `std::env::current_dir()` or `gix_fs::current_dir(…)` when `core.precomposeUnicode` is known.
259
- /// As a `current_dir` like `/c` can be exhausted by paths like `../../r`, `None` will be returned to indicate the inability
260
- /// to produce a logically consistent path.
253
+ /// If the input path was relative and ends up being the `current_dir`, `.` is returned instead of
254
+ /// the full path to `current_dir`.
255
+ ///
256
+ /// Single `.` components as well as duplicate separators are left untouched.
257
+ ///
258
+ /// This is particularly useful when manipulating paths that are based on user input, and not
259
+ /// resolving intermediate symlinks keeps the path similar to what the user provided. If that's not
260
+ /// desirable, use `[realpath()][crate::realpath()` instead.
261
+ ///
262
+ /// Note that we might access the `current_dir` if we run out of path components to pop off, which
263
+ /// is expected to be absolute as typical return value of `std::env::current_dir()` or
264
+ /// `gix_fs::current_dir(…)` when `core.precomposeUnicode` is known. As a `current_dir` like `/c`
265
+ /// can be exhausted by paths like `../../r`, `None` will be returned to indicate the inability to
266
+ /// produce a logically consistent path.
261
267
pub fn normalize < ' a > ( path : Cow < ' a , Path > , current_dir : & Path ) -> Option < Cow < ' a , Path > > {
262
268
use std:: path:: Component :: ParentDir ;
263
269
@@ -290,14 +296,16 @@ pub fn normalize<'a>(path: Cow<'a, Path>, current_dir: &Path) -> Option<Cow<'a,
290
296
. into ( )
291
297
}
292
298
293
- /// Rebuild the worktree-relative `relative_path` to be relative to `prefix`, which is the worktree-relative
294
- /// path equivalent to the position of the user, or current working directory.
299
+ /// Rebuild the worktree-relative `relative_path` to be relative to `prefix`, which is the
300
+ /// worktree-relative path equivalent to the position of the user, or current working directory.
301
+ ///
295
302
/// This is a no-op if `prefix` is empty.
296
303
///
297
- /// Note that both `relative_path` and `prefix` are assumed to be [normalized](normalize()), and failure to do so
298
- /// will lead to incorrect results.
304
+ /// Note that both `relative_path` and `prefix` are assumed to be [normalized](normalize()), and
305
+ /// failure to do so will lead to incorrect results.
299
306
///
300
- /// Note that both input paths are expected to be equal in terms of case too, as comparisons will be case-sensitive.
307
+ /// Note that both input paths are expected to be equal in terms of case too, as comparisons will
308
+ /// be case-sensitive.
301
309
pub fn relativize_with_prefix < ' a > ( relative_path : & ' a Path , prefix : & Path ) -> Cow < ' a , Path > {
302
310
if prefix. as_os_str ( ) . is_empty ( ) {
303
311
return Cow :: Borrowed ( relative_path) ;
0 commit comments