1
- //! Parse `.gitattribute` and `.gitignore` files and provide utilities to match against them.
1
+ //! Parse `.gitattribute` files and provide utilities to match against them.
2
2
//!
3
3
//! ## Feature Flags
4
4
#![ cfg_attr(
5
5
feature = "document-features" ,
6
6
cfg_attr( doc, doc = :: document_features:: document_features!( ) )
7
7
) ]
8
8
#![ cfg_attr( docsrs, feature( doc_cfg, doc_auto_cfg) ) ]
9
- #![ deny( missing_docs, rust_2018_idioms) ]
10
- #![ forbid( unsafe_code) ]
9
+ #![ deny( missing_docs, rust_2018_idioms, unsafe_code) ]
11
10
12
- use std:: path:: PathBuf ;
13
-
14
- use bstr:: { BStr , BString } ;
15
11
pub use gix_glob as glob;
12
+ use kstring:: { KString , KStringRef } ;
16
13
17
14
mod assignment;
18
15
///
19
16
pub mod name;
20
- mod state;
17
+ ///
18
+ pub mod state;
21
19
22
- mod match_group ;
23
- pub use match_group :: { Attributes , Ignore , Match , Pattern } ;
20
+ ///
21
+ pub mod search ;
24
22
25
23
///
26
24
pub mod parse;
27
- /// Parse attribute assignments line by line from `bytes`.
25
+
26
+ /// Parse attribute assignments line by line from `bytes`, and fail the operation on error.
27
+ ///
28
+ /// For leniency, ignore errors using `filter_map(Result::ok)` for example.
28
29
pub fn parse ( bytes : & [ u8 ] ) -> parse:: Lines < ' _ > {
29
30
parse:: Lines :: new ( bytes)
30
31
}
@@ -42,7 +43,7 @@ pub enum StateRef<'a> {
42
43
/// The attribute is set to the given value, which followed the `=` sign.
43
44
/// Note that values can be empty.
44
45
#[ cfg_attr( feature = "serde1" , serde( borrow) ) ]
45
- Value ( & ' a BStr ) ,
46
+ Value ( state :: ValueRef < ' a > ) ,
46
47
/// The attribute isn't mentioned with a given path or is explicitly set to `Unspecified` using the `!` sign.
47
48
Unspecified ,
48
49
}
@@ -59,19 +60,19 @@ pub enum State {
59
60
Unset ,
60
61
/// The attribute is set to the given value, which followed the `=` sign.
61
62
/// Note that values can be empty.
62
- Value ( BString ) , // TODO(performance): Is there a non-utf8 compact_str/KBString crate? See https://github.com/cobalt-org/kstring/issues/37#issuecomment-1446777265 .
63
+ Value ( state :: Value ) ,
63
64
/// The attribute isn't mentioned with a given path or is explicitly set to `Unspecified` using the `!` sign.
64
65
Unspecified ,
65
66
}
66
67
67
68
/// Represents a validated attribute name
68
69
#[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone ) ]
69
70
#[ cfg_attr( feature = "serde1" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
70
- pub struct Name ( pub ( crate ) String ) ; // TODO(performance): See if `KBString` or `compact_string` could be meaningful here.
71
+ pub struct Name ( pub ( crate ) KString ) ;
71
72
72
73
/// Holds a validated attribute name as a reference
73
- #[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd ) ]
74
- pub struct NameRef < ' a > ( & ' a str ) ;
74
+ #[ derive( Copy , Clone , PartialEq , Eq , Debug , Hash , Ord , PartialOrd ) ]
75
+ pub struct NameRef < ' a > ( KStringRef < ' a > ) ;
75
76
76
77
/// Name an attribute and describe it's assigned state.
77
78
#[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone ) ]
@@ -84,54 +85,42 @@ pub struct Assignment {
84
85
}
85
86
86
87
/// Holds validated attribute data as a reference
87
- #[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd ) ]
88
+ #[ derive( Copy , Clone , PartialEq , Eq , Debug , Hash , Ord , PartialOrd ) ]
88
89
pub struct AssignmentRef < ' a > {
89
90
/// The name of the attribute.
90
91
pub name : NameRef < ' a > ,
91
92
/// The state of the attribute.
92
93
pub state : StateRef < ' a > ,
93
94
}
94
95
95
- /// A grouping of lists of patterns while possibly keeping associated to their base path.
96
+ /// A grouping of lists of patterns while possibly keeping associated to their base path in order to find matches .
96
97
///
97
98
/// Pattern lists with base path are queryable relative to that base, otherwise they are relative to the repository root.
98
99
#[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone , Default ) ]
99
- pub struct MatchGroup < T : Pattern = Attributes > {
100
+ pub struct Search {
100
101
/// A list of pattern lists, each representing a patterns from a file or specified by hand, in the order they were
101
102
/// specified in.
102
103
///
103
- /// During matching, this order is reversed.
104
- pub patterns : Vec < PatternList < T > > ,
104
+ /// When matching, this order is reversed.
105
+ patterns : Vec < gix_glob :: search :: pattern :: List < search :: Attributes > > ,
105
106
}
106
107
107
- /// A list of patterns which optionally know where they were loaded from and what their base is .
108
+ /// A list of known global sources for git attribute files in order of ascending precedence .
108
109
///
109
- /// Knowing their base which is relative to a source directory, it will ignore all path to match against
110
- /// that don't also start with said base.
111
- #[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone , Default ) ]
112
- pub struct PatternList < T : Pattern > {
113
- /// Patterns and their associated data in the order they were loaded in or specified,
114
- /// the line number in its source file or its sequence number (_`(pattern, value, line_number)`_).
110
+ /// This means that values from the first variant will be returned first.
111
+ #[ derive( Clone , Copy , Debug , Eq , PartialEq , Hash , Ord , PartialOrd ) ]
112
+ pub enum Source {
113
+ /// The attribute file that the installation itself ships with.
114
+ GitInstallation ,
115
+ /// System-wide attributes file. This is typically defined as
116
+ /// `$(prefix)/etc/gitattributes` (where prefix is the git-installation directory).
117
+ System ,
118
+ /// This is `<xdg-config-home>/git/attributes` and is git application configuration per user.
115
119
///
116
- /// During matching, this order is reversed.
117
- pub patterns : Vec < PatternMapping < T :: Value > > ,
118
-
119
- /// The path from which the patterns were read, or `None` if the patterns
120
- /// don't originate in a file on disk.
121
- pub source : Option < PathBuf > ,
122
-
123
- /// The parent directory of source, or `None` if the patterns are _global_ to match against the repository root.
124
- /// It's processed to contain slashes only and to end with a trailing slash, and is relative to the repository root.
125
- pub base : Option < BString > ,
120
+ /// Note that there is no `~/.gitattributes` file.
121
+ Git ,
122
+ /// The configuration of the repository itself, located in `$GIT_DIR/info/attributes`.
123
+ Local ,
126
124
}
127
125
128
- /// An association of a pattern with its value, along with a sequence number providing a sort order in relation to its peers.
129
- #[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone ) ]
130
- pub struct PatternMapping < T > {
131
- /// The pattern itself, like `/target/*`
132
- pub pattern : gix_glob:: Pattern ,
133
- /// The value associated with the pattern.
134
- pub value : T ,
135
- /// Typically the line number in the file the pattern was parsed from.
136
- pub sequence_number : usize ,
137
- }
126
+ mod source;
0 commit comments