Skip to main content

git_atomic/config/
git_provider.rs

1use figment::value::{Dict, Map};
2use figment::{Error, Metadata, Profile, Provider};
3
4/// Custom figment `Provider` that reads `atomic.*` keys from git config.
5///
6/// Uses gix's `config_snapshot()` which automatically merges
7/// system < global < local < worktree scopes. The merged result
8/// is treated as a single "git config" source for provenance tracking.
9pub struct GitConfigProvider {
10    settings: Dict,
11}
12
13impl GitConfigProvider {
14    /// Build provider from a gix repository. Returns empty provider if `None`.
15    pub fn new(repo: Option<&gix::Repository>) -> Self {
16        let mut settings = Dict::new();
17        if let Some(repo) = repo {
18            let snapshot = repo.config_snapshot();
19
20            if let Some(v) = snapshot.string_by("atomic", None, "baseBranch") {
21                settings.insert("base_branch".into(), v.to_string().into());
22            }
23            if let Some(v) = snapshot.string_by("atomic", None, "branchTemplate") {
24                settings.insert("branch_template".into(), v.to_string().into());
25            }
26            if let Some(v) = snapshot.string_by("atomic", None, "unmatchedFiles") {
27                settings.insert("unmatched_files".into(), v.to_string().into());
28            }
29            if let Some(v) = snapshot.string_by("atomic", None, "defaultCommitType") {
30                settings.insert("default_commit_type".into(), v.to_string().into());
31            }
32        }
33        Self { settings }
34    }
35}
36
37impl Provider for GitConfigProvider {
38    fn metadata(&self) -> Metadata {
39        Metadata::named("git config")
40    }
41
42    fn data(&self) -> Result<Map<Profile, Dict>, Error> {
43        // Nest settings under "settings" key to match Config struct
44        let mut inner = Dict::new();
45        if !self.settings.is_empty() {
46            inner.insert("settings".into(), self.settings.clone().into());
47        }
48        Ok(Profile::Default.collect(inner))
49    }
50}