diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2025-05-23 08:13:21 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2025-05-23 08:14:53 +0100 |
| commit | d803f0d52a7b5ca95f798d624b1deeb26052da81 (patch) | |
| tree | dcd24673724406c42e095941ef4c274b6b722319 | |
| parent | 2596140ba29dc959643ae02fb2db4de980ee12e9 (diff) | |
fix(init): ngit-relay usage detection
make sure blossom server is also present
| -rw-r--r-- | src/bin/ngit/sub_commands/init.rs | 62 |
1 files changed, 47 insertions, 15 deletions
diff --git a/src/bin/ngit/sub_commands/init.rs b/src/bin/ngit/sub_commands/init.rs index 5c090ae..41ed407 100644 --- a/src/bin/ngit/sub_commands/init.rs +++ b/src/bin/ngit/sub_commands/init.rs | |||
| @@ -248,10 +248,11 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { | |||
| 248 | // ignore so a script running `ngit init` can contiue without prompts | 248 | // ignore so a script running `ngit init` can contiue without prompts |
| 249 | vec![] | 249 | vec![] |
| 250 | } else { | 250 | } else { |
| 251 | let mut options: Vec<String> = guess_at_existing_ngit_relays( | 251 | let mut options: Vec<String> = detect_existing_ngit_relays( |
| 252 | repo_ref.as_ref(), | 252 | repo_ref.as_ref(), |
| 253 | &args.relays, | 253 | &args.relays, |
| 254 | &args.clone_url, | 254 | &args.clone_url, |
| 255 | &args.blossoms, | ||
| 255 | &identifier, | 256 | &identifier, |
| 256 | ); | 257 | ); |
| 257 | let mut selections: Vec<bool> = vec![true; options.len()]; // Initialize selections based on existing options | 258 | let mut selections: Vec<bool> = vec![true; options.len()]; // Initialize selections based on existing options |
| @@ -868,10 +869,11 @@ where | |||
| 868 | Ok(selected_choices) | 869 | Ok(selected_choices) |
| 869 | } | 870 | } |
| 870 | 871 | ||
| 871 | fn guess_at_existing_ngit_relays( | 872 | fn detect_existing_ngit_relays( |
| 872 | repo_ref: Option<&RepoRef>, | 873 | repo_ref: Option<&RepoRef>, |
| 873 | args_relays: &[String], | 874 | args_relays: &[String], |
| 874 | args_clone_url: &[String], | 875 | args_clone_url: &[String], |
| 876 | args_blossoms: &[String], | ||
| 875 | identifier: &str, | 877 | identifier: &str, |
| 876 | ) -> Vec<String> { | 878 | ) -> Vec<String> { |
| 877 | // Collect clone URLs from arguments or repo_ref | 879 | // Collect clone URLs from arguments or repo_ref |
| @@ -895,22 +897,52 @@ fn guess_at_existing_ngit_relays( | |||
| 895 | Vec::new() | 897 | Vec::new() |
| 896 | }; | 898 | }; |
| 897 | 899 | ||
| 900 | // Collect blossom server URLs from arguments or repo_ref | ||
| 901 | let blossoms: Vec<Url> = if !args_blossoms.is_empty() { | ||
| 902 | args_blossoms | ||
| 903 | .iter() | ||
| 904 | .filter_map(|r| Url::parse(r).ok()) | ||
| 905 | .collect() | ||
| 906 | } else if let Some(repo) = repo_ref { | ||
| 907 | repo.blossoms.clone() | ||
| 908 | } else { | ||
| 909 | Vec::new() | ||
| 910 | }; | ||
| 911 | |||
| 898 | let mut existing_ngit_relays = Vec::new(); | 912 | let mut existing_ngit_relays = Vec::new(); |
| 899 | for url in &clone_urls { | 913 | for url in &clone_urls { |
| 900 | if let Ok(npub) = extract_npub(url) { | 914 | let Ok(formatted_as_ngit_relay_url) = normalize_ngit_relay_url(url) else { |
| 901 | let postfix = format!("/{npub}/{identifier}.git"); | 915 | continue; |
| 902 | if url.contains(&postfix) { | 916 | }; |
| 903 | if let Ok(ngit_relay_url) = normalize_ngit_relay_url(url) { | 917 | if existing_ngit_relays.contains(&formatted_as_ngit_relay_url) { |
| 904 | let is_also_relay = relays.iter().any(|r| { | 918 | continue; |
| 905 | normalize_ngit_relay_url(&r.to_string()) | ||
| 906 | .is_ok_and(|r| r.eq(&ngit_relay_url)) | ||
| 907 | }); | ||
| 908 | if !existing_ngit_relays.contains(&ngit_relay_url) && is_also_relay { | ||
| 909 | existing_ngit_relays.push(ngit_relay_url); | ||
| 910 | } | ||
| 911 | } | ||
| 912 | } | ||
| 913 | } | 919 | } |
| 920 | |||
| 921 | let clone_url_is_ngit_relay_format = if let Ok(npub) = extract_npub(url) { | ||
| 922 | url.contains(&format!("/{npub}/{identifier}.git")) | ||
| 923 | } else { | ||
| 924 | false | ||
| 925 | }; | ||
| 926 | if !clone_url_is_ngit_relay_format { | ||
| 927 | continue; | ||
| 928 | } | ||
| 929 | |||
| 930 | let matches_relay = relays.iter().any(|r| { | ||
| 931 | normalize_ngit_relay_url(&r.to_string()) | ||
| 932 | .is_ok_and(|r| r.eq(&formatted_as_ngit_relay_url)) | ||
| 933 | }); | ||
| 934 | if !matches_relay { | ||
| 935 | continue; | ||
| 936 | } | ||
| 937 | |||
| 938 | let matches_blossoms = blossoms.iter().any(|r| { | ||
| 939 | normalize_ngit_relay_url(r.as_str()).is_ok_and(|r| r.eq(&formatted_as_ngit_relay_url)) | ||
| 940 | }); | ||
| 941 | if !matches_blossoms { | ||
| 942 | continue; | ||
| 943 | } | ||
| 944 | |||
| 945 | existing_ngit_relays.push(formatted_as_ngit_relay_url); | ||
| 914 | } | 946 | } |
| 915 | existing_ngit_relays | 947 | existing_ngit_relays |
| 916 | } | 948 | } |