upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/bin/git_remote_nostr/utils.rs
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2024-09-09 07:36:47 +0100
committerDanConwayDev <DanConwayDev@protonmail.com>2024-09-09 07:36:47 +0100
commitd2d0eeb72912809a00f09fafdae4e827a34d0e26 (patch)
tree5f87f87ce8f068d4fa22a9f7f12701ea467bf0ff /src/bin/git_remote_nostr/utils.rs
parentad28bd8db1a6e953dce999eea85becda1d90beae (diff)
feat(remote): push protocol selection / fallback
enable override from nostr url clone url is filesystem use filesystem otherwise try ssh, then https authenticated unless clone url is http, then try ssh then http as we assume, we are on a local trusted network.
Diffstat (limited to 'src/bin/git_remote_nostr/utils.rs')
-rw-r--r--src/bin/git_remote_nostr/utils.rs76
1 files changed, 34 insertions, 42 deletions
diff --git a/src/bin/git_remote_nostr/utils.rs b/src/bin/git_remote_nostr/utils.rs
index 90ea848..3039fe3 100644
--- a/src/bin/git_remote_nostr/utils.rs
+++ b/src/bin/git_remote_nostr/utils.rs
@@ -91,47 +91,6 @@ pub fn read_line<'a>(stdin: &io::Stdin, line: &'a mut String) -> io::Result<Vec<
91 Ok(tokens) 91 Ok(tokens)
92} 92}
93 93
94pub fn switch_clone_url_between_ssh_and_https(url: &str) -> Result<String> {
95 if url.starts_with("https://") {
96 // Convert HTTPS to git@ syntax
97 let parts: Vec<&str> = url.trim_start_matches("https://").split('/').collect();
98 if parts.len() >= 2 {
99 // Construct the git@ URL
100 Ok(format!("git@{}:{}", parts[0], parts[1..].join("/")))
101 } else {
102 // If the format is unexpected, return an error
103 bail!("Invalid HTTPS URL format: {}", url);
104 }
105 } else if url.starts_with("ssh://") {
106 // Convert SSH to git@ syntax
107 let parts: Vec<&str> = url.trim_start_matches("ssh://").split('/').collect();
108 if parts.len() >= 2 {
109 // Construct the git@ URL
110 Ok(format!("git@{}:{}", parts[0], parts[1..].join("/")))
111 } else {
112 // If the format is unexpected, return an error
113 bail!("Invalid SSH URL format: {}", url);
114 }
115 } else if url.starts_with("git@") {
116 // Convert git@ syntax to HTTPS
117 let parts: Vec<&str> = url.split(':').collect();
118 if parts.len() == 2 {
119 // Construct the HTTPS URL
120 Ok(format!(
121 "https://{}/{}",
122 parts[0].trim_end_matches('@'),
123 parts[1]
124 ))
125 } else {
126 // If the format is unexpected, return an error
127 bail!("Invalid git@ URL format: {}", url);
128 }
129 } else {
130 // If the URL is neither HTTPS, SSH, nor git@, return an error
131 bail!("Unsupported URL protocol: {}", url);
132 }
133}
134
135pub async fn get_open_proposals( 94pub async fn get_open_proposals(
136 git_repo: &Repo, 95 git_repo: &Repo,
137 repo_ref: &RepoRef, 96 repo_ref: &RepoRef,
@@ -297,7 +256,40 @@ pub fn get_read_protocols_to_try(
297 } 256 }
298} 257}
299 258
300pub fn error_is_not_authentication_failure(error: &anyhow::Error) -> bool { 259/// get an ordered vector of server protocols to attempt
260pub fn get_write_protocols_to_try(
261 server_url: &CloneUrl,
262 decoded_nostr_url: &NostrUrlDecoded,
263) -> Vec<ServerProtocol> {
264 if server_url.protocol() == ServerProtocol::Filesystem {
265 vec![(ServerProtocol::Filesystem)]
266 } else if let Some(protocol) = &decoded_nostr_url.protocol {
267 vec![protocol.clone()]
268 } else if server_url.protocol() == ServerProtocol::Http {
269 vec![
270 ServerProtocol::Ssh,
271 // note: list and fetch stop here if ssh was authenticated
272 ServerProtocol::Http,
273 ]
274 } else if server_url.protocol() == ServerProtocol::Ftp {
275 vec![ServerProtocol::Ssh, ServerProtocol::Ftp]
276 } else {
277 vec![
278 ServerProtocol::Ssh,
279 // note: list and fetch stop here if ssh was authenticated
280 ServerProtocol::Https,
281 ]
282 }
283}
284
285/// to understand whether to try over another protocol
286pub fn fetch_or_list_error_is_not_authentication_failure(error: &anyhow::Error) -> bool {
287 let error_str = error.to_string();
288 error_str.contains("Permission to") || error_str.contains("Repository not found")
289}
290
291/// to understand whether to try over another protocol
292pub fn push_error_is_not_authentication_failure(error: &anyhow::Error) -> bool {
301 let error_str = error.to_string(); 293 let error_str = error.to_string();
302 error_str.contains("Permission to") || error_str.contains("Repository not found") 294 error_str.contains("Permission to") || error_str.contains("Repository not found")
303} 295}