Skip to main content

gix_submodule/
is_active_platform.rs

1use bstr::BStr;
2use gix_error::ExnMessageResult;
3
4use crate::IsActivePlatform;
5
6impl IsActivePlatform {
7    /// Returns `true` if the submodule named `name` is active or `false` otherwise.
8    /// `config` is the configuration that was passed to the originating [modules file](crate::File).
9    /// `attributes(relative_path, case, is_dir, outcome)` provides a way to resolve the attributes mentioned
10    /// in `submodule.active` pathspecs that are evaluated in the platforms git configuration.
11    ///
12    /// A submodule's active state is determined in the following order
13    ///
14    /// * it's `submodule.<name>.active` is set in `config`
15    /// * it matches a `submodule.active` pathspec either positively or negatively via `:!<spec>`
16    /// * it's active if it has any `url` set in `config`
17    pub fn is_active(
18        &mut self,
19        config: &gix_config::File,
20        name: &BStr,
21        attributes: &mut dyn FnMut(
22            &BStr,
23            gix_pathspec::attributes::glob::pattern::Case,
24            bool,
25            &mut gix_pathspec::attributes::search::Outcome,
26        ) -> bool,
27    ) -> ExnMessageResult<bool> {
28        if let Some(val) = config.boolean(&format!("submodule.{name}.active"))? {
29            return Ok(val);
30        }
31        if let Some(val) = self.search.as_mut().map(|search| {
32            search
33                .pattern_matching_relative_path(name, Some(true), attributes)
34                .is_some_and(|m| !m.is_excluded())
35        }) {
36            return Ok(val);
37        }
38        Ok(config.string(&format!("submodule.{name}.url")).is_some())
39    }
40}