guess host from remote tracking branch before enumerating remotes

This commit is contained in:
Cyborus 2024-04-30 10:51:24 -04:00
parent a002adc7fe
commit 4ecb241fb5
No known key found for this signature in database

View file

@ -108,25 +108,46 @@ impl RepoInfo {
let (remote_url, remote_repo_name) = { let (remote_url, remote_repo_name) = {
let mut out = (None, None); let mut out = (None, None);
if let Ok(local_repo) = git2::Repository::open(".") { if let Ok(local_repo) = git2::Repository::open(".") {
// help to escape scopes
let tmp; let tmp;
let mut tmp2;
let mut name = remote; let mut name = remote;
// if the user didn't specify a remote, try guessing other ways // if there's only one remote, use that
let mut tmp2;
if name.is_none() { if name.is_none() {
let all_remotes = local_repo.remotes()?; let all_remotes = local_repo.remotes()?;
// if there's only one remote, use that
if all_remotes.len() == 1 { if all_remotes.len() == 1 {
if let Some(remote_name) = all_remotes.get(0) { if let Some(remote_name) = all_remotes.get(0) {
tmp2 = Some(remote_name.to_owned()); tmp2 = Some(remote_name.to_owned());
name = tmp2.as_deref(); name = tmp2.as_deref();
} }
}
}
// if the current branch is tracking a remote branch, use that remote
if name.is_none() {
let head = local_repo.head()?;
let branch_name = head.name().ok_or_else(|| eyre!("branch name not UTF-8"))?;
tmp = local_repo.branch_upstream_remote(branch_name).ok();
name = tmp
.as_ref()
.map(|remote| {
remote
.as_str()
.ok_or_else(|| eyre!("remote name not UTF-8"))
})
.transpose()?;
}
// if there's a remote whose host url matches the one // if there's a remote whose host url matches the one
// specified with `--host`, use that // specified with `--host`, use that
// //
// This is different than using `--host` itself, since this // This is different than using `--host` itself, since this
// will include the repo name, which `--host` can't do. // will include the repo name, which `--host` can't do.
} else if let Some(host_url) = &host_url { if name.is_none() {
if let Some(host_url) = &host_url {
let all_remotes = local_repo.remotes()?;
for remote_name in all_remotes.iter() { for remote_name in all_remotes.iter() {
let Some(remote_name) = remote_name else { let Some(remote_name) = remote_name else {
continue; continue;
@ -146,22 +167,6 @@ impl RepoInfo {
} }
} }
// if there isn't an obvious answer, guess from the current
// branch's tracking remote
if name.is_none() {
let head = local_repo.head()?;
let branch_name = head.name().ok_or_else(|| eyre!("branch name not UTF-8"))?;
tmp = local_repo.branch_upstream_remote(branch_name).ok();
name = tmp
.as_ref()
.map(|remote| {
remote
.as_str()
.ok_or_else(|| eyre!("remote name not UTF-8"))
})
.transpose()?;
}
if let Some(name) = name { if let Some(name) = name {
if let Ok(remote) = local_repo.find_remote(name) { if let Ok(remote) = local_repo.find_remote(name) {
let url_s = std::str::from_utf8(remote.url_bytes())?; let url_s = std::str::from_utf8(remote.url_bytes())?;