Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More idiomatic way to fetch repository remote URL #1516

Merged
merged 2 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading