summaryrefslogtreecommitdiffstats
path: root/vendor/gix/src/repository/attributes.rs
blob: 7f747f7fd98144ce30ad6858da619182c577be49 (plain)
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
//! exclude information
use crate::{config, AttributeStack, Repository};

/// The error returned by [`Repository::attributes()`].
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
    #[error(transparent)]
    ConfigureAttributes(#[from] config::attribute_stack::Error),
    #[error(transparent)]
    ConfigureExcludes(#[from] config::exclude_stack::Error),
}

impl Repository {
    /// Configure a file-system cache for accessing git attributes *and* excludes on a per-path basis.
    ///
    /// Use `attribute_source` to specify where to read attributes from. Also note that exclude information will
    /// always try to read `.gitignore` files from disk before trying to read it from the `index`.
    ///
    /// Note that no worktree is required for this to work, even though access to in-tree `.gitattributes` and `.gitignore` files
    /// would require a non-empty `index` that represents a git tree.
    ///
    /// This takes into consideration all the usual repository configuration, namely:
    ///
    /// * `$XDG_CONFIG_HOME/…/ignore|attributes` if `core.excludesFile|attributesFile` is *not* set, otherwise use the configured file.
    /// * `$GIT_DIR/info/exclude|attributes` if present.
    #[cfg(feature = "attributes")]
    pub fn attributes(
        &self,
        index: &gix_index::State,
        attributes_source: gix_worktree::stack::state::attributes::Source,
        ignore_source: gix_worktree::stack::state::ignore::Source,
        exclude_overrides: Option<gix_ignore::Search>,
    ) -> Result<AttributeStack<'_>, Error> {
        let case = if self.config.ignore_case {
            gix_glob::pattern::Case::Fold
        } else {
            gix_glob::pattern::Case::Sensitive
        };
        let (attributes, mut buf) = self.config.assemble_attribute_globals(
            self.git_dir(),
            attributes_source,
            self.options.permissions.attributes,
        )?;
        let ignore =
            self.config
                .assemble_exclude_globals(self.git_dir(), exclude_overrides, ignore_source, &mut buf)?;
        let state = gix_worktree::stack::State::AttributesAndIgnoreStack { attributes, ignore };
        let attribute_list = state.id_mappings_from_index(index, index.path_backing(), case);
        Ok(AttributeStack::new(
            gix_worktree::Stack::new(
                // this is alright as we don't cause mutation of that directory, it's virtual.
                self.work_dir().unwrap_or(self.git_dir()),
                state,
                case,
                buf,
                attribute_list,
            ),
            self,
        ))
    }

    /// Like [attributes()][Self::attributes()], but without access to exclude/ignore information.
    #[cfg(feature = "attributes")]
    pub fn attributes_only(
        &self,
        index: &gix_index::State,
        attributes_source: gix_worktree::stack::state::attributes::Source,
    ) -> Result<AttributeStack<'_>, config::attribute_stack::Error> {
        let case = if self.config.ignore_case {
            gix_glob::pattern::Case::Fold
        } else {
            gix_glob::pattern::Case::Sensitive
        };
        let (attributes, buf) = self.config.assemble_attribute_globals(
            self.git_dir(),
            attributes_source,
            self.options.permissions.attributes,
        )?;
        let state = gix_worktree::stack::State::AttributesStack(attributes);
        let attribute_list = state.id_mappings_from_index(index, index.path_backing(), case);
        Ok(AttributeStack::new(
            gix_worktree::Stack::new(
                // this is alright as we don't cause mutation of that directory, it's virtual.
                self.work_dir().unwrap_or(self.git_dir()),
                state,
                case,
                buf,
                attribute_list,
            ),
            self,
        ))
    }

    /// Configure a file-system cache checking if files below the repository are excluded, reading `.gitignore` files from
    /// the specified `source`.
    ///
    /// Note that no worktree is required for this to work, even though access to in-tree `.gitignore` files would require
    /// a non-empty `index` that represents a tree with `.gitignore` files.
    ///
    /// This takes into consideration all the usual repository configuration, namely:
    ///
    /// * `$XDG_CONFIG_HOME/…/ignore` if `core.excludesFile` is *not* set, otherwise use the configured file.
    /// * `$GIT_DIR/info/exclude` if present.
    ///
    /// When only excludes are desired, this is the most efficient way to obtain them. Otherwise use
    /// [`Repository::attributes()`] for accessing both attributes and excludes.
    // TODO: test
    #[cfg(feature = "excludes")]
    pub fn excludes(
        &self,
        index: &gix_index::State,
        overrides: Option<gix_ignore::Search>,
        source: gix_worktree::stack::state::ignore::Source,
    ) -> Result<AttributeStack<'_>, config::exclude_stack::Error> {
        let case = if self.config.ignore_case {
            gix_glob::pattern::Case::Fold
        } else {
            gix_glob::pattern::Case::Sensitive
        };
        let mut buf = Vec::with_capacity(512);
        let ignore = self
            .config
            .assemble_exclude_globals(self.git_dir(), overrides, source, &mut buf)?;
        let state = gix_worktree::stack::State::IgnoreStack(ignore);
        let attribute_list = state.id_mappings_from_index(index, index.path_backing(), case);
        Ok(AttributeStack::new(
            gix_worktree::Stack::new(
                // this is alright as we don't cause mutation of that directory, it's virtual.
                self.work_dir().unwrap_or(self.git_dir()),
                state,
                case,
                buf,
                attribute_list,
            ),
            self,
        ))
    }
}