diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2025-11-11 12:30:00 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2025-11-13 14:55:50 +0000 |
| commit | ebab8d2aa487d1814e802c5a51b19d4bb1592e01 (patch) | |
| tree | 0b0e8e0b179ac365da9182a01f2a7b26929d7dba /src/lib/utils.rs | |
| parent | 65f3bd360c065aca493eddf7eb5d3d8191a84b56 (diff) | |
refactor: simplify get_short_git_server_name
so it doesnt use the git_repo
Diffstat (limited to 'src/lib/utils.rs')
| -rw-r--r-- | src/lib/utils.rs | 39 |
1 files changed, 29 insertions, 10 deletions
diff --git a/src/lib/utils.rs b/src/lib/utils.rs index 431a14f..11ea8a6 100644 --- a/src/lib/utils.rs +++ b/src/lib/utils.rs | |||
| @@ -10,7 +10,7 @@ use std::{ | |||
| 10 | use anyhow::{Context, Result, bail}; | 10 | use anyhow::{Context, Result, bail}; |
| 11 | use git2::Repository; | 11 | use git2::Repository; |
| 12 | use nostr::nips::nip19::ToBech32; | 12 | use nostr::nips::nip19::ToBech32; |
| 13 | use nostr_sdk::{Event, EventId, Kind, PublicKey, Url}; | 13 | use nostr_sdk::{Event, EventId, Kind, PublicKey}; |
| 14 | 14 | ||
| 15 | use crate::{ | 15 | use crate::{ |
| 16 | client::{ | 16 | client::{ |
| @@ -26,19 +26,38 @@ use crate::{ | |||
| 26 | get_pr_tip_event_or_most_recent_patch_with_ancestors, get_status, | 26 | get_pr_tip_event_or_most_recent_patch_with_ancestors, get_status, |
| 27 | is_event_proposal_root_for_branch, status_kinds, | 27 | is_event_proposal_root_for_branch, status_kinds, |
| 28 | }, | 28 | }, |
| 29 | repo_ref::RepoRef, | 29 | repo_ref::{RepoRef, extract_npub, is_grasp_server_clone_url}, |
| 30 | }; | 30 | }; |
| 31 | 31 | ||
| 32 | pub fn get_short_git_server_name(git_repo: &Repo, url: &str) -> std::string::String { | 32 | pub fn get_short_git_server_name(url: &str) -> std::string::String { |
| 33 | if let Ok(name) = get_remote_name_by_url(&git_repo.git_repo, url) { | 33 | // Check if this is a grasp server URL |
| 34 | return name; | 34 | if is_grasp_server_clone_url(url) { |
| 35 | } | 35 | // Try to extract and format the grasp server URL |
| 36 | if let Ok(url) = Url::parse(url) { | 36 | if let Ok(npub) = extract_npub(url) { |
| 37 | if let Some(domain) = url.domain() { | 37 | // Parse the URL to get the domain and identifier |
| 38 | return domain.to_string(); | 38 | if let Ok(clone_url) = CloneUrl::from_str(url) { |
| 39 | let domain = clone_url.domain(); | ||
| 40 | let path = clone_url.short_name(); | ||
| 41 | |||
| 42 | // Extract the identifier from the path (after /{npub}/) | ||
| 43 | if let Some(after_npub) = path.split(&format!("/{}/", npub)).nth(1) { | ||
| 44 | // Truncate npub to show first 10 and last 5 characters | ||
| 45 | let truncated_npub = if npub.len() > 20 { | ||
| 46 | format!("{}...{}", &npub[..4], &npub[npub.len() - 5..]) | ||
| 47 | } else { | ||
| 48 | npub.to_string() | ||
| 49 | }; | ||
| 50 | |||
| 51 | return format!("{}/{}/{}", domain, truncated_npub, after_npub); | ||
| 52 | } | ||
| 53 | } | ||
| 39 | } | 54 | } |
| 40 | } | 55 | } |
| 41 | url.to_string() | 56 | |
| 57 | // Fall back to default behavior for non-grasp server URLs | ||
| 58 | CloneUrl::from_str(url) | ||
| 59 | .map(|u| u.short_name()) | ||
| 60 | .unwrap_or_else(|_| url.to_string()) | ||
| 42 | } | 61 | } |
| 43 | 62 | ||
| 44 | pub fn get_remote_name_by_url(git_repo: &Repository, url: &str) -> Result<String> { | 63 | pub fn get_remote_name_by_url(git_repo: &Repository, url: &str) -> Result<String> { |