upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/lib/repo_ref.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/repo_ref.rs')
-rw-r--r--src/lib/repo_ref.rs34
1 files changed, 25 insertions, 9 deletions
diff --git a/src/lib/repo_ref.rs b/src/lib/repo_ref.rs
index da76182..5d6f4eb 100644
--- a/src/lib/repo_ref.rs
+++ b/src/lib/repo_ref.rs
@@ -19,7 +19,10 @@ use crate::{
19 Interactor, InteractorPrompt, PromptChoiceParms, PromptConfirmParms, PromptInputParms, 19 Interactor, InteractorPrompt, PromptChoiceParms, PromptConfirmParms, PromptInputParms,
20 }, 20 },
21 client::{consolidate_fetch_reports, get_repo_ref_from_cache, sign_event, Connect}, 21 client::{consolidate_fetch_reports, get_repo_ref_from_cache, sign_event, Connect},
22 git::{nostr_url::NostrUrlDecoded, Repo, RepoActions}, 22 git::{
23 nostr_url::{use_nip05_git_config_cache_to_find_nip05_from_public_key, NostrUrlDecoded},
24 Repo, RepoActions,
25 },
23 login::user::get_user_details, 26 login::user::get_user_details,
24}; 27};
25 28
@@ -41,6 +44,7 @@ impl TryFrom<(nostr::Event, Option<PublicKey>)> for RepoRef {
41 type Error = anyhow::Error; 44 type Error = anyhow::Error;
42 45
43 fn try_from((event, trusted_maintainer): (nostr::Event, Option<PublicKey>)) -> Result<Self> { 46 fn try_from((event, trusted_maintainer): (nostr::Event, Option<PublicKey>)) -> Result<Self> {
47 // TODO: turn trusted maintainer into NostrUrlDecoded
44 if !event.kind.eq(&Kind::GitRepoAnnouncement) { 48 if !event.kind.eq(&Kind::GitRepoAnnouncement) {
45 bail!("incorrect kind"); 49 bail!("incorrect kind");
46 } 50 }
@@ -231,12 +235,18 @@ impl RepoRef {
231 .collect::<Vec<(Coordinate, Option<Timestamp>)>>() 235 .collect::<Vec<(Coordinate, Option<Timestamp>)>>()
232 } 236 }
233 237
234 pub fn to_nostr_git_url(&self) -> String { 238 pub fn to_nostr_git_url(&self, git_repo: &Option<&Repo>) -> String {
239 let c = self.coordinate_with_hint();
235 format!( 240 format!(
236 "{}", 241 "{}",
237 NostrUrlDecoded { 242 NostrUrlDecoded {
238 original_string: String::new(), 243 original_string: String::new(),
239 coordinate: self.coordinate_with_hint(), 244 nip05: use_nip05_git_config_cache_to_find_nip05_from_public_key(
245 &c.public_key,
246 git_repo,
247 )
248 .unwrap_or_default(),
249 coordinate: c,
240 protocol: None, 250 protocol: None,
241 user: None, 251 user: None,
242 } 252 }
@@ -259,7 +269,7 @@ pub async fn get_repo_coordinates_when_remote_unknown(
259pub async fn try_and_get_repo_coordinates_when_remote_unknown( 269pub async fn try_and_get_repo_coordinates_when_remote_unknown(
260 git_repo: &Repo, 270 git_repo: &Repo,
261) -> Result<Coordinate> { 271) -> Result<Coordinate> {
262 let remote_coordinates = get_repo_coordinates_from_nostr_remotes(git_repo)?; 272 let remote_coordinates = get_repo_coordinates_from_nostr_remotes(git_repo).await?;
263 if remote_coordinates.is_empty() { 273 if remote_coordinates.is_empty() {
264 if let Ok(c) = get_repo_coordinates_from_git_config(git_repo) { 274 if let Ok(c) = get_repo_coordinates_from_git_config(git_repo) {
265 Ok(c) 275 Ok(c)
@@ -327,11 +337,15 @@ fn get_repo_coordinates_from_git_config(git_repo: &Repo) -> Result<Coordinate> {
327 .context("git config item \"nostr.repo\" is not an naddr") 337 .context("git config item \"nostr.repo\" is not an naddr")
328} 338}
329 339
330fn get_repo_coordinates_from_nostr_remotes(git_repo: &Repo) -> Result<HashMap<String, Coordinate>> { 340async fn get_repo_coordinates_from_nostr_remotes(
341 git_repo: &Repo,
342) -> Result<HashMap<String, Coordinate>> {
331 let mut repo_coordinates = HashMap::new(); 343 let mut repo_coordinates = HashMap::new();
332 for remote_name in git_repo.git_repo.remotes()?.iter().flatten() { 344 for remote_name in git_repo.git_repo.remotes()?.iter().flatten() {
333 if let Some(remote_url) = git_repo.git_repo.find_remote(remote_name)?.url() { 345 if let Some(remote_url) = git_repo.git_repo.find_remote(remote_name)?.url() {
334 if let Ok(nostr_url_decoded) = NostrUrlDecoded::from_str(remote_url) { 346 if let Ok(nostr_url_decoded) =
347 NostrUrlDecoded::parse_and_resolve(remote_url, &Some(git_repo)).await
348 {
335 repo_coordinates.insert(remote_name.to_string(), nostr_url_decoded.coordinate); 349 repo_coordinates.insert(remote_name.to_string(), nostr_url_decoded.coordinate);
336 } 350 }
337 } 351 }
@@ -383,7 +397,9 @@ async fn get_repo_coordinate_from_user_prompt(
383 .input(PromptInputParms::default().with_prompt("nostr repository"))?; 397 .input(PromptInputParms::default().with_prompt("nostr repository"))?;
384 let coordinate = if let Ok(c) = Coordinate::parse(&input) { 398 let coordinate = if let Ok(c) = Coordinate::parse(&input) {
385 c 399 c
386 } else if let Ok(nostr_url) = NostrUrlDecoded::from_str(&input) { 400 } else if let Ok(nostr_url) =
401 NostrUrlDecoded::parse_and_resolve(&input, &Some(git_repo)).await
402 {
387 nostr_url.coordinate 403 nostr_url.coordinate
388 } else { 404 } else {
389 eprintln!("not a valid naddr or git nostr remote URL starting nostr://"); 405 eprintln!("not a valid naddr or git nostr remote URL starting nostr://");
@@ -438,10 +454,10 @@ fn set_or_create_git_remote_with_nostr_url(
438 repo_ref: &RepoRef, 454 repo_ref: &RepoRef,
439 git_repo: &Repo, 455 git_repo: &Repo,
440) -> Result<()> { 456) -> Result<()> {
441 let url = repo_ref.to_nostr_git_url(); 457 let url = repo_ref.to_nostr_git_url(&Some(git_repo));
442 if git_repo 458 if git_repo
443 .git_repo 459 .git_repo
444 .remote_set_url(name, &repo_ref.to_nostr_git_url()) 460 .remote_set_url(name, &repo_ref.to_nostr_git_url(&Some(git_repo)))
445 .is_err() 461 .is_err()
446 { 462 {
447 git_repo.git_repo.remote(name, &url)?; 463 git_repo.git_repo.remote(name, &url)?;