upleb.uk

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

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2024-07-31 15:59:17 +0100
committerDanConwayDev <DanConwayDev@protonmail.com>2024-07-31 15:59:17 +0100
commit3acdeabfc3ab55d3e92d76d92d8ab6ad0383dd09 (patch)
treeb5abbca08ec004d31569db9166c37166c39baef2
parent46116e561fb62ad939c53e937461f2346022565d (diff)
feat(remote): `push` issues state event
if no previous state events can be found it replicates git server otherwise it just updates pushed value
-rw-r--r--src/git_remote_helper.rs239
-rw-r--r--src/login.rs20
-rw-r--r--src/repo_state.rs5
-rw-r--r--src/sub_commands/init.rs2
-rw-r--r--src/sub_commands/login.rs2
-rw-r--r--src/sub_commands/push.rs2
-rw-r--r--src/sub_commands/send.rs11
-rw-r--r--tests/git_remote_helper.rs99
8 files changed, 333 insertions, 47 deletions
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};
16use auth_git2::GitAuthenticator; 16use auth_git2::GitAuthenticator;
17#[cfg(not(test))] 17#[cfg(not(test))]
18use client::Connect; 18use client::Connect;
19use client::{fetching_with_report, get_repo_ref_from_cache}; 19use client::{
20 fetching_with_report, get_repo_ref_from_cache, get_state_from_cache, sign_event, STATE_KIND,
21};
20use git::RepoActions; 22use git::RepoActions;
21use git2::{Remote, Repository}; 23use git2::{Oid, Repository};
22use nostr::nips::nip01::Coordinate; 24use nostr::nips::nip01::Coordinate;
23use nostr_sdk::Url; 25use nostr_sdk::{EventBuilder, Tag, Url};
26use nostr_signer::NostrSigner;
24use repo_ref::RepoRef; 27use repo_ref::RepoRef;
28use repo_state::RepoState;
29use sub_commands::send::send_events;
25 30
26#[cfg(not(test))] 31#[cfg(not(test))]
27use crate::client::Client; 32use crate::client::Client;
@@ -93,12 +98,14 @@ async fn main() -> Result<()> {
93 } 98 }
94 ["push", refspec] => { 99 ["push", refspec] => {
95 push( 100 push(
96 &git_repo.git_repo, 101 &git_repo,
97 &repo_ref, 102 &repo_ref,
98 nostr_remote_url, 103 nostr_remote_url,
99 &stdin, 104 &stdin,
100 refspec, 105 refspec,
101 )?; 106 &client,
107 )
108 .await?;
102 } 109 }
103 ["list"] => { 110 ["list"] => {
104 list(&git_repo.git_repo, &repo_ref, false)?; 111 list(&git_repo.git_repo, &repo_ref, false)?;
@@ -171,12 +178,14 @@ fn fetch(git_repo: &Repository, repo_ref: &RepoRef, stdin: &Stdin, refstr: &str)
171 Ok(()) 178 Ok(())
172} 179}
173 180
174fn push( 181async fn push(
175 git_repo: &Repository, 182 git_repo: &Repo,
176 repo_ref: &RepoRef, 183 repo_ref: &RepoRef,
177 nostr_remote_url: &str, 184 nostr_remote_url: &str,
178 stdin: &Stdin, 185 stdin: &Stdin,
179 initial_refspec: &str, 186 initial_refspec: &str,
187 #[cfg(test)] client: &crate::client::MockConnect,
188 #[cfg(not(test))] client: &Client,
180) -> Result<()> { 189) -> Result<()> {
181 // if no state events - create from first git server listed 190 // if no state events - create from first git server listed
182 let refspecs = get_refspecs_from_push_batch(stdin, initial_refspec)?; 191 let refspecs = get_refspecs_from_push_batch(stdin, initial_refspec)?;
@@ -184,9 +193,10 @@ fn push(
184 .git_server 193 .git_server
185 .first() 194 .first()
186 .context("no git server listed in nostr repository announcement")?; 195 .context("no git server listed in nostr repository announcement")?;
187 let mut git_server_remote = git_repo.remote_anonymous(git_server_url)?; 196 let mut git_server_remote = git_repo.git_repo.remote_anonymous(git_server_url)?;
197
188 let auth = GitAuthenticator::default(); 198 let auth = GitAuthenticator::default();
189 let git_config = git_repo.config()?; 199 let git_config = git_repo.git_repo.config()?;
190 let mut push_options = git2::PushOptions::new(); 200 let mut push_options = git2::PushOptions::new();
191 let mut remote_callbacks = git2::RemoteCallbacks::new(); 201 let mut remote_callbacks = git2::RemoteCallbacks::new();
192 remote_callbacks.credentials(auth.credentials(&git_config)); 202 remote_callbacks.credentials(auth.credentials(&git_config));
@@ -198,8 +208,9 @@ fn push(
198 .iter() 208 .iter()
199 .find(|r| r.contains(format!(":{name}").as_str())) 209 .find(|r| r.contains(format!(":{name}").as_str()))
200 { 210 {
201 if let Err(e) = update_remote_refs_pushed(git_repo, refspec, nostr_remote_url) 211 if let Err(e) =
202 .context("could not update remote_ref locally") 212 update_remote_refs_pushed(&git_repo.git_repo, refspec, nostr_remote_url)
213 .context("could not update remote_ref locally")
203 { 214 {
204 return Err(git2::Error::from_str(e.to_string().as_str())); 215 return Err(git2::Error::from_str(e.to_string().as_str()));
205 } 216 }
@@ -211,49 +222,125 @@ fn push(
211 push_options.remote_callbacks(remote_callbacks); 222 push_options.remote_callbacks(remote_callbacks);
212 git_server_remote.push(&refspecs, Some(&mut push_options))?; 223 git_server_remote.push(&refspecs, Some(&mut push_options))?;
213 git_server_remote.disconnect()?; 224 git_server_remote.disconnect()?;
225
226 // TODO check whether push was succesful before proceeding - geting outcome from
227 // callback isn't straightforward
228
229 let new_state = generate_updated_state(git_repo, repo_ref, &refspecs).await?;
230
231 // TODO enable interactive login
232 let (signer, user_ref) = login::launch(
233 git_repo,
234 &None,
235 &None,
236 &None,
237 &None,
238 Some(client),
239 false,
240 true,
241 )
242 .await?;
243 let new_repo_state = RepoState::build(repo_ref.identifier.clone(), new_state, &signer).await?;
244
245 send_events(
246 client,
247 git_repo.get_path()?,
248 vec![new_repo_state.event],
249 user_ref.relays.write(),
250 repo_ref.relays.clone(),
251 false,
252 true,
253 )
254 .await?;
255
214 println!(); 256 println!();
215 Ok(()) 257 Ok(())
216} 258}
217 259
260async fn generate_updated_state(
261 git_repo: &Repo,
262 repo_ref: &RepoRef,
263 refspecs: &Vec<String>,
264) -> Result<Vec<(String, String)>> {
265 let new_state = {
266 if let Ok(mut repo_state) = get_state_from_cache(git_repo.get_path()?, repo_ref).await {
267 for refspec in refspecs {
268 let (from, to) = refspec_to_from_to(refspec)?;
269 if to.is_empty() {
270 // delete
271 repo_state.state.retain(|(name, _)| !name.eq(to));
272 } else if repo_state.state.iter().any(|(name, _)| name.eq(from)) {
273 // update
274 repo_state.state = repo_state
275 .state
276 .iter()
277 .map(|(name, value)| {
278 (
279 name.clone(),
280 if name.eq(to) {
281 reference_to_ref_value(&git_repo.git_repo, to).unwrap()
282 } else {
283 value.to_string()
284 },
285 )
286 })
287 .collect();
288 } else {
289 // add
290 repo_state.state.push((
291 to.to_string(),
292 reference_to_ref_value(&git_repo.git_repo, to).unwrap(),
293 ));
294 }
295 }
296 repo_state.state
297 } else {
298 let mut state = vec![];
299 let git_server_url = repo_ref
300 .git_server
301 .first()
302 .context("no git server listed in nostr repository announcement")?;
303 let mut git_server_remote = git_repo.git_repo.remote_anonymous(git_server_url)?;
304 git_server_remote.connect(git2::Direction::Fetch)?;
305 for head in git_server_remote.list()? {
306 state.push((
307 head.name().to_string(),
308 if let Some(symbolic_ref) = head.symref_target() {
309 format!("ref: {}", symbolic_ref)
310 } else {
311 head.oid().to_string()
312 },
313 ));
314 }
315 git_server_remote.disconnect()?;
316 state
317 }
318 };
319 Ok(new_state)
320}
321
218fn update_remote_refs_pushed( 322fn update_remote_refs_pushed(
219 git_repo: &Repository, 323 git_repo: &Repository,
220 refspec: &str, 324 refspec: &str,
221 nostr_remote_url: &str, 325 nostr_remote_url: &str,
222) -> Result<()> { 326) -> Result<()> {
223 if !refspec.contains(':') { 327 let (from, _) = refspec_to_from_to(refspec)?;
224 bail!(
225 "refspec should contain a colon (:) but consists of: {}",
226 refspec
227 );
228 }
229 let parts = refspec.split(':').collect::<Vec<&str>>();
230 let from = parts.first().unwrap();
231 let to = parts.get(1).unwrap();
232 328
233 let nostr_remote = get_remote_by_url(git_repo, nostr_remote_url)?; 329 let target_ref_name = refspec_remote_ref_name(git_repo, refspec, nostr_remote_url)?;
234 330
235 let target_ref_name = format!(
236 "refs/remotes/{}/{}",
237 nostr_remote.name().context("remote should have a name")?,
238 to.replace("refs/heads/", ""), // TODO only replace if it begins with this
239 );
240 if from.is_empty() { 331 if from.is_empty() {
241 if let Ok(mut remote_ref) = git_repo.find_reference(&target_ref_name) { 332 if let Ok(mut remote_ref) = git_repo.find_reference(&target_ref_name) {
242 remote_ref.delete()?; 333 remote_ref.delete()?;
243 } 334 }
244 } else { 335 } else {
245 let local_ref = git_repo 336 let commit = reference_to_commit(git_repo, from)
246 .find_reference(from) 337 .context(format!("cannot get commit of reference {from}"))?;
247 .context(format!("from ref in refspec should exist: {from}"))?;
248 let commit = local_ref
249 .peel_to_commit()
250 .context(format!("from ref in refspec should peel to commit: {from}"))?;
251 if let Ok(mut remote_ref) = git_repo.find_reference(&target_ref_name) { 338 if let Ok(mut remote_ref) = git_repo.find_reference(&target_ref_name) {
252 remote_ref.set_target(commit.id(), "updated by nostr remote helper")?; 339 remote_ref.set_target(commit, "updated by nostr remote helper")?;
253 } else { 340 } else {
254 git_repo.reference( 341 git_repo.reference(
255 &target_ref_name, 342 &target_ref_name,
256 commit.id(), 343 commit,
257 false, 344 false,
258 "created by nostr remote helper", 345 "created by nostr remote helper",
259 )?; 346 )?;
@@ -262,9 +349,61 @@ fn update_remote_refs_pushed(
262 Ok(()) 349 Ok(())
263} 350}
264 351
265fn get_remote_by_url<'a>(git_repo: &'a Repository, url: &'a str) -> Result<Remote<'a>> { 352fn refspec_to_from_to(refspec: &str) -> Result<(&str, &str)> {
353 if !refspec.contains(':') {
354 bail!(
355 "refspec should contain a colon (:) but consists of: {}",
356 refspec
357 );
358 }
359 let parts = refspec.split(':').collect::<Vec<&str>>();
360 Ok((parts.first().unwrap(), parts.get(1).unwrap()))
361}
362
363fn refspec_remote_ref_name(
364 git_repo: &Repository,
365 refspec: &str,
366 nostr_remote_url: &str,
367) -> Result<String> {
368 let (_, to) = refspec_to_from_to(refspec)?;
369 let nostr_remote = git_repo
370 .find_remote(&get_remote_name_by_url(git_repo, nostr_remote_url)?)
371 .context("we should have just located this remote")?;
372 Ok(format!(
373 "refs/remotes/{}/{}",
374 nostr_remote.name().context("remote should have a name")?,
375 to.replace("refs/heads/", ""), // TODO only replace if it begins with this
376 ))
377}
378
379fn reference_to_commit(git_repo: &Repository, reference: &str) -> Result<Oid> {
380 Ok(git_repo
381 .find_reference(reference)
382 .context(format!("cannot find reference: {reference}"))?
383 .peel_to_commit()
384 .context(format!("cannot get commit from reference: {reference}"))?
385 .id())
386}
387
388// this maybe a commit id or a ref: pointer
389fn reference_to_ref_value(git_repo: &Repository, reference: &str) -> Result<String> {
390 let reference_obj = git_repo
391 .find_reference(reference)
392 .context(format!("cannot find reference: {reference}"))?;
393 if let Some(symref) = reference_obj.symbolic_target() {
394 Ok(symref.to_string())
395 } else {
396 Ok(reference_obj
397 .peel_to_commit()
398 .context(format!("cannot get commit from reference: {reference}"))?
399 .id()
400 .to_string())
401 }
402}
403
404fn get_remote_name_by_url(git_repo: &Repository, url: &str) -> Result<String> {
266 let remotes = git_repo.remotes()?; 405 let remotes = git_repo.remotes()?;
267 let remote_name = remotes 406 Ok(remotes
268 .iter() 407 .iter()
269 .find(|r| { 408 .find(|r| {
270 if let Some(name) = r { 409 if let Some(name) = r {
@@ -278,10 +417,8 @@ fn get_remote_by_url<'a>(git_repo: &'a Repository, url: &'a str) -> Result<Remot
278 } 417 }
279 }) 418 })
280 .context("could not find remote with matching url")? 419 .context("could not find remote with matching url")?
281 .context("remote with matching url must be named")?; 420 .context("remote with matching url must be named")?
282 git_repo 421 .to_string())
283 .find_remote(remote_name)
284 .context("we should have just located this remote")
285} 422}
286 423
287fn get_refstrs_from_fetch_batch(stdin: &Stdin, initial_refstr: &str) -> Result<Vec<String>> { 424fn get_refstrs_from_fetch_batch(stdin: &Stdin, initial_refstr: &str) -> Result<Vec<String>> {
@@ -319,3 +456,25 @@ fn get_refspecs_from_push_batch(stdin: &Stdin, initial_refspec: &str) -> Result<
319 } 456 }
320 Ok(refspecs) 457 Ok(refspecs)
321} 458}
459
460impl RepoState {
461 pub async fn build(
462 identifier: String,
463 state: Vec<(String, String)>,
464 signer: &NostrSigner,
465 ) -> Result<RepoState> {
466 let mut tags = vec![Tag::identifier(identifier.clone())];
467 for (name, value) in &state {
468 tags.push(Tag::custom(
469 nostr_sdk::TagKind::Custom(name.into()),
470 vec![value.clone()],
471 ));
472 }
473 let event = sign_event(EventBuilder::new(STATE_KIND, "", tags), signer).await?;
474 Ok(RepoState {
475 identifier,
476 state,
477 event,
478 })
479 }
480}
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::{
25}; 25};
26 26
27/// handles the encrpytion and storage of key material 27/// handles the encrpytion and storage of key material
28#[allow(clippy::too_many_arguments)]
28pub async fn launch( 29pub async fn launch(
29 git_repo: &Repo, 30 git_repo: &Repo,
30 bunker_uri: &Option<String>, 31 bunker_uri: &Option<String>,
@@ -34,6 +35,7 @@ pub async fn launch(
34 #[cfg(test)] client: Option<&MockConnect>, 35 #[cfg(test)] client: Option<&MockConnect>,
35 #[cfg(not(test))] client: Option<&Client>, 36 #[cfg(not(test))] client: Option<&Client>,
36 change_user: bool, 37 change_user: bool,
38 silent: bool,
37) -> Result<(NostrSigner, UserRef)> { 39) -> Result<(NostrSigner, UserRef)> {
38 if let Ok(signer) = match get_signer_without_prompts( 40 if let Ok(signer) = match get_signer_without_prompts(
39 git_repo, 41 git_repo,
@@ -58,7 +60,8 @@ pub async fn launch(
58 .unwrap_or("unknown ncryptsec".to_string()), 60 .unwrap_or("unknown ncryptsec".to_string()),
59 ) { 61 ) {
60 if let Ok(user_ref) = 62 if let Ok(user_ref) =
61 get_user_details(&public_key, client, git_repo.get_path()?).await 63 get_user_details(&public_key, client, git_repo.get_path()?, silent)
64 .await
62 { 65 {
63 user_ref.metadata.name 66 user_ref.metadata.name
64 } else { 67 } else {
@@ -94,10 +97,15 @@ pub async fn launch(
94 .context("cannot get public key from signer")?, 97 .context("cannot get public key from signer")?,
95 client, 98 client,
96 git_repo.get_path()?, 99 git_repo.get_path()?,
100 silent,
97 ) 101 )
98 .await?; 102 .await?;
99 print_logged_in_as(&user_ref, client.is_none())?; 103 if !silent {
104 print_logged_in_as(&user_ref, client.is_none())?;
105 }
100 Ok((signer, user_ref)) 106 Ok((signer, user_ref))
107 } else if silent {
108 bail!("TODO: enable interactive login in nostr git remote helper");
101 } else { 109 } else {
102 fresh_login(git_repo, client, change_user).await 110 fresh_login(git_repo, client, change_user).await
103 } 111 }
@@ -396,7 +404,7 @@ async fn fresh_login(
396 signer.public_key().await? 404 signer.public_key().await?
397 }; 405 };
398 // lookup profile 406 // lookup profile
399 let user_ref = get_user_details(&public_key, client, git_repo.get_path()?).await?; 407 let user_ref = get_user_details(&public_key, client, git_repo.get_path()?, false).await?;
400 print_logged_in_as(&user_ref, client.is_none())?; 408 print_logged_in_as(&user_ref, client.is_none())?;
401 Ok((signer, user_ref)) 409 Ok((signer, user_ref))
402} 410}
@@ -612,6 +620,7 @@ async fn get_user_details(
612 #[cfg(test)] client: Option<&crate::client::MockConnect>, 620 #[cfg(test)] client: Option<&crate::client::MockConnect>,
613 #[cfg(not(test))] client: Option<&Client>, 621 #[cfg(not(test))] client: Option<&Client>,
614 git_repo_path: &Path, 622 git_repo_path: &Path,
623 cache_only: bool,
615) -> Result<UserRef> { 624) -> Result<UserRef> {
616 if let Ok(user_ref) = get_user_ref_from_cache(git_repo_path, public_key).await { 625 if let Ok(user_ref) = get_user_ref_from_cache(git_repo_path, public_key).await {
617 Ok(user_ref) 626 Ok(user_ref)
@@ -621,8 +630,9 @@ async fn get_user_details(
621 metadata: extract_user_metadata(public_key, &[])?, 630 metadata: extract_user_metadata(public_key, &[])?,
622 relays: extract_user_relays(public_key, &[]), 631 relays: extract_user_relays(public_key, &[]),
623 }; 632 };
624 633 if cache_only {
625 if let Some(client) = client { 634 Ok(empty)
635 } else if let Some(client) = client {
626 let term = console::Term::stderr(); 636 let term = console::Term::stderr();
627 term.write_line("searching for profile...")?; 637 term.write_line("searching for profile...")?;
628 let (_, progress_reporter) = client 638 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};
2use git2::Oid; 2use git2::Oid;
3 3
4pub struct RepoState { 4pub struct RepoState {
5 pub identifier: String,
5 pub state: Vec<(String, String)>, 6 pub state: Vec<(String, String)>,
6 pub event: nostr::Event, 7 pub event: nostr::Event,
7} 8}
@@ -26,6 +27,10 @@ impl RepoState {
26 } 27 }
27 } 28 }
28 Ok(RepoState { 29 Ok(RepoState {
30 identifier: event
31 .identifier()
32 .context("existing event must have an identifier")?
33 .to_string(),
29 state, 34 state,
30 event: event.clone(), 35 event: event.clone(),
31 }) 36 })
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<()> {
89 &cli_args.password, 89 &cli_args.password,
90 Some(&client), 90 Some(&client),
91 false, 91 false,
92 false,
92 ) 93 )
93 .await?; 94 .await?;
94 95
@@ -330,6 +331,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> {
330 user_ref.relays.write(), 331 user_ref.relays.write(),
331 relays.clone(), 332 relays.clone(),
332 !cli_args.disable_cli_spinners, 333 !cli_args.disable_cli_spinners,
334 false,
333 ) 335 )
334 .await?; 336 .await?;
335 337
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<()> {
25 &args.password, 25 &args.password,
26 None, 26 None,
27 true, 27 true,
28 false,
28 ) 29 )
29 .await?; 30 .await?;
30 Ok(()) 31 Ok(())
@@ -42,6 +43,7 @@ pub async fn launch(args: &Cli, command_args: &SubCommandArgs) -> Result<()> {
42 &args.password, 43 &args.password,
43 Some(&client), 44 Some(&client),
44 true, 45 true,
46 false,
45 ) 47 )
46 .await?; 48 .await?;
47 client.disconnect().await?; 49 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<()> {
178 &cli_args.password, 178 &cli_args.password,
179 Some(&client), 179 Some(&client),
180 false, 180 false,
181 false,
181 ) 182 )
182 .await?; 183 .await?;
183 184
@@ -212,6 +213,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> {
212 user_ref.relays.write(), 213 user_ref.relays.write(),
213 repo_ref.relays.clone(), 214 repo_ref.relays.clone(),
214 !cli_args.disable_cli_spinners, 215 !cli_args.disable_cli_spinners,
216 false,
215 ) 217 )
216 .await?; 218 .await?;
217 219
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};
3use anyhow::{bail, Context, Result}; 3use anyhow::{bail, Context, Result};
4use console::Style; 4use console::Style;
5use futures::future::join_all; 5use futures::future::join_all;
6use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; 6use indicatif::{MultiProgress, ProgressBar, ProgressDrawTarget, ProgressStyle};
7use nostr::{ 7use nostr::{
8 nips::{ 8 nips::{
9 nip01::Coordinate, 9 nip01::Coordinate,
@@ -195,6 +195,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs, no_fetch: bool) -> Re
195 &cli_args.password, 195 &cli_args.password,
196 Some(&client), 196 Some(&client),
197 false, 197 false,
198 false,
198 ) 199 )
199 .await?; 200 .await?;
200 201
@@ -244,6 +245,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs, no_fetch: bool) -> Re
244 user_ref.relays.write(), 245 user_ref.relays.write(),
245 repo_ref.relays.clone(), 246 repo_ref.relays.clone(),
246 !cli_args.disable_cli_spinners, 247 !cli_args.disable_cli_spinners,
248 false,
247 ) 249 )
248 .await?; 250 .await?;
249 251
@@ -285,6 +287,7 @@ pub async fn send_events(
285 my_write_relays: Vec<String>, 287 my_write_relays: Vec<String>,
286 repo_read_relays: Vec<String>, 288 repo_read_relays: Vec<String>,
287 animate: bool, 289 animate: bool,
290 silent: bool,
288) -> Result<()> { 291) -> Result<()> {
289 let fallback = [ 292 let fallback = [
290 client.get_fallback_relays().clone(), 293 client.get_fallback_relays().clone(),
@@ -327,7 +330,11 @@ pub async fn send_events(
327 } 330 }
328 } 331 }
329 332
330 let m = MultiProgress::new(); 333 let m = if silent {
334 MultiProgress::with_draw_target(ProgressDrawTarget::hidden())
335 } else {
336 MultiProgress::new()
337 };
331 let pb_style = ProgressStyle::with_template(if animate { 338 let pb_style = ProgressStyle::with_template(if animate {
332 " {spinner} {prefix} {bar} {pos}/{len} {msg}" 339 " {spinner} {prefix} {bar} {pos}/{len} {msg}"
333 } else { 340 } else {
diff --git a/tests/git_remote_helper.rs b/tests/git_remote_helper.rs
index a575b58..ea76445 100644
--- a/tests/git_remote_helper.rs
+++ b/tests/git_remote_helper.rs
@@ -9,6 +9,7 @@ use serial_test::serial;
9use test_utils::{git::GitTestRepo, *}; 9use test_utils::{git::GitTestRepo, *};
10 10
11static NOSTR_REMOTE_NAME: &str = "nostr"; 11static NOSTR_REMOTE_NAME: &str = "nostr";
12static STATE_KIND: nostr::Kind = Kind::Custom(30618);
12 13
13fn get_nostr_remote_url() -> Result<String> { 14fn get_nostr_remote_url() -> Result<String> {
14 let repo_event = generate_repo_ref_event(); 15 let repo_event = generate_repo_ref_event();
@@ -429,6 +430,104 @@ mod push {
429 async_run_test().await 430 async_run_test().await
430 } 431 }
431 } 432 }
433 mod no_existing_state_event {
434 use super::*;
435
436 mod state_on_git_server_published_in_nostr_state_event {
437
438 use super::*;
439
440 async fn async_run_test() -> Result<()> {
441 let git_repo = prep_git_repo()?;
442 let source_git_repo = GitTestRepo::recreate_as_bare(&git_repo)?;
443
444 std::fs::write(git_repo.dir.join("commit.md"), "some content")?;
445 let main_commit_id = git_repo.stage_and_commit("commit.md")?;
446
447 git_repo.create_branch("vnext")?;
448 git_repo.checkout("vnext")?;
449 std::fs::write(git_repo.dir.join("vnext.md"), "some content")?;
450 let vnext_commit_id = git_repo.stage_and_commit("vnext.md")?;
451
452 let events = vec![
453 generate_test_key_1_metadata_event("fred"),
454 generate_test_key_1_relay_list_event(),
455 generate_repo_ref_event_with_git_server(
456 source_git_repo.dir.to_str().unwrap(),
457 ),
458 ];
459 // fallback (51,52) user write (53, 55) repo (55, 56) blaster (57)
460 let (mut r51, mut r52, mut r53, mut r55, mut r56, mut r57) = (
461 Relay::new(8051, None, None),
462 Relay::new(8052, None, None),
463 Relay::new(8053, None, None),
464 Relay::new(8055, None, None),
465 Relay::new(8056, None, None),
466 Relay::new(8057, None, None),
467 );
468 r51.events = events.clone();
469 r55.events = events;
470
471 let cli_tester_handle = std::thread::spawn(move || -> Result<()> {
472 let mut p = cli_tester_after_fetch(&git_repo)?;
473 p.send_line("push refs/heads/main:refs/heads/main")?;
474 p.send_line("push refs/heads/vnext:refs/heads/vnext")?;
475 p.send_line("")?;
476 p.expect("ok refs/heads/main\r\n")?;
477 p.expect("ok refs/heads/vnext\r\n")?;
478 p.expect("\r\n")?;
479
480 p.exit()?;
481 for p in [51, 52, 53, 55, 56, 57] {
482 relay::shutdown_relay(8000 + p)?;
483 }
484 Ok(())
485 });
486 // launch relays
487 let _ = join!(
488 r51.listen_until_close(),
489 r52.listen_until_close(),
490 r53.listen_until_close(),
491 r55.listen_until_close(),
492 r56.listen_until_close(),
493 r57.listen_until_close(),
494 );
495 cli_tester_handle.join().unwrap()?;
496
497 let state_event = r56
498 .events
499 .iter()
500 .find(|e| e.kind().eq(&STATE_KIND))
501 .context("state event not created")?;
502
503 assert_eq!(
504 state_event.identifier(),
505 generate_repo_ref_event().identifier(),
506 );
507 // println!("{:#?}", state_event);
508 assert_eq!(
509 state_event
510 .tags
511 .iter()
512 .filter(|t| t.kind().to_string().as_str().ne("d"))
513 .map(|t| t.as_vec().to_vec())
514 .collect::<HashSet<Vec<String>>>(),
515 HashSet::from([
516 vec!["HEAD".to_string(), "ref: refs/heads/main".to_string()],
517 vec!["refs/heads/main".to_string(), main_commit_id.to_string()],
518 vec!["refs/heads/vnext".to_string(), vnext_commit_id.to_string()],
519 ]),
520 );
521 Ok(())
522 }
523
524 #[tokio::test]
525 #[serial]
526 async fn sate_event_reflects_git_server_state() -> Result<()> {
527 async_run_test().await
528 }
529 }
530 }
432 } 531 }
433 mod delete_one_branch { 532 mod delete_one_branch {
434 533