Skip to content

Commit e0fd7fa

Browse files
authored
More idiomatic way to fetch repository remote URL (#1516)
1 parent fcebf16 commit e0fd7fa

File tree

2 files changed

+10
-21
lines changed

2 files changed

+10
-21
lines changed

src/info/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ pub fn build_info(cli_options: &CliOptions) -> Result<Info> {
147147
&repo,
148148
cli_options.info.hide_token,
149149
cli_options.info.http_url,
150-
);
150+
)?;
151151

152152
let git_metrics = traverse_commit_graph(
153153
&repo,

src/info/url.rs

+9-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::info::utils::info_field::InfoField;
2+
use anyhow::Result;
23
use gix::Repository;
34
use regex::Regex;
45
use serde::Serialize;
@@ -16,29 +17,17 @@ impl UrlInfo {
1617
}
1718
}
1819

19-
pub fn get_repo_url(repo: &Repository, hide_token: bool, http_url: bool) -> String {
20-
let config = repo.config_snapshot();
21-
let remotes = match config.plumbing().sections_by_name("remote") {
22-
Some(sections) => sections,
23-
None => return String::default(),
20+
pub fn get_repo_url(repo: &Repository, hide_token: bool, http_url: bool) -> Result<String> {
21+
let remote = match repo.try_find_remote("origin") {
22+
Some(remote) => remote?,
23+
None => return Ok(String::new()),
2424
};
2525

26-
let mut remote_url: Option<String> = None;
27-
for (name, url) in remotes.filter_map(|section| {
28-
let remote_name = section.header().subsection_name()?;
29-
let url = section.value("url")?;
30-
(remote_name, url).into()
31-
}) {
32-
remote_url = url.to_string().into();
33-
if name == "origin" {
34-
break;
35-
}
36-
}
26+
Ok(remote
27+
.url(gix::remote::Direction::Push)
28+
.map(|url| format_url(&url.to_string(), hide_token, http_url))
29+
.unwrap_or_default())
3730

38-
match remote_url {
39-
Some(url) => format_url(&url, hide_token, http_url),
40-
None => String::default(),
41-
}
4231
}
4332

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

0 commit comments

Comments
 (0)