upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/git/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/git/mod.rs')
-rw-r--r--src/git/mod.rs68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/git/mod.rs b/src/git/mod.rs
index 599a94b..5c99b3e 100644
--- a/src/git/mod.rs
+++ b/src/git/mod.rs
@@ -340,6 +340,74 @@ pub fn validate_nostr_ref(
340 Ok(true) 340 Ok(true)
341} 341}
342 342
343/// Clean up placeholder refs from all repositories on shutdown.
344///
345/// Walks through all git repositories in the git_data_path and deletes
346/// `refs/nostr/<event-id>` refs for the given event IDs. This is called
347/// on shutdown to clean up placeholders created when git data arrived
348/// before the corresponding PR event.
349///
350/// # Arguments
351/// * `git_data_path` - Base directory containing git repositories
352/// * `event_ids` - Event IDs whose refs/nostr/ refs should be deleted
353///
354/// # Returns
355/// Number of refs successfully deleted
356pub fn cleanup_placeholder_refs(git_data_path: &str, event_ids: &[String]) -> usize {
357 if event_ids.is_empty() {
358 return 0;
359 }
360
361 let git_path = PathBuf::from(git_data_path);
362 if !git_path.exists() {
363 debug!("Git data path does not exist: {}", git_data_path);
364 return 0;
365 }
366
367 let mut deleted_count = 0;
368
369 // Walk through all repositories (npub/repo.git structure)
370 if let Ok(npub_entries) = std::fs::read_dir(&git_path) {
371 for npub_entry in npub_entries.flatten() {
372 if !npub_entry.path().is_dir() {
373 continue;
374 }
375
376 // For each npub directory, check repos
377 if let Ok(repo_entries) = std::fs::read_dir(npub_entry.path()) {
378 for repo_entry in repo_entries.flatten() {
379 let repo_path = repo_entry.path();
380 if !repo_path.is_dir() || !repo_path.to_string_lossy().ends_with(".git") {
381 continue;
382 }
383
384 // Try to delete refs/nostr/<event-id> for each placeholder event
385 for event_id in event_ids {
386 let ref_name = format!("refs/nostr/{}", event_id);
387 if delete_ref(&repo_path, &ref_name).is_ok() {
388 deleted_count += 1;
389 info!(
390 "Cleaned up placeholder ref {} from {}",
391 ref_name,
392 repo_path.display()
393 );
394 }
395 }
396 }
397 }
398 }
399 }
400
401 if deleted_count > 0 {
402 info!(
403 "Shutdown cleanup: removed {} placeholder refs from git repositories",
404 deleted_count
405 );
406 }
407
408 deleted_count
409}
410
343/// Get the current HEAD ref from a repository 411/// Get the current HEAD ref from a repository
344/// 412///
345/// # Arguments 413/// # Arguments