@@ -21,15 +21,20 @@ pub struct Match<'a> {
2121 pub sequence_number : usize ,
2222}
2323
24- /// An implementation of the [`Pattern`] trait for ignore patterns.
25- #[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone , Default ) ]
26- pub struct Ignore ;
24+ /// An implementation of the [`Pattern`] trait for ignore-patterns.
25+ #[ derive( Default , PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone , Copy ) ]
26+ pub struct Ignore {
27+ /// If `support_precious` is `true`, we will parse `$` prefixed entries as precious.
28+ /// This is backward-incompatible as files that actually start with `$` like `$houdini`
29+ /// will then not be ignored anymore, instead it ignores `houdini`.
30+ pub support_precious : bool ,
31+ }
2732
2833impl Pattern for Ignore {
2934 type Value = crate :: Kind ;
3035
31- fn bytes_to_patterns ( bytes : & [ u8 ] , _source : & std:: path:: Path ) -> Vec < pattern:: Mapping < Self :: Value > > {
32- crate :: parse ( bytes)
36+ fn bytes_to_patterns ( & self , bytes : & [ u8 ] , _source : & std:: path:: Path ) -> Vec < pattern:: Mapping < Self :: Value > > {
37+ crate :: parse ( bytes, self . support_precious )
3338 . map ( |( pattern, line_number, kind) | pattern:: Mapping {
3439 pattern,
3540 value : kind,
@@ -44,38 +49,48 @@ impl Search {
4449 /// Given `git_dir`, a `.git` repository, load static ignore patterns from `info/exclude`
4550 /// and from `excludes_file` if it is provided.
4651 /// Note that it's not considered an error if the provided `excludes_file` does not exist.
47- pub fn from_git_dir ( git_dir : & Path , excludes_file : Option < PathBuf > , buf : & mut Vec < u8 > ) -> std:: io:: Result < Self > {
52+ /// `parse` is a way to parse bytes to ignore patterns.
53+ pub fn from_git_dir (
54+ git_dir : & Path ,
55+ excludes_file : Option < PathBuf > ,
56+ buf : & mut Vec < u8 > ,
57+ parse : Ignore ,
58+ ) -> std:: io:: Result < Self > {
4859 let mut group = Self :: default ( ) ;
4960
5061 let follow_symlinks = true ;
5162 // order matters! More important ones first.
5263 group. patterns . extend (
5364 excludes_file
54- . and_then ( |file| pattern:: List :: < Ignore > :: from_file ( file, None , follow_symlinks, buf) . transpose ( ) )
65+ . and_then ( |file| {
66+ pattern:: List :: < Ignore > :: from_file ( file, None , follow_symlinks, buf, parse) . transpose ( )
67+ } )
5568 . transpose ( ) ?,
5669 ) ;
5770 group. patterns . extend ( pattern:: List :: < Ignore > :: from_file (
5871 git_dir. join ( "info" ) . join ( "exclude" ) ,
5972 None ,
6073 follow_symlinks,
6174 buf,
75+ parse,
6276 ) ?) ;
6377 Ok ( group)
6478 }
6579
6680 /// Parse a list of ignore patterns, using slashes as path separators.
67- pub fn from_overrides ( patterns : impl IntoIterator < Item = impl Into < OsString > > ) -> Self {
68- Self :: from_overrides_inner ( & mut patterns. into_iter ( ) . map ( Into :: into) )
81+ /// `parse` is a way to parse bytes to ignore patterns.
82+ pub fn from_overrides ( patterns : impl IntoIterator < Item = impl Into < OsString > > , parse : Ignore ) -> Self {
83+ Self :: from_overrides_inner ( & mut patterns. into_iter ( ) . map ( Into :: into) , parse)
6984 }
7085
71- fn from_overrides_inner ( patterns : & mut dyn Iterator < Item = OsString > ) -> Self {
86+ fn from_overrides_inner ( patterns : & mut dyn Iterator < Item = OsString > , parse : Ignore ) -> Self {
7287 Search {
7388 patterns : vec ! [ pattern:: List {
7489 patterns: patterns
7590 . enumerate( )
7691 . filter_map( |( seq_id, pattern) | {
7792 let pattern = gix_path:: try_into_bstr( PathBuf :: from( pattern) ) . ok( ) ?;
78- crate :: parse( pattern. as_ref( ) )
93+ crate :: parse( pattern. as_ref( ) , parse . support_precious )
7994 . next( )
8095 . map( |( p, _seq_id, kind) | pattern:: Mapping {
8196 pattern: p,
@@ -95,9 +110,16 @@ impl Search {
95110impl Search {
96111 /// Add patterns as parsed from `bytes`, providing their `source` path and possibly their `root` path, the path they
97112 /// are relative to. This also means that `source` is contained within `root` if `root` is provided.
98- pub fn add_patterns_buffer ( & mut self , bytes : & [ u8 ] , source : impl Into < PathBuf > , root : Option < & Path > ) {
113+ /// Use `parse` to control how ignore patterns are parsed.
114+ pub fn add_patterns_buffer (
115+ & mut self ,
116+ bytes : & [ u8 ] ,
117+ source : impl Into < PathBuf > ,
118+ root : Option < & Path > ,
119+ parse : Ignore ,
120+ ) {
99121 self . patterns
100- . push ( pattern:: List :: from_bytes ( bytes, source. into ( ) , root) ) ;
122+ . push ( pattern:: List :: from_bytes ( bytes, source. into ( ) , root, parse ) ) ;
101123 }
102124}
103125
0 commit comments