From 8cc4a21061433c9a29e36c4f33e4d84bea1defee Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 20 Dec 2024 08:07:22 +0000 Subject: refactor: use nostr url from repo_ref simplify to allow the removal of warning: `#[allow(clippy::too_many_arguments)]` --- src/bin/git_remote_nostr/fetch.rs | 3 +- src/bin/git_remote_nostr/list.rs | 14 ++--- src/bin/git_remote_nostr/main.rs | 21 +++----- src/bin/git_remote_nostr/push.rs | 109 ++++++++++++++++++-------------------- 4 files changed, 67 insertions(+), 80 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 7012955..024a923 100644 --- a/src/bin/git_remote_nostr/fetch.rs +++ b/src/bin/git_remote_nostr/fetch.rs @@ -32,7 +32,6 @@ use crate::utils::{ pub async fn run_fetch( git_repo: &Repo, repo_ref: &RepoRef, - decoded_nostr_url: &NostrUrlDecoded, stdin: &Stdin, oid: &str, refstr: &str, @@ -54,7 +53,7 @@ pub async fn run_fetch( git_repo, &oids_from_git_servers, git_server_url, - decoded_nostr_url, + &repo_ref.to_nostr_git_url(&None), &term, ) { errors.push(error); diff --git a/src/bin/git_remote_nostr/list.rs b/src/bin/git_remote_nostr/list.rs index bba15aa..07c6f59 100644 --- a/src/bin/git_remote_nostr/list.rs +++ b/src/bin/git_remote_nostr/list.rs @@ -31,7 +31,6 @@ use crate::{ pub async fn run_list( git_repo: &Repo, repo_ref: &RepoRef, - decoded_nostr_url: &NostrUrlDecoded, for_push: bool, ) -> Result>> { let nostr_state = @@ -43,7 +42,12 @@ pub async fn run_list( let term = console::Term::stderr(); - let remote_states = list_from_remotes(&term, git_repo, &repo_ref.git_server, decoded_nostr_url); + let remote_states = list_from_remotes( + &term, + git_repo, + &repo_ref.git_server, + &repo_ref.to_nostr_git_url(&None), + ); let mut state = if let Some(nostr_state) = nostr_state { for (name, value) in &nostr_state.state { @@ -89,8 +93,7 @@ pub async fn run_list( state.retain(|k, _| !k.starts_with("refs/heads/pr/")); let proposals_state = - get_open_proposals_state(&term, git_repo, repo_ref, decoded_nostr_url, &remote_states) - .await?; + get_open_proposals_state(&term, git_repo, repo_ref, &remote_states).await?; state.extend(proposals_state); @@ -114,7 +117,6 @@ async fn get_open_proposals_state( term: &console::Term, git_repo: &Repo, repo_ref: &RepoRef, - decoded_nostr_url: &NostrUrlDecoded, remote_states: &HashMap>, ) -> Result> { // we cannot use commit_id in the latest patch in a proposal because: @@ -133,7 +135,7 @@ async fn get_open_proposals_state( .cloned() .collect::>(), git_server_url, - decoded_nostr_url, + &repo_ref.to_nostr_git_url(&None), term, ) .is_ok() diff --git a/src/bin/git_remote_nostr/main.rs b/src/bin/git_remote_nostr/main.rs index e6f7af3..44359dd 100644 --- a/src/bin/git_remote_nostr/main.rs +++ b/src/bin/git_remote_nostr/main.rs @@ -53,9 +53,11 @@ async fn main() -> Result<()> { fetching_with_report_for_helper(git_repo_path, &client, &decoded_nostr_url.coordinate).await?; - let repo_ref = + let mut repo_ref = get_repo_ref_from_cache(Some(git_repo_path), &decoded_nostr_url.coordinate).await?; + repo_ref.set_nostr_git_url(decoded_nostr_url.clone()); + let stdin = io::stdin(); let mut line = String::new(); @@ -77,21 +79,12 @@ async fn main() -> Result<()> { println!("unsupported"); } ["fetch", oid, refstr] => { - fetch::run_fetch( - &git_repo, - &repo_ref, - &decoded_nostr_url, - &stdin, - oid, - refstr, - ) - .await?; + fetch::run_fetch(&git_repo, &repo_ref, &stdin, oid, refstr).await?; } ["push", refspec] => { push::run_push( &git_repo, &repo_ref, - &decoded_nostr_url, &stdin, refspec, &client, @@ -100,12 +93,10 @@ async fn main() -> Result<()> { .await?; } ["list"] => { - list_outputs = - Some(list::run_list(&git_repo, &repo_ref, &decoded_nostr_url, false).await?); + list_outputs = Some(list::run_list(&git_repo, &repo_ref, false).await?); } ["list", "for-push"] => { - list_outputs = - Some(list::run_list(&git_repo, &repo_ref, &decoded_nostr_url, true).await?); + list_outputs = Some(list::run_list(&git_repo, &repo_ref, true).await?); } [] => { return Ok(()); diff --git a/src/bin/git_remote_nostr/push.rs b/src/bin/git_remote_nostr/push.rs index e8e1587..05c8fc2 100644 --- a/src/bin/git_remote_nostr/push.rs +++ b/src/bin/git_remote_nostr/push.rs @@ -53,7 +53,6 @@ use crate::{ pub async fn run_push( git_repo: &Repo, repo_ref: &RepoRef, - decoded_nostr_url: &NostrUrlDecoded, stdin: &Stdin, initial_refspec: &str, client: &Client, @@ -75,16 +74,18 @@ pub async fn run_push( let term = console::Term::stderr(); - let list_outputs = match list_outputs { - Some(outputs) => outputs, - _ => list_from_remotes(&term, git_repo, &repo_ref.git_server, decoded_nostr_url), - }; - - let nostr_state = get_state_from_cache(Some(git_repo.get_path()?), repo_ref).await; + let list_outputs = list_outputs.unwrap_or_else(|| { + list_from_remotes( + &term, + git_repo, + &repo_ref.git_server, + &repo_ref.to_nostr_git_url(&None), + ) + }); let existing_state = { // if no state events - create from first git server listed - if let Ok(nostr_state) = &nostr_state { + if let Ok(nostr_state) = &get_state_from_cache(Some(git_repo.get_path()?), repo_ref).await { nostr_state.state.clone() } else if let Some(url) = repo_ref .git_server @@ -118,55 +119,51 @@ pub async fn run_push( } }); - if git_server_refspecs.is_empty() && proposal_refspecs.is_empty() { - // all refspecs rejected - println!(); - return Ok(()); - } - - let (rejected_proposal_refspecs, rejected) = create_and_publish_events( - git_repo, - repo_ref, - decoded_nostr_url, - &git_server_refspecs, - &proposal_refspecs, - client, - existing_state, - &term, - ) - .await?; + // all refspecs aren't rejected + if !(git_server_refspecs.is_empty() && proposal_refspecs.is_empty()) { + let (rejected_proposal_refspecs, rejected) = create_and_publish_events( + git_repo, + repo_ref, + &git_server_refspecs, + &proposal_refspecs, + client, + existing_state, + &term, + ) + .await?; - if !rejected { - for refspec in &[git_server_refspecs.clone(), proposal_refspecs.clone()].concat() { - if rejected_proposal_refspecs.contains(refspec) { - continue; + if !rejected { + for refspec in git_server_refspecs.iter().chain(proposal_refspecs.iter()) { + if rejected_proposal_refspecs.contains(refspec) { + continue; + } + let (_, to) = refspec_to_from_to(refspec)?; + println!("ok {to}"); + update_remote_refs_pushed( + &git_repo.git_repo, + refspec, + &repo_ref.to_nostr_git_url(&None).to_string(), + ) + .context("could not update remote_ref locally")?; } - let (_, to) = refspec_to_from_to(refspec)?; - println!("ok {to}"); - update_remote_refs_pushed( - &git_repo.git_repo, - refspec, - &decoded_nostr_url.original_string, - ) - .context("could not update remote_ref locally")?; - } - // TODO make async - check gitlib2 callbacks work async + // 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() { - let _ = push_to_remote( - git_repo, - &git_server_url, - decoded_nostr_url, - &remote_refspecs, - &term, - ); + 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() { + let _ = push_to_remote( + git_repo, + &git_server_url, + &repo_ref.to_nostr_git_url(&None), + &remote_refspecs, + &term, + ); + } } } } @@ -175,11 +172,9 @@ pub async fn run_push( Ok(()) } -#[allow(clippy::too_many_arguments)] async fn create_and_publish_events( git_repo: &Repo, repo_ref: &RepoRef, - decoded_nostr_url: &NostrUrlDecoded, git_server_refspecs: &Vec, proposal_refspecs: &Vec, client: &Client, @@ -222,7 +217,7 @@ async fn create_and_publish_events( for event in get_merged_status_events( term, - decoded_nostr_url, + &repo_ref.to_nostr_git_url(&None), repo_ref, git_repo, &signer, @@ -235,7 +230,7 @@ async fn create_and_publish_events( if let Ok(Some(repo_ref_event)) = get_maintainers_yaml_update( term, - decoded_nostr_url, + &repo_ref.to_nostr_git_url(&None), repo_ref, git_repo, &signer, -- cgit v1.2.3