gix_worktree/stack/state/mod.rs
1use bstr::{BString, ByteSlice};
2use gix_glob::pattern::Case;
3
4use crate::{stack::State, PathIdMapping};
5
6#[cfg(feature = "attributes")]
7type AttributeMatchGroup = gix_attributes::Search;
8type IgnoreMatchGroup = gix_ignore::Search;
9
10/// State related to attributes associated with files in the repository.
11#[derive(Default, Clone)]
12#[cfg(feature = "attributes")]
13pub struct Attributes {
14 /// Attribute patterns which aren't tied to the repository root, hence are global, they contribute first.
15 globals: AttributeMatchGroup,
16 /// Attribute patterns that match the currently set directory (in the stack).
17 ///
18 /// Note that the root-level file is always loaded, if present, followed by, the `$GIT_DIR/info/attributes`, if present, based
19 /// on the location of the `info_attributes` file.
20 stack: AttributeMatchGroup,
21 /// The first time we push the root, we have to load additional information from this file if it exists along with the root attributes
22 /// file if possible, and keep them there throughout.
23 info_attributes: Option<std::path::PathBuf>,
24 /// A lookup table to accelerate searches.
25 collection: gix_attributes::search::MetadataCollection,
26 /// Where to read `.gitattributes` data from.
27 source: attributes::Source,
28}
29
30/// State related to the exclusion of files, supporting static overrides and globals, along with a stack of dynamically read
31/// ignore files from disk or from the index each time the directory changes.
32#[derive(Default, Clone)]
33#[allow(unused)]
34pub struct Ignore {
35 /// Ignore patterns passed as overrides to everything else, typically passed on the command-line and the first patterns to
36 /// be consulted.
37 overrides: IgnoreMatchGroup,
38 /// Ignore patterns that match the currently set director (in the stack), which is pushed and popped as needed.
39 stack: IgnoreMatchGroup,
40 /// Ignore patterns which aren't tied to the repository root, hence are global. They are consulted last.
41 globals: IgnoreMatchGroup,
42 /// A matching stack of pattern indices which is empty if we have just been initialized to indicate that the
43 /// currently set directory had a pattern matched. Note that this one could be negated.
44 /// (index into match groups, index into list of pattern lists, index into pattern list)
45 matched_directory_patterns_stack: Vec<Option<(usize, usize, usize)>>,
46 /// The name of the file to look for in directories.
47 pub(crate) exclude_file_name_for_directories: BString,
48 /// Where to read ignore files from
49 source: ignore::Source,
50}
51
52///
53#[cfg(feature = "attributes")]
54pub mod attributes;
55///
56pub mod ignore;
57
58/// Initialization
59impl State {
60 /// Configure a state to be suitable for checking out files, which only needs access to attribute files read from the index.
61 #[cfg(feature = "attributes")]
62 pub fn for_checkout(
63 unlink_on_collision: bool,
64 validate: gix_validate::path::component::Options,
65 attributes: Attributes,
66 ) -> Self {
67 State::CreateDirectoryAndAttributesStack {
68 unlink_on_collision,
69 validate,
70 attributes,
71 }
72 }
73
74 /// Configure a state for adding files, with support for ignore files and attribute files.
75 #[cfg(feature = "attributes")]
76 pub fn for_add(attributes: Attributes, ignore: Ignore) -> Self {
77 State::AttributesAndIgnoreStack { attributes, ignore }
78 }
79}
80
81/// Utilities
82impl State {
83 /// Returns a vec of tuples of relative index paths along with the best usable blob OID for
84 /// either *ignore* or *attribute* files or both. This allows files to be accessed directly from
85 /// the object database without the need for a worktree checkout.
86 ///
87 /// Note that this method…
88 /// - ignores entries which aren't blobs.
89 /// - ignores ignore entries which are not skip-worktree.
90 /// - within merges, picks 'our' stage both for *ignore* and *attribute* files.
91 ///
92 /// * `index` is where we look for suitable files by path in order to obtain their blob hash.
93 /// * `paths` is the indices storage backend for paths.
94 /// * `case` determines if the search for files should be case-sensitive or not.
95 pub fn id_mappings_from_index(
96 &self,
97 index: &gix_index::State,
98 paths: &gix_index::PathStorageRef,
99 case: Case,
100 ) -> Vec<PathIdMapping> {
101 let a1_backing;
102 #[cfg(feature = "attributes")]
103 let a2_backing;
104 let names = match self {
105 State::IgnoreStack(ignore) => {
106 a1_backing = [(
107 ignore.exclude_file_name_for_directories.as_bytes().as_bstr(),
108 Some(ignore.source),
109 )];
110 a1_backing.as_ref()
111 }
112 #[cfg(feature = "attributes")]
113 State::AttributesAndIgnoreStack { ignore, .. } => {
114 a2_backing = [
115 (
116 ignore.exclude_file_name_for_directories.as_bytes().as_bstr(),
117 Some(ignore.source),
118 ),
119 (".gitattributes".into(), None),
120 ];
121 a2_backing.as_ref()
122 }
123 #[cfg(feature = "attributes")]
124 State::CreateDirectoryAndAttributesStack { .. } | State::AttributesStack(_) => {
125 a1_backing = [(".gitattributes".into(), None)];
126 a1_backing.as_ref()
127 }
128 };
129
130 index
131 .entries()
132 .iter()
133 .filter_map(move |entry| {
134 let path = entry.path_in(paths);
135
136 // Stage 0 means there is no merge going on, stage 2 means it's 'our' side of the merge, but then
137 // there won't be a stage 0.
138 if entry.mode == gix_index::entry::Mode::FILE && (entry.stage_raw() == 0 || entry.stage_raw() == 2) {
139 let basename = path.rfind_byte(b'/').map_or(path, |pos| path[pos + 1..].as_bstr());
140 let ignore_source = names.iter().find_map(|t| {
141 match case {
142 Case::Sensitive => basename == t.0,
143 Case::Fold => basename.eq_ignore_ascii_case(t.0),
144 }
145 .then_some(t.1)
146 })?;
147 if let Some(source) = ignore_source {
148 match source {
149 ignore::Source::IdMapping => {}
150 ignore::Source::WorktreeThenIdMappingIfNotSkipped => {
151 // See https://github.com/git/git/blob/master/dir.c#L912:L912
152 if !entry.flags.contains(gix_index::entry::Flags::SKIP_WORKTREE) {
153 return None;
154 }
155 }
156 }
157 }
158 Some((path.to_owned(), entry.id))
159 } else {
160 None
161 }
162 })
163 .collect()
164 }
165
166 pub(crate) fn ignore_or_panic(&self) -> &Ignore {
167 match self {
168 State::IgnoreStack(v) => v,
169 #[cfg(feature = "attributes")]
170 State::AttributesAndIgnoreStack { ignore, .. } => ignore,
171 #[cfg(feature = "attributes")]
172 State::AttributesStack(_) | State::CreateDirectoryAndAttributesStack { .. } => {
173 unreachable!("BUG: must not try to check excludes without it being setup")
174 }
175 }
176 }
177
178 #[cfg(feature = "attributes")]
179 pub(crate) fn attributes_or_panic(&self) -> &Attributes {
180 match self {
181 State::AttributesStack(attributes)
182 | State::AttributesAndIgnoreStack { attributes, .. }
183 | State::CreateDirectoryAndAttributesStack { attributes, .. } => attributes,
184 State::IgnoreStack(_) => {
185 unreachable!("BUG: must not try to check excludes without it being setup")
186 }
187 }
188 }
189}