From d2d0eeb72912809a00f09fafdae4e827a34d0e26 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Mon, 9 Sep 2024 07:36:47 +0100 Subject: feat(remote): push protocol selection / fallback enable override from nostr url clone url is filesystem use filesystem otherwise try ssh, then https authenticated unless clone url is http, then try ssh then http as we assume, we are on a local trusted network. --- src/bin/git_remote_nostr/fetch.rs | 6 ++- src/bin/git_remote_nostr/list.rs | 8 ++-- src/bin/git_remote_nostr/push.rs | 92 +++++++++++++++++++++++++++++++-------- src/bin/git_remote_nostr/utils.rs | 76 +++++++++++++++----------------- 4 files changed, 117 insertions(+), 65 deletions(-) (limited to 'src/bin/git_remote_nostr') diff --git a/src/bin/git_remote_nostr/fetch.rs b/src/bin/git_remote_nostr/fetch.rs index c48da85..83b94b8 100644 --- a/src/bin/git_remote_nostr/fetch.rs +++ b/src/bin/git_remote_nostr/fetch.rs @@ -15,7 +15,7 @@ use ngit::{ }; use crate::utils::{ - error_is_not_authentication_failure, find_proposal_and_patches_by_branch_name, + fetch_or_list_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, }; @@ -140,7 +140,9 @@ 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) { + if protocol == &ServerProtocol::Ssh + && fetch_or_list_error_is_not_authentication_failure(&error) + { // authenticated by failed to complete request break; } diff --git a/src/bin/git_remote_nostr/list.rs b/src/bin/git_remote_nostr/list.rs index 3614626..c8843e3 100644 --- a/src/bin/git_remote_nostr/list.rs +++ b/src/bin/git_remote_nostr/list.rs @@ -22,8 +22,8 @@ use repo_ref::RepoRef; use crate::{ git::Repo, utils::{ - error_is_not_authentication_failure, get_open_proposals, get_read_protocols_to_try, - get_short_git_server_name, join_with_and, + fetch_or_list_error_is_not_authentication_failure, get_open_proposals, + get_read_protocols_to_try, get_short_git_server_name, join_with_and, }, }; @@ -197,7 +197,9 @@ 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) { + if protocol == &ServerProtocol::Ssh + && fetch_or_list_error_is_not_authentication_failure(&error) + { // authenticated by failed to complete request break; } diff --git a/src/bin/git_remote_nostr/push.rs b/src/bin/git_remote_nostr/push.rs index 2c9c7e1..364555a 100644 --- a/src/bin/git_remote_nostr/push.rs +++ b/src/bin/git_remote_nostr/push.rs @@ -4,7 +4,7 @@ use std::{ io::Stdin, }; -use anyhow::{bail, Context, Result}; +use anyhow::{anyhow, bail, Context, Result}; use auth_git2::GitAuthenticator; use client::{get_events_from_cache, get_state_from_cache, send_events, sign_event, STATE_KIND}; use console::Term; @@ -15,7 +15,10 @@ use git_events::{ }; use ngit::{ client::{self, get_event_from_cache_by_id}, - git::{self, nostr_url::NostrUrlDecoded}, + git::{ + self, + nostr_url::{CloneUrl, NostrUrlDecoded}, + }, git_events::{self, get_event_root}, login::{self, get_curent_user}, repo_ref, repo_state, @@ -34,7 +37,8 @@ use crate::{ list::list_from_remotes, utils::{ find_proposal_and_patches_by_branch_name, get_all_proposals, get_remote_name_by_url, - get_short_git_server_name, read_line, switch_clone_url_between_ssh_and_https, + get_short_git_server_name, get_write_protocols_to_try, join_with_and, + push_error_is_not_authentication_failure, read_line, }, }; @@ -315,27 +319,21 @@ pub async fn run_push( } // TODO make async - check gitlib2 callbacks work async + for (git_server_url, remote_refspecs) in remote_refspecs { let remote_refspecs = remote_refspecs .iter() .filter(|refspec| git_server_refspecs.contains(refspec)) .cloned() .collect::>(); - if !refspecs.is_empty() - && push_to_remote(git_repo, &git_server_url, &remote_refspecs, &term).is_err() - { - if let Ok(alternative_url) = switch_clone_url_between_ssh_and_https(&git_server_url) { - if push_to_remote(git_repo, &alternative_url, &remote_refspecs, &term).is_err() { - // errors get printed as part of callback - // TODO prevent 2 warning messages and instead use one - // to say it didnt work over either https or ssh - } else { - term.write_line( - format!("but succeed over alterantive protocol {alternative_url}",) - .as_str(), - )?; - } - } + if !refspecs.is_empty() { + let _ = push_to_remote( + git_repo, + &git_server_url, + decoded_nostr_url, + &remote_refspecs, + &term, + ); } } println!(); @@ -343,6 +341,64 @@ pub async fn run_push( } fn push_to_remote( + git_repo: &Repo, + git_server_url: &str, + decoded_nostr_url: &NostrUrlDecoded, + remote_refspecs: &[String], + term: &Term, +) -> Result<()> { + let server_url = git_server_url.parse::()?; + let protocols_to_attempt = get_write_protocols_to_try(&server_url, decoded_nostr_url); + + let mut failed_protocols = vec![]; + let mut success = false; + + for protocol in &protocols_to_attempt { + term.write_line( + format!( + "fetching ref list over {protocol} from {}...", + server_url.short_name(), + ) + .as_str(), + )?; + + let formatted_url = server_url.format_as(protocol, &decoded_nostr_url.user)?; + + if let Err(error) = push_to_remote_url(git_repo, &formatted_url, remote_refspecs, term) { + term.write_line( + format!("push: {formatted_url} failed over {protocol}: {error}").as_str(), + )?; + failed_protocols.push(protocol); + if push_error_is_not_authentication_failure(&error) { + break; + } + } else { + success = true; + if !failed_protocols.is_empty() { + term.write_line(format!("fetch: succeeded over {protocol}").as_str())?; + } + } + term.clear_last_lines(1)?; + } + if success { + Ok(()) + } else { + let error = anyhow!( + "{} failed over {}{}", + server_url.short_name(), + 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) + } +} + +fn push_to_remote_url( git_repo: &Repo, git_server_url: &str, remote_refspecs: &[String], diff --git a/src/bin/git_remote_nostr/utils.rs b/src/bin/git_remote_nostr/utils.rs index 90ea848..3039fe3 100644 --- a/src/bin/git_remote_nostr/utils.rs +++ b/src/bin/git_remote_nostr/utils.rs @@ -91,47 +91,6 @@ pub fn read_line<'a>(stdin: &io::Stdin, line: &'a mut String) -> io::Result Result { - if url.starts_with("https://") { - // Convert HTTPS to git@ syntax - let parts: Vec<&str> = url.trim_start_matches("https://").split('/').collect(); - if parts.len() >= 2 { - // Construct the git@ URL - Ok(format!("git@{}:{}", parts[0], parts[1..].join("/"))) - } else { - // If the format is unexpected, return an error - bail!("Invalid HTTPS URL format: {}", url); - } - } else if url.starts_with("ssh://") { - // Convert SSH to git@ syntax - let parts: Vec<&str> = url.trim_start_matches("ssh://").split('/').collect(); - if parts.len() >= 2 { - // Construct the git@ URL - Ok(format!("git@{}:{}", parts[0], parts[1..].join("/"))) - } else { - // If the format is unexpected, return an error - bail!("Invalid SSH URL format: {}", url); - } - } else if url.starts_with("git@") { - // Convert git@ syntax to HTTPS - let parts: Vec<&str> = url.split(':').collect(); - if parts.len() == 2 { - // Construct the HTTPS URL - Ok(format!( - "https://{}/{}", - parts[0].trim_end_matches('@'), - parts[1] - )) - } else { - // If the format is unexpected, return an error - bail!("Invalid git@ URL format: {}", url); - } - } else { - // If the URL is neither HTTPS, SSH, nor git@, return an error - bail!("Unsupported URL protocol: {}", url); - } -} - pub async fn get_open_proposals( git_repo: &Repo, repo_ref: &RepoRef, @@ -297,7 +256,40 @@ pub fn get_read_protocols_to_try( } } -pub fn error_is_not_authentication_failure(error: &anyhow::Error) -> bool { +/// get an ordered vector of server protocols to attempt +pub fn get_write_protocols_to_try( + server_url: &CloneUrl, + decoded_nostr_url: &NostrUrlDecoded, +) -> Vec { + if server_url.protocol() == ServerProtocol::Filesystem { + vec![(ServerProtocol::Filesystem)] + } else if let Some(protocol) = &decoded_nostr_url.protocol { + vec![protocol.clone()] + } else if server_url.protocol() == ServerProtocol::Http { + vec![ + ServerProtocol::Ssh, + // note: list and fetch stop here if ssh was authenticated + ServerProtocol::Http, + ] + } else if server_url.protocol() == ServerProtocol::Ftp { + vec![ServerProtocol::Ssh, ServerProtocol::Ftp] + } else { + vec![ + ServerProtocol::Ssh, + // note: list and fetch stop here if ssh was authenticated + ServerProtocol::Https, + ] + } +} + +/// to understand whether to try over another protocol +pub fn fetch_or_list_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") +} + +/// to understand whether to try over another protocol +pub fn push_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") } -- cgit v1.2.3