From d8b85cbce5cba9bfb8b15a8bd5c1b7200ff3e488 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Thu, 26 Feb 2026 14:00:12 +0000 Subject: fix: advertise only state events with resolvable git objects git-remote-nostr now walks the per-relay state events captured in FetchReport::state_per_relay (newest first) and advertises the first one whose every OID is either present on at least one git server (confirmed via list_refs) or already available locally. If no such state event exists it falls back to the raw git server state. Previously the latest nostr state event was always used regardless of whether its OIDs had been pushed to any server, causing catastrophic missing-object errors during clone or fetch when a state event was published ahead of the corresponding git push. --- src/bin/git_remote_nostr/list.rs | 56 +++++++++++++++++++++++++++++++++++++--- src/bin/git_remote_nostr/main.rs | 18 ++++++++----- 2 files changed, 65 insertions(+), 9 deletions(-) (limited to 'src/bin') diff --git a/src/bin/git_remote_nostr/list.rs b/src/bin/git_remote_nostr/list.rs index 4a7c1ec..a32ed67 100644 --- a/src/bin/git_remote_nostr/list.rs +++ b/src/bin/git_remote_nostr/list.rs @@ -4,13 +4,14 @@ use anyhow::{Context, Result}; use client::get_state_from_cache; use git::RepoActions; use ngit::{ - client::{self, is_verbose}, + client::{self, FetchReport, is_verbose}, fetch::fetch_from_git_server, git::{self}, git_events::{KIND_PULL_REQUEST, KIND_PULL_REQUEST_UPDATE, event_to_cover_letter, tag_value}, list::list_from_remotes, login::get_curent_user, repo_ref::{self}, + repo_state::RepoState, utils::{get_all_proposals, get_open_or_draft_proposals}, }; use repo_ref::RepoRef; @@ -22,6 +23,7 @@ pub async fn run_list( git_repo: &Repo, repo_ref: &RepoRef, for_push: bool, + fetch_report: &FetchReport, ) -> Result, bool)>> { let nostr_state = (get_state_from_cache(Some(git_repo.get_path()?), repo_ref).await).ok(); @@ -30,6 +32,8 @@ pub async fn run_list( if is_verbose() { term.write_line("git servers: listing refs...")?; } + // nostr_state is passed to list_from_remotes only for the sync-status + // display; the actual ref state we advertise is determined below. let remote_states = list_from_remotes( &term, git_repo, @@ -39,9 +43,55 @@ pub async fn run_list( ) .await; - let mut state = if let Some(nostr_state) = nostr_state { - nostr_state.state + // Collect all OIDs confirmed present on at least one git server. + let git_server_oids: std::collections::HashSet = remote_states + .values() + .flat_map(|(state, _)| state.values()) + .filter(|v| !v.starts_with("ref: ")) + .cloned() + .collect(); + + // From the per-relay state events captured during the nostr fetch, find + // the newest state event whose every OID is either: + // (a) confirmed present on at least one git server, or + // (b) already available locally. + // This prevents advertising refs whose git objects haven't been pushed to + // any server yet, which would cause `git clone` / `git fetch` to fail. + let mut candidates: Vec<&nostr::Event> = fetch_report + .state_per_relay + .values() + .filter_map(|maybe| maybe.as_ref()) + .collect(); + // Sort newest-first (by created_at, then by id for tie-breaking). + candidates.sort_by(|a, b| { + b.created_at + .cmp(&a.created_at) + .then_with(|| b.id.cmp(&a.id)) + }); + // Deduplicate by event id so we don't check the same event twice. + candidates.dedup_by_key(|e| e.id); + + let best_state: Option> = candidates.into_iter().find_map(|event| { + if let Ok(rs) = RepoState::try_from(vec![event.clone()]) { + let all_resolvable = rs.state.values().all(|v| { + v.starts_with("ref: ") + || git_server_oids.contains(v) + || git_repo.does_commit_exist(v).is_ok_and(|exists| exists) + }); + if all_resolvable { Some(rs.state) } else { None } + } else { + None + } + }); + + let mut state = if let Some(state) = best_state { + state } else { + // No relay returned a state event whose OIDs are all resolvable + // (either no state events were seen on any relay, or every candidate + // references git objects not yet on any server). Fall back to + // whatever the git servers actually report so we never advertise OIDs + // that cannot be fetched. let (state, _is_grasp_server) = repo_ref .git_server .iter() diff --git a/src/bin/git_remote_nostr/main.rs b/src/bin/git_remote_nostr/main.rs index 6186ed3..dad8a99 100644 --- a/src/bin/git_remote_nostr/main.rs +++ b/src/bin/git_remote_nostr/main.rs @@ -12,7 +12,9 @@ use std::{ }; use anyhow::{Context, Result, bail}; -use client::{Connect, consolidate_fetch_reports, get_repo_ref_from_cache, is_verbose}; +use client::{ + Connect, FetchReport, consolidate_fetch_reports, get_repo_ref_from_cache, is_verbose, +}; use git::{RepoActions, nostr_url::NostrUrlDecoded}; use ngit::{ client::{self, Params}, @@ -149,7 +151,9 @@ async fn main() -> Result<()> { client.set_signer(signer).await; } - fetching_with_report_for_helper(git_repo_path, &client, &decoded_nostr_url.coordinate).await?; + let fetch_report = + fetching_with_report_for_helper(git_repo_path, &client, &decoded_nostr_url.coordinate) + .await?; let mut repo_ref = get_repo_ref_from_cache(Some(git_repo_path), &decoded_nostr_url.coordinate).await?; @@ -221,10 +225,12 @@ async fn main() -> Result<()> { push_options = PushOptions::default(); } ["list"] => { - list_outputs = Some(list::run_list(&git_repo, &repo_ref, false).await?); + list_outputs = + Some(list::run_list(&git_repo, &repo_ref, false, &fetch_report).await?); } ["list", "for-push"] => { - list_outputs = Some(list::run_list(&git_repo, &repo_ref, true).await?); + list_outputs = + Some(list::run_list(&git_repo, &repo_ref, true, &fetch_report).await?); } [] => { return Ok(()); @@ -283,7 +289,7 @@ async fn fetching_with_report_for_helper( git_repo_path: &Path, client: &Client, trusted_maintainer_coordinate: &Nip19Coordinate, -) -> Result<()> { +) -> Result { let term = console::Term::stderr(); let verbose = is_verbose(); if verbose { @@ -308,7 +314,7 @@ async fn fetching_with_report_for_helper( } else { term.write_line(&format!("nostr updates: {report}"))?; } - Ok(()) + Ok(report) } #[cfg(test)] -- cgit v1.2.3