upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/cleanup_empty_repos.rs
blob: 8f5492dfd783471c8c44676f06da33c14c37f7a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//! Cleanup Empty Repositories
//!
//! Scans the LMDB database for kind 30617 (repository announcement) events whose
//! corresponding bare git repository on disk is empty (no refs) or missing entirely.
//! For each such repository, also removes any kind 30618 (state) events for the same
//! (pubkey, identifier) coordinate.
//!
//! ## Rationale
//!
//! A relay should not store announcement or state events for a repository that has no
//! git data. If the bare repo is empty or absent, the events are stale and should be
//! removed so the relay does not serve them.
//!
//! ## Usage
//!
//! ```text
//! # Dry-run (default): print what would be deleted
//! ngit-grasp cleanup-empty-repos --relay-data-path /var/lib/ngit-grasp/relay \
//!                                 --git-data-path   /var/lib/ngit-grasp/git
//!
//! # Execute: delete the bare repos and remove events from the DB
//! ngit-grasp cleanup-empty-repos --relay-data-path /var/lib/ngit-grasp/relay \
//!                                 --git-data-path   /var/lib/ngit-grasp/git \
//!                                 --execute
//! ```
//!
//! The relay service should be stopped before running with `--execute` to avoid
//! races with the live relay process.

use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::Arc;

use anyhow::{Context, Result};
use clap::Args;
use nostr_lmdb::NostrLmdb;
use nostr_sdk::prelude::*;

use crate::nostr::events::RepositoryAnnouncement;

/// Arguments for the `cleanup-empty-repos` subcommand.
#[derive(Debug, Args)]
pub struct CleanupArgs {
    /// Path to the LMDB relay data directory (contains the nostr event database).
    ///
    /// Defaults to `./data/relay` (same default as the relay itself).
    #[arg(long, env = "NGIT_RELAY_DATA_PATH", default_value = "./data/relay")]
    pub relay_data_path: String,

    /// Path to the git data directory (contains bare repositories).
    ///
    /// Defaults to `./data/git` (same default as the relay itself).
    #[arg(long, env = "NGIT_GIT_DATA_PATH", default_value = "./data/git")]
    pub git_data_path: String,

    /// Actually delete empty repositories and remove their events from the database.
    ///
    /// Without this flag the command runs in dry-run mode and only prints what
    /// would be deleted. Stop the relay service before using this flag.
    #[arg(long, default_value_t = false)]
    pub execute: bool,
}

/// A repository that has an empty (or missing) bare git repo on disk.
#[derive(Debug)]
struct EmptyRepo {
    /// The kind 30617 event
    announcement: Event,
    /// Derived npub (bech32) of the owner
    npub: String,
    /// Repository identifier (d-tag value)
    identifier: String,
    /// Absolute path to the bare repo directory
    repo_path: PathBuf,
    /// Whether the directory exists at all (vs exists but is empty)
    repo_exists: bool,
    /// Any kind 30618 state events found in the local DB for this coordinate
    state_events: Vec<Event>,
}

/// Run the cleanup-empty-repos subcommand.
pub async fn run(args: &CleanupArgs) -> Result<()> {
    let relay_data_path = Path::new(&args.relay_data_path);
    let git_data_path = Path::new(&args.git_data_path);

    if args.execute {
        println!("=== cleanup-empty-repos (EXECUTE MODE) ===");
        println!("WARNING: This will permanently delete data. The relay should be stopped.");
        println!();
    } else {
        println!("=== cleanup-empty-repos (DRY-RUN MODE) ===");
        println!("Pass --execute to actually delete. Stop the relay first.");
        println!();
    }

    println!("Relay data path : {}", relay_data_path.display());
    println!("Git data path   : {}", git_data_path.display());
    println!();

    // Open the LMDB database
    println!("Opening LMDB database...");
    let database: Arc<dyn NostrDatabase> = Arc::new(
        NostrLmdb::open(relay_data_path)
            .await
            .with_context(|| format!("Failed to open LMDB at {}", relay_data_path.display()))?,
    );
    println!("Database opened.");
    println!();

    // Query all kind 30617 events
    let filter = Filter::new().kind(Kind::GitRepoAnnouncement);
    let announcements = database
        .query(filter)
        .await
        .context("Failed to query kind 30617 events")?;

    println!("Found {} kind 30617 announcement(s) in database.", announcements.len());
    println!();

    // Identify empty repos
    let mut empty_repos: Vec<EmptyRepo> = Vec::new();

    for event in announcements.iter() {
        let announcement = match RepositoryAnnouncement::from_event(event.clone()) {
            Ok(a) => a,
            Err(e) => {
                eprintln!(
                    "  WARN: Could not parse announcement {} (skipping): {}",
                    event.id.to_hex(),
                    e
                );
                continue;
            }
        };

        let npub = announcement.owner_npub();
        let identifier = announcement.identifier.clone();
        let repo_path = git_data_path.join(&announcement.repo_path());

        let (repo_exists, is_empty) = check_repo_empty(&repo_path);

        if !is_empty {
            // Repo has git data — leave it alone
            continue;
        }

        // Look up any kind 30618 state events for this (pubkey, identifier) in the local DB
        let state_filter = Filter::new()
            .kind(Kind::RepoState)
            .author(event.pubkey)
            .identifier(identifier.clone());

        let state_events = database
            .query(state_filter)
            .await
            .with_context(|| {
                format!(
                    "Failed to query kind 30618 for {}/{}",
                    npub, identifier
                )
            })?;

        empty_repos.push(EmptyRepo {
            announcement: event.clone(),
            npub,
            identifier,
            repo_path,
            repo_exists,
            state_events: state_events.into_iter().collect(),
        });
    }

    if empty_repos.is_empty() {
        println!("No empty repositories found. Nothing to do.");
        return Ok(());
    }

    // Print report
    println!(
        "Found {} repository/repositories with empty or missing git data:\n",
        empty_repos.len()
    );

    for (i, repo) in empty_repos.iter().enumerate() {
        let repo_status = if repo.repo_exists {
            "exists but empty (no refs)"
        } else {
            "missing from disk"
        };
        println!(
            "  [{:>3}] {}/{} — git repo {}",
            i + 1,
            repo.npub,
            repo.identifier,
            repo_status,
        );
        println!(
            "         30617 event : {}",
            repo.announcement.id.to_hex()
        );
        if repo.state_events.is_empty() {
            println!("         30618 events: none in local DB");
        } else {
            for se in &repo.state_events {
                println!("         30618 event : {}", se.id.to_hex());
            }
        }
        println!(
            "         repo path   : {}",
            repo.repo_path.display()
        );
    }

    println!();

    if !args.execute {
        println!(
            "DRY-RUN: {} repository/repositories would be cleaned up.",
            empty_repos.len()
        );
        println!("Run with --execute to perform the cleanup (stop the relay first).");
        return Ok(());
    }

    // Execute: delete repos and remove events
    println!("Executing cleanup...");
    println!();

    let mut deleted_repos = 0usize;
    let mut failed_repos = 0usize;
    let mut deleted_announcements = 0usize;
    let mut deleted_state_events = 0usize;

    for repo in &empty_repos {
        println!("Cleaning up {}/{}...", repo.npub, repo.identifier);

        // 1. Delete the bare repo directory (if it exists)
        if repo.repo_exists {
            match std::fs::remove_dir_all(&repo.repo_path) {
                Ok(()) => {
                    println!("  Deleted git repo: {}", repo.repo_path.display());
                    deleted_repos += 1;

                    // Remove the parent npub directory if now empty
                    if let Some(npub_dir) = repo.repo_path.parent() {
                        if npub_dir.exists() {
                            match std::fs::read_dir(npub_dir) {
                                Ok(mut entries) => {
                                    if entries.next().is_none() {
                                        if let Err(e) = std::fs::remove_dir(npub_dir) {
                                            eprintln!(
                                                "  WARN: Could not remove empty npub dir {}: {}",
                                                npub_dir.display(),
                                                e
                                            );
                                        } else {
                                            println!(
                                                "  Removed empty npub dir: {}",
                                                npub_dir.display()
                                            );
                                        }
                                    }
                                }
                                Err(e) => {
                                    eprintln!(
                                        "  WARN: Could not read npub dir {}: {}",
                                        npub_dir.display(),
                                        e
                                    );
                                }
                            }
                        }
                    }
                }
                Err(e) => {
                    eprintln!(
                        "  ERROR: Failed to delete git repo {}: {}",
                        repo.repo_path.display(),
                        e
                    );
                    failed_repos += 1;
                    // Continue — still try to remove the DB events
                }
            }
        }

        // 2. Remove the kind 30617 announcement from the DB
        //    Use a filter matching the specific event ID so we only delete this exact event.
        let announcement_filter = Filter::new()
            .kind(Kind::GitRepoAnnouncement)
            .id(repo.announcement.id);

        match database.delete(announcement_filter).await {
            Ok(()) => {
                println!("  Deleted 30617 event: {}", repo.announcement.id.to_hex());
                deleted_announcements += 1;
            }
            Err(e) => {
                eprintln!(
                    "  ERROR: Failed to delete 30617 event {}: {}",
                    repo.announcement.id.to_hex(),
                    e
                );
            }
        }

        // 3. Remove any kind 30618 state events for this coordinate
        if !repo.state_events.is_empty() {
            let state_filter = Filter::new()
                .kind(Kind::RepoState)
                .author(repo.announcement.pubkey)
                .identifier(repo.identifier.clone());

            match database.delete(state_filter).await {
                Ok(()) => {
                    for se in &repo.state_events {
                        println!("  Deleted 30618 event: {}", se.id.to_hex());
                        deleted_state_events += 1;
                    }
                }
                Err(e) => {
                    eprintln!(
                        "  ERROR: Failed to delete 30618 events for {}/{}: {}",
                        repo.npub, repo.identifier, e
                    );
                }
            }
        }
    }

    println!();
    println!("=== Cleanup complete ===");
    println!("  Git repos deleted       : {}", deleted_repos);
    if failed_repos > 0 {
        println!("  Git repos failed        : {} (see errors above)", failed_repos);
    }
    println!("  30617 events removed    : {}", deleted_announcements);
    println!("  30618 events removed    : {}", deleted_state_events);

    Ok(())
}

/// Check whether a bare git repository is empty (has no refs).
///
/// Returns `(exists, is_empty)`:
/// - `(false, true)` — path does not exist (treated as empty)
/// - `(true, true)`  — path exists but `git --git-dir=<path> for-each-ref` returns no output
/// - `(true, false)` — path exists and has at least one ref
fn check_repo_empty(repo_path: &Path) -> (bool, bool) {
    if !repo_path.exists() {
        return (false, true);
    }

    // Run `git --git-dir=<path> for-each-ref` — empty output means no refs.
    // --git-dir must be a global option before the subcommand, not an argument to for-each-ref.
    let output = Command::new("git")
        .arg("--git-dir")
        .arg(repo_path)
        .args(["for-each-ref", "--format=%(refname)"])
        .output();

    match output {
        Ok(out) => {
            // Trim whitespace; if nothing remains, the repo is empty
            let stdout = String::from_utf8_lossy(&out.stdout);
            let is_empty = stdout.trim().is_empty();
            (true, is_empty)
        }
        Err(_) => {
            // Could not run git — treat as empty to be safe (will be reported)
            (true, true)
        }
    }
}