1
- use std:: { borrow:: Cow , env, path:: PathBuf } ;
2
-
3
- use bstr:: { ByteSlice , ByteVec } ;
1
+ use std:: ffi:: OsStr ;
2
+ use std:: { env, path:: PathBuf } ;
4
3
5
4
/// The error returned by [git_discover::upwards()][crate::upwards()].
6
5
#[ derive( Debug , thiserror:: Error ) ]
@@ -81,7 +80,7 @@ impl Options<'_> {
81
80
// TODO: test
82
81
pub fn apply_environment ( mut self ) -> Self {
83
82
let name = "GIT_CEILING_DIRECTORIES" ;
84
- if let Some ( ceiling_dirs) = env:: var_os ( name) . and_then ( |c| Vec :: from_os_string ( c ) . ok ( ) ) {
83
+ if let Some ( ceiling_dirs) = env:: var_os ( name) {
85
84
self . ceiling_dirs = parse_ceiling_dirs ( & ceiling_dirs) ;
86
85
}
87
86
self
@@ -92,35 +91,29 @@ impl Options<'_> {
92
91
/// On Windows, paths are separated by `;`.
93
92
/// Non-absolute paths are discarded.
94
93
/// To match git, all paths are normalized, until an empty path is encountered.
95
- pub ( crate ) fn parse_ceiling_dirs ( ceiling_dirs : & [ u8 ] ) -> Vec < PathBuf > {
94
+ pub ( crate ) fn parse_ceiling_dirs ( ceiling_dirs : & OsStr ) -> Vec < PathBuf > {
96
95
let mut should_normalize = true ;
97
- let mut result = Vec :: new ( ) ;
98
- let path_separator = if cfg ! ( windows) { ";" } else { ":" } ;
99
- for ceiling_dir in ceiling_dirs. split_str ( path_separator) {
100
- if ceiling_dir. is_empty ( ) {
96
+ let mut out = Vec :: new ( ) ;
97
+ for ceiling_dir in std:: env:: split_paths ( ceiling_dirs) {
98
+ if ceiling_dir. as_os_str ( ) . is_empty ( ) {
101
99
should_normalize = false ;
102
100
continue ;
103
101
}
104
102
105
- // Paths that are invalid unicode can't be handled
106
- let mut dir = match ceiling_dir. to_path ( ) {
107
- Ok ( dir) => Cow :: Borrowed ( dir) ,
108
- Err ( _) => continue ,
109
- } ;
110
-
111
103
// Only absolute paths are allowed
112
- if dir . is_relative ( ) {
104
+ if ceiling_dir . is_relative ( ) {
113
105
continue ;
114
106
}
115
107
108
+ let mut dir = ceiling_dir;
116
109
if should_normalize {
117
110
if let Ok ( normalized) = git_path:: realpath ( & dir) {
118
- dir = Cow :: Owned ( normalized) ;
111
+ dir = normalized;
119
112
}
120
113
}
121
- result . push ( dir. into_owned ( ) ) ;
114
+ out . push ( dir) ;
122
115
}
123
- result
116
+ out
124
117
}
125
118
126
119
#[ cfg( test) ]
@@ -143,7 +136,7 @@ mod tests {
143
136
// Parse & build ceiling dirs string
144
137
let symlink_str = symlink_path. to_str ( ) . expect ( "symlink path is valid utf8" ) ;
145
138
let ceiling_dir_string = format ! ( "{}:relative::{}" , symlink_str, symlink_str) ;
146
- let ceiling_dirs = parse_ceiling_dirs ( ceiling_dir_string. as_bytes ( ) ) ;
139
+ let ceiling_dirs = parse_ceiling_dirs ( OsStr :: new ( ceiling_dir_string. as_str ( ) ) ) ;
147
140
148
141
assert_eq ! ( ceiling_dirs. len( ) , 2 , "Relative path is discarded" ) ;
149
142
assert_eq ! (
@@ -176,7 +169,7 @@ mod tests {
176
169
// Parse & build ceiling dirs string
177
170
let symlink_str = symlink_path. to_str ( ) . expect ( "symlink path is valid utf8" ) ;
178
171
let ceiling_dir_string = format ! ( "{};relative;;{}" , symlink_str, symlink_str) ;
179
- let ceiling_dirs = parse_ceiling_dirs ( ceiling_dir_string. as_bytes ( ) ) ;
172
+ let ceiling_dirs = parse_ceiling_dirs ( OsStr :: new ( ceiling_dir_string. as_str ( ) ) ) ;
180
173
181
174
assert_eq ! ( ceiling_dirs. len( ) , 2 , "Relative path is discarded" ) ;
182
175
assert_eq ! ( ceiling_dirs[ 0 ] , direct_path, "Symlinks are resolved" ) ;
0 commit comments