From 3acdeabfc3ab55d3e92d76d92d8ab6ad0383dd09 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 31 Jul 2024 15:59:17 +0100 Subject: feat(remote): `push` issues state event if no previous state events can be found it replicates git server otherwise it just updates pushed value --- src/git_remote_helper.rs | 239 ++++++++++++++++++++++++++++++++++++++-------- src/login.rs | 20 +++- src/repo_state.rs | 5 + src/sub_commands/init.rs | 2 + src/sub_commands/login.rs | 2 + src/sub_commands/push.rs | 2 + src/sub_commands/send.rs | 11 ++- 7 files changed, 234 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/git_remote_helper.rs b/src/git_remote_helper.rs index ba8ab61..68aa681 100644 --- a/src/git_remote_helper.rs +++ b/src/git_remote_helper.rs @@ -16,12 +16,17 @@ use anyhow::{bail, Context, Result}; use auth_git2::GitAuthenticator; #[cfg(not(test))] use client::Connect; -use client::{fetching_with_report, get_repo_ref_from_cache}; +use client::{ + fetching_with_report, get_repo_ref_from_cache, get_state_from_cache, sign_event, STATE_KIND, +}; use git::RepoActions; -use git2::{Remote, Repository}; +use git2::{Oid, Repository}; use nostr::nips::nip01::Coordinate; -use nostr_sdk::Url; +use nostr_sdk::{EventBuilder, Tag, Url}; +use nostr_signer::NostrSigner; use repo_ref::RepoRef; +use repo_state::RepoState; +use sub_commands::send::send_events; #[cfg(not(test))] use crate::client::Client; @@ -93,12 +98,14 @@ async fn main() -> Result<()> { } ["push", refspec] => { push( - &git_repo.git_repo, + &git_repo, &repo_ref, nostr_remote_url, &stdin, refspec, - )?; + &client, + ) + .await?; } ["list"] => { list(&git_repo.git_repo, &repo_ref, false)?; @@ -171,12 +178,14 @@ fn fetch(git_repo: &Repository, repo_ref: &RepoRef, stdin: &Stdin, refstr: &str) Ok(()) } -fn push( - git_repo: &Repository, +async fn push( + git_repo: &Repo, repo_ref: &RepoRef, nostr_remote_url: &str, stdin: &Stdin, initial_refspec: &str, + #[cfg(test)] client: &crate::client::MockConnect, + #[cfg(not(test))] client: &Client, ) -> Result<()> { // if no state events - create from first git server listed let refspecs = get_refspecs_from_push_batch(stdin, initial_refspec)?; @@ -184,9 +193,10 @@ fn push( .git_server .first() .context("no git server listed in nostr repository announcement")?; - let mut git_server_remote = git_repo.remote_anonymous(git_server_url)?; + let mut git_server_remote = git_repo.git_repo.remote_anonymous(git_server_url)?; + let auth = GitAuthenticator::default(); - let git_config = git_repo.config()?; + let git_config = git_repo.git_repo.config()?; let mut push_options = git2::PushOptions::new(); let mut remote_callbacks = git2::RemoteCallbacks::new(); remote_callbacks.credentials(auth.credentials(&git_config)); @@ -198,8 +208,9 @@ fn push( .iter() .find(|r| r.contains(format!(":{name}").as_str())) { - if let Err(e) = update_remote_refs_pushed(git_repo, refspec, nostr_remote_url) - .context("could not update remote_ref locally") + if let Err(e) = + update_remote_refs_pushed(&git_repo.git_repo, refspec, nostr_remote_url) + .context("could not update remote_ref locally") { return Err(git2::Error::from_str(e.to_string().as_str())); } @@ -211,49 +222,125 @@ fn push( push_options.remote_callbacks(remote_callbacks); git_server_remote.push(&refspecs, Some(&mut push_options))?; git_server_remote.disconnect()?; + + // TODO check whether push was succesful before proceeding - geting outcome from + // callback isn't straightforward + + let new_state = generate_updated_state(git_repo, repo_ref, &refspecs).await?; + + // TODO enable interactive login + let (signer, user_ref) = login::launch( + git_repo, + &None, + &None, + &None, + &None, + Some(client), + false, + true, + ) + .await?; + let new_repo_state = RepoState::build(repo_ref.identifier.clone(), new_state, &signer).await?; + + send_events( + client, + git_repo.get_path()?, + vec![new_repo_state.event], + user_ref.relays.write(), + repo_ref.relays.clone(), + false, + true, + ) + .await?; + println!(); Ok(()) } +async fn generate_updated_state( + git_repo: &Repo, + repo_ref: &RepoRef, + refspecs: &Vec, +) -> Result> { + let new_state = { + if let Ok(mut repo_state) = get_state_from_cache(git_repo.get_path()?, repo_ref).await { + for refspec in refspecs { + let (from, to) = refspec_to_from_to(refspec)?; + if to.is_empty() { + // delete + repo_state.state.retain(|(name, _)| !name.eq(to)); + } else if repo_state.state.iter().any(|(name, _)| name.eq(from)) { + // update + repo_state.state = repo_state + .state + .iter() + .map(|(name, value)| { + ( + name.clone(), + if name.eq(to) { + reference_to_ref_value(&git_repo.git_repo, to).unwrap() + } else { + value.to_string() + }, + ) + }) + .collect(); + } else { + // add + repo_state.state.push(( + to.to_string(), + reference_to_ref_value(&git_repo.git_repo, to).unwrap(), + )); + } + } + repo_state.state + } else { + let mut state = vec![]; + let git_server_url = repo_ref + .git_server + .first() + .context("no git server listed in nostr repository announcement")?; + let mut git_server_remote = git_repo.git_repo.remote_anonymous(git_server_url)?; + git_server_remote.connect(git2::Direction::Fetch)?; + for head in git_server_remote.list()? { + state.push(( + head.name().to_string(), + if let Some(symbolic_ref) = head.symref_target() { + format!("ref: {}", symbolic_ref) + } else { + head.oid().to_string() + }, + )); + } + git_server_remote.disconnect()?; + state + } + }; + Ok(new_state) +} + fn update_remote_refs_pushed( git_repo: &Repository, refspec: &str, nostr_remote_url: &str, ) -> Result<()> { - if !refspec.contains(':') { - bail!( - "refspec should contain a colon (:) but consists of: {}", - refspec - ); - } - let parts = refspec.split(':').collect::>(); - let from = parts.first().unwrap(); - let to = parts.get(1).unwrap(); + let (from, _) = refspec_to_from_to(refspec)?; - let nostr_remote = get_remote_by_url(git_repo, nostr_remote_url)?; + let target_ref_name = refspec_remote_ref_name(git_repo, refspec, nostr_remote_url)?; - let target_ref_name = format!( - "refs/remotes/{}/{}", - nostr_remote.name().context("remote should have a name")?, - to.replace("refs/heads/", ""), // TODO only replace if it begins with this - ); if from.is_empty() { if let Ok(mut remote_ref) = git_repo.find_reference(&target_ref_name) { remote_ref.delete()?; } } else { - let local_ref = git_repo - .find_reference(from) - .context(format!("from ref in refspec should exist: {from}"))?; - let commit = local_ref - .peel_to_commit() - .context(format!("from ref in refspec should peel to commit: {from}"))?; + let commit = reference_to_commit(git_repo, from) + .context(format!("cannot get commit of reference {from}"))?; if let Ok(mut remote_ref) = git_repo.find_reference(&target_ref_name) { - remote_ref.set_target(commit.id(), "updated by nostr remote helper")?; + remote_ref.set_target(commit, "updated by nostr remote helper")?; } else { git_repo.reference( &target_ref_name, - commit.id(), + commit, false, "created by nostr remote helper", )?; @@ -262,9 +349,61 @@ fn update_remote_refs_pushed( Ok(()) } -fn get_remote_by_url<'a>(git_repo: &'a Repository, url: &'a str) -> Result> { +fn refspec_to_from_to(refspec: &str) -> Result<(&str, &str)> { + if !refspec.contains(':') { + bail!( + "refspec should contain a colon (:) but consists of: {}", + refspec + ); + } + let parts = refspec.split(':').collect::>(); + Ok((parts.first().unwrap(), parts.get(1).unwrap())) +} + +fn refspec_remote_ref_name( + git_repo: &Repository, + refspec: &str, + nostr_remote_url: &str, +) -> Result { + let (_, to) = refspec_to_from_to(refspec)?; + let nostr_remote = git_repo + .find_remote(&get_remote_name_by_url(git_repo, nostr_remote_url)?) + .context("we should have just located this remote")?; + Ok(format!( + "refs/remotes/{}/{}", + nostr_remote.name().context("remote should have a name")?, + to.replace("refs/heads/", ""), // TODO only replace if it begins with this + )) +} + +fn reference_to_commit(git_repo: &Repository, reference: &str) -> Result { + Ok(git_repo + .find_reference(reference) + .context(format!("cannot find reference: {reference}"))? + .peel_to_commit() + .context(format!("cannot get commit from reference: {reference}"))? + .id()) +} + +// this maybe a commit id or a ref: pointer +fn reference_to_ref_value(git_repo: &Repository, reference: &str) -> Result { + let reference_obj = git_repo + .find_reference(reference) + .context(format!("cannot find reference: {reference}"))?; + if let Some(symref) = reference_obj.symbolic_target() { + Ok(symref.to_string()) + } else { + Ok(reference_obj + .peel_to_commit() + .context(format!("cannot get commit from reference: {reference}"))? + .id() + .to_string()) + } +} + +fn get_remote_name_by_url(git_repo: &Repository, url: &str) -> Result { let remotes = git_repo.remotes()?; - let remote_name = remotes + Ok(remotes .iter() .find(|r| { if let Some(name) = r { @@ -278,10 +417,8 @@ fn get_remote_by_url<'a>(git_repo: &'a Repository, url: &'a str) -> Result Result> { @@ -319,3 +456,25 @@ fn get_refspecs_from_push_batch(stdin: &Stdin, initial_refspec: &str) -> Result< } Ok(refspecs) } + +impl RepoState { + pub async fn build( + identifier: String, + state: Vec<(String, String)>, + signer: &NostrSigner, + ) -> Result { + let mut tags = vec![Tag::identifier(identifier.clone())]; + for (name, value) in &state { + tags.push(Tag::custom( + nostr_sdk::TagKind::Custom(name.into()), + vec![value.clone()], + )); + } + let event = sign_event(EventBuilder::new(STATE_KIND, "", tags), signer).await?; + Ok(RepoState { + identifier, + state, + event, + }) + } +} diff --git a/src/login.rs b/src/login.rs index be358de..19bb97c 100644 --- a/src/login.rs +++ b/src/login.rs @@ -25,6 +25,7 @@ use crate::{ }; /// handles the encrpytion and storage of key material +#[allow(clippy::too_many_arguments)] pub async fn launch( git_repo: &Repo, bunker_uri: &Option, @@ -34,6 +35,7 @@ pub async fn launch( #[cfg(test)] client: Option<&MockConnect>, #[cfg(not(test))] client: Option<&Client>, change_user: bool, + silent: bool, ) -> Result<(NostrSigner, UserRef)> { if let Ok(signer) = match get_signer_without_prompts( git_repo, @@ -58,7 +60,8 @@ pub async fn launch( .unwrap_or("unknown ncryptsec".to_string()), ) { if let Ok(user_ref) = - get_user_details(&public_key, client, git_repo.get_path()?).await + get_user_details(&public_key, client, git_repo.get_path()?, silent) + .await { user_ref.metadata.name } else { @@ -94,10 +97,15 @@ pub async fn launch( .context("cannot get public key from signer")?, client, git_repo.get_path()?, + silent, ) .await?; - print_logged_in_as(&user_ref, client.is_none())?; + if !silent { + print_logged_in_as(&user_ref, client.is_none())?; + } Ok((signer, user_ref)) + } else if silent { + bail!("TODO: enable interactive login in nostr git remote helper"); } else { fresh_login(git_repo, client, change_user).await } @@ -396,7 +404,7 @@ async fn fresh_login( signer.public_key().await? }; // lookup profile - let user_ref = get_user_details(&public_key, client, git_repo.get_path()?).await?; + let user_ref = get_user_details(&public_key, client, git_repo.get_path()?, false).await?; print_logged_in_as(&user_ref, client.is_none())?; Ok((signer, user_ref)) } @@ -612,6 +620,7 @@ async fn get_user_details( #[cfg(test)] client: Option<&crate::client::MockConnect>, #[cfg(not(test))] client: Option<&Client>, git_repo_path: &Path, + cache_only: bool, ) -> Result { if let Ok(user_ref) = get_user_ref_from_cache(git_repo_path, public_key).await { Ok(user_ref) @@ -621,8 +630,9 @@ async fn get_user_details( metadata: extract_user_metadata(public_key, &[])?, relays: extract_user_relays(public_key, &[]), }; - - if let Some(client) = client { + if cache_only { + Ok(empty) + } else if let Some(client) = client { let term = console::Term::stderr(); term.write_line("searching for profile...")?; let (_, progress_reporter) = client diff --git a/src/repo_state.rs b/src/repo_state.rs index 33bc90f..0c1aa30 100644 --- a/src/repo_state.rs +++ b/src/repo_state.rs @@ -2,6 +2,7 @@ use anyhow::{Context, Result}; use git2::Oid; pub struct RepoState { + pub identifier: String, pub state: Vec<(String, String)>, pub event: nostr::Event, } @@ -26,6 +27,10 @@ impl RepoState { } } Ok(RepoState { + identifier: event + .identifier() + .context("existing event must have an identifier")? + .to_string(), state, event: event.clone(), }) diff --git a/src/sub_commands/init.rs b/src/sub_commands/init.rs index ba188c9..bb437a5 100644 --- a/src/sub_commands/init.rs +++ b/src/sub_commands/init.rs @@ -89,6 +89,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { &cli_args.password, Some(&client), false, + false, ) .await?; @@ -330,6 +331,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { user_ref.relays.write(), relays.clone(), !cli_args.disable_cli_spinners, + false, ) .await?; diff --git a/src/sub_commands/login.rs b/src/sub_commands/login.rs index 77fecdd..8a3788f 100644 --- a/src/sub_commands/login.rs +++ b/src/sub_commands/login.rs @@ -25,6 +25,7 @@ pub async fn launch(args: &Cli, command_args: &SubCommandArgs) -> Result<()> { &args.password, None, true, + false, ) .await?; Ok(()) @@ -42,6 +43,7 @@ pub async fn launch(args: &Cli, command_args: &SubCommandArgs) -> Result<()> { &args.password, Some(&client), true, + false, ) .await?; client.disconnect().await?; diff --git a/src/sub_commands/push.rs b/src/sub_commands/push.rs index 56927fe..7a82c7a 100644 --- a/src/sub_commands/push.rs +++ b/src/sub_commands/push.rs @@ -178,6 +178,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { &cli_args.password, Some(&client), false, + false, ) .await?; @@ -212,6 +213,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { user_ref.relays.write(), repo_ref.relays.clone(), !cli_args.disable_cli_spinners, + false, ) .await?; diff --git a/src/sub_commands/send.rs b/src/sub_commands/send.rs index 07eb343..f10d2d3 100644 --- a/src/sub_commands/send.rs +++ b/src/sub_commands/send.rs @@ -3,7 +3,7 @@ use std::{path::Path, str::FromStr, time::Duration}; use anyhow::{bail, Context, Result}; use console::Style; use futures::future::join_all; -use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; +use indicatif::{MultiProgress, ProgressBar, ProgressDrawTarget, ProgressStyle}; use nostr::{ nips::{ nip01::Coordinate, @@ -195,6 +195,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs, no_fetch: bool) -> Re &cli_args.password, Some(&client), false, + false, ) .await?; @@ -244,6 +245,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs, no_fetch: bool) -> Re user_ref.relays.write(), repo_ref.relays.clone(), !cli_args.disable_cli_spinners, + false, ) .await?; @@ -285,6 +287,7 @@ pub async fn send_events( my_write_relays: Vec, repo_read_relays: Vec, animate: bool, + silent: bool, ) -> Result<()> { let fallback = [ client.get_fallback_relays().clone(), @@ -327,7 +330,11 @@ pub async fn send_events( } } - let m = MultiProgress::new(); + let m = if silent { + MultiProgress::with_draw_target(ProgressDrawTarget::hidden()) + } else { + MultiProgress::new() + }; let pb_style = ProgressStyle::with_template(if animate { " {spinner} {prefix} {bar} {pos}/{len} {msg}" } else { -- cgit v1.2.3