From 0b9ebd63be13ee403c415d4a29538b94f9e7cffe Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 6 Sep 2024 11:29:27 +0100 Subject: feat(remote): just use ssh if auth succeeds and don't proceed to https or http --- src/bin/git_remote_nostr/fetch.rs | 33 ++++++++++++++++++++------------- src/bin/git_remote_nostr/list.rs | 7 ++++++- src/bin/git_remote_nostr/utils.rs | 7 +++++++ 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/bin/git_remote_nostr/fetch.rs b/src/bin/git_remote_nostr/fetch.rs index f591fed..0a98db2 100644 --- a/src/bin/git_remote_nostr/fetch.rs +++ b/src/bin/git_remote_nostr/fetch.rs @@ -1,6 +1,6 @@ use std::io::Stdin; -use anyhow::{bail, Result}; +use anyhow::{anyhow, bail, Result}; use auth_git2::GitAuthenticator; use git2::Repository; use ngit::{ @@ -15,8 +15,8 @@ use ngit::{ }; use crate::utils::{ - find_proposal_and_patches_by_branch_name, get_oids_from_fetch_batch, get_open_proposals, - get_read_protocols_to_try, join_with_and, + error_is_not_authentication_failure, find_proposal_and_patches_by_branch_name, + get_oids_from_fetch_batch, get_open_proposals, get_read_protocols_to_try, join_with_and, }; pub async fn run_fetch( @@ -136,6 +136,10 @@ fn fetch_from_git_server( format!("fetch: {formatted_url} failed over {protocol}: {error}").as_str(), )?; failed_protocols.push(protocol); + if protocol == &ServerProtocol::Ssh && error_is_not_authentication_failure(&error) { + // authenticated by failed to complete request + break; + } } else { success = true; if !failed_protocols.is_empty() { @@ -143,19 +147,22 @@ fn fetch_from_git_server( } } } - if !success { - if decoded_nostr_url.protocol.is_some() { - term.write_line( - "fetch: protocol override in nostr url so not attempting with any other protocols", - )?; - } - bail!( - "{} failed over {}", + if success { + Ok(()) + } else { + let error = anyhow!( + "{} failed over {}{}", server_url.domain(), - join_with_and(&failed_protocols) + join_with_and(&failed_protocols), + if decoded_nostr_url.protocol.is_some() { + " and nostr url contains protocol override so no other protocols were attempted" + } else { + "" + }, ); + term.write_line(format!("fetch: {error}").as_str())?; + Err(error) } - Ok(()) } fn fetch_from_git_server_url( diff --git a/src/bin/git_remote_nostr/list.rs b/src/bin/git_remote_nostr/list.rs index 26f699d..ae76614 100644 --- a/src/bin/git_remote_nostr/list.rs +++ b/src/bin/git_remote_nostr/list.rs @@ -22,7 +22,8 @@ use repo_ref::RepoRef; use crate::{ git::Repo, utils::{ - get_open_proposals, get_read_protocols_to_try, get_short_git_server_name, join_with_and, + error_is_not_authentication_failure, get_open_proposals, get_read_protocols_to_try, + get_short_git_server_name, join_with_and, }, }; @@ -196,6 +197,10 @@ pub fn list_from_remote( format!("list: {formatted_url} failed over {protocol}: {error}").as_str(), )?; failed_protocols.push(protocol); + if protocol == &ServerProtocol::Ssh && error_is_not_authentication_failure(&error) { + // authenticated by failed to complete request + break; + } } } term.clear_last_lines(1)?; diff --git a/src/bin/git_remote_nostr/utils.rs b/src/bin/git_remote_nostr/utils.rs index a31dcbf..90ea848 100644 --- a/src/bin/git_remote_nostr/utils.rs +++ b/src/bin/git_remote_nostr/utils.rs @@ -282,6 +282,7 @@ pub fn get_read_protocols_to_try( vec![ ServerProtocol::UnauthHttp, ServerProtocol::Ssh, + // note: list and fetch stop here if ssh was authenticated ServerProtocol::Http, ] } else if server_url.protocol() == ServerProtocol::Ftp { @@ -290,11 +291,17 @@ pub fn get_read_protocols_to_try( vec![ ServerProtocol::UnauthHttps, ServerProtocol::Ssh, + // note: list and fetch stop here if ssh was authenticated ServerProtocol::Https, ] } } +pub fn error_is_not_authentication_failure(error: &anyhow::Error) -> bool { + let error_str = error.to_string(); + error_str.contains("Permission to") || error_str.contains("Repository not found") +} + #[cfg(test)] mod tests { use super::*; -- cgit v1.2.3