From cea93cee5edf67499cbd4be0843b67f68265be3e Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Sat, 31 May 2025 15:56:27 +0100 Subject: feat(init): only use one ngit-relay git server url that of the current users pubkey. ngit will push to the git servers of other maintainers anyway. --- src/bin/ngit/sub_commands/init.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'src/bin') diff --git a/src/bin/ngit/sub_commands/init.rs b/src/bin/ngit/sub_commands/init.rs index aea4f8b..cd94a5a 100644 --- a/src/bin/ngit/sub_commands/init.rs +++ b/src/bin/ngit/sub_commands/init.rs @@ -297,8 +297,38 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { if args.clone_url.is_empty() { let clone_url = format_ngit_relay_url_as_clone_url(ngit_relay, &user_ref.public_key, &identifier)?; - if !git_server_defaults.contains(&clone_url) { + + let ngit_relay_clone_root = if clone_url.contains("https://") { + format!("https://{ngit_relay}") + } else { + ngit_relay.to_string() + }; + + // Find all positions of entries containing the relay root + let matching_positions: Vec = git_server_defaults + .iter() + .enumerate() + .filter_map(|(idx, url)| { + if url.contains(&ngit_relay_clone_root) { + Some(idx) + } else { + None + } + }) + .collect(); + + // If we found any matches + if matching_positions.is_empty() { + // No existing entries found, so add a new one git_server_defaults.push(clone_url); + } else { + // Replace the first occurrence + git_server_defaults[matching_positions[0]] = clone_url; + + // Remove any subsequent occurrences (in reverse order to avoid index issues) + for &position in matching_positions.iter().skip(1).rev() { + git_server_defaults.remove(position); + } } } if args.relays.is_empty() { -- cgit v1.2.3