diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2025-07-28 14:10:03 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2025-07-28 14:10:03 +0100 |
| commit | caed1d751eb29d329119c1372c99a651980f42a4 (patch) | |
| tree | 469a3c1ba4681c209997e8f55dc5a7f7fd9baf74 /src/lib/list.rs | |
| parent | e89dbc142f5a0a517f197562f5f228681d9aed47 (diff) | |
| parent | ee50baf800f4cb46d17858ba87a3648bb084d8b9 (diff) | |
Merge branch 'add-ngit-sync-cmd'
Diffstat (limited to 'src/lib/list.rs')
| -rw-r--r-- | src/lib/list.rs | 159 |
1 files changed, 159 insertions, 0 deletions
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 @@ | |||
| 1 | use std::collections::HashMap; | ||
| 2 | |||
| 3 | use anyhow::{Result, anyhow}; | ||
| 4 | use auth_git2::GitAuthenticator; | ||
| 5 | use nostr::hashes::sha1::Hash as Sha1Hash; | ||
| 6 | |||
| 7 | use crate::{ | ||
| 8 | git::{ | ||
| 9 | Repo, RepoActions, | ||
| 10 | nostr_url::{CloneUrl, NostrUrlDecoded, ServerProtocol}, | ||
| 11 | }, | ||
| 12 | repo_ref::is_grasp_server, | ||
| 13 | utils::{Direction, get_read_protocols_to_try, join_with_and, set_protocol_preference}, | ||
| 14 | }; | ||
| 15 | |||
| 16 | pub fn list_from_remotes( | ||
| 17 | term: &console::Term, | ||
| 18 | git_repo: &Repo, | ||
| 19 | git_servers: &Vec<String>, | ||
| 20 | decoded_nostr_url: &NostrUrlDecoded, | ||
| 21 | grasp_servers: &[String], | ||
| 22 | ) -> HashMap<String, (HashMap<String, String>, bool)> { | ||
| 23 | let mut remote_states = HashMap::new(); | ||
| 24 | let mut errors = HashMap::new(); | ||
| 25 | for url in git_servers { | ||
| 26 | let is_grasp_server = is_grasp_server(url, grasp_servers); | ||
| 27 | match list_from_remote(term, git_repo, url, decoded_nostr_url, is_grasp_server) { | ||
| 28 | Err(error) => { | ||
| 29 | errors.insert(url, error); | ||
| 30 | } | ||
| 31 | Ok(state) => { | ||
| 32 | remote_states.insert(url.to_string(), (state, is_grasp_server)); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | } | ||
| 36 | remote_states | ||
| 37 | } | ||
| 38 | |||
| 39 | pub fn list_from_remote( | ||
| 40 | term: &console::Term, | ||
| 41 | git_repo: &Repo, | ||
| 42 | git_server_url: &str, | ||
| 43 | decoded_nostr_url: &NostrUrlDecoded, | ||
| 44 | is_grasp_server: bool, | ||
| 45 | ) -> Result<HashMap<String, String>> { | ||
| 46 | let server_url = git_server_url.parse::<CloneUrl>()?; | ||
| 47 | let protocols_to_attempt = | ||
| 48 | get_read_protocols_to_try(git_repo, &server_url, decoded_nostr_url, is_grasp_server); | ||
| 49 | |||
| 50 | let mut failed_protocols = vec![]; | ||
| 51 | let mut remote_state: Option<HashMap<String, String>> = None; | ||
| 52 | |||
| 53 | for protocol in &protocols_to_attempt { | ||
| 54 | term.write_line( | ||
| 55 | format!( | ||
| 56 | "fetching {} ref list over {protocol}...", | ||
| 57 | server_url.short_name(), | ||
| 58 | ) | ||
| 59 | .as_str(), | ||
| 60 | )?; | ||
| 61 | |||
| 62 | let formatted_url = server_url.format_as(protocol, &decoded_nostr_url.user)?; | ||
| 63 | let res = list_from_remote_url( | ||
| 64 | git_repo, | ||
| 65 | &formatted_url, | ||
| 66 | [ServerProtocol::UnauthHttps, ServerProtocol::UnauthHttp].contains(protocol), | ||
| 67 | term, | ||
| 68 | ); | ||
| 69 | |||
| 70 | match res { | ||
| 71 | Ok(state) => { | ||
| 72 | remote_state = Some(state); | ||
| 73 | term.clear_last_lines(1)?; | ||
| 74 | if !failed_protocols.is_empty() { | ||
| 75 | term.write_line( | ||
| 76 | format!( | ||
| 77 | "list: succeeded over {protocol} from {}", | ||
| 78 | server_url.short_name(), | ||
| 79 | ) | ||
| 80 | .as_str(), | ||
| 81 | )?; | ||
| 82 | let _ = | ||
| 83 | set_protocol_preference(git_repo, protocol, &server_url, &Direction::Fetch); | ||
| 84 | } | ||
| 85 | break; | ||
| 86 | } | ||
| 87 | Err(error) => { | ||
| 88 | term.clear_last_lines(1)?; | ||
| 89 | term.write_line( | ||
| 90 | format!("list: {formatted_url} failed over {protocol}: {error}").as_str(), | ||
| 91 | )?; | ||
| 92 | failed_protocols.push(protocol); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | } | ||
| 96 | if let Some(remote_state) = remote_state { | ||
| 97 | if failed_protocols.is_empty() { | ||
| 98 | term.clear_last_lines(1)?; | ||
| 99 | } | ||
| 100 | Ok(remote_state) | ||
| 101 | } else { | ||
| 102 | let error = anyhow!( | ||
| 103 | "{} failed over {}{}", | ||
| 104 | server_url.short_name(), | ||
| 105 | join_with_and(&failed_protocols), | ||
| 106 | if decoded_nostr_url.protocol.is_some() { | ||
| 107 | " and nostr url contains protocol override so no other protocols were attempted" | ||
| 108 | } else { | ||
| 109 | "" | ||
| 110 | }, | ||
| 111 | ); | ||
| 112 | term.write_line(format!("list: {error}").as_str())?; | ||
| 113 | Err(error) | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | fn list_from_remote_url( | ||
| 118 | git_repo: &Repo, | ||
| 119 | git_server_remote_url: &str, | ||
| 120 | dont_authenticate: bool, | ||
| 121 | term: &console::Term, | ||
| 122 | ) -> Result<HashMap<String, String>> { | ||
| 123 | let git_config = git_repo.git_repo.config()?; | ||
| 124 | |||
| 125 | let mut git_server_remote = git_repo.git_repo.remote_anonymous(git_server_remote_url)?; | ||
| 126 | // authentication may be required | ||
| 127 | let auth = GitAuthenticator::default(); | ||
| 128 | let mut remote_callbacks = git2::RemoteCallbacks::new(); | ||
| 129 | if !dont_authenticate { | ||
| 130 | remote_callbacks.credentials(auth.credentials(&git_config)); | ||
| 131 | } | ||
| 132 | term.write_line("list: connecting...")?; | ||
| 133 | git_server_remote.connect_auth(git2::Direction::Fetch, Some(remote_callbacks), None)?; | ||
| 134 | term.clear_last_lines(1)?; | ||
| 135 | let mut state = HashMap::new(); | ||
| 136 | for head in git_server_remote.list()? { | ||
| 137 | if let Some(symbolic_reference) = head.symref_target() { | ||
| 138 | state.insert( | ||
| 139 | head.name().to_string(), | ||
| 140 | format!("ref: {symbolic_reference}"), | ||
| 141 | ); | ||
| 142 | // ignore dereferenced tags | ||
| 143 | } else if !head.name().to_string().ends_with("^{}") { | ||
| 144 | state.insert(head.name().to_string(), head.oid().to_string()); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | git_server_remote.disconnect()?; | ||
| 148 | Ok(state) | ||
| 149 | } | ||
| 150 | |||
| 151 | pub fn get_ahead_behind( | ||
| 152 | git_repo: &Repo, | ||
| 153 | base_ref_or_oid: &str, | ||
| 154 | latest_ref_or_oid: &str, | ||
| 155 | ) -> Result<(Vec<Sha1Hash>, Vec<Sha1Hash>)> { | ||
| 156 | let base = git_repo.get_commit_or_tip_of_reference(base_ref_or_oid)?; | ||
| 157 | let latest = git_repo.get_commit_or_tip_of_reference(latest_ref_or_oid)?; | ||
| 158 | git_repo.get_commits_ahead_behind(&base, &latest) | ||
| 159 | } | ||