diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-08-19 08:47:53 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-08-19 08:47:53 +0100 |
| commit | 4291428c714b8cc7b7a5f3c1a1504b0098e00455 (patch) | |
| tree | 537d200bcc06c835fd02231f811ede743cb00241 /src/git_remote_helper.rs | |
| parent | 6b46ad9809b42d355c883ff2c75f8ac75d4bd989 (diff) | |
refactor(remote): move url_to_repo_coordinates
so it can be used by repo_ref which doesnt import git_remote_helper
Diffstat (limited to 'src/git_remote_helper.rs')
| -rw-r--r-- | src/git_remote_helper.rs | 53 |
1 files changed, 1 insertions, 52 deletions
diff --git a/src/git_remote_helper.rs b/src/git_remote_helper.rs index cb13e8c..9b9b000 100644 --- a/src/git_remote_helper.rs +++ b/src/git_remote_helper.rs | |||
| @@ -18,7 +18,7 @@ use client::{ | |||
| 18 | consolidate_fetch_reports, get_events_from_cache, get_repo_ref_from_cache, | 18 | consolidate_fetch_reports, get_events_from_cache, get_repo_ref_from_cache, |
| 19 | get_state_from_cache, sign_event, Connect, STATE_KIND, | 19 | get_state_from_cache, sign_event, Connect, STATE_KIND, |
| 20 | }; | 20 | }; |
| 21 | use git::{sha1_to_oid, RepoActions}; | 21 | use git::{nostr_git_url_to_repo_coordinates, sha1_to_oid, RepoActions}; |
| 22 | use git2::{Oid, Repository}; | 22 | use git2::{Oid, Repository}; |
| 23 | use nostr::nips::{nip01::Coordinate, nip10::Marker}; | 23 | use nostr::nips::{nip01::Coordinate, nip10::Marker}; |
| 24 | use nostr_sdk::{ | 24 | use nostr_sdk::{ |
| @@ -152,57 +152,6 @@ pub(crate) fn read_line<'a>(stdin: &io::Stdin, line: &'a mut String) -> io::Resu | |||
| 152 | Ok(tokens) | 152 | Ok(tokens) |
| 153 | } | 153 | } |
| 154 | 154 | ||
| 155 | fn nostr_git_url_to_repo_coordinates(url: &str) -> Result<HashSet<Coordinate>> { | ||
| 156 | let mut repo_coordinattes = HashSet::new(); | ||
| 157 | let url = Url::parse(url)?; | ||
| 158 | |||
| 159 | if url.scheme().ne("nostr") { | ||
| 160 | bail!("nostr git url must start with nostr://") | ||
| 161 | } | ||
| 162 | |||
| 163 | if let Ok(coordinate) = Coordinate::parse(url.domain().context("no naddr")?) { | ||
| 164 | if coordinate.kind.eq(&nostr_sdk::Kind::GitRepoAnnouncement) { | ||
| 165 | repo_coordinattes.insert(coordinate); | ||
| 166 | return Ok(repo_coordinattes); | ||
| 167 | } | ||
| 168 | bail!("naddr doesnt point to a git repository announcement"); | ||
| 169 | } | ||
| 170 | |||
| 171 | if let Some(domain) = url.domain() { | ||
| 172 | if let Ok(public_key) = PublicKey::parse(domain) { | ||
| 173 | if url.path().len() < 2 { | ||
| 174 | bail!( | ||
| 175 | "nostr git url should include the repo identifier eg nostr://npub123/the-repo-identifer" | ||
| 176 | ); | ||
| 177 | } | ||
| 178 | let mut relays = vec![]; | ||
| 179 | for (name, value) in url.query_pairs() { | ||
| 180 | if name.contains("relay") { | ||
| 181 | let mut decoded = urlencoding::decode(&value) | ||
| 182 | .context("could not parse relays in nostr git url")? | ||
| 183 | .to_string(); | ||
| 184 | if !decoded.starts_with("ws://") && !decoded.starts_with("wss://") { | ||
| 185 | decoded = format!("wss://{decoded}"); | ||
| 186 | } | ||
| 187 | let url = | ||
| 188 | Url::parse(&decoded).context("could not parse relays in nostr git url")?; | ||
| 189 | relays.push(url.to_string()); | ||
| 190 | } | ||
| 191 | } | ||
| 192 | repo_coordinattes.insert(Coordinate { | ||
| 193 | identifier: url.path()[1..].to_string(), | ||
| 194 | public_key, | ||
| 195 | kind: nostr_sdk::Kind::GitRepoAnnouncement, | ||
| 196 | relays, | ||
| 197 | }); | ||
| 198 | return Ok(repo_coordinattes); | ||
| 199 | } | ||
| 200 | } | ||
| 201 | bail!( | ||
| 202 | "nostr git url must be in format nostr://naddr123 or nostr://npub123/identifer?relay=wss://relay-example.com&relay1=wss://relay-example.org" | ||
| 203 | ); | ||
| 204 | } | ||
| 205 | |||
| 206 | async fn fetching_with_report_for_helper( | 155 | async fn fetching_with_report_for_helper( |
| 207 | git_repo_path: &Path, | 156 | git_repo_path: &Path, |
| 208 | #[cfg(test)] client: &crate::client::MockConnect, | 157 | #[cfg(test)] client: &crate::client::MockConnect, |