Skip to content

Commit

Permalink
Move remote url getter to GitConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
nickelc committed Mar 5, 2023
1 parent 89d7cbd commit 7ae9561
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
21 changes: 4 additions & 17 deletions src/features/hyperlinks.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
16 changes: 15 additions & 1 deletion src/git_config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
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,
Expand Down Expand Up @@ -95,6 +96,19 @@ impl GitConfig {
}
}

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>),
Expand Down

0 comments on commit 7ae9561

Please # to comment.