From 47271a823485c002ab6c3f304b86dbba2d7594dd Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Tue, 6 Aug 2024 11:49:28 +0100 Subject: feat(remote): `fetch` report on progress so that user knows what step we are on --- src/git_remote_helper.rs | 51 ++++++++++++++++++++++++++++++++++++---------- tests/git_remote_helper.rs | 15 +++++++++++--- 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<()> { ["option", ..] => { println!("unsupported"); } - ["fetch", oid, _refstr] => { - fetch(&git_repo.git_repo, &repo_ref, &stdin, oid)?; + ["fetch", oid, refstr] => { + fetch(&git_repo, &repo_ref, &stdin, oid, refstr)?; } ["push", refspec] => { push( @@ -431,18 +431,42 @@ async fn get_open_proposals( Ok(open_proposals) } -fn fetch(git_repo: &Repository, repo_ref: &RepoRef, stdin: &Stdin, oid: &str) -> Result<()> { - let oids = get_oids_from_fetch_batch(stdin, oid)?; +fn fetch( + git_repo: &Repo, + repo_ref: &RepoRef, + stdin: &Stdin, + oid: &str, + refstr: &str, +) -> Result<()> { + let fetch_batch = get_oids_from_fetch_batch(stdin, oid, refstr)?; + + let oids_from_git_servers = fetch_batch.values().cloned().collect::>(); let mut errors = HashMap::new(); + let term = console::Term::stderr(); + for git_server_url in &repo_ref.git_server { - if let Err(e) = fetch_from_git_server(git_repo, &oids, git_server_url) { - errors.insert(git_server_url.to_string(), e); + let term = console::Term::stderr(); + let short_name = get_short_git_server_name(git_repo, git_server_url); + term.write_line(format!("fetching from {short_name}...").as_str())?; + let res = fetch_from_git_server(&git_repo.git_repo, &oids_from_git_servers, git_server_url); + term.clear_last_lines(1)?; + if let Err(e) = res { + term.write_line( + format!( + "WARNING: failed to fetch from {short_name} error: + {e}" + ) + .as_str(), + )?; + errors.insert(short_name.to_string(), e); } else { + term.flush()?; println!(); return Ok(()); } } + term.flush()?; bail!( "failed to fetch objects in nostr state event from:\r\n{}", errors @@ -916,14 +940,19 @@ fn get_short_git_server_name(git_repo: &Repo, url: &str) -> std::string::String url.to_string() } -fn get_oids_from_fetch_batch(stdin: &Stdin, initial_oid: &str) -> Result> { +fn get_oids_from_fetch_batch( + stdin: &Stdin, + initial_oid: &str, + initial_refstr: &str, +) -> Result> { let mut line = String::new(); - let mut oids = vec![initial_oid.to_string()]; + let mut batch = HashMap::new(); + batch.insert(initial_refstr.to_string(), initial_oid.to_string()); loop { let tokens = read_line(stdin, &mut line)?; match tokens.as_slice() { - ["fetch", oid, _refstr] => { - oids.push((*oid).to_string()); + ["fetch", oid, refstr] => { + batch.insert((*refstr).to_string(), (*oid).to_string()); } [] => break, _ => bail!( @@ -931,7 +960,7 @@ fn get_oids_from_fetch_batch(stdin: &Stdin, initial_oid: &str) -> Result Result> { 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 { #[serial] async fn fetch_downloads_speficied_commits_from_git_server() -> Result<()> { let source_git_repo = prep_git_repo()?; + let source_path = source_git_repo.dir.to_str().unwrap().to_string(); + std::fs::write(source_git_repo.dir.join("commit.md"), "some content")?; let main_commit_id = source_git_repo.stage_and_commit("commit.md")?; @@ -594,7 +596,8 @@ mod fetch { p.send_line(format!("fetch {main_commit_id} main").as_str())?; p.send_line(format!("fetch {vnext_commit_id} vnext").as_str())?; p.send_line("")?; - p.expect("\r\n")?; + p.expect(format!("fetching from {source_path}...\r\n").as_str())?; + p.expect_eventually_and_print("\r\n")?; assert!(git_repo.git_repo.find_commit(main_commit_id).is_ok()); assert!(git_repo.git_repo.find_commit(vnext_commit_id).is_ok()); @@ -625,6 +628,8 @@ mod fetch { #[serial] async fn fetch_downloads_speficied_commits_from_second_git_server() -> Result<()> { let (state_event, source_git_repo) = generate_repo_with_state_event().await?; + // let source_path = source_git_repo.dir.to_str().unwrap().to_string(); + let error_path = "./path-doesnt-exist".to_string(); let main_commit_id = source_git_repo.get_tip_of_local_branch("main")?; @@ -634,7 +639,7 @@ mod fetch { generate_test_key_1_metadata_event("fred"), generate_test_key_1_relay_list_event(), generate_repo_ref_event_with_git_server(vec![ - "./path-doesnt-exist".to_string(), + error_path.to_string(), source_git_repo.dir.to_str().unwrap().to_string(), ]), state_event, @@ -657,7 +662,11 @@ mod fetch { let mut p = cli_tester_after_fetch(&git_repo)?; p.send_line(format!("fetch {main_commit_id} main").as_str())?; p.send_line("")?; - p.expect("\r\n")?; + p.expect(format!("fetching from {error_path}...\r\n").as_str())?; + // not sure why the below isn't appearing + // p.expect(format!("fetching from {source_path}...\r\n").as_str())?; + p.expect_eventually_and_print("\r\n")?; + // p.expect("\r\n")?; assert!(git_repo.git_repo.find_commit(main_commit_id).is_ok()); -- cgit v1.2.3