upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/bin/git_remote_nostr/list.rs
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2025-07-25 21:02:26 +0100
committerDanConwayDev <DanConwayDev@protonmail.com>2025-07-25 22:17:23 +0100
commit513fce723c7e37aa353844303f36022517f2db43 (patch)
tree05e16bdd4789036af04b6c046823396f9f0faf22 /src/bin/git_remote_nostr/list.rs
parente89dbc142f5a0a517f197562f5f228681d9aed47 (diff)
refactor: move `utils` and `list` helpers to lib
to enable forthcoming `ngit sync` cmd
Diffstat (limited to 'src/bin/git_remote_nostr/list.rs')
-rw-r--r--src/bin/git_remote_nostr/list.rs163
1 files changed, 5 insertions, 158 deletions
diff --git a/src/bin/git_remote_nostr/list.rs b/src/bin/git_remote_nostr/list.rs
index 7bdf170..df98c12 100644
--- a/src/bin/git_remote_nostr/list.rs
+++ b/src/bin/git_remote_nostr/list.rs
@@ -1,30 +1,22 @@
1use core::str;
2use std::collections::HashMap; 1use std::collections::HashMap;
3 2
4use anyhow::{Context, Result, anyhow}; 3use anyhow::{Context, Result};
5use auth_git2::GitAuthenticator;
6use client::get_state_from_cache; 4use client::get_state_from_cache;
7use git::RepoActions; 5use git::RepoActions;
8use ngit::{ 6use ngit::{
9 client, 7 client,
10 git::{ 8 git::{self},
11 self,
12 nostr_url::{CloneUrl, NostrUrlDecoded, ServerProtocol},
13 },
14 git_events::{KIND_PULL_REQUEST, KIND_PULL_REQUEST_UPDATE, event_to_cover_letter, tag_value}, 9 git_events::{KIND_PULL_REQUEST, KIND_PULL_REQUEST_UPDATE, event_to_cover_letter, tag_value},
10 list::{get_ahead_behind, list_from_remotes},
15 login::get_curent_user, 11 login::get_curent_user,
16 repo_ref::{self, is_grasp_server}, 12 repo_ref::{self},
13 utils::{get_open_or_draft_proposals, get_short_git_server_name},
17}; 14};
18use nostr_sdk::hashes::sha1::Hash as Sha1Hash;
19use repo_ref::RepoRef; 15use repo_ref::RepoRef;
20 16
21use crate::{ 17use crate::{
22 fetch::{fetch_from_git_server, make_commits_for_proposal}, 18 fetch::{fetch_from_git_server, make_commits_for_proposal},
23 git::Repo, 19 git::Repo,
24 utils::{
25 Direction, get_open_or_draft_proposals, get_read_protocols_to_try,
26 get_short_git_server_name, join_with_and, set_protocol_preference,
27 },
28}; 20};
29 21
30pub async fn run_list( 22pub async fn run_list(
@@ -210,148 +202,3 @@ async fn get_open_and_draft_proposals_state(
210 } 202 }
211 Ok(state) 203 Ok(state)
212} 204}
213
214pub fn list_from_remotes(
215 term: &console::Term,
216 git_repo: &Repo,
217 git_servers: &Vec<String>,
218 decoded_nostr_url: &NostrUrlDecoded,
219 grasp_servers: &[String],
220) -> HashMap<String, (HashMap<String, String>, bool)> {
221 let mut remote_states = HashMap::new();
222 let mut errors = HashMap::new();
223 for url in git_servers {
224 let is_grasp_server = is_grasp_server(url, grasp_servers);
225 match list_from_remote(term, git_repo, url, decoded_nostr_url, is_grasp_server) {
226 Err(error) => {
227 errors.insert(url, error);
228 }
229 Ok(state) => {
230 remote_states.insert(url.to_string(), (state, is_grasp_server));
231 }
232 }
233 }
234 remote_states
235}
236
237pub fn list_from_remote(
238 term: &console::Term,
239 git_repo: &Repo,
240 git_server_url: &str,
241 decoded_nostr_url: &NostrUrlDecoded,
242 is_grasp_server: bool,
243) -> Result<HashMap<String, String>> {
244 let server_url = git_server_url.parse::<CloneUrl>()?;
245 let protocols_to_attempt =
246 get_read_protocols_to_try(git_repo, &server_url, decoded_nostr_url, is_grasp_server);
247
248 let mut failed_protocols = vec![];
249 let mut remote_state: Option<HashMap<String, String>> = None;
250
251 for protocol in &protocols_to_attempt {
252 term.write_line(
253 format!(
254 "fetching {} ref list over {protocol}...",
255 server_url.short_name(),
256 )
257 .as_str(),
258 )?;
259
260 let formatted_url = server_url.format_as(protocol, &decoded_nostr_url.user)?;
261 let res = list_from_remote_url(
262 git_repo,
263 &formatted_url,
264 [ServerProtocol::UnauthHttps, ServerProtocol::UnauthHttp].contains(protocol),
265 term,
266 );
267
268 match res {
269 Ok(state) => {
270 remote_state = Some(state);
271 term.clear_last_lines(1)?;
272 if !failed_protocols.is_empty() {
273 term.write_line(
274 format!(
275 "list: succeeded over {protocol} from {}",
276 server_url.short_name(),
277 )
278 .as_str(),
279 )?;
280 let _ =
281 set_protocol_preference(git_repo, protocol, &server_url, &Direction::Fetch);
282 }
283 break;
284 }
285 Err(error) => {
286 term.clear_last_lines(1)?;
287 term.write_line(
288 format!("list: {formatted_url} failed over {protocol}: {error}").as_str(),
289 )?;
290 failed_protocols.push(protocol);
291 }
292 }
293 }
294 if let Some(remote_state) = remote_state {
295 if failed_protocols.is_empty() {
296 term.clear_last_lines(1)?;
297 }
298 Ok(remote_state)
299 } else {
300 let error = anyhow!(
301 "{} failed over {}{}",
302 server_url.short_name(),
303 join_with_and(&failed_protocols),
304 if decoded_nostr_url.protocol.is_some() {
305 " and nostr url contains protocol override so no other protocols were attempted"
306 } else {
307 ""
308 },
309 );
310 term.write_line(format!("list: {error}").as_str())?;
311 Err(error)
312 }
313}
314
315fn list_from_remote_url(
316 git_repo: &Repo,
317 git_server_remote_url: &str,
318 dont_authenticate: bool,
319 term: &console::Term,
320) -> Result<HashMap<String, String>> {
321 let git_config = git_repo.git_repo.config()?;
322
323 let mut git_server_remote = git_repo.git_repo.remote_anonymous(git_server_remote_url)?;
324 // authentication may be required
325 let auth = GitAuthenticator::default();
326 let mut remote_callbacks = git2::RemoteCallbacks::new();
327 if !dont_authenticate {
328 remote_callbacks.credentials(auth.credentials(&git_config));
329 }
330 term.write_line("list: connecting...")?;
331 git_server_remote.connect_auth(git2::Direction::Fetch, Some(remote_callbacks), None)?;
332 term.clear_last_lines(1)?;
333 let mut state = HashMap::new();
334 for head in git_server_remote.list()? {
335 if let Some(symbolic_reference) = head.symref_target() {
336 state.insert(
337 head.name().to_string(),
338 format!("ref: {symbolic_reference}"),
339 );
340 // ignore dereferenced tags
341 } else if !head.name().to_string().ends_with("^{}") {
342 state.insert(head.name().to_string(), head.oid().to_string());
343 }
344 }
345 git_server_remote.disconnect()?;
346 Ok(state)
347}
348
349fn get_ahead_behind(
350 git_repo: &Repo,
351 base_ref_or_oid: &str,
352 latest_ref_or_oid: &str,
353) -> Result<(Vec<Sha1Hash>, Vec<Sha1Hash>)> {
354 let base = git_repo.get_commit_or_tip_of_reference(base_ref_or_oid)?;
355 let latest = git_repo.get_commit_or_tip_of_reference(latest_ref_or_oid)?;
356 git_repo.get_commits_ahead_behind(&base, &latest)
357}