upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/git_mirror.rs
blob: 47b0efb0c1c3c72fc7fc1a41f3fbcd96bbba1183 (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
use crate::db::MirrorDb;
use crate::discovery::DiscoveredRepo;
use crate::health::GraspServer;
use crate::nip46::Nip46Client;
use anyhow::{Context, Result};
use git2::RemoteCallbacks;
use nostr_sdk::prelude::*;
use std::path::Path;
use std::sync::Arc;

pub struct GitMirror {
    mirror_dir: std::path::PathBuf,
}

impl GitMirror {
    pub fn new(mirror_dir: &Path) -> Self {
        Self {
            mirror_dir: mirror_dir.to_path_buf(),
        }
    }

    fn repo_path(&self, pubkey: &str, identifier: &str) -> std::path::PathBuf {
        self.mirror_dir
            .join(pubkey)
            .join(format!("{}.git", identifier))
    }

    pub async fn mirror_repo_to_servers(
        &self,
        db: &MirrorDb,
        repo: &DiscoveredRepo,
        target_servers: &[GraspServer],
        nip46_client: Option<&Arc<Nip46Client>>,
        nostr_client: &nostr_sdk::Client,
    ) -> Result<()> {
        if target_servers.is_empty() {
            tracing::debug!(
                identifier = %repo.identifier,
                "no missing servers to mirror to"
            );
            return Ok(());
        }

        let pk_hex = repo.pubkey.to_hex();
        let repo_path = self.repo_path(&pk_hex, &repo.identifier);

        if !repo_path.exists() {
            self.clone_bare(&repo_path, &repo.clone_urls)?;
        }

        let state_event = match self.build_state_event(&repo_path, repo, nip46_client).await {
            Ok(Some(event)) => Some(event),
            Ok(None) => {
                tracing::warn!(
                    identifier = %repo.identifier,
                    "could not build state event — push may be rejected by GRASP servers"
                );
                None
            }
            Err(e) => {
                tracing::error!(
                    identifier = %repo.identifier,
                    error = %e,
                    "failed to build state event"
                );
                None
            }
        };

        for server in target_servers {
            let target_url = server.clone_url(&pk_hex, &repo.identifier);

            tracing::info!(
                identifier = %repo.identifier,
                server = %server.domain,
                target = %target_url,
                "mirroring git data"
            );

            if let Some(ref event) = state_event {
                let relay_url = server.relay_url.clone();
                if let Ok(url) = RelayUrl::parse(&relay_url) {
                    let urls = vec![url];
                    if let Err(e) = nostr_client.send_event_to(urls, event.clone()).await {
                        tracing::warn!(
                            server = %server.domain,
                            error = %e,
                            "failed to publish state event to server relay"
                        );
                    }
                }
            }

            let repo_id = db.get_all_repos().await.ok().and_then(|repos| {
                repos
                    .iter()
                    .find(|r| r.pubkey == pk_hex && r.identifier == repo.identifier)
                    .map(|r| r.id)
            });

            match self.push_mirror(&repo_path, &target_url) {
                Ok(()) => {
                    tracing::info!(
                        identifier = %repo.identifier,
                        server = %server.domain,
                        "git mirror succeeded"
                    );
                    if let Some(id) = repo_id {
                        let _ = db.mark_git_synced(id, &server.domain).await;
                    }
                }
                Err(e) => {
                    tracing::error!(
                        identifier = %repo.identifier,
                        server = %server.domain,
                        error = %e,
                        "git mirror failed"
                    );
                    if let Some(id) = repo_id {
                        let _ = db.mark_sync_error(id, &server.domain, &e.to_string()).await;
                    }
                }
            }
        }

        Ok(())
    }

    async fn build_state_event(
        &self,
        repo_path: &std::path::PathBuf,
        repo: &DiscoveredRepo,
        nip46_client: Option<&Arc<Nip46Client>>,
    ) -> Result<Option<Event>> {
        let nip46 = match nip46_client {
            Some(c) => c,
            None => return Ok(None),
        };

        let git_repo = git2::Repository::open(repo_path)
            .with_context(|| format!("failed to open bare repo at {:?}", repo_path))?;

        let mut tags: Vec<Tag> = vec![
            Tag::custom(TagKind::Custom("d".into()), [&repo.identifier]),
        ];

        let refs = git_repo.references()?;
        for reference in refs {
            let reference = reference?;
            let name = reference.name().unwrap_or("");
            if name.is_empty() {
                continue;
            }
            if let Some(oid) = reference.target() {
                tags.push(Tag::custom(
                    TagKind::Custom("ref".into()),
                    [name, &oid.to_string()],
                ));
            }
        }

        let builder = EventBuilder::new(Kind::Custom(30618), "").tags(tags);
        let unsigned = builder.build(repo.pubkey);

        match nip46.sign_event(&repo.pubkey, &unsigned).await {
            Ok(signed) => {
                tracing::info!(
                    identifier = %repo.identifier,
                    event_id = %signed.id.to_hex(),
                    "signed kind:30618 state event via NIP-46"
                );
                Ok(Some(signed))
            }
            Err(e) => {
                tracing::error!(
                    identifier = %repo.identifier,
                    error = %e,
                    "NIP-46 signing failed for state event"
                );
                Err(e)
            }
        }
    }

    fn clone_bare(&self, repo_path: &Path, clone_urls: &[String]) -> Result<()> {
        if let Some(parent) = repo_path.parent() {
            std::fs::create_dir_all(parent)
                .with_context(|| format!("failed to create {:?}", parent))?;
        }

        let mut last_error = None;

        for url in clone_urls {
            if url.is_empty() {
                continue;
            }
            tracing::info!(url = %url, path = ?repo_path, "cloning bare repo");

            let mut callbacks = RemoteCallbacks::new();
            callbacks.credentials(|_url, _username, _allowed| git2::Cred::default());

            let mut fetch_opts = git2::FetchOptions::new();
            fetch_opts.remote_callbacks(callbacks);

            let mut builder = git2::build::RepoBuilder::new();
            builder.bare(true).fetch_options(fetch_opts);

            match builder.clone(url, repo_path) {
                Ok(_) => {
                    tracing::info!(url = %url, "bare clone succeeded");
                    return Ok(());
                }
                Err(e) => {
                    tracing::warn!(url = %url, error = %e, "clone failed, trying next URL");
                    last_error = Some(e);
                    if repo_path.exists() {
                        let _ = std::fs::remove_dir_all(repo_path);
                    }
                }
            }
        }

        let err = last_error.unwrap_or_else(|| git2::Error::from_str("no clone URLs available"));
        Err(err).with_context(|| format!("all clone attempts failed for {:?}", repo_path))
    }

    fn push_mirror(&self, repo_path: &Path, target_url: &str) -> Result<()> {
        let repo = git2::Repository::open(repo_path)
            .with_context(|| format!("failed to open bare repo at {:?}", repo_path))?;

        let remote_name = "push_target";

        match repo.find_remote(remote_name) {
            Ok(_) => {
                repo.remote_set_url(remote_name, target_url)?;
            }
            Err(_) => {
                repo.remote(remote_name, target_url)?;
            }
        }

        let mut remote = repo.find_remote(remote_name)?;

        let mut callbacks = RemoteCallbacks::new();
        callbacks.credentials(|_url, _username, _allowed| git2::Cred::default());
        callbacks.push_update_reference(|_refname, status| {
            if let Some(s) = status {
                tracing::warn!(status = %s, "push rejected");
            }
            Ok(())
        });

        let mut push_opts = git2::PushOptions::new();
        push_opts.remote_callbacks(callbacks);

        let refspecs = ["+refs/*:refs/*"];

        remote
            .push(&refspecs, Some(&mut push_opts))
            .with_context(|| format!("failed to push mirror to {}", target_url))?;

        Ok(())
    }
}