upleb.uk

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

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2024-08-19 08:47:53 +0100
committerDanConwayDev <DanConwayDev@protonmail.com>2024-08-19 08:47:53 +0100
commit4291428c714b8cc7b7a5f3c1a1504b0098e00455 (patch)
tree537d200bcc06c835fd02231f811ede743cb00241
parent6b46ad9809b42d355c883ff2c75f8ac75d4bd989 (diff)
refactor(remote): move url_to_repo_coordinates
so it can be used by repo_ref which doesnt import git_remote_helper
-rw-r--r--src/git.rs58
-rw-r--r--src/git_remote_helper.rs53
2 files changed, 58 insertions, 53 deletions
diff --git a/src/git.rs b/src/git.rs
index eebca29..e720c50 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -1,11 +1,16 @@
1use std::{ 1use std::{
2 collections::HashSet,
2 env::current_dir, 3 env::current_dir,
3 path::{Path, PathBuf}, 4 path::{Path, PathBuf},
4}; 5};
5 6
6use anyhow::{bail, Context, Result}; 7use anyhow::{bail, Context, Result};
7use git2::{DiffOptions, Oid, Revwalk}; 8use git2::{DiffOptions, Oid, Revwalk};
8use nostr_sdk::hashes::{sha1::Hash as Sha1Hash, Hash}; 9use nostr::nips::nip01::Coordinate;
10use nostr_sdk::{
11 hashes::{sha1::Hash as Sha1Hash, Hash},
12 PublicKey, Url,
13};
9 14
10use crate::sub_commands::list::{get_commit_id_from_patch, tag_value}; 15use crate::sub_commands::list::{get_commit_id_from_patch, tag_value};
11 16
@@ -830,6 +835,57 @@ fn extract_sig_from_patch_tags<'a>(
830 .context("failed to create git signature") 835 .context("failed to create git signature")
831} 836}
832 837
838pub fn nostr_git_url_to_repo_coordinates(url: &str) -> Result<HashSet<Coordinate>> {
839 let mut repo_coordinattes = HashSet::new();
840 let url = Url::parse(url)?;
841
842 if url.scheme().ne("nostr") {
843 bail!("nostr git url must start with nostr://")
844 }
845
846 if let Ok(coordinate) = Coordinate::parse(url.domain().context("no naddr")?) {
847 if coordinate.kind.eq(&nostr_sdk::Kind::GitRepoAnnouncement) {
848 repo_coordinattes.insert(coordinate);
849 return Ok(repo_coordinattes);
850 }
851 bail!("naddr doesnt point to a git repository announcement");
852 }
853
854 if let Some(domain) = url.domain() {
855 if let Ok(public_key) = PublicKey::parse(domain) {
856 if url.path().len() < 2 {
857 bail!(
858 "nostr git url should include the repo identifier eg nostr://npub123/the-repo-identifer"
859 );
860 }
861 let mut relays = vec![];
862 for (name, value) in url.query_pairs() {
863 if name.contains("relay") {
864 let mut decoded = urlencoding::decode(&value)
865 .context("could not parse relays in nostr git url")?
866 .to_string();
867 if !decoded.starts_with("ws://") && !decoded.starts_with("wss://") {
868 decoded = format!("wss://{decoded}");
869 }
870 let url =
871 Url::parse(&decoded).context("could not parse relays in nostr git url")?;
872 relays.push(url.to_string());
873 }
874 }
875 repo_coordinattes.insert(Coordinate {
876 identifier: url.path()[1..].to_string(),
877 public_key,
878 kind: nostr_sdk::Kind::GitRepoAnnouncement,
879 relays,
880 });
881 return Ok(repo_coordinattes);
882 }
883 }
884 bail!(
885 "nostr git url must be in format nostr://naddr123 or nostr://npub123/identifer?relay=wss://relay-example.com&relay1=wss://relay-example.org"
886 );
887}
888
833#[cfg(test)] 889#[cfg(test)]
834mod tests { 890mod tests {
835 use std::fs; 891 use std::fs;
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};
21use git::{sha1_to_oid, RepoActions}; 21use git::{nostr_git_url_to_repo_coordinates, sha1_to_oid, RepoActions};
22use git2::{Oid, Repository}; 22use git2::{Oid, Repository};
23use nostr::nips::{nip01::Coordinate, nip10::Marker}; 23use nostr::nips::{nip01::Coordinate, nip10::Marker};
24use nostr_sdk::{ 24use 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
155fn 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
206async fn fetching_with_report_for_helper( 155async 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,