1use std::path::{Component, Path, PathBuf};
2
3use bstr::{BStr, BString, ByteSlice, ByteVec};
4
5use crate::{normalize, MagicSignature, Pattern, SearchMode};
6
7impl Pattern {
9    pub fn is_nil(&self) -> bool {
13        self.nil
14    }
15
16    pub fn prefix_directory(&self) -> &BStr {
23        self.path[..self.prefix_len].as_bstr()
24    }
25
26    pub fn path(&self) -> &BStr {
28        self.path.as_ref()
29    }
30}
31
32impl Pattern {
34    pub fn normalize(&mut self, prefix: &Path, root: &Path) -> Result<&mut Self, normalize::Error> {
44        fn prefix_components_to_subtract(path: &Path) -> usize {
45            let parent_component_end_bound = path.components().enumerate().fold(None::<usize>, |acc, (idx, c)| {
46                matches!(c, Component::ParentDir).then_some(idx + 1).or(acc)
47            });
48            let count = path
49                .components()
50                .take(parent_component_end_bound.unwrap_or(0))
51                .map(|c| match c {
52                    Component::ParentDir => 1_isize,
53                    Component::Normal(_) => -1,
54                    _ => 0,
55                })
56                .sum::<isize>();
57            if count > 0 {
58                count as usize
59            } else {
60                Default::default()
61            }
62        }
63
64        let mut path = gix_path::from_bstr(self.path.as_bstr());
65        let mut num_prefix_components = 0;
66        let mut was_absolute = false;
67        if gix_path::is_absolute(path.as_ref()) {
68            was_absolute = true;
69            let rela_path = match path.strip_prefix(root) {
70                Ok(path) => path,
71                Err(_) => {
72                    return Err(normalize::Error::AbsolutePathOutsideOfWorktree {
73                        path: path.into_owned(),
74                        worktree_path: root.into(),
75                    })
76                }
77            };
78            path = rela_path.to_owned().into();
79        } else if !prefix.as_os_str().is_empty() && !self.signature.contains(MagicSignature::TOP) {
80            debug_assert_eq!(
81                prefix
82                    .components()
83                    .filter(|c| matches!(c, Component::Normal(_)))
84                    .count(),
85                prefix.components().count(),
86                "BUG: prefixes must not have relative path components, or calculations here will be wrong so pattern won't match"
87            );
88            num_prefix_components = prefix
89                .components()
90                .count()
91                .saturating_sub(prefix_components_to_subtract(path.as_ref()));
92            path = prefix.join(path).into();
93        }
94
95        let assure_path_cannot_break_out_upwards = Path::new("");
96        let path = match gix_path::normalize(path.as_ref().into(), assure_path_cannot_break_out_upwards) {
97            Some(path) => {
98                if was_absolute {
99                    num_prefix_components = path.components().count().saturating_sub(
100                        if self.signature.contains(MagicSignature::MUST_BE_DIR) {
101                            0
102                        } else {
103                            1
104                        },
105                    );
106                }
107                path
108            }
109            None => {
110                return Err(normalize::Error::OutsideOfWorktree {
111                    path: path.into_owned(),
112                })
113            }
114        };
115
116        self.path = if path == Path::new(".") {
117            self.nil = true;
118            BString::from(".")
119        } else {
120            let cleaned = PathBuf::from_iter(path.components().filter(|c| !matches!(c, Component::CurDir)));
121            let mut out = gix_path::to_unix_separators_on_windows(gix_path::into_bstr(cleaned)).into_owned();
122            self.prefix_len = {
123                if self.signature.contains(MagicSignature::MUST_BE_DIR) {
124                    out.push(b'/');
125                }
126                let len = out
127                    .find_iter(b"/")
128                    .take(num_prefix_components)
129                    .last()
130                    .unwrap_or_default();
131                if self.signature.contains(MagicSignature::MUST_BE_DIR) {
132                    out.pop();
133                }
134                len
135            };
136            out
137        };
138
139        Ok(self)
140    }
141}
142
143impl Pattern {
145    pub fn is_excluded(&self) -> bool {
147        self.signature.contains(MagicSignature::EXCLUDE)
148    }
149
150    pub fn always_matches(&self) -> bool {
153        self.is_nil() || self.path.is_empty()
154    }
155
156    pub fn to_bstring(&self) -> BString {
160        if self.is_nil() {
161            ":".into()
162        } else {
163            let mut buf: BString = ":(".into();
164            if self.signature.contains(MagicSignature::TOP) {
165                buf.push_str("top,");
166            }
167            if self.signature.contains(MagicSignature::EXCLUDE) {
168                buf.push_str("exclude,");
169            }
170            if self.signature.contains(MagicSignature::ICASE) {
171                buf.push_str("icase,");
172            }
173            match self.search_mode {
174                SearchMode::ShellGlob => {}
175                SearchMode::Literal => buf.push_str("literal,"),
176                SearchMode::PathAwareGlob => buf.push_str("glob,"),
177            }
178            if self.attributes.is_empty() {
179                if buf.last() == Some(&b',') {
180                    buf.pop();
181                }
182            } else {
183                buf.push_str("attr:");
184                for attr in &self.attributes {
185                    let attr = attr.as_ref().to_string().replace(',', r"\,");
186                    buf.push_str(&attr);
187                    buf.push(b' ');
188                }
189                buf.pop(); }
191            buf.push(b')');
192            buf.extend_from_slice(&self.path);
193            if self.signature.contains(MagicSignature::MUST_BE_DIR) {
194                buf.push(b'/');
195            }
196            buf
197        }
198    }
199}
200
201impl std::fmt::Display for Pattern {
202    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
203        self.to_bstring().fmt(f)
204    }
205}