diff options
Diffstat (limited to 'src/git_config/mod.rs')
-rw-r--r-- | src/git_config/mod.rs | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/src/git_config/mod.rs b/src/git_config/mod.rs index 228fd4d0..8246df4a 100644 --- a/src/git_config/mod.rs +++ b/src/git_config/mod.rs @@ -7,14 +7,15 @@ use regex::Regex; use std::collections::HashMap; #[cfg(test)] use std::path::Path; +use std::str::FromStr; use lazy_static::lazy_static; pub struct GitConfig { - pub config: git2::Config, + config: git2::Config, config_from_env_var: HashMap<String, String>, pub enabled: bool, - pub repo: Option<git2::Repository>, + repo: Option<git2::Repository>, // To make GitConfig cloneable when testing (in turn to make Config cloneable): #[cfg(test)] path: std::path::PathBuf, @@ -94,6 +95,31 @@ impl GitConfig { None } } + + pub fn get_remote_url(&self) -> Option<GitConfigEntry> { + self.repo + .as_ref()? + .find_remote("origin") + .ok()? + .url() + .and_then(|url| { + GitRemoteRepo::from_str(url) + .ok() + .map(GitConfigEntry::GitRemote) + }) + } + + pub fn for_each<F>(&self, regex: &str, mut f: F) + where + F: FnMut(&str, Option<&str>), + { + let mut entries = self.config.entries(Some(regex)).unwrap(); + while let Some(entry) = entries.next() { + let entry = entry.unwrap(); + let name = entry.name().unwrap(); + f(name, entry.value()); + } + } } fn parse_config_from_env_var(env: &DeltaEnv) -> HashMap<String, String> { |