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 1 commit
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,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<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;
}
}

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())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IDK if you have a preference, but this can also be written with Option::map and Option::unwrap_or_default I think.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
}

Expand Down
Loading