From d2478dbca6c5d3f61331ceabe20c6d9315cd6840 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Tue, 3 Dec 2024 15:29:06 +0000 Subject: refactor: limit fetch to 1 trusted coordinate previously fetch supported fetching multiple trusted coordinates at once. The additional complexity trade off isn't worth it. It will still fetch events related to the coordinates of maintainers that the trusted maintainer lists. A scenario where there might be multiple trusted coordinates is where a large repo is split into multiple nostr repos to manage pull requests and issues in seperate sub-units. I might therefore have different remotes that target different identifiers. There also might be different set of maintainers groups that are have different views on the latest state of the same codebase and they haven't split off into using different identifiers. Its simplier ngit to fetch these different nostr repos independantly when requested. git-remote-nostr will do this automatically as it works through remotes but for ngit cmds a further change is required so `get_repo_coordinates` and `try_and_get_repo_coordinates` prompts to select the desired one. --- src/bin/git_remote_nostr/main.rs | 12 ++++--- src/bin/ngit/sub_commands/init.rs | 66 ++++++++++++++------------------------- 2 files changed, 31 insertions(+), 47 deletions(-) (limited to 'src/bin') diff --git a/src/bin/git_remote_nostr/main.rs b/src/bin/git_remote_nostr/main.rs index 54fb7cf..8e12d68 100644 --- a/src/bin/git_remote_nostr/main.rs +++ b/src/bin/git_remote_nostr/main.rs @@ -43,10 +43,10 @@ async fn main() -> Result<()> { client.set_signer(signer).await; } - fetching_with_report_for_helper(git_repo_path, &client, &decoded_nostr_url.coordinates).await?; + fetching_with_report_for_helper(git_repo_path, &client, &decoded_nostr_url.coordinate).await?; let repo_ref = - get_repo_ref_from_cache(Some(git_repo_path), &decoded_nostr_url.coordinates).await?; + get_repo_ref_from_cache(Some(git_repo_path), &decoded_nostr_url.coordinate).await?; let stdin = io::stdin(); let mut line = String::new(); @@ -148,12 +148,16 @@ fn process_args() -> Result> { async fn fetching_with_report_for_helper( git_repo_path: &Path, client: &Client, - repo_coordinates: &HashSet, + trusted_maintainer_coordinate: &Coordinate, ) -> Result<()> { let term = console::Term::stderr(); term.write_line("nostr: fetching...")?; let (relay_reports, progress_reporter) = client - .fetch_all(Some(git_repo_path), repo_coordinates, &HashSet::new()) + .fetch_all( + Some(git_repo_path), + Some(trusted_maintainer_coordinate), + &HashSet::new(), + ) .await?; if !relay_reports.iter().any(std::result::Result::is_err) { let _ = progress_reporter.clear(); diff --git a/src/bin/ngit/sub_commands/init.rs b/src/bin/ngit/sub_commands/init.rs index bf57769..9d87ba2 100644 --- a/src/bin/ngit/sub_commands/init.rs +++ b/src/bin/ngit/sub_commands/init.rs @@ -60,18 +60,17 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { let mut client = Client::default(); - let repo_coordinates = if let Ok(repo_coordinates) = + let repo_coordinate = if let Ok(repo_coordinate) = try_and_get_repo_coordinates(&git_repo, &client, false).await { - Some(repo_coordinates) + Some(repo_coordinate) } else { None }; - let repo_ref = if let Some(repo_coordinates) = repo_coordinates.clone() { - fetching_with_report(git_repo_path, &client, &repo_coordinates).await?; - if let Ok(repo_ref) = get_repo_ref_from_cache(Some(git_repo_path), &repo_coordinates).await - { + let repo_ref = if let Some(repo_coordinate) = &repo_coordinate { + fetching_with_report(git_repo_path, &client, repo_coordinate).await?; + if let Ok(repo_ref) = get_repo_ref_from_cache(Some(git_repo_path), repo_coordinate).await { Some(repo_ref) } else { None @@ -98,12 +97,8 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { .with_prompt("repo name") .with_default(if let Some(repo_ref) = &repo_ref { repo_ref.name.clone() - } else if let Some(repo_coordinates) = repo_coordinates.clone() { - if let Some(coordinate) = repo_coordinates.iter().next() { - coordinate.identifier.clone() - } else { - String::new() - } + } else if let Some(coordinate) = &repo_coordinate { + coordinate.identifier.clone() } else { String::new() }), @@ -119,12 +114,8 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { ) .with_default(if let Some(repo_ref) = &repo_ref { repo_ref.identifier.clone() - } else if let Some(repo_coordinates) = repo_coordinates.clone() { - if let Some(coordinate) = repo_coordinates.iter().next() { - coordinate.identifier.clone() - } else { - String::new() - } + } else if let Some(repo_coordinate) = &repo_coordinate { + repo_coordinate.identifier.clone() } else { let fallback = name .clone() @@ -503,32 +494,21 @@ fn prompt_to_set_nostr_url_as_origin(repo_ref: &RepoRef, git_repo: &Repo) -> Res if let Ok(origin_remote) = git_repo.git_repo.find_remote("origin") { if let Some(origin_url) = origin_remote.url() { if let Ok(nostr_url) = NostrUrlDecoded::from_str(origin_url) { - if let Some(c) = &nostr_url.coordinates.iter().next() { - if c.identifier == repo_ref.identifier { - if nostr_url - .coordinates - .iter() - .next() - .context( - "a decoded nostr url will always have at least one coordinate", - )? - .public_key - == repo_ref.trusted_maintainer - { - return Ok(()); - } - // origin is set to a different trusted maintainer - println!( - "warning: currently git remote 'origin' is set to a different trusted maintainer with the same identifier" - ); - ask_to_set_origin_remote(repo_ref, git_repo)?; - } else { - // origin is linked to a different identifier - println!( - "warning: currently git remote 'origin' is set to a different repository identifier" - ); - ask_to_set_origin_remote(repo_ref, git_repo)?; + if nostr_url.coordinate.identifier == repo_ref.identifier { + if nostr_url.coordinate.public_key == repo_ref.trusted_maintainer { + return Ok(()); } + // origin is set to a different trusted maintainer + println!( + "warning: currently git remote 'origin' is set to a different trusted maintainer with the same identifier" + ); + ask_to_set_origin_remote(repo_ref, git_repo)?; + } else { + // origin is linked to a different identifier + println!( + "warning: currently git remote 'origin' is set to a different repository identifier" + ); + ask_to_set_origin_remote(repo_ref, git_repo)?; } } else { // remote is non-nostr url -- cgit v1.2.3