From 513fce723c7e37aa353844303f36022517f2db43 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 25 Jul 2025 21:02:26 +0100 Subject: refactor: move `utils` and `list` helpers to lib to enable forthcoming `ngit sync` cmd --- src/lib/list.rs | 159 ++++++++++++++++++++ src/lib/mod.rs | 2 + src/lib/utils.rs | 449 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 610 insertions(+) create mode 100644 src/lib/list.rs create mode 100644 src/lib/utils.rs (limited to 'src/lib') diff --git a/src/lib/list.rs b/src/lib/list.rs new file mode 100644 index 0000000..b940546 --- /dev/null +++ b/src/lib/list.rs @@ -0,0 +1,159 @@ +use std::collections::HashMap; + +use anyhow::{Result, anyhow}; +use auth_git2::GitAuthenticator; +use nostr::hashes::sha1::Hash as Sha1Hash; + +use crate::{ + git::{ + Repo, RepoActions, + nostr_url::{CloneUrl, NostrUrlDecoded, ServerProtocol}, + }, + repo_ref::is_grasp_server, + utils::{Direction, get_read_protocols_to_try, join_with_and, set_protocol_preference}, +}; + +pub fn list_from_remotes( + term: &console::Term, + git_repo: &Repo, + git_servers: &Vec, + decoded_nostr_url: &NostrUrlDecoded, + grasp_servers: &[String], +) -> HashMap, bool)> { + let mut remote_states = HashMap::new(); + let mut errors = HashMap::new(); + for url in git_servers { + let is_grasp_server = is_grasp_server(url, grasp_servers); + match list_from_remote(term, git_repo, url, decoded_nostr_url, is_grasp_server) { + Err(error) => { + errors.insert(url, error); + } + Ok(state) => { + remote_states.insert(url.to_string(), (state, is_grasp_server)); + } + } + } + remote_states +} + +pub fn list_from_remote( + term: &console::Term, + git_repo: &Repo, + git_server_url: &str, + decoded_nostr_url: &NostrUrlDecoded, + is_grasp_server: bool, +) -> Result> { + let server_url = git_server_url.parse::()?; + let protocols_to_attempt = + get_read_protocols_to_try(git_repo, &server_url, decoded_nostr_url, is_grasp_server); + + let mut failed_protocols = vec![]; + let mut remote_state: Option> = None; + + for protocol in &protocols_to_attempt { + term.write_line( + format!( + "fetching {} ref list over {protocol}...", + server_url.short_name(), + ) + .as_str(), + )?; + + let formatted_url = server_url.format_as(protocol, &decoded_nostr_url.user)?; + let res = list_from_remote_url( + git_repo, + &formatted_url, + [ServerProtocol::UnauthHttps, ServerProtocol::UnauthHttp].contains(protocol), + term, + ); + + match res { + Ok(state) => { + remote_state = Some(state); + term.clear_last_lines(1)?; + if !failed_protocols.is_empty() { + term.write_line( + format!( + "list: succeeded over {protocol} from {}", + server_url.short_name(), + ) + .as_str(), + )?; + let _ = + set_protocol_preference(git_repo, protocol, &server_url, &Direction::Fetch); + } + break; + } + Err(error) => { + term.clear_last_lines(1)?; + term.write_line( + format!("list: {formatted_url} failed over {protocol}: {error}").as_str(), + )?; + failed_protocols.push(protocol); + } + } + } + if let Some(remote_state) = remote_state { + if failed_protocols.is_empty() { + term.clear_last_lines(1)?; + } + Ok(remote_state) + } 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!("list: {error}").as_str())?; + Err(error) + } +} + +fn list_from_remote_url( + git_repo: &Repo, + git_server_remote_url: &str, + dont_authenticate: bool, + term: &console::Term, +) -> Result> { + let git_config = git_repo.git_repo.config()?; + + let mut git_server_remote = git_repo.git_repo.remote_anonymous(git_server_remote_url)?; + // authentication may be required + let auth = GitAuthenticator::default(); + let mut remote_callbacks = git2::RemoteCallbacks::new(); + if !dont_authenticate { + remote_callbacks.credentials(auth.credentials(&git_config)); + } + term.write_line("list: connecting...")?; + git_server_remote.connect_auth(git2::Direction::Fetch, Some(remote_callbacks), None)?; + term.clear_last_lines(1)?; + let mut state = HashMap::new(); + for head in git_server_remote.list()? { + if let Some(symbolic_reference) = head.symref_target() { + state.insert( + head.name().to_string(), + format!("ref: {symbolic_reference}"), + ); + // ignore dereferenced tags + } else if !head.name().to_string().ends_with("^{}") { + state.insert(head.name().to_string(), head.oid().to_string()); + } + } + git_server_remote.disconnect()?; + Ok(state) +} + +pub fn get_ahead_behind( + git_repo: &Repo, + base_ref_or_oid: &str, + latest_ref_or_oid: &str, +) -> Result<(Vec, Vec)> { + let base = git_repo.get_commit_or_tip_of_reference(base_ref_or_oid)?; + let latest = git_repo.get_commit_or_tip_of_reference(latest_ref_or_oid)?; + git_repo.get_commits_ahead_behind(&base, &latest) +} diff --git a/src/lib/mod.rs b/src/lib/mod.rs index 7c7bd6a..076c2d2 100644 --- a/src/lib/mod.rs +++ b/src/lib/mod.rs @@ -2,9 +2,11 @@ pub mod cli_interactor; pub mod client; pub mod git; pub mod git_events; +pub mod list; pub mod login; pub mod repo_ref; pub mod repo_state; +pub mod utils; use anyhow::{Result, anyhow}; use directories::ProjectDirs; diff --git a/src/lib/utils.rs b/src/lib/utils.rs new file mode 100644 index 0000000..431757f --- /dev/null +++ b/src/lib/utils.rs @@ -0,0 +1,449 @@ +use core::str; +use std::{ + collections::HashMap, + fmt, + io::{self, Stdin}, + str::FromStr, +}; + +use anyhow::{Context, Result, bail}; +use git2::Repository; +use nostr_sdk::{Event, EventId, Kind, PublicKey, Url}; + +use crate::{ + client::{ + get_all_proposal_patch_pr_pr_update_events_from_cache, get_events_from_local_cache, + get_proposals_and_revisions_from_cache, + }, + git::{ + Repo, RepoActions, + nostr_url::{CloneUrl, NostrUrlDecoded, ServerProtocol}, + }, + git_events::{ + event_is_revision_root, get_pr_tip_event_or_most_recent_patch_with_ancestors, get_status, + is_event_proposal_root_for_branch, status_kinds, + }, + repo_ref::RepoRef, +}; + +pub fn get_short_git_server_name(git_repo: &Repo, url: &str) -> std::string::String { + if let Ok(name) = get_remote_name_by_url(&git_repo.git_repo, url) { + return name; + } + if let Ok(url) = Url::parse(url) { + if let Some(domain) = url.domain() { + return domain.to_string(); + } + } + url.to_string() +} + +pub fn get_remote_name_by_url(git_repo: &Repository, url: &str) -> Result { + let remotes = git_repo.remotes()?; + Ok(remotes + .iter() + .find(|r| { + if let Some(name) = r { + if let Some(remote_url) = git_repo.find_remote(name).unwrap().url() { + url == remote_url + } else { + false + } + } else { + false + } + }) + .context("could not find remote with matching url")? + .context("remote with matching url must be named")? + .to_string()) +} + +pub fn get_oids_from_fetch_batch( + stdin: &Stdin, + initial_oid: &str, + initial_refstr: &str, +) -> Result> { + let mut line = String::new(); + let mut batch = HashMap::new(); + batch.insert(initial_refstr.to_string(), initial_oid.to_string()); + loop { + let tokens = read_line(stdin, &mut line)?; + match tokens.as_slice() { + ["fetch", oid, refstr] => { + batch.insert((*refstr).to_string(), (*oid).to_string()); + } + [] => break, + _ => bail!( + "after a `fetch` command we are only expecting another fetch or an empty line" + ), + } + } + Ok(batch) +} + +/// Read one line from stdin, and split it into tokens. +pub fn read_line<'a>(stdin: &io::Stdin, line: &'a mut String) -> io::Result> { + line.clear(); + + let read = stdin.read_line(line)?; + if read == 0 { + return Ok(vec![]); + } + let line = line.trim(); + let tokens = line.split(' ').filter(|t| !t.is_empty()).collect(); + + Ok(tokens) +} + +pub async fn get_open_or_draft_proposals( + git_repo: &Repo, + repo_ref: &RepoRef, +) -> Result)>> { + let git_repo_path = git_repo.get_path()?; + let proposals: Vec = + get_proposals_and_revisions_from_cache(git_repo_path, repo_ref.coordinates()) + .await? + .iter() + .filter(|e| + // If we wanted to treat to list Pull Requests that revise a Patch we would do this: + // e.kind.eq(&KIND_PULL_REQUEST) || + !event_is_revision_root(e)) + .cloned() + .collect(); + + let statuses: Vec = { + let mut statuses = get_events_from_local_cache( + git_repo_path, + vec![ + nostr::Filter::default() + .kinds(status_kinds().clone()) + .events(proposals.iter().map(|e| e.id)), + ], + ) + .await?; + statuses.sort_by_key(|e| e.created_at); + statuses.reverse(); + statuses + }; + let mut open_or_draft_proposals = HashMap::new(); + + for proposal in &proposals { + let status = get_status(proposal, repo_ref, &statuses, &proposals); + if [Kind::GitStatusOpen, Kind::GitStatusDraft].contains(&status) { + if let Ok(commits_events) = get_all_proposal_patch_pr_pr_update_events_from_cache( + git_repo_path, + repo_ref, + &proposal.id, + ) + .await + { + if let Ok(most_recent_proposal_patch_chain) = + get_pr_tip_event_or_most_recent_patch_with_ancestors(commits_events.clone()) + { + open_or_draft_proposals.insert( + proposal.id, + (proposal.clone(), most_recent_proposal_patch_chain), + ); + } + } + } + } + Ok(open_or_draft_proposals) +} + +pub async fn get_all_proposals( + git_repo: &Repo, + repo_ref: &RepoRef, +) -> Result)>> { + let git_repo_path = git_repo.get_path()?; + let proposals: Vec = + get_proposals_and_revisions_from_cache(git_repo_path, repo_ref.coordinates()) + .await? + .iter() + .filter(|e| + // If we wanted to treat to list Pull Requests that revise a Patch we would do this: + // e.kind.eq(&KIND_PULL_REQUEST) || + !event_is_revision_root(e)) + .cloned() + .collect(); + + let mut all_proposals = HashMap::new(); + + for proposal in proposals { + if let Ok(commits_events) = get_all_proposal_patch_pr_pr_update_events_from_cache( + git_repo_path, + repo_ref, + &proposal.id, + ) + .await + { + if let Ok(most_recent_proposal_patch_chain) = + get_pr_tip_event_or_most_recent_patch_with_ancestors(commits_events.clone()) + { + all_proposals.insert(proposal.id, (proposal, most_recent_proposal_patch_chain)); + } + } + } + Ok(all_proposals) +} + +pub fn find_proposal_and_patches_by_branch_name<'a>( + refstr: &'a str, + proposals: &'a HashMap)>, + current_user: Option<&PublicKey>, +) -> Option<(&'a EventId, &'a (Event, Vec))> { + proposals.iter().find(|(_, (proposal, _))| { + is_event_proposal_root_for_branch(proposal, refstr, current_user).unwrap_or(false) + }) +} + +pub fn join_with_and(items: &[T]) -> String { + match items.len() { + 0 => String::new(), + 1 => items[0].to_string(), + _ => { + let last_item = items.last().unwrap().to_string(); + let rest = &items[..items.len() - 1]; + format!( + "{} and {}", + rest.iter() + .map(std::string::ToString::to_string) + .collect::>() + .join(", "), + last_item + ) + } + } +} + +/// get an ordered vector of server protocols to attempt +pub fn get_read_protocols_to_try( + git_repo: &Repo, + server_url: &CloneUrl, + decoded_nostr_url: &NostrUrlDecoded, + is_grasp_server: bool, +) -> Vec { + if is_grasp_server { + if server_url.protocol() == ServerProtocol::Http { + vec![(ServerProtocol::UnauthHttp)] + } else { + vec![(ServerProtocol::UnauthHttps)] + } + } else if server_url.protocol() == ServerProtocol::Filesystem { + vec![(ServerProtocol::Filesystem)] + } else if let Some(protocol) = &decoded_nostr_url.protocol { + vec![protocol.clone()] + } else { + let mut list = if server_url.protocol() == ServerProtocol::Http { + vec![ + ServerProtocol::UnauthHttp, + ServerProtocol::Ssh, + // note: list and fetch stop here if ssh was authenticated + ServerProtocol::Http, + ] + } else if server_url.protocol() == ServerProtocol::Ftp { + vec![ServerProtocol::Ftp, ServerProtocol::Ssh] + } else if server_url.protocol() == ServerProtocol::Git { + vec![ + ServerProtocol::Git, + ServerProtocol::UnauthHttps, + ServerProtocol::Ssh, + // note: list and fetch stop here if ssh was authenticated + ServerProtocol::Https, + ] + } else { + vec![ + ServerProtocol::UnauthHttps, + ServerProtocol::Ssh, + // note: list and fetch stop here if ssh was authenticated + ServerProtocol::Https, + ] + }; + if let Some(protocol) = get_protocol_preference(git_repo, server_url, &Direction::Fetch) { + if let Some(pos) = list.iter().position(|p| *p == protocol) { + list.remove(pos); + list.insert(0, protocol); + } + } + list + } +} + +/// get an ordered vector of server protocols to attempt +pub fn get_write_protocols_to_try( + git_repo: &Repo, + server_url: &CloneUrl, + decoded_nostr_url: &NostrUrlDecoded, + is_grasp_server: bool, +) -> Vec { + if is_grasp_server { + if server_url.protocol() == ServerProtocol::Http { + vec![(ServerProtocol::UnauthHttp)] + } else { + vec![(ServerProtocol::UnauthHttps)] + } + } else if server_url.protocol() == ServerProtocol::Filesystem { + vec![(ServerProtocol::Filesystem)] + } else if let Some(protocol) = &decoded_nostr_url.protocol { + vec![protocol.clone()] + } else { + let mut list = 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 if server_url.protocol() == ServerProtocol::Git { + vec![ + ServerProtocol::Ssh, + ServerProtocol::Git, + // note: list and fetch stop here if ssh was authenticated + ServerProtocol::Https, + ] + } else { + vec![ + ServerProtocol::Ssh, + // note: list and fetch stop here if ssh was authenticated + ServerProtocol::Https, + ] + }; + if let Some(protocol) = get_protocol_preference(git_repo, server_url, &Direction::Push) { + if let Some(pos) = list.iter().position(|p| *p == protocol) { + list.remove(pos); + list.insert(0, protocol); + } + } + + list + } +} + +#[derive(Debug, PartialEq)] +pub enum Direction { + Push, + Fetch, +} +impl fmt::Display for Direction { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Direction::Push => write!(f, "push"), + Direction::Fetch => write!(f, "fetch"), + } + } +} + +pub fn get_protocol_preference( + git_repo: &Repo, + server_url: &CloneUrl, + direction: &Direction, +) -> Option { + let server_short_name = server_url.short_name(); + if let Ok(Some(list)) = + git_repo.get_git_config_item(format!("nostr.protocol-{direction}").as_str(), Some(false)) + { + for item in list.split(';') { + let pair = item.split(',').collect::>(); + if let Some(url) = pair.get(1) { + if *url == server_short_name { + if let Some(protocol) = pair.first() { + if let Ok(protocol) = ServerProtocol::from_str(protocol) { + return Some(protocol); + } + } + } + } + } + } + None +} + +pub fn set_protocol_preference( + git_repo: &Repo, + protocol: &ServerProtocol, + server_url: &CloneUrl, + direction: &Direction, +) -> Result<()> { + let server_short_name = server_url.short_name(); + let mut new = String::new(); + if let Some(list) = + git_repo.get_git_config_item(format!("nostr.protocol-{direction}").as_str(), Some(false))? + { + for item in list.split(';') { + let pair = item.split(',').collect::>(); + if let Some(url) = pair.get(1) { + if *url != server_short_name && !item.is_empty() { + new.push_str(format!("{item};").as_str()); + } + } + } + } + new.push_str(format!("{protocol},{server_short_name};").as_str()); + + git_repo.save_git_config_item( + format!("nostr.protocol-{direction}").as_str(), + new.as_str(), + false, + ) +} + +pub fn error_might_be_authentication_related(error: &anyhow::Error) -> bool { + let error_str = error.to_string(); + for s in [ + "no ssh keys found", + "invalid or unknown remote ssh hostkey", + "authentication", + "Permission", + "permission", + "not found", + ] { + if error_str.contains(s) { + return true; + } + } + false +} + +#[cfg(test)] +mod tests { + use super::*; + mod join_with_and { + use super::*; + #[test] + fn test_empty() { + let items: Vec<&str> = vec![]; + assert_eq!(join_with_and(&items), ""); + } + + #[test] + fn test_single_item() { + let items = vec!["apple"]; + assert_eq!(join_with_and(&items), "apple"); + } + + #[test] + fn test_two_items() { + let items = vec!["apple", "banana"]; + assert_eq!(join_with_and(&items), "apple and banana"); + } + + #[test] + fn test_three_items() { + let items = vec!["apple", "banana", "cherry"]; + assert_eq!(join_with_and(&items), "apple, banana and cherry"); + } + + #[test] + fn test_four_items() { + let items = vec!["apple", "banana", "cherry", "date"]; + assert_eq!(join_with_and(&items), "apple, banana, cherry and date"); + } + + #[test] + fn test_multiple_items() { + let items = vec!["one", "two", "three", "four", "five"]; + assert_eq!(join_with_and(&items), "one, two, three, four and five"); + } + } +} -- cgit v1.2.3 From e5cc566be82bbebb78f2c27ee13f3a5fafa4a0c8 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 25 Jul 2025 22:26:59 +0100 Subject: refactor: move push helpers to lib to enable forthcoming ngit sync cmd --- src/bin/git_remote_nostr/push.rs | 308 +------------------------------------- src/lib/mod.rs | 1 + src/lib/push.rs | 310 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 317 insertions(+), 302 deletions(-) create mode 100644 src/lib/push.rs (limited to 'src/lib') diff --git a/src/bin/git_remote_nostr/push.rs b/src/bin/git_remote_nostr/push.rs index 74e98f9..4e0d760 100644 --- a/src/bin/git_remote_nostr/push.rs +++ b/src/bin/git_remote_nostr/push.rs @@ -2,12 +2,10 @@ use core::str; use std::{ collections::{HashMap, HashSet}, io::Stdin, - sync::{Arc, Mutex}, - time::Instant, + sync::Arc, }; -use anyhow::{Context, Result, anyhow, bail}; -use auth_git2::GitAuthenticator; +use anyhow::{Context, Result, bail}; use client::{get_events_from_local_cache, get_state_from_cache, send_events, sign_event}; use console::Term; use git::{RepoActions, sha1_to_oid}; @@ -17,22 +15,17 @@ use git_events::{ }; use git2::{Oid, Repository}; use ngit::{ - cli_interactor::count_lines_per_msg_vec, client::{self, get_event_from_cache_by_id, sign_draft_event}, - git::{ - self, - nostr_url::{CloneUrl, NostrUrlDecoded}, - oid_to_shorthand_string, - }, + git::{self, nostr_url::NostrUrlDecoded}, git_events::{self, KIND_PULL_REQUEST, event_to_cover_letter, get_event_root}, list::list_from_remotes, login::{self, user::UserRef}, + push::{push_to_remote, push_to_remote_url}, repo_ref::{self, get_repo_config_from_yaml, is_grasp_server, normalize_grasp_server_url}, repo_state, utils::{ - Direction, find_proposal_and_patches_by_branch_name, get_all_proposals, - get_remote_name_by_url, get_short_git_server_name, get_write_protocols_to_try, - join_with_and, read_line, set_protocol_preference, + find_proposal_and_patches_by_branch_name, get_all_proposals, get_remote_name_by_url, + get_short_git_server_name, read_line, }, }; use nostr::{event::UnsignedEvent, nips::nip10::Marker}; @@ -587,295 +580,6 @@ async fn generate_patches_or_pr_event_or_pr_updates( Ok(events) } -fn push_to_remote( - git_repo: &Repo, - git_server_url: &str, - decoded_nostr_url: &NostrUrlDecoded, - remote_refspecs: &[String], - term: &Term, - is_grasp_server: bool, -) -> Result<()> { - let server_url = git_server_url.parse::()?; - let protocols_to_attempt = - get_write_protocols_to_try(git_repo, &server_url, decoded_nostr_url, is_grasp_server); - - let mut failed_protocols = vec![]; - let mut success = false; - - for protocol in &protocols_to_attempt { - term.write_line(format!("push: {} over {protocol}...", 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); - } else { - success = true; - if !failed_protocols.is_empty() { - term.write_line(format!("push: succeeded over {protocol}").as_str())?; - let _ = set_protocol_preference(git_repo, protocol, &server_url, &Direction::Push); - } - break; - } - } - 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!("push: {error}").as_str())?; - Err(error) - } -} - -fn push_to_remote_url( - git_repo: &Repo, - git_server_url: &str, - remote_refspecs: &[String], - term: &Term, -) -> Result<()> { - let git_config = git_repo.git_repo.config()?; - let mut git_server_remote = git_repo.git_repo.remote_anonymous(git_server_url)?; - let auth = GitAuthenticator::default(); - let mut push_options = git2::PushOptions::new(); - let mut remote_callbacks = git2::RemoteCallbacks::new(); - let push_reporter = Arc::new(Mutex::new(PushReporter::new(term))); - - remote_callbacks.credentials(auth.credentials(&git_config)); - - remote_callbacks.push_update_reference({ - let push_reporter = Arc::clone(&push_reporter); - move |name, error| { - let mut reporter = push_reporter.lock().unwrap(); - if let Some(error) = error { - let existing_lines = reporter.count_all_existing_lines(); - reporter.update_reference_errors.push(format!( - "WARNING: {} failed to push {name} error: {error}", - get_short_git_server_name(git_repo, git_server_url), - )); - reporter.write_all(existing_lines); - } - Ok(()) - } - }); - - remote_callbacks.push_negotiation({ - let push_reporter = Arc::clone(&push_reporter); - move |updates| { - let mut reporter = push_reporter.lock().unwrap(); - let existing_lines = reporter.count_all_existing_lines(); - - for update in updates { - let dst_refname = update - .dst_refname() - .unwrap_or("") - .replace("refs/heads/", "") - .replace("refs/tags/", "tags/"); - let msg = if update.dst().is_zero() { - format!("push: - [delete] {dst_refname}") - } else if update.src().is_zero() { - if update.dst_refname().unwrap_or("").contains("refs/tags") { - format!("push: * [new tag] {dst_refname}") - } else { - format!("push: * [new branch] {dst_refname}") - } - } else { - let force = remote_refspecs - .iter() - .any(|r| r.contains(&dst_refname) && r.contains('+')); - format!( - "push: {} {}..{} {} -> {dst_refname}", - if force { "+" } else { " " }, - oid_to_shorthand_string(update.src()).unwrap(), - oid_to_shorthand_string(update.dst()).unwrap(), - update - .src_refname() - .unwrap_or("") - .replace("refs/heads/", "") - .replace("refs/tags/", "tags/"), - ) - }; - // other possibilities will result in push to fail but better reporting is - // needed: - // deleting a non-existant branch: - // ! [remote rejected] -> (not found) - // adding a branch that already exists: - // ! [remote rejected] -> (already exists) - // pushing without non-fast-forward without force: - // ! [rejected] -> (non-fast-forward) - reporter.negotiation.push(msg); - } - reporter.write_all(existing_lines); - Ok(()) - } - }); - - remote_callbacks.push_transfer_progress({ - let push_reporter = Arc::clone(&push_reporter); - #[allow(clippy::cast_precision_loss)] - move |current, total, bytes| { - let mut reporter = push_reporter.lock().unwrap(); - reporter.process_transfer_progress_update(current, total, bytes); - } - }); - - remote_callbacks.sideband_progress({ - let push_reporter = Arc::clone(&push_reporter); - move |data| { - let mut reporter = push_reporter.lock().unwrap(); - reporter.process_remote_msg(data); - true - } - }); - push_options.remote_callbacks(remote_callbacks); - git_server_remote.push(remote_refspecs, Some(&mut push_options))?; - let _ = git_server_remote.disconnect(); - Ok(()) -} - -#[allow(clippy::cast_precision_loss)] -#[allow(clippy::float_cmp)] -#[allow(clippy::needless_pass_by_value)] -fn report_on_transfer_progress( - current: usize, - total: usize, - bytes: usize, - start_time: &Instant, - end_time: Option<&Instant>, -) -> Option { - if total == 0 { - return None; - } - let percentage = ((current as f64 / total as f64) * 100.0) - // always round down because 100% complete is misleading when its not complete - .floor(); - let (size, unit) = if bytes as f64 >= (1024.0 * 1024.0) { - (bytes as f64 / (1024.0 * 1024.0), "MiB") - } else { - (bytes as f64 / 1024.0, "KiB") - }; - let speed = { - let duration = if let Some(end_time) = end_time { - (*end_time - *start_time).as_millis() as f64 - } else { - start_time.elapsed().as_millis() as f64 - }; - - if duration > 0.0 { - (bytes as f64 / (1024.0 * 1024.0)) / (duration / 1000.0) // Convert bytes to MiB and milliseconds to seconds - } else { - 0.0 - } - }; - - Some(format!( - "push: Writing objects: {percentage}% ({current}/{total}) {size:.2} {unit} | {speed:.2} MiB/s{}", - if current == total { ", done." } else { "" }, - )) -} - -struct PushReporter<'a> { - remote_msgs: Vec, - negotiation: Vec, - transfer_progress_msgs: Vec, - update_reference_errors: Vec, - term: &'a console::Term, - start_time: Option, - end_time: Option, -} -impl<'a> PushReporter<'a> { - fn new(term: &'a console::Term) -> Self { - Self { - remote_msgs: vec![], - negotiation: vec![], - transfer_progress_msgs: vec![], - update_reference_errors: vec![], - term, - start_time: None, - end_time: None, - } - } - fn write_all(&self, lines_to_clear: usize) { - let _ = self.term.clear_last_lines(lines_to_clear); - for msg in &self.remote_msgs { - let _ = self.term.write_line(format!("remote: {msg}").as_str()); - } - for msg in &self.negotiation { - let _ = self.term.write_line(msg); - } - for msg in &self.transfer_progress_msgs { - let _ = self.term.write_line(msg); - } - for msg in &self.update_reference_errors { - let _ = self.term.write_line(msg); - } - } - - fn count_all_existing_lines(&self) -> usize { - let width = self.term.size().1; - count_lines_per_msg_vec(width, &self.remote_msgs, "remote: ".len()) - + count_lines_per_msg_vec(width, &self.negotiation, 0) - + count_lines_per_msg_vec(width, &self.transfer_progress_msgs, 0) - + count_lines_per_msg_vec(width, &self.update_reference_errors, 0) - } - fn process_remote_msg(&mut self, data: &[u8]) { - if let Ok(data) = str::from_utf8(data) { - let data = data - .split(['\n', '\r']) - .map(str::trim) - .filter(|line| !line.trim().is_empty()) - .collect::>(); - for data in data { - let existing_lines = self.count_all_existing_lines(); - let msg = data.to_string(); - if let Some(last) = self.remote_msgs.last() { - if (last.contains('%') && !last.contains("100%")) - || last == &msg.replace(", done.", "") - { - self.remote_msgs.pop(); - self.remote_msgs.push(msg); - } else { - self.remote_msgs.push(msg); - } - } else { - self.remote_msgs.push(msg); - } - self.write_all(existing_lines); - } - } - } - fn process_transfer_progress_update(&mut self, current: usize, total: usize, bytes: usize) { - if self.start_time.is_none() { - self.start_time = Some(Instant::now()); - } - if let Some(report) = report_on_transfer_progress( - current, - total, - bytes, - &self.start_time.unwrap(), - self.end_time.as_ref(), - ) { - let existing_lines = self.count_all_existing_lines(); - if report.contains("100%") { - self.end_time = Some(Instant::now()); - } - self.transfer_progress_msgs = vec![report]; - self.write_all(existing_lines); - } - } -} - type HashMapUrlRefspecs = HashMap>; #[allow(clippy::too_many_lines)] diff --git a/src/lib/mod.rs b/src/lib/mod.rs index 076c2d2..265dd6b 100644 --- a/src/lib/mod.rs +++ b/src/lib/mod.rs @@ -4,6 +4,7 @@ pub mod git; pub mod git_events; pub mod list; pub mod login; +pub mod push; pub mod repo_ref; pub mod repo_state; pub mod utils; diff --git a/src/lib/push.rs b/src/lib/push.rs new file mode 100644 index 0000000..0d0ec93 --- /dev/null +++ b/src/lib/push.rs @@ -0,0 +1,310 @@ +use std::{ + sync::{Arc, Mutex}, + time::Instant, +}; + +use anyhow::{Result, anyhow}; +use auth_git2::GitAuthenticator; +use console::Term; + +use crate::{ + cli_interactor::count_lines_per_msg_vec, + git::{ + Repo, + nostr_url::{CloneUrl, NostrUrlDecoded}, + oid_to_shorthand_string, + }, + utils::{ + Direction, get_short_git_server_name, get_write_protocols_to_try, join_with_and, + set_protocol_preference, + }, +}; + +pub fn push_to_remote( + git_repo: &Repo, + git_server_url: &str, + decoded_nostr_url: &NostrUrlDecoded, + remote_refspecs: &[String], + term: &Term, + is_grasp_server: bool, +) -> Result<()> { + let server_url = git_server_url.parse::()?; + let protocols_to_attempt = + get_write_protocols_to_try(git_repo, &server_url, decoded_nostr_url, is_grasp_server); + + let mut failed_protocols = vec![]; + let mut success = false; + + for protocol in &protocols_to_attempt { + term.write_line(format!("push: {} over {protocol}...", 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); + } else { + success = true; + if !failed_protocols.is_empty() { + term.write_line(format!("push: succeeded over {protocol}").as_str())?; + let _ = set_protocol_preference(git_repo, protocol, &server_url, &Direction::Push); + } + break; + } + } + 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!("push: {error}").as_str())?; + Err(error) + } +} + +pub fn push_to_remote_url( + git_repo: &Repo, + git_server_url: &str, + remote_refspecs: &[String], + term: &Term, +) -> Result<()> { + let git_config = git_repo.git_repo.config()?; + let mut git_server_remote = git_repo.git_repo.remote_anonymous(git_server_url)?; + let auth = GitAuthenticator::default(); + let mut push_options = git2::PushOptions::new(); + let mut remote_callbacks = git2::RemoteCallbacks::new(); + let push_reporter = Arc::new(Mutex::new(PushReporter::new(term))); + + remote_callbacks.credentials(auth.credentials(&git_config)); + + remote_callbacks.push_update_reference({ + let push_reporter = Arc::clone(&push_reporter); + move |name, error| { + let mut reporter = push_reporter.lock().unwrap(); + if let Some(error) = error { + let existing_lines = reporter.count_all_existing_lines(); + reporter.update_reference_errors.push(format!( + "WARNING: {} failed to push {name} error: {error}", + get_short_git_server_name(git_repo, git_server_url), + )); + reporter.write_all(existing_lines); + } + Ok(()) + } + }); + + remote_callbacks.push_negotiation({ + let push_reporter = Arc::clone(&push_reporter); + move |updates| { + let mut reporter = push_reporter.lock().unwrap(); + let existing_lines = reporter.count_all_existing_lines(); + + for update in updates { + let dst_refname = update + .dst_refname() + .unwrap_or("") + .replace("refs/heads/", "") + .replace("refs/tags/", "tags/"); + let msg = if update.dst().is_zero() { + format!("push: - [delete] {dst_refname}") + } else if update.src().is_zero() { + if update.dst_refname().unwrap_or("").contains("refs/tags") { + format!("push: * [new tag] {dst_refname}") + } else { + format!("push: * [new branch] {dst_refname}") + } + } else { + let force = remote_refspecs + .iter() + .any(|r| r.contains(&dst_refname) && r.contains('+')); + format!( + "push: {} {}..{} {} -> {dst_refname}", + if force { "+" } else { " " }, + oid_to_shorthand_string(update.src()).unwrap(), + oid_to_shorthand_string(update.dst()).unwrap(), + update + .src_refname() + .unwrap_or("") + .replace("refs/heads/", "") + .replace("refs/tags/", "tags/"), + ) + }; + // other possibilities will result in push to fail but better reporting is + // needed: + // deleting a non-existant branch: + // ! [remote rejected] -> (not found) + // adding a branch that already exists: + // ! [remote rejected] -> (already exists) + // pushing without non-fast-forward without force: + // ! [rejected] -> (non-fast-forward) + reporter.negotiation.push(msg); + } + reporter.write_all(existing_lines); + Ok(()) + } + }); + + remote_callbacks.push_transfer_progress({ + let push_reporter = Arc::clone(&push_reporter); + #[allow(clippy::cast_precision_loss)] + move |current, total, bytes| { + let mut reporter = push_reporter.lock().unwrap(); + reporter.process_transfer_progress_update(current, total, bytes); + } + }); + + remote_callbacks.sideband_progress({ + let push_reporter = Arc::clone(&push_reporter); + move |data| { + let mut reporter = push_reporter.lock().unwrap(); + reporter.process_remote_msg(data); + true + } + }); + push_options.remote_callbacks(remote_callbacks); + git_server_remote.push(remote_refspecs, Some(&mut push_options))?; + let _ = git_server_remote.disconnect(); + Ok(()) +} + +#[allow(clippy::cast_precision_loss)] +#[allow(clippy::float_cmp)] +#[allow(clippy::needless_pass_by_value)] +fn report_on_transfer_progress( + current: usize, + total: usize, + bytes: usize, + start_time: &Instant, + end_time: Option<&Instant>, +) -> Option { + if total == 0 { + return None; + } + let percentage = ((current as f64 / total as f64) * 100.0) + // always round down because 100% complete is misleading when its not complete + .floor(); + let (size, unit) = if bytes as f64 >= (1024.0 * 1024.0) { + (bytes as f64 / (1024.0 * 1024.0), "MiB") + } else { + (bytes as f64 / 1024.0, "KiB") + }; + let speed = { + let duration = if let Some(end_time) = end_time { + (*end_time - *start_time).as_millis() as f64 + } else { + start_time.elapsed().as_millis() as f64 + }; + + if duration > 0.0 { + (bytes as f64 / (1024.0 * 1024.0)) / (duration / 1000.0) // Convert bytes to MiB and milliseconds to seconds + } else { + 0.0 + } + }; + + Some(format!( + "push: Writing objects: {percentage}% ({current}/{total}) {size:.2} {unit} | {speed:.2} MiB/s{}", + if current == total { ", done." } else { "" }, + )) +} + +pub struct PushReporter<'a> { + remote_msgs: Vec, + negotiation: Vec, + transfer_progress_msgs: Vec, + update_reference_errors: Vec, + term: &'a console::Term, + start_time: Option, + end_time: Option, +} +impl<'a> PushReporter<'a> { + fn new(term: &'a console::Term) -> Self { + Self { + remote_msgs: vec![], + negotiation: vec![], + transfer_progress_msgs: vec![], + update_reference_errors: vec![], + term, + start_time: None, + end_time: None, + } + } + fn write_all(&self, lines_to_clear: usize) { + let _ = self.term.clear_last_lines(lines_to_clear); + for msg in &self.remote_msgs { + let _ = self.term.write_line(format!("remote: {msg}").as_str()); + } + for msg in &self.negotiation { + let _ = self.term.write_line(msg); + } + for msg in &self.transfer_progress_msgs { + let _ = self.term.write_line(msg); + } + for msg in &self.update_reference_errors { + let _ = self.term.write_line(msg); + } + } + + fn count_all_existing_lines(&self) -> usize { + let width = self.term.size().1; + count_lines_per_msg_vec(width, &self.remote_msgs, "remote: ".len()) + + count_lines_per_msg_vec(width, &self.negotiation, 0) + + count_lines_per_msg_vec(width, &self.transfer_progress_msgs, 0) + + count_lines_per_msg_vec(width, &self.update_reference_errors, 0) + } + fn process_remote_msg(&mut self, data: &[u8]) { + if let Ok(data) = str::from_utf8(data) { + let data = data + .split(['\n', '\r']) + .map(str::trim) + .filter(|line| !line.trim().is_empty()) + .collect::>(); + for data in data { + let existing_lines = self.count_all_existing_lines(); + let msg = data.to_string(); + if let Some(last) = self.remote_msgs.last() { + if (last.contains('%') && !last.contains("100%")) + || last == &msg.replace(", done.", "") + { + self.remote_msgs.pop(); + self.remote_msgs.push(msg); + } else { + self.remote_msgs.push(msg); + } + } else { + self.remote_msgs.push(msg); + } + self.write_all(existing_lines); + } + } + } + fn process_transfer_progress_update(&mut self, current: usize, total: usize, bytes: usize) { + if self.start_time.is_none() { + self.start_time = Some(Instant::now()); + } + if let Some(report) = report_on_transfer_progress( + current, + total, + bytes, + &self.start_time.unwrap(), + self.end_time.as_ref(), + ) { + let existing_lines = self.count_all_existing_lines(); + if report.contains("100%") { + self.end_time = Some(Instant::now()); + } + self.transfer_progress_msgs = vec![report]; + self.write_all(existing_lines); + } + } +} -- cgit v1.2.3