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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
use std::collections::HashMap;
use anyhow::{Context, Result, bail};
use console::Term;
use git2::Oid;
use ngit::{
client::{
Client, Connect, Params, fetching_with_report, get_repo_ref_from_cache,
get_state_from_cache, send_events,
},
fetch::fetch_from_git_server,
git::{Repo, RepoActions, nostr_url::NostrUrlDecoded},
list::{get_ahead_behind, list_from_remotes},
login::existing::load_existing_login,
push::push_to_remote,
repo_ref::{
format_grasp_server_url_as_relay_url, get_repo_coordinates_when_remote_unknown,
is_grasp_server_clone_url,
},
repo_state::RepoState,
utils::{get_short_git_server_name, join_with_and},
};
use nostr_sdk::RelayUrl;
#[derive(Debug, clap::Args)]
pub struct SubCommandArgs {
/// optionally just sync a specific reference. eg main or v1.5.2
#[clap(short, long)]
pub(crate) ref_name: Option<String>,
/// force push updates and delete refs from non-grasp git servers
#[arg(long, action)]
force: bool,
}
#[allow(clippy::too_many_lines)]
pub async fn launch(args: &SubCommandArgs) -> Result<()> {
let git_repo = Repo::discover().context("failed to find a git repository")?;
let git_repo_path = git_repo.get_path()?;
let full_ref_name = if let Some(ref_name) = &args.ref_name {
if ref_name.starts_with("refs/") {
if git_repo.git_repo.find_reference(ref_name).is_ok() {
Some(ref_name.clone())
} else {
bail!("could not find reference {ref_name}");
}
} else if git_repo
.git_repo
.find_reference(&format!("refs/tags/{ref_name}"))
.is_ok()
{
Some(format!("refs/tags/{ref_name}"))
} else if git_repo
.git_repo
.find_reference(&format!("refs/heads/{ref_name}"))
.is_ok()
{
Some(format!("refs/heads/{ref_name}"))
} else {
bail!("could not find reference {ref_name}");
}
} else {
None
};
let mut client = Client::new(Params::with_git_config_relay_defaults(&Some(&git_repo)));
let (nostr_remote_name, decoded_nostr_url) = git_repo
.get_first_nostr_remote_when_in_ngit_binary()
.await.context("failed to list git remotes")?
.context("no `nostr://` remote detected. `ngit sync` must be run from a repo with a nostr remote")?;
let repo_coordinate = get_repo_coordinates_when_remote_unknown(&git_repo, &client).await?;
let fetch_report = fetching_with_report(git_repo_path, &client, &repo_coordinate).await?;
let repo_ref = get_repo_ref_from_cache(Some(git_repo_path), &repo_coordinate).await?;
let nostr_state = get_state_from_cache(Some(git_repo_path), &repo_ref).await?;
// Publish the current state event to any grasp server relays that are
// missing it or have a stale version. Grasp servers reject git pushes
// unless the state event is already present on their relay, so we must
// do this before attempting any git push.
//
// We use the per-relay state events captured during the fetch rather than
// the local database, because the database only stores the canonical latest
// event and cannot tell us what each individual relay holds.
let grasp_relays_needing_state: Vec<RelayUrl> = repo_ref
.git_server
.iter()
.filter(|url| is_grasp_server_clone_url(url))
.filter_map(|url| {
format_grasp_server_url_as_relay_url(url)
.ok()
.and_then(|relay_str| RelayUrl::parse(&relay_str).ok())
})
.filter(|relay_url| {
// Include this relay if it was absent from the fetch results, had
// no state event, or had a state event older than the canonical one.
match fetch_report.state_per_relay.get(relay_url) {
// relay wasn't queried, or returned no state event
None | Some(None) => true,
Some(Some(relay_event)) => relay_event.id != nostr_state.event.id,
}
})
.collect();
// relay URL -> whether the state event was successfully published to it.
// Only populated for grasp relays that needed the state event; grasp
// relays that already had the current state event are considered succeeded.
let mut grasp_relay_publish_results: HashMap<String, bool> = HashMap::new();
if !grasp_relays_needing_state.is_empty() {
// Attempt to load an existing login silently so the signer is
// available for NIP-42 auth if a relay requests it. We do not
// prompt the user, do not fetch profile updates, and ignore any
// failure — the events are already signed so publishing works
// without a signer.
if let Ok((signer, _, _)) = load_existing_login(
&Some(&git_repo),
&None,
&None,
&None,
Some(&client),
true, // silent
false, // prompt_for_password
false, // fetch_profile_updates
)
.await
{
client.set_signer(signer).await;
}
// Send only to the specific grasp relays that are missing or have a
// stale state event — no user write relays.
if let Ok(results) = send_events(
&client,
Some(git_repo_path),
vec![nostr_state.event.clone()],
vec![], // no user write relays
grasp_relays_needing_state,
true,
false,
)
.await
{
for (relay_url, succeeded) in results {
grasp_relay_publish_results.insert(relay_url, succeeded);
}
}
}
let term = console::Term::stderr();
let remote_states = list_from_remotes(
&term,
&git_repo,
&repo_ref.git_server,
&decoded_nostr_url,
Some(&nostr_state),
)
.await;
let missing_refs =
fetch_missing_refs(&git_repo, &nostr_state, &remote_states, &decoded_nostr_url);
for (url, (remote_state, is_grasp_server)) in &remote_states {
let remote_name = get_short_git_server_name(url);
let mut refspecs = vec![];
// delete ref from remote
let mut not_deleted = vec![];
for remote_ref_name in remote_state.keys() {
// skip unspecified refs
if let Some(full_ref_name) = &full_ref_name {
if remote_ref_name != full_ref_name {
continue;
}
}
if (!remote_ref_name.starts_with("refs/heads/pr/")
&& (remote_ref_name.starts_with("refs/heads/")
|| remote_ref_name.starts_with("refs/tags/")))
&& !nostr_state
.state
.keys()
.any(|nostr_ref| nostr_ref.eq(remote_ref_name))
{
if *is_grasp_server || args.force {
// delete branches / tags not on nostr
refspecs.push(format!(":{remote_ref_name}"));
} else {
not_deleted.push(remote_ref_name);
}
}
}
// add or update ref on remote
let mut not_updated = vec![];
for nostr_ref_name in nostr_state.state.keys() {
// skip unspecified refs
if let Some(full_ref_name) = &full_ref_name {
if nostr_ref_name != full_ref_name {
continue;
}
}
// skip refs missing locally
if missing_refs.contains(nostr_ref_name) {
continue;
}
// strip refs/heads/ or refs/tags/ prefix to get the tracking ref segment
// e.g. refs/heads/master -> master, refs/tags/v1.0.0 -> v1.0.0
let tracking_ref_name = nostr_ref_name
.strip_prefix("refs/heads/")
.or_else(|| nostr_ref_name.strip_prefix("refs/tags/"))
.unwrap_or(nostr_ref_name.as_str());
if invalid_nostr_state_ref(nostr_ref_name) {
// ensure nostr_state only supports refs/heads and refs/tags/
// and not refs/heads/prs/*
} else if let Some(remote_ref_value) = remote_state.get(nostr_ref_name) {
// update ref
let force_required = {
if let Ok((ahead, _)) =
get_ahead_behind(&git_repo, nostr_ref_name, remote_ref_value)
{
!ahead.is_empty()
} else {
true
}
};
if nostr_state
.state
.get(nostr_ref_name)
.is_none_or(|nostr_ref_value| nostr_ref_value.eq(remote_ref_value))
{
// no action if ref in sync
} else if remote_ref_value.starts_with("ref ") && !(args.force || *is_grasp_server)
{
// dont try and sync push symbolic refs
} else if !force_required {
refspecs.push(format!(
"refs/remotes/{nostr_remote_name}/{tracking_ref_name}:{nostr_ref_name}",
));
} else if *is_grasp_server || args.force {
refspecs.push(format!(
"+refs/remotes/{nostr_remote_name}/{tracking_ref_name}:{nostr_ref_name}",
));
} else {
not_updated.push(nostr_ref_name);
}
} else {
// add missing refs
refspecs.push(format!(
"refs/remotes/{nostr_remote_name}/{tracking_ref_name}:{nostr_ref_name}",
));
}
}
// Skip grasp servers whose relay did not receive the state event —
// they would reject the git push anyway.
if (*is_grasp_server || is_grasp_server_clone_url(url))
&& !grasp_relay_publish_results.is_empty()
{
if let Ok(relay_url) = format_grasp_server_url_as_relay_url(url) {
if grasp_relay_publish_results
.get(&relay_url)
.is_some_and(|succeeded| !succeeded)
{
term.write_line(&format!(
"WARNING: skipping {remote_name} - state event failed to reach its relay"
))?;
continue;
}
}
}
if refspecs.is_empty() {
if !not_updated.is_empty() || !not_deleted.is_empty() {
term.write_line(&format!("{remote_name} in sync excluding"))?;
} else {
term.write_line(&format!("{remote_name} already in sync"))?;
}
// report already in sync
} else {
match push_to_remote(
&git_repo,
url,
&decoded_nostr_url,
&refspecs,
&term,
*is_grasp_server || is_grasp_server_clone_url(url),
&[],
) {
Err(error) => {
term.write_line(&format!(
"error pushing updates to {remote_name}: error: {error}"
))?;
}
Ok(updated_refs) => {
if updated_refs.values().all(std::option::Option::is_none) {
if *is_grasp_server || args.force {
term.write_line(&format!("{remote_name} sync completed"))?;
// TODO we only know if there was an error but not
// if it rejected any
// updates
} else {
// we should report on refs not force pushed
term.write_line(&format!("{remote_name} sync completed"))?;
}
} else {
term.write_line(&format!(
"{remote_name} sync completed but not all changes were accepted"
))?;
}
for name in ¬_deleted {
term.write_line(&format!(" - {name} not deleted"))?;
}
for name in ¬_updated {
term.write_line(&format!(" - {name} not updated due to conflicts"))?;
}
if !not_updated.is_empty() || !not_deleted.is_empty() {
term.write_line("run `ngit sync --force` to delete refs or overwrite conflicts and potentially lose work")?;
}
}
}
}
}
if !missing_refs.is_empty() {
println!(
"skipped the following refs as could not find them locally or on any git servers: {}",
join_with_and(&missing_refs)
);
}
Ok(())
}
fn invalid_nostr_state_ref(ref_name: &str) -> bool {
ref_name.starts_with("refs/heads/pr/")
&& !(ref_name.starts_with("refs/heads/") || ref_name.starts_with("refs/tags/"))
}
fn identify_missing_refs(git_repo: &Repo, state: &HashMap<String, String>) -> Vec<String> {
let mut missing_oids = vec![];
for tip in state.values() {
if let Ok(exist) = git_repo.does_commit_exist(tip) {
let oid_exists_as_tag = Oid::from_str(tip).is_ok_and(|tip| {
git_repo
.git_repo
.find_object(tip, Some(git2::ObjectType::Tag))
.is_ok()
});
if !exist && !oid_exists_as_tag {
missing_oids.push(tip.to_string());
}
}
}
missing_oids
}
/// returns refs that are still missing
fn fetch_missing_refs(
git_repo: &Repo,
nostr_state: &RepoState,
remote_states: &HashMap<String, (HashMap<String, String>, bool)>,
nostr_url_decoded: &NostrUrlDecoded,
) -> Vec<String> {
let mut tried_remotes: Vec<String> = vec![];
let required_oids = identify_missing_refs(git_repo, &nostr_state.state);
if !required_oids.is_empty() {
println!("fetching git data missing locally");
}
loop {
let required_oids = identify_missing_refs(git_repo, &nostr_state.state);
let mut oids_on_remote: HashMap<String, Vec<String>> = HashMap::new();
if !required_oids.is_empty() {
for (url, (state, _)) in remote_states {
if tried_remotes.contains(url) {
continue;
}
for oid in &required_oids {
if state.values().any(|v| v.eq(oid)) {
oids_on_remote
.entry(url.to_string())
.or_default()
.push(oid.clone());
}
}
}
}
if let Some((url, oids)) = oids_on_remote.iter().max_by_key(|(url, vec)| {
if tried_remotes.contains(url) {
0
} else {
vec.len()
}
}) {
if oids.is_empty() || tried_remotes.contains(url) {
break;
}
tried_remotes.push(url.clone());
let _ = fetch_from_git_server(
git_repo,
oids,
url,
nostr_url_decoded,
&Term::stdout(),
is_grasp_server_clone_url(url),
);
} else {
break;
}
}
let still_missing_oids = identify_missing_refs(git_repo, &nostr_state.state);
if still_missing_oids.is_empty() {
vec![]
} else {
let missing_refs: Vec<String> = nostr_state
.state
.iter()
.filter_map(|(key, value)| {
if still_missing_oids.contains(value) {
Some(key.clone())
} else {
None
}
})
.collect();
println!(
"could not find refs on repo git servers: {}",
join_with_and(&missing_refs)
);
missing_refs
}
}
|