summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornickelc <constantin.nickel@gmail.com>2023-03-05 20:59:30 +0100
committerGitHub <noreply@github.com>2023-03-05 14:59:30 -0500
commita30aa5f88fb707953e2c78e9f888d147d03a3fe3 (patch)
tree3afd0a3ea1b362e2111624c0e2736423c8f28302
parent420b1acea1f977aa45712ca22309fad20a5a6018 (diff)
Encapsulate `git2` types (#1326)
* Move theme iteration logic to `GitConfig` The `git2::Config` type is now encapsulated and the regex is given to `libgit2` to filter the config entries. * Move remote url getter to `GitConfig`
-rw-r--r--src/features/hyperlinks.rs21
-rw-r--r--src/git_config/mod.rs30
-rw-r--r--src/options/get.rs24
3 files changed, 41 insertions, 34 deletions
diff --git a/src/features/hyperlinks.rs b/src/features/hyperlinks.rs
index aac32210..118d8355 100644
--- a/src/features/hyperlinks.rs
+++ b/src/features/hyperlinks.rs
@@ -1,6 +1,5 @@
use std::borrow::Cow;
use std::path::Path;
-use std::str::FromStr;
use lazy_static::lazy_static;
use regex::{Captures, Regex};
@@ -33,8 +32,10 @@ pub fn format_commit_line_with_osc8_commit_hyperlink<'a>(
format_osc8_hyperlink(&commit_link_format.replace("{commit}", commit), commit);
format!("{prefix}{formatted_commit}{suffix}")
})
- } else if let Some(GitConfigEntry::GitRemote(repo)) =
- config.git_config.as_ref().and_then(get_remote_url)
+ } else if let Some(GitConfigEntry::GitRemote(repo)) = config
+ .git_config
+ .as_ref()
+ .and_then(GitConfig::get_remote_url)
{
COMMIT_LINE_REGEX.replace(line, |captures: &Captures| {
format_commit_line_captures_with_osc8_commit_hyperlink(captures, &repo)
@@ -44,20 +45,6 @@ pub fn format_commit_line_with_osc8_commit_hyperlink<'a>(
}
}
-fn get_remote_url(git_config: &GitConfig) -> Option<GitConfigEntry> {
- git_config
- .repo
- .as_ref()?
- .find_remote("origin")
- .ok()?
- .url()
- .and_then(|url| {
- GitRemoteRepo::from_str(url)
- .ok()
- .map(GitConfigEntry::GitRemote)
- })
-}
-
/// Create a file hyperlink, displaying `text`.
pub fn format_osc8_file_hyperlink<'a, P>(
absolute_path: P,
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> {
diff --git a/src/options/get.rs b/src/options/get.rs
index 53a4d635..2ee3ee1b 100644
--- a/src/options/get.rs
+++ b/src/options/get.rs
@@ -1,5 +1,3 @@
-use lazy_static::lazy_static;
-use regex::Regex;
use std::collections::HashMap;
use crate::cli;
@@ -40,25 +38,21 @@ where
T::get_option_value(option_name, builtin_features, opt, git_config)
}
-lazy_static! {
- static ref GIT_CONFIG_THEME_REGEX: Regex = Regex::new(r"^delta\.(.+)\.(light|dark)$").unwrap();
-}
+static GIT_CONFIG_THEME_REGEX: &str = r#"^delta\.(.+)\.(light|dark)$"#;
pub fn get_themes(git_config: Option<git_config::GitConfig>) -> Vec<String> {
let mut themes: Vec<String> = Vec::new();
let git_config = git_config.unwrap();
- let mut entries = git_config.config.entries(None).unwrap();
- while let Some(e) = entries.next() {
- let entry = e.unwrap();
- let entry_name = entry.name().unwrap();
- let caps = GIT_CONFIG_THEME_REGEX.captures(entry_name);
- if let Some(caps) = caps {
- let name = caps.get(1).map_or("", |m| m.as_str()).to_string();
- if !themes.contains(&name) {
- themes.push(name)
+ git_config.for_each(GIT_CONFIG_THEME_REGEX, |name, _| {
+ if let Some(name) = name.strip_prefix("delta.") {
+ if let Some((name, _)) = name.rsplit_once('.') {
+ let name = name.to_owned();
+ if !themes.contains(&name) {
+ themes.push(name);
+ }
}
}
- }
+ });
themes.sort_by_key(|a| a.to_lowercase());
themes
}