@@ -3,7 +3,7 @@ use gix_macros::momo;
3
3
use gix_odb:: FindExt ;
4
4
pub use gix_pathspec:: * ;
5
5
6
- use crate :: { bstr:: BStr , AttributeStack , Pathspec , Repository } ;
6
+ use crate :: { bstr:: BStr , AttributeStack , Pathspec , PathspecDetached , Repository } ;
7
7
8
8
///
9
9
pub mod init {
@@ -75,6 +75,15 @@ impl<'repo> Pathspec<'repo> {
75
75
self . stack . map ( |stack| AttributeStack :: new ( stack, self . repo ) ) ,
76
76
)
77
77
}
78
+
79
+ /// Turn ourselves into an implementation that works without a repository instance and that is rather minimal.
80
+ pub fn detach ( self ) -> std:: io:: Result < PathspecDetached > {
81
+ Ok ( PathspecDetached {
82
+ search : self . search ,
83
+ stack : self . stack ,
84
+ odb : self . repo . objects . clone ( ) . into_arc ( ) ?,
85
+ } )
86
+ }
78
87
}
79
88
80
89
/// Access
@@ -145,8 +154,49 @@ impl<'repo> Pathspec<'repo> {
145
154
}
146
155
}
147
156
157
+ /// Access
158
+ impl PathspecDetached {
159
+ /// Return the first [`Match`](search::Match) of `relative_path`, or `None`.
160
+ /// Note that the match might [be excluded](search::Match::is_excluded()).
161
+ /// `is_dir` is true if `relative_path` is a directory.
162
+ #[ doc(
163
+ alias = "match_diff" ,
164
+ alias = "match_tree" ,
165
+ alias = "match_index" ,
166
+ alias = "match_workdir" ,
167
+ alias = "matches_path" ,
168
+ alias = "git2"
169
+ ) ]
170
+ #[ momo]
171
+ pub fn pattern_matching_relative_path < ' a > (
172
+ & mut self ,
173
+ relative_path : impl Into < & ' a BStr > ,
174
+ is_dir : Option < bool > ,
175
+ ) -> Option < gix_pathspec:: search:: Match < ' _ > > {
176
+ self . search . pattern_matching_relative_path (
177
+ relative_path. into ( ) ,
178
+ is_dir,
179
+ & mut |relative_path, case, is_dir, out| {
180
+ let stack = self . stack . as_mut ( ) . expect ( "initialized in advance" ) ;
181
+ stack
182
+ . set_case ( case)
183
+ . at_entry ( relative_path, Some ( is_dir) , |id, buf| self . odb . find_blob ( id, buf) )
184
+ . map_or ( false , |platform| platform. matching_attributes ( out) )
185
+ } ,
186
+ )
187
+ }
188
+
189
+ /// The simplified version of [`pattern_matching_relative_path()`](Self::pattern_matching_relative_path()) which returns
190
+ /// `true` if `relative_path` is included in the set of positive pathspecs, while not being excluded.
191
+ #[ momo]
192
+ pub fn is_included < ' a > ( & mut self , relative_path : impl Into < & ' a BStr > , is_dir : Option < bool > ) -> bool {
193
+ self . pattern_matching_relative_path ( relative_path, is_dir)
194
+ . map_or ( false , |m| !m. is_excluded ( ) )
195
+ }
196
+ }
197
+
148
198
#[ cfg( feature = "status" ) ]
149
- impl gix_status:: Pathspec for Pathspec < ' _ > {
199
+ impl gix_status:: Pathspec for PathspecDetached {
150
200
fn common_prefix ( & self ) -> & BStr {
151
201
self . search . common_prefix ( )
152
202
}
0 commit comments