gix_submodule/
is_active_platform.rs

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