Skip to content

Commit

Permalink
Fix repo name extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
m1guelpf committed Aug 4, 2023
1 parent d9e2c89 commit e06b3ad
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,24 @@ pub fn get_repo_name(repo: &Repository) -> Option<String> {
let remote = repo.find_remote("origin").ok()?;
let url = remote.url()?;

let split_by_colon: Vec<&str> = url.rsplitn(2, ':').collect();
let split_by_slash: Vec<String> = if split_by_colon.len() > 1 {
// If it's an SSH url, we split the second part by /
split_by_colon[0]
.rsplitn(2, '/')
.map(ToString::to_string)
.collect()
} else {
// If it's an HTTPS url, we remove .git and split by /
url.replace(".git", "")
.rsplitn(3, '/')
.map(ToString::to_string)
.collect()
};
let url_parts: Vec<&str> = url.split('/').collect();
if url.starts_with("http") && url_parts.len() >= 5 {
return Some(format!(
"{}/{}",
url_parts.get(3)?,
url_parts.get(4)?.trim_end_matches(".git")
));
}

if url.contains('@') && url.contains(':') {
let url_parts: Vec<&str> = url.split(':').collect();

if split_by_slash.len() < 2 {
return None;
return url_parts
.get(1)
.map(|s| s.trim_end_matches(".git").to_string());
}

Some(format!("{}/{}", split_by_slash[1], split_by_slash[0]))
None
}

#[derive(Debug, Clone, serde::Serialize)]
Expand Down

0 comments on commit e06b3ad

Please sign in to comment.