From ebab8d2aa487d1814e802c5a51b19d4bb1592e01 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Tue, 11 Nov 2025 12:30:00 +0000 Subject: refactor: simplify get_short_git_server_name so it doesnt use the git_repo --- src/lib/list.rs | 5 ++--- src/lib/push.rs | 2 +- src/lib/utils.rs | 39 +++++++++++++++++++++++++++++---------- 3 files changed, 32 insertions(+), 14 deletions(-) (limited to 'src/lib') diff --git a/src/lib/list.rs b/src/lib/list.rs index b4d6a5e..08b197c 100644 --- a/src/lib/list.rs +++ b/src/lib/list.rs @@ -237,7 +237,7 @@ pub fn identify_remote_sync_issues( for (name, value) in &nostr_state.state { for (url, (remote_state, _is_grasp_server)) in remote_states { - let remote_name = get_short_git_server_name(git_repo, url); + let remote_name = get_short_git_server_name(url); let issues = remote_issues.entry(remote_name.clone()).or_default(); let is_branch = name.starts_with("refs/heads/"); @@ -416,7 +416,6 @@ pub fn format_ref_issue_simple( /// Generate warning messages for remote sync issues pub fn generate_remote_sync_warnings( - git_repo: &Repo, remote_issues: &HashMap, remote_states: &HashMap, bool)>, ) -> Vec { @@ -430,7 +429,7 @@ pub fn generate_remote_sync_warnings( // Find remote state for this remote let remote_state = remote_states .iter() - .find(|(url, _)| &get_short_git_server_name(git_repo, url) == remote_name) + .find(|(url, _)| &get_short_git_server_name(url) == remote_name) .map(|(_, (state, _))| state); if let Some(state) = remote_state { diff --git a/src/lib/push.rs b/src/lib/push.rs index bd45553..274a16a 100644 --- a/src/lib/push.rs +++ b/src/lib/push.rs @@ -181,7 +181,7 @@ pub fn push_to_remote_url( let existing_lines = reporter.count_all_existing_lines(); reporter.update_reference_errors.push(format!( "WARNING: {} failed to push {name} error: {error}", - get_short_git_server_name(git_repo, git_server_url), + get_short_git_server_name(git_server_url), )); reporter.write_all(existing_lines); } 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::{ use anyhow::{Context, Result, bail}; use git2::Repository; use nostr::nips::nip19::ToBech32; -use nostr_sdk::{Event, EventId, Kind, PublicKey, Url}; +use nostr_sdk::{Event, EventId, Kind, PublicKey}; use crate::{ client::{ @@ -26,19 +26,38 @@ use crate::{ get_pr_tip_event_or_most_recent_patch_with_ancestors, get_status, is_event_proposal_root_for_branch, status_kinds, }, - repo_ref::RepoRef, + repo_ref::{RepoRef, extract_npub, is_grasp_server_clone_url}, }; -pub fn get_short_git_server_name(git_repo: &Repo, url: &str) -> std::string::String { - if let Ok(name) = get_remote_name_by_url(&git_repo.git_repo, url) { - return name; - } - if let Ok(url) = Url::parse(url) { - if let Some(domain) = url.domain() { - return domain.to_string(); +pub fn get_short_git_server_name(url: &str) -> std::string::String { + // Check if this is a grasp server URL + if is_grasp_server_clone_url(url) { + // Try to extract and format the grasp server URL + if let Ok(npub) = extract_npub(url) { + // Parse the URL to get the domain and identifier + if let Ok(clone_url) = CloneUrl::from_str(url) { + let domain = clone_url.domain(); + let path = clone_url.short_name(); + + // Extract the identifier from the path (after /{npub}/) + if let Some(after_npub) = path.split(&format!("/{}/", npub)).nth(1) { + // Truncate npub to show first 10 and last 5 characters + let truncated_npub = if npub.len() > 20 { + format!("{}...{}", &npub[..4], &npub[npub.len() - 5..]) + } else { + npub.to_string() + }; + + return format!("{}/{}/{}", domain, truncated_npub, after_npub); + } + } } } - url.to_string() + + // Fall back to default behavior for non-grasp server URLs + CloneUrl::from_str(url) + .map(|u| u.short_name()) + .unwrap_or_else(|_| url.to_string()) } pub fn get_remote_name_by_url(git_repo: &Repository, url: &str) -> Result { -- cgit v1.2.3