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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use std::path::Path;
use bstr::{BStr, BString, ByteSlice};
use git_glob::pattern::Case;
use git_hash::oid;
use crate::fs::{cache::State, PathOidMapping};
type AttributeMatchGroup = git_attributes::MatchGroup<git_attributes::Attributes>;
type IgnoreMatchGroup = git_attributes::MatchGroup<git_attributes::Ignore>;
#[derive(Default, Clone)]
#[allow(unused)]
pub struct Attributes {
pub stack: AttributeMatchGroup,
pub globals: AttributeMatchGroup,
}
#[derive(Default, Clone)]
#[allow(unused)]
pub struct Ignore {
overrides: IgnoreMatchGroup,
stack: IgnoreMatchGroup,
globals: IgnoreMatchGroup,
matched_directory_patterns_stack: Vec<Option<(usize, usize, usize)>>,
exclude_file_name_for_directories: BString,
case: Case,
}
impl Ignore {
pub fn new(
overrides: IgnoreMatchGroup,
globals: IgnoreMatchGroup,
exclude_file_name_for_directories: Option<&BStr>,
case: Case,
) -> Self {
Ignore {
case,
overrides,
globals,
stack: Default::default(),
matched_directory_patterns_stack: Vec::with_capacity(6),
exclude_file_name_for_directories: exclude_file_name_for_directories
.map(ToOwned::to_owned)
.unwrap_or_else(|| ".gitignore".into()),
}
}
}
impl Ignore {
pub(crate) fn pop_directory(&mut self) {
self.matched_directory_patterns_stack.pop().expect("something to pop");
self.stack.patterns.pop().expect("something to pop");
}
pub(crate) fn match_groups(&self) -> [&IgnoreMatchGroup; 3] {
[&self.globals, &self.stack, &self.overrides]
}
pub(crate) fn matching_exclude_pattern(
&self,
relative_path: &BStr,
is_dir: Option<bool>,
case: Case,
) -> Option<git_attributes::Match<'_, ()>> {
let groups = self.match_groups();
let mut dir_match = None;
if let Some((source, mapping)) = self
.matched_directory_patterns_stack
.iter()
.rev()
.filter_map(|v| *v)
.map(|(gidx, plidx, pidx)| {
let list = &groups[gidx].patterns[plidx];
(list.source.as_deref(), &list.patterns[pidx])
})
.next()
{
let match_ = git_attributes::Match {
pattern: &mapping.pattern,
value: &mapping.value,
sequence_number: mapping.sequence_number,
source,
};
if mapping.pattern.is_negative() {
dir_match = Some(match_);
} else {
return match_.into();
}
}
groups
.iter()
.rev()
.find_map(|group| group.pattern_matching_relative_path(relative_path.as_ref(), is_dir, case))
.or(dir_match)
}
pub(crate) fn matching_exclude_pattern_no_dir(
&self,
relative_path: &BStr,
is_dir: Option<bool>,
case: Case,
) -> Option<(usize, usize, usize)> {
let groups = self.match_groups();
groups.iter().enumerate().rev().find_map(|(gidx, group)| {
let basename_pos = relative_path.rfind(b"/").map(|p| p + 1);
group
.patterns
.iter()
.enumerate()
.rev()
.find_map(|(plidx, pl)| {
pl.pattern_idx_matching_relative_path(relative_path, basename_pos, is_dir, case)
.map(|idx| (plidx, idx))
})
.map(|(plidx, pidx)| (gidx, plidx, pidx))
})
}
pub(crate) fn push_directory<Find, E>(
&mut self,
root: &Path,
dir: &Path,
buf: &mut Vec<u8>,
attribute_files_in_index: &[PathOidMapping],
mut find: Find,
) -> std::io::Result<()>
where
Find: for<'b> FnMut(&oid, &'b mut Vec<u8>) -> Result<git_object::BlobRef<'b>, E>,
E: std::error::Error + Send + Sync + 'static,
{
let rela_dir = dir.strip_prefix(root).expect("dir in root");
self.matched_directory_patterns_stack
.push(self.matching_exclude_pattern_no_dir(git_path::into_bstr(rela_dir).as_ref(), Some(true), self.case));
let ignore_path_relative = rela_dir.join(".gitignore");
let ignore_path_relative = git_path::to_unix_separators_on_windows(git_path::into_bstr(ignore_path_relative));
let ignore_file_in_index =
attribute_files_in_index.binary_search_by(|t| t.0.as_bstr().cmp(ignore_path_relative.as_ref()));
let follow_symlinks = ignore_file_in_index.is_err();
if !self
.stack
.add_patterns_file(dir.join(".gitignore"), follow_symlinks, Some(root), buf)?
{
match ignore_file_in_index {
Ok(idx) => {
let ignore_blob = find(&attribute_files_in_index[idx].1, buf)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))?;
let ignore_path = git_path::from_bstring(ignore_path_relative.into_owned());
self.stack
.add_patterns_buffer(ignore_blob.data, ignore_path, Some(root));
}
Err(_) => {
self.stack.patterns.push(Default::default())
}
}
}
Ok(())
}
}
impl Attributes {
pub fn new(globals: AttributeMatchGroup) -> Self {
Attributes {
globals,
stack: Default::default(),
}
}
}
impl From<AttributeMatchGroup> for Attributes {
fn from(group: AttributeMatchGroup) -> Self {
Attributes::new(group)
}
}
impl State {
pub fn for_checkout(unlink_on_collision: bool, attributes: Attributes) -> Self {
State::CreateDirectoryAndAttributesStack {
unlink_on_collision,
#[cfg(debug_assertions)]
test_mkdir_calls: 0,
attributes,
}
}
pub fn for_add(attributes: Attributes, ignore: Ignore) -> Self {
State::AttributesAndIgnoreStack { attributes, ignore }
}
pub fn for_status(ignore: Ignore) -> Self {
State::IgnoreStack(ignore)
}
}
impl State {
pub fn build_attribute_list<'paths>(
&self,
index: &git_index::State,
paths: &'paths git_index::PathStorageRef,
case: Case,
) -> Vec<PathOidMapping> {
let a1_backing;
let a2_backing;
let names = match self {
State::IgnoreStack(v) => {
a1_backing = [(v.exclude_file_name_for_directories.as_bytes().as_bstr(), true)];
a1_backing.as_ref()
}
State::AttributesAndIgnoreStack { ignore, .. } => {
a2_backing = [
(ignore.exclude_file_name_for_directories.as_bytes().as_bstr(), true),
(".gitattributes".into(), false),
];
a2_backing.as_ref()
}
State::CreateDirectoryAndAttributesStack { .. } => {
a1_backing = [(".gitattributes".into(), true)];
a1_backing.as_ref()
}
};
index
.entries()
.iter()
.filter_map(move |entry| {
let path = entry.path_in(paths);
if entry.mode == git_index::entry::Mode::FILE && (entry.stage() == 0 || entry.stage() == 2) {
let basename = path
.rfind_byte(b'/')
.map(|pos| path[pos + 1..].as_bstr())
.unwrap_or(path);
let is_ignore = names.iter().find_map(|t| {
match case {
Case::Sensitive => basename == t.0,
Case::Fold => basename.eq_ignore_ascii_case(t.0),
}
.then(|| t.1)
})?;
if is_ignore && !entry.flags.contains(git_index::entry::Flags::SKIP_WORKTREE) {
return None;
}
Some((path.to_owned(), entry.id))
} else {
None
}
})
.collect()
}
pub(crate) fn ignore_or_panic(&self) -> &Ignore {
match self {
State::IgnoreStack(v) => v,
State::AttributesAndIgnoreStack { ignore, .. } => ignore,
State::CreateDirectoryAndAttributesStack { .. } => {
unreachable!("BUG: must not try to check excludes without it being setup")
}
}
}
}