gix_worktree/stack/
platform.rs

1use std::path::Path;
2
3use bstr::ByteSlice;
4
5use crate::stack::Platform;
6
7/// Access
8impl<'a> Platform<'a> {
9    /// The full path to `relative` will be returned for use on the file system.
10    pub fn path(&self) -> &'a Path {
11        self.parent.stack.current()
12    }
13
14    /// See if the currently set entry is excluded as per exclude and git-ignore files.
15    ///
16    /// Note that this threats both classes, [*trashable*](gix_ignore::Kind::Expendable) and [*precious*](gix_ignore::Kind::Precious)
17    /// as equal. If you need to differentiate, use [`matching_exclude_pattern()`](Self::matching_exclude_pattern)
18    /// or [`excluded_kind()`](Self::excluded_kind).
19    ///
20    /// # Panics
21    ///
22    /// If the cache was configured without exclude patterns.
23    #[doc(alias = "is_path_ignored", alias = "git2")]
24    pub fn is_excluded(&self) -> bool {
25        self.matching_exclude_pattern()
26            .is_some_and(|m| !m.pattern.is_negative())
27    }
28
29    /// See if a non-negative ignore-pattern matches and obtain the kind of exclude, or return `None`
30    /// if the path isn't excluded.
31    ///
32    /// This is similar to [`is_excluded()`](Self::is_excluded), but provides details that are useful to
33    /// decide what to do with the excluded item.
34    pub fn excluded_kind(&self) -> Option<gix_ignore::Kind> {
35        self.matching_exclude_pattern()
36            .and_then(|m| (!m.pattern.is_negative()).then_some(m.kind))
37    }
38
39    /// Check all exclude patterns to see if the currently set path matches any of them.
40    ///
41    /// Note that this pattern might be negated, and means this path in included.
42    ///
43    /// # Panics
44    ///
45    /// If the cache was configured without exclude patterns.
46    pub fn matching_exclude_pattern(&self) -> Option<gix_ignore::search::Match<'_>> {
47        let ignore = self.parent.state.ignore_or_panic();
48        let relative_path =
49            gix_path::to_unix_separators_on_windows(gix_path::into_bstr(self.parent.stack.current_relative()));
50        ignore.matching_exclude_pattern(relative_path.as_bstr(), self.is_dir, self.parent.case)
51    }
52
53    /// Match all attributes at the current path and store the result in `out`, returning `true` if at least one attribute was found.
54    ///
55    /// # Panics
56    ///
57    /// If the cache was configured without attributes.
58    #[cfg(feature = "attributes")]
59    pub fn matching_attributes(&self, out: &mut gix_attributes::search::Outcome) -> bool {
60        let attrs = self.parent.state.attributes_or_panic();
61        let relative_path =
62            gix_path::to_unix_separators_on_windows(gix_path::into_bstr(self.parent.stack.current_relative()));
63        attrs.matching_attributes(relative_path.as_bstr(), self.parent.case, self.is_dir, out)
64    }
65}
66
67impl std::fmt::Debug for Platform<'_> {
68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69        std::fmt::Debug::fmt(&self.path(), f)
70    }
71}