1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
//! Pathspec plumbing and abstractions
use gix_macros::momo;
pub use gix_pathspec::*;
use crate::{bstr::BStr, AttributeStack, Pathspec, PathspecDetached, Repository};
///
pub mod init {
/// The error returned by [`Pathspec::new()`](super::Pathspec::new()).
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error(transparent)]
MakeAttributes(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
#[error(transparent)]
Defaults(#[from] crate::repository::pathspec_defaults_ignore_case::Error),
#[error(transparent)]
ParseSpec(#[from] gix_pathspec::parse::Error),
#[error(
"Could not obtain the repository prefix as the relative path of the CWD as seen from the working tree"
)]
NormalizeSpec(#[from] gix_pathspec::normalize::Error),
#[error(transparent)]
RepoPrefix(#[from] gix_path::realpath::Error),
}
}
/// Lifecycle
impl<'repo> Pathspec<'repo> {
/// Create a new instance by parsing `patterns` into [`Pathspecs`](Pattern) to make them usable for searches.
/// `make_attribute` may be called if one of the patterns has a `(attr:a)` element which requires attribute matching. It should
/// be used to control where attributes are coming from.
/// If `inherit_ignore_case` is `true`, the pathspecs may have their ignore-case default overridden to be case-insensitive by default.
/// This only works towards turning ignore-case for pathspecs on, but won't ever turn that setting off if.
///
/// ### Deviation
///
/// Pathspecs can declare to be case-insensitive as part of their elements, which is a setting that is now respected for attribute
/// queries as well.
pub fn new(
repo: &'repo Repository,
patterns: impl IntoIterator<Item = impl AsRef<BStr>>,
inherit_ignore_case: bool,
make_attributes: impl FnOnce() -> Result<gix_worktree::Stack, Box<dyn std::error::Error + Send + Sync + 'static>>,
) -> Result<Self, init::Error> {
let defaults = repo.pathspec_defaults_inherit_ignore_case(inherit_ignore_case)?;
let patterns = patterns
.into_iter()
.map(move |p| parse(p.as_ref(), defaults))
.collect::<Result<Vec<_>, _>>()?;
let needs_cache = patterns.iter().any(|p| !p.attributes.is_empty());
let search = Search::from_specs(
patterns,
repo.prefix()?,
&gix_path::realpath_opts(
repo.work_dir().unwrap_or_else(|| repo.git_dir()),
repo.options.current_dir_or_empty(),
gix_path::realpath::MAX_SYMLINKS,
)?,
)?;
let cache = needs_cache.then(make_attributes).transpose()?;
Ok(Self {
repo,
search,
stack: cache,
})
}
/// Turn ourselves into the functional parts for direct usage.
/// Note that the [`cache`](AttributeStack) is only set if one of the [`search` patterns](Search)
/// is specifying attributes to match for.
pub fn into_parts(self) -> (Search, Option<AttributeStack<'repo>>) {
(
self.search,
self.stack.map(|stack| AttributeStack::new(stack, self.repo)),
)
}
/// Turn ourselves into an implementation that works without a repository instance and that is rather minimal.
pub fn detach(self) -> std::io::Result<PathspecDetached> {
Ok(PathspecDetached {
search: self.search,
stack: self.stack,
odb: self.repo.objects.clone().into_arc()?,
})
}
}
/// Access
impl<'repo> Pathspec<'repo> {
/// Return the attributes cache which is used when matching attributes in pathspecs, or `None` if none of the pathspecs require that.
pub fn attributes(&self) -> Option<&gix_worktree::Stack> {
self.stack.as_ref()
}
/// Return the search itself which can be used for matching paths or accessing the actual patterns that will be used.
pub fn search(&self) -> &gix_pathspec::Search {
&self.search
}
/// Return the first [`Match`](search::Match) of `relative_path`, or `None`.
/// Note that the match might [be excluded](search::Match::is_excluded()).
/// `is_dir` is true if `relative_path` is a directory.
#[doc(
alias = "match_diff",
alias = "match_tree",
alias = "match_index",
alias = "match_workdir",
alias = "matches_path",
alias = "git2"
)]
#[momo]
pub fn pattern_matching_relative_path<'a>(
&mut self,
relative_path: impl Into<&'a BStr>,
is_dir: Option<bool>,
) -> Option<gix_pathspec::search::Match<'_>> {
self.search.pattern_matching_relative_path(
relative_path.into(),
is_dir,
&mut |relative_path, case, is_dir, out| {
let stack = self.stack.as_mut().expect("initialized in advance");
stack
.set_case(case)
.at_entry(relative_path, Some(is_dir), &self.repo.objects)
.map_or(false, |platform| platform.matching_attributes(out))
},
)
}
/// The simplified version of [`pattern_matching_relative_path()`](Self::pattern_matching_relative_path()) which returns
/// `true` if `relative_path` is included in the set of positive pathspecs, while not being excluded.
#[momo]
pub fn is_included<'a>(&mut self, relative_path: impl Into<&'a BStr>, is_dir: Option<bool>) -> bool {
self.pattern_matching_relative_path(relative_path, is_dir)
.map_or(false, |m| !m.is_excluded())
}
/// Return an iterator over all entries along with their path if the path matches the pathspec, or `None` if the pathspec is
/// known to match no entry.
// TODO: tests
pub fn index_entries_with_paths<'s: 'repo, 'a: 'repo>(
&'s mut self,
index: &'a gix_index::State,
) -> Option<impl Iterator<Item = (&'a BStr, &'a gix_index::Entry)> + 'repo + 's> {
index.prefixed_entries(self.search.common_prefix()).map(|entries| {
entries.iter().filter_map(move |entry| {
let path = entry.path(index);
self.is_included(path, Some(false)).then_some((path, entry))
})
})
}
}
/// Access
impl PathspecDetached {
/// Return the first [`Match`](search::Match) of `relative_path`, or `None`.
/// Note that the match might [be excluded](search::Match::is_excluded()).
/// `is_dir` is true if `relative_path` is a directory.
#[doc(
alias = "match_diff",
alias = "match_tree",
alias = "match_index",
alias = "match_workdir",
alias = "matches_path",
alias = "git2"
)]
#[momo]
pub fn pattern_matching_relative_path<'a>(
&mut self,
relative_path: impl Into<&'a BStr>,
is_dir: Option<bool>,
) -> Option<gix_pathspec::search::Match<'_>> {
self.search.pattern_matching_relative_path(
relative_path.into(),
is_dir,
&mut |relative_path, case, is_dir, out| {
let stack = self.stack.as_mut().expect("initialized in advance");
stack
.set_case(case)
.at_entry(relative_path, Some(is_dir), &self.odb)
.map_or(false, |platform| platform.matching_attributes(out))
},
)
}
/// The simplified version of [`pattern_matching_relative_path()`](Self::pattern_matching_relative_path()) which returns
/// `true` if `relative_path` is included in the set of positive pathspecs, while not being excluded.
#[momo]
pub fn is_included<'a>(&mut self, relative_path: impl Into<&'a BStr>, is_dir: Option<bool>) -> bool {
self.pattern_matching_relative_path(relative_path, is_dir)
.map_or(false, |m| !m.is_excluded())
}
}
#[cfg(feature = "status")]
impl gix_status::Pathspec for PathspecDetached {
fn common_prefix(&self) -> &BStr {
self.search.common_prefix()
}
fn is_included(&mut self, relative_path: &BStr, is_dir: Option<bool>) -> bool {
self.is_included(relative_path, is_dir)
}
}
|