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-08-06 11:49:28 +0100
committerDanConwayDev <DanConwayDev@protonmail.com>2024-08-06 13:09:49 +0100
commit47271a823485c002ab6c3f304b86dbba2d7594dd (patch)
tree75d06b6e6c4c3f3b82baf3b5565b8471243b85e6
parentf1aa1be738af0dc80eb3b5827249bb537de1e0cd (diff)
feat(remote): `fetch` report on progress
so that user knows what step we are on
-rw-r--r--src/git_remote_helper.rs51
-rw-r--r--tests/git_remote_helper.rs15
2 files changed, 52 insertions, 14 deletions
diff --git a/src/git_remote_helper.rs b/src/git_remote_helper.rs
index f851c90..0cfd268 100644
--- a/src/git_remote_helper.rs
+++ b/src/git_remote_helper.rs
@@ -103,8 +103,8 @@ async fn main() -> Result<()> {
103 ["option", ..] => { 103 ["option", ..] => {
104 println!("unsupported"); 104 println!("unsupported");
105 } 105 }
106 ["fetch", oid, _refstr] => { 106 ["fetch", oid, refstr] => {
107 fetch(&git_repo.git_repo, &repo_ref, &stdin, oid)?; 107 fetch(&git_repo, &repo_ref, &stdin, oid, refstr)?;
108 } 108 }
109 ["push", refspec] => { 109 ["push", refspec] => {
110 push( 110 push(
@@ -431,18 +431,42 @@ async fn get_open_proposals(
431 Ok(open_proposals) 431 Ok(open_proposals)
432} 432}
433 433
434fn fetch(git_repo: &Repository, repo_ref: &RepoRef, stdin: &Stdin, oid: &str) -> Result<()> { 434fn fetch(
435 let oids = get_oids_from_fetch_batch(stdin, oid)?; 435 git_repo: &Repo,
436 repo_ref: &RepoRef,
437 stdin: &Stdin,
438 oid: &str,
439 refstr: &str,
440) -> Result<()> {
441 let fetch_batch = get_oids_from_fetch_batch(stdin, oid, refstr)?;
442
443 let oids_from_git_servers = fetch_batch.values().cloned().collect::<Vec<String>>();
436 444
437 let mut errors = HashMap::new(); 445 let mut errors = HashMap::new();
446 let term = console::Term::stderr();
447
438 for git_server_url in &repo_ref.git_server { 448 for git_server_url in &repo_ref.git_server {
439 if let Err(e) = fetch_from_git_server(git_repo, &oids, git_server_url) { 449 let term = console::Term::stderr();
440 errors.insert(git_server_url.to_string(), e); 450 let short_name = get_short_git_server_name(git_repo, git_server_url);
451 term.write_line(format!("fetching from {short_name}...").as_str())?;
452 let res = fetch_from_git_server(&git_repo.git_repo, &oids_from_git_servers, git_server_url);
453 term.clear_last_lines(1)?;
454 if let Err(e) = res {
455 term.write_line(
456 format!(
457 "WARNING: failed to fetch from {short_name} error:
458 {e}"
459 )
460 .as_str(),
461 )?;
462 errors.insert(short_name.to_string(), e);
441 } else { 463 } else {
464 term.flush()?;
442 println!(); 465 println!();
443 return Ok(()); 466 return Ok(());
444 } 467 }
445 } 468 }
469 term.flush()?;
446 bail!( 470 bail!(
447 "failed to fetch objects in nostr state event from:\r\n{}", 471 "failed to fetch objects in nostr state event from:\r\n{}",
448 errors 472 errors
@@ -916,14 +940,19 @@ fn get_short_git_server_name(git_repo: &Repo, url: &str) -> std::string::String
916 url.to_string() 940 url.to_string()
917} 941}
918 942
919fn get_oids_from_fetch_batch(stdin: &Stdin, initial_oid: &str) -> Result<Vec<String>> { 943fn get_oids_from_fetch_batch(
944 stdin: &Stdin,
945 initial_oid: &str,
946 initial_refstr: &str,
947) -> Result<HashMap<String, String>> {
920 let mut line = String::new(); 948 let mut line = String::new();
921 let mut oids = vec![initial_oid.to_string()]; 949 let mut batch = HashMap::new();
950 batch.insert(initial_refstr.to_string(), initial_oid.to_string());
922 loop { 951 loop {
923 let tokens = read_line(stdin, &mut line)?; 952 let tokens = read_line(stdin, &mut line)?;
924 match tokens.as_slice() { 953 match tokens.as_slice() {
925 ["fetch", oid, _refstr] => { 954 ["fetch", oid, refstr] => {
926 oids.push((*oid).to_string()); 955 batch.insert((*refstr).to_string(), (*oid).to_string());
927 } 956 }
928 [] => break, 957 [] => break,
929 _ => bail!( 958 _ => bail!(
@@ -931,7 +960,7 @@ fn get_oids_from_fetch_batch(stdin: &Stdin, initial_oid: &str) -> Result<Vec<Str
931 ), 960 ),
932 } 961 }
933 } 962 }
934 Ok(oids) 963 Ok(batch)
935} 964}
936 965
937fn get_refspecs_from_push_batch(stdin: &Stdin, initial_refspec: &str) -> Result<Vec<String>> { 966fn get_refspecs_from_push_batch(stdin: &Stdin, initial_refspec: &str) -> Result<Vec<String>> {
diff --git a/tests/git_remote_helper.rs b/tests/git_remote_helper.rs
index 77d7ecb..f86cb7a 100644
--- a/tests/git_remote_helper.rs
+++ b/tests/git_remote_helper.rs
@@ -558,6 +558,8 @@ mod fetch {
558 #[serial] 558 #[serial]
559 async fn fetch_downloads_speficied_commits_from_git_server() -> Result<()> { 559 async fn fetch_downloads_speficied_commits_from_git_server() -> Result<()> {
560 let source_git_repo = prep_git_repo()?; 560 let source_git_repo = prep_git_repo()?;
561 let source_path = source_git_repo.dir.to_str().unwrap().to_string();
562
561 std::fs::write(source_git_repo.dir.join("commit.md"), "some content")?; 563 std::fs::write(source_git_repo.dir.join("commit.md"), "some content")?;
562 let main_commit_id = source_git_repo.stage_and_commit("commit.md")?; 564 let main_commit_id = source_git_repo.stage_and_commit("commit.md")?;
563 565
@@ -594,7 +596,8 @@ mod fetch {
594 p.send_line(format!("fetch {main_commit_id} main").as_str())?; 596 p.send_line(format!("fetch {main_commit_id} main").as_str())?;
595 p.send_line(format!("fetch {vnext_commit_id} vnext").as_str())?; 597 p.send_line(format!("fetch {vnext_commit_id} vnext").as_str())?;
596 p.send_line("")?; 598 p.send_line("")?;
597 p.expect("\r\n")?; 599 p.expect(format!("fetching from {source_path}...\r\n").as_str())?;
600 p.expect_eventually_and_print("\r\n")?;
598 601
599 assert!(git_repo.git_repo.find_commit(main_commit_id).is_ok()); 602 assert!(git_repo.git_repo.find_commit(main_commit_id).is_ok());
600 assert!(git_repo.git_repo.find_commit(vnext_commit_id).is_ok()); 603 assert!(git_repo.git_repo.find_commit(vnext_commit_id).is_ok());
@@ -625,6 +628,8 @@ mod fetch {
625 #[serial] 628 #[serial]
626 async fn fetch_downloads_speficied_commits_from_second_git_server() -> Result<()> { 629 async fn fetch_downloads_speficied_commits_from_second_git_server() -> Result<()> {
627 let (state_event, source_git_repo) = generate_repo_with_state_event().await?; 630 let (state_event, source_git_repo) = generate_repo_with_state_event().await?;
631 // let source_path = source_git_repo.dir.to_str().unwrap().to_string();
632 let error_path = "./path-doesnt-exist".to_string();
628 633
629 let main_commit_id = source_git_repo.get_tip_of_local_branch("main")?; 634 let main_commit_id = source_git_repo.get_tip_of_local_branch("main")?;
630 635
@@ -634,7 +639,7 @@ mod fetch {
634 generate_test_key_1_metadata_event("fred"), 639 generate_test_key_1_metadata_event("fred"),
635 generate_test_key_1_relay_list_event(), 640 generate_test_key_1_relay_list_event(),
636 generate_repo_ref_event_with_git_server(vec![ 641 generate_repo_ref_event_with_git_server(vec![
637 "./path-doesnt-exist".to_string(), 642 error_path.to_string(),
638 source_git_repo.dir.to_str().unwrap().to_string(), 643 source_git_repo.dir.to_str().unwrap().to_string(),
639 ]), 644 ]),
640 state_event, 645 state_event,
@@ -657,7 +662,11 @@ mod fetch {
657 let mut p = cli_tester_after_fetch(&git_repo)?; 662 let mut p = cli_tester_after_fetch(&git_repo)?;
658 p.send_line(format!("fetch {main_commit_id} main").as_str())?; 663 p.send_line(format!("fetch {main_commit_id} main").as_str())?;
659 p.send_line("")?; 664 p.send_line("")?;
660 p.expect("\r\n")?; 665 p.expect(format!("fetching from {error_path}...\r\n").as_str())?;
666 // not sure why the below isn't appearing
667 // p.expect(format!("fetching from {source_path}...\r\n").as_str())?;
668 p.expect_eventually_and_print("\r\n")?;
669 // p.expect("\r\n")?;
661 670
662 assert!(git_repo.git_repo.find_commit(main_commit_id).is_ok()); 671 assert!(git_repo.git_repo.find_commit(main_commit_id).is_ok());
663 672