Skip to content

Commit

Permalink
More idiomatic way to fetch repository remote URL (#1516)
Browse files Browse the repository at this point in the history
  • Loading branch information
o2sh authored Jan 25, 2025
1 parent fcebf16 commit e0fd7fa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub fn build_info(cli_options: &CliOptions) -> Result<Info> {
&repo,
cli_options.info.hide_token,
cli_options.info.http_url,
);
)?;

let git_metrics = traverse_commit_graph(
&repo,
Expand Down
29 changes: 9 additions & 20 deletions src/info/url.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::info::utils::info_field::InfoField;
use anyhow::Result;
use gix::Repository;
use regex::Regex;
use serde::Serialize;
Expand All @@ -16,29 +17,17 @@ 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<String> {
let remote = match repo.try_find_remote("origin") {
Some(remote) => remote?,
None => return Ok(String::new()),
};

let mut remote_url: Option<String> = 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;
}
}
Ok(remote
.url(gix::remote::Direction::Push)
.map(|url| format_url(&url.to_string(), hide_token, http_url))
.unwrap_or_default())

match remote_url {
Some(url) => format_url(&url, hide_token, http_url),
None => String::default(),
}
}

fn format_url(url: &str, hide_token: bool, http_url: bool) -> String {
Expand Down

0 comments on commit e0fd7fa

Please sign in to comment.