1
- use std:: path:: { Component , Path , PathBuf } ;
1
+ use std:: path:: { Path , PathBuf } ;
2
2
3
3
use crate :: Stack ;
4
4
@@ -62,8 +62,14 @@ impl Stack {
62
62
/// `relative` paths are terminal, so point to their designated file or directory.
63
63
/// The path is also expected to be normalized, and should not contain extra separators, and must not contain `..`
64
64
/// 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 ( ) {
67
73
return Err ( std:: io:: Error :: new (
68
74
std:: io:: ErrorKind :: Other ,
69
75
"empty inputs are not allowed" ,
@@ -73,16 +79,21 @@ impl Stack {
73
79
delegate. push_directory ( self ) ?;
74
80
}
75
81
76
- let mut components = relative . components ( ) . peekable ( ) ;
82
+ let mut components = components. into_iter ( ) . peekable ( ) ;
77
83
let mut existing_components = self . current_relative . components ( ) ;
78
84
let mut matching_components = 0 ;
79
85
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
+ } ;
86
97
}
87
98
88
99
for _ in 0 ..self . valid_components - matching_components {
@@ -100,15 +111,12 @@ impl Stack {
100
111
}
101
112
102
113
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
+ } ;
112
120
let is_last_component = components. peek ( ) . is_none ( ) ;
113
121
self . current_is_directory = !is_last_component;
114
122
self . current . push ( comp) ;
0 commit comments