From 54409410dfdab3780b808d24c66e107687a64b7d Mon Sep 17 00:00:00 2001 From: o2sh Date: Fri, 24 Jan 2025 22:55:20 +0100 Subject: [PATCH 1/2] init --- src/info/mod.rs | 2 +- src/info/url.rs | 29 +++++++++-------------------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/src/info/mod.rs b/src/info/mod.rs index 1f5def4e0..dedc08efd 100644 --- a/src/info/mod.rs +++ b/src/info/mod.rs @@ -147,7 +147,7 @@ pub fn build_info(cli_options: &CliOptions) -> Result { &repo, cli_options.info.hide_token, cli_options.info.http_url, - ); + )?; let git_metrics = traverse_commit_graph( &repo, diff --git a/src/info/url.rs b/src/info/url.rs index 83bf22bae..89678462a 100644 --- a/src/info/url.rs +++ b/src/info/url.rs @@ -1,4 +1,5 @@ use crate::info::utils::info_field::InfoField; +use anyhow::Result; use gix::Repository; use regex::Regex; use serde::Serialize; @@ -16,28 +17,16 @@ impl UrlInfo { } } -pub fn get_repo_url(repo: &Repository, hide_token: bool, http_url: bool) -> String { - let config = repo.config_snapshot(); - let remotes = match config.plumbing().sections_by_name("remote") { - Some(sections) => sections, - None => return String::default(), +pub fn get_repo_url(repo: &Repository, hide_token: bool, http_url: bool) -> Result { + let remote = match repo.try_find_remote("origin") { + Some(remote) => remote?, + None => return Ok(String::new()), }; - let mut remote_url: Option = None; - for (name, url) in remotes.filter_map(|section| { - let remote_name = section.header().subsection_name()?; - let url = section.value("url")?; - (remote_name, url).into() - }) { - remote_url = url.to_string().into(); - if name == "origin" { - break; - } - } - - match remote_url { - Some(url) => format_url(&url, hide_token, http_url), - None => String::default(), + if let Some(url) = remote.url(gix::remote::Direction::Push) { + Ok(format_url(&url.to_string(), hide_token, http_url)) + } else { + Ok(String::new()) } } From af7e84cf1e142f4a0fc761f864c0e97410edda4e Mon Sep 17 00:00:00 2001 From: o2sh Date: Sat, 25 Jan 2025 23:33:36 +0100 Subject: [PATCH 2/2] review --- src/info/url.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/info/url.rs b/src/info/url.rs index 89678462a..54f57562f 100644 --- a/src/info/url.rs +++ b/src/info/url.rs @@ -23,11 +23,11 @@ pub fn get_repo_url(repo: &Repository, hide_token: bool, http_url: bool) -> Resu None => return Ok(String::new()), }; - if let Some(url) = remote.url(gix::remote::Direction::Push) { - Ok(format_url(&url.to_string(), hide_token, http_url)) - } else { - Ok(String::new()) - } + Ok(remote + .url(gix::remote::Direction::Push) + .map(|url| format_url(&url.to_string(), hide_token, http_url)) + .unwrap_or_default()) + } fn format_url(url: &str, hide_token: bool, http_url: bool) -> String {