OXYGENHUB
◆ v0.9.0 oxygen / src / cli / sync.rs
39.7 KiB · Rust · last changed A refused checkout token falls back to this computer's sign-in for the hub, and says so (closes #155) 7 days ago source blame history raw
1
//! Commands that talk to remotes, and the reference server.
2
3
use super::format::plural;
4
use super::json;
5
use super::open;
6
use super::ui;
7
use anyhow::{Context, Result};
8
use oxy_core::repo::Repo;
9
use std::path::PathBuf;
10
11
pub fn clone(
12
    url: &str,
13
    dest: Option<PathBuf>,
14
    max_blob: Option<&str>,
15
    sparse: &[String],
16
    depth: Option<u32>,
17
    recursive: bool,
18
) -> Result<i32> {
19
    let dest = dest.unwrap_or_else(|| {
20
        let base = url
21
            .trim_end_matches('/')
22
            .rsplit('/')
23
            .next()
24
            .unwrap_or("repo");
25
        PathBuf::from(base)
26
    });
27
    let max_blob = max_blob.map(oxy_core::repo::parse_size).transpose()?;
28
    let sp = ui::spinner(&format!("Cloning {url}…"));
29
    let (repo, skipped) = Repo::clone_reporting(url, &dest, max_blob, sparse, depth)?;
30
    let heads = repo.bookmark_heads()?;
31
    sp.done(&format!(
32
        "{} into {} {}",
33
        ui::done("Cloned"),
34
        ui::path(repo.root().display()),
35
        ui::dim(format!(
36
            "({url}, {} with {})",
37
            ui::bookmark(&repo.config.bookmark),
38
            plural(heads.len(), "head")
39
        ))
40
    ));
41
    warn_skipped(&skipped);
42
    let others = repo.remote_only_bookmarks()?.len();
43
    if others > 0 {
44
        println!(
45
            "  {} {}",
46
            ui::note(),
47
            ui::dim(format!(
48
                "origin has {} more: oxy bookmark list --all, then oxy bookmark switch NAME",
49
                plural(others, "bookmark")
50
            ))
51
        );
52
    }
53
    let subrepos = repo.subrepos()?.len();
54
    if subrepos == 0 {
55
        return Ok(0);
56
    }
57
    if recursive {
58
        let sp = ui::spinner("Cloning subrepos…");
59
        let results = repo.submodule_update(true, &[])?;
60
        sp.clear();
61
        return Ok(print_subrepo_results(results));
62
    }
63
    println!(
64
        "  {} {}",
65
        ui::note(),
66
        ui::dim(format!(
67
            "{} not cloned: oxy submodule update --init (or clone with --recursive)",
68
            plural(subrepos, "subrepo")
69
        ))
70
    );
71
    Ok(0)
72
}
73
74
/// After a switch or pull: say when subrepos are not at their pins.
75
pub fn subrepos_behind(repo: &Repo) {
76
    let Ok(subs) = repo.subrepos() else {
77
        return;
78
    };
79
    let stale = subs
80
        .iter()
81
        .filter(|s| s.committed && s.actual.as_ref() != Some(&s.pin))
82
        .count();
83
    if stale > 0 {
84
        println!(
85
            "  {} {}",
86
            ui::note(),
87
            ui::dim(format!(
88
                "{} not at {} pin: oxy submodule update --init",
89
                plural(stale, "subrepo"),
90
                if stale == 1 { "its" } else { "their" }
91
            ))
92
        );
93
    }
94
}
95
96
/// Whether a URL names a store (a bucket or a directory in the store
97
/// layout) rather than a repository.
98
fn is_store_url(url: &str) -> bool {
99
    ["s3://", "gs://", "az://", "dir://"]
100
        .iter()
101
        .any(|p| url.starts_with(p))
102
}
103
104
/// Percent-encode what would break a query parameter.
105
fn encode_param(v: &str) -> String {
106
    let mut out = String::with_capacity(v.len());
107
    for b in v.bytes() {
108
        match b {
109
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' | b'/' | b':' => {
110
                out.push(b as char)
111
            }
112
            _ => out.push_str(&format!("%{b:02X}")),
113
        }
114
    }
115
    out
116
}
117
118
/// A store URL with settings added as query parameters, which is where a
119
/// store remote keeps them (`s3://b/p?profile=work&region=eu-west-2`).
120
fn url_with_settings(url: &str, settings: &[(&str, Option<String>)]) -> Result<String> {
121
    let given: Vec<(&str, &str)> = settings
122
        .iter()
123
        .filter_map(|(k, v)| v.as_deref().map(|v| (*k, v)))
124
        .collect();
125
    if given.is_empty() {
126
        return Ok(url.to_string());
127
    }
128
    if !is_store_url(url) || url.starts_with("dir://") {
129
        let names: Vec<String> = given.iter().map(|(k, _)| format!("--{k}")).collect();
130
        anyhow::bail!(
131
            "{} apply to a store remote (s3://, gs://, az://), not to {url}",
132
            names.join(", ")
133
        );
134
    }
135
    let mut out = url.to_string();
136
    for (k, v) in given {
137
        out.push(if out.contains('?') { '&' } else { '?' });
138
        out.push_str(k);
139
        out.push('=');
140
        out.push_str(&encode_param(v));
141
    }
142
    Ok(out)
143
}
144
145
pub fn remote_add(name: &str, url: &str, settings: &[(&str, Option<String>)]) -> Result<i32> {
146
    let mut repo = open()?;
147
    let url = url_with_settings(url, settings)?;
148
    let url = url.as_str();
149
    repo.config.set(
150
        &format!("remote.{name}.url"),
151
        &oxy_core::protocol::stored_url(url),
152
    )?;
153
    repo.save_config()?;
154
    println!(
155
        "{} remote {} {} {url}",
156
        ui::done("Added"),
157
        ui::bookmark(name),
158
        ui::dim(ui::glyph("→", "->"))
159
    );
160
    Ok(0)
161
}
162
163
/// `oxy remote check NAME`: reach the remote with the credentials in
164
/// hand, say what it holds, and, for a store, put it through the
165
/// conformance check under a probe prefix (left empty afterwards).
166
pub fn remote_check(name: &str) -> Result<i32> {
167
    let repo = open()?;
168
    let Some(url) = repo.config.remotes.get(name).cloned() else {
169
        anyhow::bail!("no remote {name:?}; oxy remote list shows the remotes there are");
170
    };
171
    let options = repo.options_for_url(&url);
172
    let mine = repo.id()?;
173
    let started = std::time::Instant::now();
174
    let sp = ui::spinner(&format!("Checking {name}…"));
175
    let Some(store) = oxy_core::protocol::open_store(&url, &options) else {
176
        // A hub or another repository: reach it and read its references.
177
        let (_, remote) = repo.open_remote_at(&url)?;
178
        let id = remote.repo_id()?;
179
        let refs = remote.refs()?;
180
        sp.done(&format!(
181
            "{} {} {}",
182
            ui::done("Reachable"),
183
            ui::bookmark(name),
184
            ui::dim(format!("({url}, {} ms)", started.elapsed().as_millis()))
185
        ));
186
        describe_holding(id, mine, refs.refs.len(), refs.tags.len(), None);
187
        return Ok(0);
188
    };
189
    let store = store.with_context(|| format!("opening the store of {name}"))?;
190
    // What it holds, in one listing and two reads.
191
    let marker = match store.get("oxygen.toml") {
192
        Ok(b) => Some(b),
193
        Err(oxy_core::strata::Error::NotFound(_)) => None,
194
        Err(e) => {
195
            sp.clear();
196
            return Err(anyhow::anyhow!("{e}"))
197
                .with_context(|| format!("reaching the store of {name}"));
198
        }
199
    };
200
    let reached = started.elapsed();
201
    let bundles = store.list("bundles/")?.len();
202
    let record = store.read_ref("refs")?;
203
    sp.done(&format!(
204
        "{} {} {}",
205
        ui::done("Reachable"),
206
        ui::bookmark(name),
207
        ui::dim(format!("({url}, {} ms)", reached.as_millis()))
208
    ));
209
    match marker {
210
        None => println!(
211
            "  {} {}",
212
            ui::note(),
213
            ui::dim("holds no repository yet: oxy push NAME binds it to this one")
214
        ),
215
        Some(_) => {
216
            let (id, bookmarks, tags) = match record {
217
                Some((bytes, _)) => {
218
                    let (id, resp) = oxy_core::protocol::wire::decode_refs(&bytes)?;
219
                    (id, resp.refs.len(), resp.tags.len())
220
                }
221
                None => (mine, 0, 0),
222
            };
223
            describe_holding(id, mine, bookmarks, tags, Some(bundles));
224
        }
225
    }
226
    // Then the behaviour a repository depends on, under a prefix of its own.
227
    let sp = ui::spinner("Checking that the store behaves…");
228
    let probe = format!("oxy-check-{}", std::process::id());
229
    let began = std::time::Instant::now();
230
    match oxy_core::strata::conformance(store.as_ref(), &probe) {
231
        Ok(passed) => {
232
            sp.clear();
233
            for p in &passed {
234
                println!("  {} {}", ui::ok(ui::glyph("✔", "*")), p);
235
            }
236
            println!(
237
                "{} {}",
238
                ui::done("Behaves"),
239
                ui::dim(format!(
240
                    "({} checks in {} ms; nothing left behind)",
241
                    passed.len(),
242
                    began.elapsed().as_millis()
243
                ))
244
            );
245
            Ok(0)
246
        }
247
        Err(e) => {
248
            sp.clear();
249
            // Leave nothing behind, even after a failed check.
250
            if let Ok(left) = store.list(&probe) {
251
                for entry in left {
252
                    let _ = store.delete(&entry.key);
253
                }
254
            }
255
            println!("{} {}", ui::err(ui::glyph("✘", "x")), e);
256
            println!(
257
                "  {} {}",
258
                ui::note(),
259
                ui::dim("a store that fails this cannot keep a repository safely; check the service and the credentials' permissions")
260
            );
261
            Ok(1)
262
        }
263
    }
264
}
265
266
fn describe_holding(
267
    id: [u8; 16],
268
    mine: [u8; 16],
269
    bookmarks: usize,
270
    tags: usize,
271
    bundles: Option<usize>,
272
) {
273
    let short: String = id[..6].iter().map(|b| format!("{b:02x}")).collect();
274
    let mut what = format!(
275
        "repository {short}: {} and {}",
276
        plural(bookmarks, "bookmark"),
277
        plural(tags, "tag")
278
    );
279
    if let Some(b) = bundles {
280
        what.push_str(&format!(" in {}", plural(b, "bundle")));
281
    }
282
    if id == mine {
283
        println!("  {} {}", ui::note(), ui::dim(format!("holds this {what}")));
284
    } else if id == oxy_core::protocol::UNBOUND_ID {
285
        println!("  {} {}", ui::note(), ui::dim("holds no repository yet"));
286
    } else {
287
        println!(
288
            "  {} {}",
289
            ui::caution(),
290
            ui::warn(format!("holds another {what}; a push would be refused"))
291
        );
292
    }
293
}
294
295
pub fn remote_remove(name: &str) -> Result<i32> {
296
    let mut repo = open()?;
297
    repo.remove_remote(name)?;
298
    println!("{} remote {}", ui::done("Removed"), ui::bookmark(name));
299
    Ok(0)
300
}
301
302
pub fn remote_list(as_json: bool) -> Result<i32> {
303
    let repo = open()?;
304
    let view = repo.view()?;
305
    if as_json {
306
        let items = repo.config.remotes.iter().map(|(name, url)| {
307
            let bms = view.remotes.get(name).map(|b| {
308
                json::arr(b.iter().map(|(bm, st)| {
309
                    let heads = repo
310
                        .store()
311
                        .get_state(st)
312
                        .map(|s| s.heads)
313
                        .unwrap_or_default();
314
                    json::obj([("name", json::s(bm)), ("heads", json::hashes(&heads))])
315
                }))
316
            });
317
            json::obj([
318
                ("name", json::s(name)),
319
                ("url", json::s(url)),
320
                // The CA file `remote.NAME.ca` names, for tools that talk to
321
                // the same hub (an editor's forge view, `oh`).
322
                (
323
                    "ca",
324
                    json::opt(repo.config.remote_cas.get(name), |c| json::s(c)),
325
                ),
326
                ("bookmarks", bms.unwrap_or_else(|| "[]".into())),
327
            ])
328
        });
329
        println!("{}", json::arr(items));
330
        return Ok(0);
331
    }
332
    if repo.config.remotes.is_empty() {
333
        println!(
334
            "{}",
335
            ui::dim("No remotes; add one with oxy remote add <name> <url>.")
336
        );
337
    }
338
    for (name, url) in &repo.config.remotes {
339
        println!(
340
            "{} {}{}{}",
341
            ui::ok(ui::glyph("⇄", "<>")),
342
            ui::bookmark(name),
343
            ui::sep(),
344
            ui::dim(url)
345
        );
346
        if let Some(bms) = view.remotes.get(name) {
347
            for (bm, st) in bms {
348
                let heads = repo
349
                    .store()
350
                    .get_state(st)
351
                    .map(|s| s.heads.len())
352
                    .unwrap_or(0);
353
                println!(
354
                    "    {} {}{}{}",
355
                    ui::dim(ui::glyph("↳", "->")),
356
                    ui::id(format!("{name}/{bm}")),
357
                    ui::sep(),
358
                    ui::dim(plural(heads, "head"))
359
                );
360
            }
361
        }
362
    }
363
    Ok(0)
364
}
365
366
/// The remote named, else `origin`, else the only remote there is.
367
fn remote_name(repo: &oxy_core::repo::Repo, remote: Option<&str>) -> String {
368
    remote.map_or_else(|| repo.default_remote(), str::to_string)
369
}
370
371
pub fn fetch(remote: Option<&str>, as_json: bool) -> Result<i32> {
372
    let repo = open()?;
373
    let remote = remote_name(&repo, remote);
374
    let sp = ui::spinner(&format!("Fetching from {remote}…"));
375
    let report = noted(&repo, &remote, repo.fetch(&remote))?;
376
    sp.clear();
377
    if as_json {
378
        println!(
379
            "{}",
380
            json::obj([
381
                ("remote", json::s(&remote)),
382
                (
383
                    "refs",
384
                    json::arr(report.refs.iter().map(|(name, heads, n)| {
385
                        let t = repo.tracking(&remote, name).ok().flatten();
386
                        json::obj([
387
                            ("name", json::s(name)),
388
                            ("heads", json::hashes(heads)),
389
                            ("new", json::n(*n)),
390
                            (
391
                                "ahead",
392
                                t.map_or_else(|| "null".into(), |(a, _)| json::n(a)),
393
                            ),
394
                            (
395
                                "behind",
396
                                t.map_or_else(|| "null".into(), |(_, b)| json::n(b)),
397
                            ),
398
                        ])
399
                    }))
400
                ),
401
                ("new_tags", json::n(report.new_tags)),
402
                (
403
                    "withdrawn_tags",
404
                    json::arr(report.withdrawn_tags.iter().map(|n| json::s(n))),
405
                ),
406
                ("unverified", json::b(report.unverified)),
407
                (
408
                    "log_notes",
409
                    json::arr(report.log_notes.iter().map(|n| json::s(n))),
410
                ),
411
                ("skipped", skipped_json(&report.skipped)),
412
            ])
413
        );
414
        return Ok(0);
415
    }
416
    if report.refs.is_empty() {
417
        println!(
418
            "{} {}",
419
            ui::done("Fetched"),
420
            ui::dim(format!("nothing new from {remote}"))
421
        );
422
    }
423
    for (name, heads, n) in &report.refs {
424
        // What was fetched, then how the local bookmark stands against it:
425
        // "up to date" only when the two are the same (oxygen#56).
426
        let fetched =
427
            (*n > 0).then(|| format!("{}{}", ui::ok(plural(*n, "new change")), ui::sep()));
428
        let them = |k: usize| if k == 1 { "it" } else { "them" };
429
        let standing = match repo.tracking(&remote, name).ok().flatten() {
430
            None => ui::dim(format!("no local bookmark {name}")),
431
            Some((0, 0)) => ui::dim(format!("{name} up to date")),
432
            Some((0, b)) if *name == repo.config.bookmark => ui::warn(format!(
433
                "{name} is {} behind: oxy pull to take {}",
434
                plural(b, "change"),
435
                them(b)
436
            )),
437
            Some((0, b)) => ui::warn(format!(
438
                "{name} is {} behind: oxy bookmark switch {name} then oxy pull to take {}",
439
                plural(b, "change"),
440
                them(b)
441
            )),
442
            Some((a, 0)) => ui::dim(format!(
443
                "{name} is {} ahead: oxy push to send {}",
444
                plural(a, "change"),
445
                them(a)
446
            )),
447
            Some((a, b)) => ui::warn(format!(
448
                "{name} and {remote}/{name} have diverged ({a} here, {b} there): oxy pull to merge them"
449
            )),
450
        };
451
        let what = format!("{}{standing}", fetched.unwrap_or_default());
452
        println!(
453
            "{} {}{}{}{}{what}",
454
            ui::done("Fetched"),
455
            ui::id(format!("{remote}/{name}")),
456
            ui::sep(),
457
            ui::dim(plural(heads.len(), "head")),
458
            ui::sep()
459
        );
460
    }
461
    if report.new_tags > 0 {
462
        println!(
463
            "  {} {}",
464
            ui::note(),
465
            ui::dim(format!("{} adopted", plural(report.new_tags, "tag")))
466
        );
467
    }
468
    for t in &report.withdrawn_tags {
469
        println!(
470
            "  {} {}",
471
            ui::note(),
472
            ui::dim(format!("tag {t} was withdrawn on {remote}, and here"))
473
        );
474
    }
475
    if report.unverified {
476
        eprintln!(
477
            "{} {remote} has no trust policy; nothing fetched from it was verified",
478
            ui::caution()
479
        );
480
    }
481
    for n in &report.log_notes {
482
        eprintln!("{} {n}", ui::caution());
483
    }
484
    warn_skipped(&report.skipped);
485
    Ok(0)
486
}
487
488
/// Bookmarks a fetch left as they were, their attestation unverifiable
489
/// (oxygenhub#266): one warning each, naming it and why.
490
fn warn_skipped(skipped: &[(String, String)]) {
491
    for (name, why) in skipped {
492
        eprintln!(
493
            "{} {} {}",
494
            ui::caution(),
495
            ui::warn(format!("{name} not updated:")),
496
            ui::dim(why)
497
        );
498
    }
499
}
500
501
fn skipped_json(skipped: &[(String, String)]) -> String {
502
    json::arr(
503
        skipped
504
            .iter()
505
            .map(|(name, why)| json::obj([("name", json::s(name)), ("reason", json::s(why))])),
506
    )
507
}
508
509
/// Pull acts on the current bookmark only: name the others the fetch
510
/// found behind their remote, which is easy to take for the current one
511
/// (oxygen#29).
512
fn others_behind(repo: &oxy_core::repo::Repo, remote: &str) {
513
    let Ok(bookmarks) = repo.bookmarks() else {
514
        return;
515
    };
516
    for b in bookmarks {
517
        let Some(n) = b.behind.filter(|n| *n > 0) else {
518
            continue;
519
        };
520
        if b.current {
521
            continue;
522
        }
523
        println!(
524
            "  {} {}",
525
            ui::note(),
526
            ui::dim(format!(
527
                "{} is {} behind {remote}/{}: oxy bookmark switch {} then oxy pull to take {}",
528
                b.name,
529
                plural(n, "change"),
530
                b.name,
531
                b.name,
532
                if n == 1 { "it" } else { "them" }
533
            ))
534
        );
535
    }
536
}
537
538
pub fn pull(remote: Option<&str>, as_json: bool) -> Result<i32> {
539
    let repo = open()?;
540
    let remote = remote_name(&repo, remote);
541
    let sp = ui::spinner(&format!("Pulling from {remote}…"));
542
    let r = noted(&repo, &remote, repo.pull(&remote))?;
543
    if as_json {
544
        sp.clear();
545
        let conflicted = repo
546
            .snapshot_status("oxy pull")?
547
            .base_snapshot
548
            .is_conflicted();
549
        println!(
550
            "{}",
551
            json::obj([
552
                ("remote", json::s(&remote)),
553
                ("bookmark", json::s(&repo.config.bookmark)),
554
                ("heads", json::hashes(&r.heads)),
555
                ("changed", json::b(r.changed)),
556
                ("checked_out", json::b(r.checked_out)),
557
                ("conflicted", json::b(conflicted)),
558
                ("others", json::arr(r.others.iter().map(|o| json::s(o)))),
559
                ("skipped", skipped_json(&r.skipped))
560
            ])
561
        );
562
        return Ok(0);
563
    }
564
    if !r.changed {
565
        sp.done(&format!(
566
            "{} {}{}",
567
            ui::ok(ui::glyph("✔", "*")),
568
            ui::dim(format!(
569
                "{} is up to date with {remote}.",
570
                repo.config.bookmark
571
            )),
572
            if r.checked_out {
573
                ui::dim(" The working copy had fallen behind it and is updated.")
574
            } else {
575
                String::new()
576
            }
577
        ));
578
        others_pulled(&r.others);
579
        others_behind(&repo, &remote);
580
        warn_skipped(&r.skipped);
581
        if r.checked_out {
582
            super::workspace::where_now(&repo);
583
        }
584
        return Ok(0);
585
    }
586
    sp.done(&format!(
587
        "{} {} now has {}{}",
588
        ui::done("Pulled:"),
589
        ui::bookmark(&repo.config.bookmark),
590
        plural(r.heads.len(), "head"),
591
        ui::dim(if r.checked_out {
592
            "; working copy updated"
593
        } else {
594
            "; run oxy checkout to update the working copy"
595
        })
596
    ));
597
    let snap = repo.snapshot("oxy pull")?;
598
    if snap.base_snapshot.is_conflicted() {
599
        println!(
600
            "  {} {}",
601
            ui::caution(),
602
            ui::warn("the result has conflicts; see oxy status")
603
        );
604
    }
605
    others_pulled(&r.others);
606
    others_behind(&repo, &remote);
607
    warn_skipped(&r.skipped);
608
    subrepos_behind(&repo);
609
    super::workspace::where_now(&repo);
610
    Ok(0)
611
}
612
613
/// The other bookmarks a pull moved.
614
fn others_pulled(others: &[String]) {
615
    if others.is_empty() {
616
        return;
617
    }
618
    let shown: Vec<String> = others.iter().take(8).map(ui::bookmark).collect();
619
    let more = others.len().saturating_sub(8);
620
    println!(
621
        "  {} {}{}",
622
        ui::done("Also updated"),
623
        shown.join(", "),
624
        if more > 0 {
625
            ui::dim(format!(" and {more} more"))
626
        } else {
627
            String::new()
628
        }
629
    );
630
}
631
632
/// `oxy push [REMOTE] --delete NAME`.
633
pub fn push_delete(remote: Option<&str>, name: &str, as_json: bool) -> Result<i32> {
634
    let repo = open()?;
635
    let remote = remote_name(&repo, remote);
636
    let sp = ui::spinner(&format!("Deleting {remote}/{name}…"));
637
    let results = repo.push_delete(&remote, name)?;
638
    sp.clear();
639
    if as_json {
640
        println!(
641
            "{}",
642
            json::arr(results.iter().map(|r| {
643
                json::obj([
644
                    ("name", json::s(&r.name)),
645
                    ("ok", json::b(r.ok)),
646
                    ("reason", json::s(&r.reason)),
647
                    ("state", json::s("")),
648
                ])
649
            }))
650
        );
651
        return Ok(if results.iter().all(|r| r.ok) { 0 } else { 1 });
652
    }
653
    let mut code = 0;
654
    for r in results {
655
        if r.ok {
656
            let done = if r.name.starts_with("tags/") {
657
                "Withdrew"
658
            } else {
659
                "Deleted"
660
            };
661
            println!(
662
                "{} {}",
663
                ui::done(done),
664
                ui::id(format!("{remote}/{}", r.name))
665
            );
666
        } else {
667
            println!(
668
                "{} {} {}{}{}",
669
                ui::err(ui::glyph("✖", "x")),
670
                ui::err("Rejected"),
671
                ui::id(format!("{remote}/{}", r.name)),
672
                ui::sep(),
673
                r.reason
674
            );
675
            code = 1;
676
        }
677
    }
678
    Ok(code)
679
}
680
681
pub fn push(
682
    remote: Option<&str>,
683
    force: bool,
684
    bookmark: Option<&str>,
685
    as_json: bool,
686
) -> Result<i32> {
687
    let repo = open()?;
688
    let remote = remote_name(&repo, remote);
689
    let sp = ui::spinner(&format!("Pushing to {remote}…"));
690
    let bookmark_name = bookmark.map_or_else(|| repo.config.bookmark.clone(), String::from);
691
    // A long push goes in parts: the spinner says which.
692
    let results = noted(
693
        &repo,
694
        &remote,
695
        repo.push_bookmark_with(&remote, &bookmark_name, force, &mut |m| {
696
            sp.set(&format!("Pushing to {remote}… {m}"))
697
        }),
698
    )?;
699
    sp.clear();
700
    if as_json {
701
        // The state each accepted bookmark now holds on the remote (its local
702
        // state object), so a caller can look up what the push started, such
703
        // as the checks a forge runs on it.
704
        let view = repo.view()?;
705
        println!(
706
            "{}",
707
            json::arr(results.iter().map(|r| {
708
                let state = view
709
                    .bookmarks
710
                    .get(&r.name)
711
                    .filter(|_| r.ok)
712
                    .map(|st| st.to_hex())
713
                    .unwrap_or_default();
714
                json::obj([
715
                    ("name", json::s(&r.name)),
716
                    ("ok", json::b(r.ok)),
717
                    ("reason", json::s(&r.reason)),
718
                    ("state", json::s(&state)),
719
                ])
720
            }))
721
        );
722
        return Ok(if results.iter().all(|r| r.ok) { 0 } else { 1 });
723
    }
724
    let mut code = 0;
725
    for r in results {
726
        if r.ok {
727
            println!(
728
                "{} {}{}{}",
729
                ui::done("Pushed"),
730
                ui::id(format!("{remote}/{}", r.name)),
731
                ui::sep(),
732
                ui::dim(if r.reason.is_empty() {
733
                    "updated"
734
                } else {
735
                    r.reason.as_str()
736
                })
737
            );
738
        } else {
739
            println!(
740
                "{} {} {}{}{}",
741
                ui::err(ui::glyph("✖", "x")),
742
                ui::err("Rejected"),
743
                ui::id(format!("{remote}/{}", r.name)),
744
                ui::sep(),
745
                r.reason
746
            );
747
            code = 1;
748
        }
749
    }
750
    Ok(code)
751
}
752
753
pub fn serve(
754
    root: PathBuf,
755
    bind: &str,
756
    tokens: Vec<String>,
757
    tls: Option<(PathBuf, PathBuf)>,
758
) -> Result<i32> {
759
    let listener = std::net::TcpListener::bind(bind).with_context(|| format!("binding {bind}"))?;
760
    let root = oxy_core::portable::canonical(&root)?;
761
    let tls = tls.map(|(cert, key)| oxy_server::TlsFiles { cert, key });
762
    println!(
763
        "Serving repositories under {} on {}://{bind}/<repo>/oxy/v1/ ({})",
764
        root.display(),
765
        if tls.is_some() { "https" } else { "http" },
766
        if tokens.is_empty() {
767
            "open: no push tokens configured"
768
        } else {
769
            "push requires a token"
770
        }
771
    );
772
    if tls.is_none()
773
        && !bind.starts_with("127.")
774
        && !bind.starts_with("localhost")
775
        && !bind.starts_with("[::1]")
776
    {
777
        eprintln!("warning: serving plain HTTP on a non-loopback address; put a TLS terminator in front or pass --tls-cert/--tls-key");
778
    }
779
    oxy_server::run_blocking(listener, oxy_server::ServerConfig { root, tokens, tls })?;
780
    Ok(0)
781
}
782
783
pub fn git_export(git_dir: &std::path::Path, branch: Option<&str>) -> Result<i32> {
784
    let repo = open()?;
785
    let sp = ui::spinner("Exporting to Git…");
786
    let r = match branch {
787
        Some(b) => repo.git_export_as(git_dir, b)?,
788
        None => repo.git_export(git_dir)?,
789
    };
790
    sp.clear();
791
    println!(
792
        "{} {} to {} as branch {} {}",
793
        ui::done("Exported"),
794
        plural(r.exported, "new state"),
795
        ui::path(git_dir.display()),
796
        ui::bookmark(&r.branch),
797
        ui::dim(format!(
798
            "(currently {}; {} already exported)",
799
            plural(r.heads, "head"),
800
            r.skipped
801
        ))
802
    );
803
    Ok(0)
804
}
805
806
pub fn git_recover() -> Result<i32> {
807
    let repo = open()?;
808
    let sp = ui::spinner("Reading the store…");
809
    let (changes, heads) = repo.recover_git_import(&|m| sp.set(m))?;
810
    sp.done(&format!(
811
        "{} {} from imported Git commits: {} now has {}",
812
        ui::done("Recovered"),
813
        plural(changes, "change"),
814
        ui::bookmark(&repo.config.bookmark),
815
        plural(heads, "head")
816
    ));
817
    println!(
818
        "  {} {}",
819
        ui::note(),
820
        ui::dim("carry on with oxy git fetch GIT_DIR --branch BRANCH")
821
    );
822
    Ok(0)
823
}
824
825
/// `--claim` (oxygen#97): record `git.claim`, so this import and every
826
/// later fetch author the identity's own commits with its key.
827
fn claim_commits(repo: &mut Repo) -> Result<()> {
828
    repo.config.set("git.claim", "true")?;
829
    repo.save_config()?;
830
    Ok(())
831
}
832
833
pub fn git_fetch(url: &str, branch: &str, claim: bool) -> Result<i32> {
834
    let mut repo = open()?;
835
    if claim {
836
        claim_commits(&mut repo)?;
837
    }
838
    let sp = ui::spinner(&format!("Fetching {url} {branch}…"));
839
    let r = repo.git_fetch(url, branch)?;
840
    sp.clear();
841
    if r.commits == 0 {
842
        println!(
843
            "{} {}",
844
            ui::done("Fetched"),
845
            ui::dim(format!("nothing new from {url} {branch}"))
846
        );
847
    } else {
848
        println!(
849
            "{} {} ({} merges) from {} {}{}{} now has {}",
850
            ui::done("Fetched"),
851
            plural(r.commits, "new commit"),
852
            r.merges,
853
            ui::path(url),
854
            ui::bookmark(branch),
855
            ui::sep(),
856
            ui::bookmark(&repo.config.bookmark),
857
            plural(r.heads, "head")
858
        );
859
        if !repo.snapshot("oxy git fetch")?.changed {
860
            println!("  {} {}", ui::note(), ui::dim("working copy updated"));
861
        } else {
862
            println!(
863
                "  {} {}",
864
                ui::note(),
865
                ui::dim("run oxy checkout to update the working copy")
866
            );
867
        }
868
    }
869
    if r.submodules > 0 {
870
        println!(
871
            "  {} {}",
872
            ui::note(),
873
            ui::dim(format!(
874
                "{} submodule {} imported as subrepos with Git pins",
875
                r.submodules,
876
                if r.submodules == 1 {
877
                    "entry"
878
                } else {
879
                    "entries"
880
                }
881
            ))
882
        );
883
    }
884
    Ok(0)
885
}
886
887
pub fn git_push(url: &str, branch: &str) -> Result<i32> {
888
    let repo = open()?;
889
    let sp = ui::spinner(&format!("Pushing to {url} {branch}…"));
890
    let r = repo.git_push(url, branch)?;
891
    sp.clear();
892
    println!(
893
        "{} {} to {} {} {}",
894
        ui::done("Pushed"),
895
        plural(r.exported, "new commit"),
896
        ui::path(url),
897
        ui::bookmark(&r.branch),
898
        ui::dim(format!("({} already there)", r.skipped))
899
    );
900
    Ok(0)
901
}
902
903
/// `oxy git verify`: exit 1 if any commit's tree is not reproduced.
904
pub fn git_verify(
905
    git_dir: &std::path::Path,
906
    branch: &str,
907
    last: Option<usize>,
908
    max: usize,
909
) -> Result<i32> {
910
    let repo = open()?;
911
    let started = std::time::Instant::now();
912
    let sp = ui::spinner(&format!(
913
        "Rebuilding every commit of {} {branch}…",
914
        git_dir.display()
915
    ));
916
    let r = repo.verify_git_import_last(git_dir, branch, last, max)?;
917
    sp.clear();
918
    for (_, what) in &r.mismatches {
919
        println!("{} {what}", ui::err(ui::glyph("✖", "x")));
920
    }
921
    if r.not_yet > 0 {
922
        println!(
923
            "  {} {}",
924
            ui::note(),
925
            ui::dim(format!(
926
                "{} not imported yet, after the last imported commit: not checked",
927
                plural(r.not_yet, "commit")
928
            ))
929
        );
930
    }
931
    if r.mismatches.is_empty() {
932
        println!(
933
            "{}",
934
            ui::done(if r.skipped > 0 {
935
                format!(
936
                    "The last {} commits reproduced ({} followed), {} files compared, in {:.1}s",
937
                    r.commits - r.skipped,
938
                    r.skipped,
939
                    r.files_compared,
940
                    started.elapsed().as_secs_f64()
941
                )
942
            } else {
943
                format!(
944
                    "Every commit reproduced: {} commits, {} files compared, in {:.1}s",
945
                    r.commits,
946
                    r.files_compared,
947
                    started.elapsed().as_secs_f64()
948
                )
949
            })
950
        );
951
        Ok(0)
952
    } else {
953
        // What differs, by kind, the commit taken off: one difference
954
        // carried through history reads as one line.
955
        let mut kinds: std::collections::BTreeMap<String, usize> = Default::default();
956
        for (_, what) in &r.mismatches {
957
            let kind = what.split_once(": ").map_or(what.as_str(), |(_, k)| k);
958
            *kinds.entry(kind.to_string()).or_default() += 1;
959
        }
960
        let mut kinds: Vec<_> = kinds.into_iter().collect();
961
        kinds.sort_by_key(|k| std::cmp::Reverse(k.1));
962
        for (kind, n) in kinds.iter().take(10) {
963
            println!("  {} {}", ui::dim(format!("{n:>6} ×")), kind);
964
        }
965
        println!(
966
            "{} differences in {} commits checked{}",
967
            r.mismatches.len(),
968
            r.commits,
969
            if max > 0 && r.mismatches.len() >= max {
970
                format!(" (stopped at {max}; --max-differences 0 checks every commit)")
971
            } else {
972
                String::new()
973
            }
974
        );
975
        Ok(1)
976
    }
977
}
978
979
pub fn git_import(
980
    git_dir: &std::path::Path,
981
    dest: &std::path::Path,
982
    branch: &str,
983
    claim: bool,
984
) -> Result<i32> {
985
    let mut repo = Repo::init(dest)?;
986
    if claim {
987
        claim_commits(&mut repo)?;
988
    }
989
    let started = std::time::Instant::now();
990
    let sp = ui::spinner(&format!("Importing {} {branch}…", git_dir.display()));
991
    let r = repo.import_git_with(git_dir, branch, &mut |where_| sp.set(where_))?;
992
    sp.clear();
993
    println!(
994
        "{} {} ({} merges) from {} {} into {} {}",
995
        ui::done("Imported"),
996
        plural(r.commits, "commit"),
997
        r.merges,
998
        ui::path(git_dir.display()),
999
        ui::bookmark(branch),
1000
        ui::path(repo.root().display()),
1001
        ui::dim(format!(
1002
            "in {:.1?}; {} now has {}",
1003
            started.elapsed(),
1004
            repo.config.bookmark,
1005
            plural(r.heads, "head")
1006
        ))
1007
    );
1008
    if r.submodules > 0 {
1009
        println!(
1010
            "  {} {}",
1011
            ui::note(),
1012
            ui::dim(format!(
1013
                "{} submodule {} imported as subrepos with Git pins",
1014
                r.submodules,
1015
                if r.submodules == 1 {
1016
                    "entry"
1017
                } else {
1018
                    "entries"
1019
                }
1020
            ))
1021
        );
1022
    }
1023
    Ok(0)
1024
}
1025
1026
pub fn submodule_add(url: &str, path: &str, bookmark: Option<&str>) -> Result<i32> {
1027
    let repo = open()?;
1028
    let sp = ui::spinner(&format!("Cloning {url} into {path}…"));
1029
    let pin = repo.submodule_add(url, path, bookmark)?;
1030
    sp.done(&format!(
1031
        "{} subrepo {} {} {}",
1032
        ui::done("Added"),
1033
        ui::path(path),
1034
        ui::dim(ui::glyph("→", "->")),
1035
        ui::dim(pin.summary())
1036
    ));
1037
    println!(
1038
        "  {} {}",
1039
        ui::note(),
1040
        ui::dim("recorded in .oxymodules; commit to pin it")
1041
    );
1042
    Ok(0)
1043
}
1044
1045
pub fn submodule_update(init: bool, paths: &[String]) -> Result<i32> {
1046
    let repo = open()?;
1047
    let sp = ui::spinner("Updating subrepos…");
1048
    let results = repo.submodule_update(init, paths)?;
1049
    let unpinned: Vec<_> = repo
1050
        .unpinned_modules()?
1051
        .into_iter()
1052
        .filter(|(p, _)| paths.is_empty() || paths.iter().any(|q| q.trim_end_matches('/') == p))
1053
        .collect();
1054
    sp.clear();
1055
    if results.is_empty() && unpinned.is_empty() {
1056
        println!("{}", ui::dim("No subrepos."));
1057
    }
1058
    let code = print_subrepo_results(results);
1059
    print_unpinned(&unpinned);
1060
    Ok(if unpinned.is_empty() { code } else { 1 })
1061
}
1062
1063
/// `.oxymodules` entries never added: nothing to clone, so say how to add
1064
/// them (oxygen#72).
1065
fn print_unpinned(unpinned: &[(String, oxy_core::subrepo::Module)]) {
1066
    for (path, m) in unpinned {
1067
        let url = if m.url.is_empty() { "<url>" } else { &m.url };
1068
        println!(
1069
            "{} {}",
1070
            ui::warn(format!(
1071
                "{} in .oxymodules but never added, so no pin",
1072
                ui::glyph("⚠", "!")
1073
            )),
1074
            ui::path(path)
1075
        );
1076
        println!(
1077
            "  {}",
1078
            ui::dim(format!("add it with: oxy submodule add {url} {path}"))
1079
        );
1080
    }
1081
}
1082
1083
fn print_subrepo_results(results: Vec<(String, oxy_core::repo::SubrepoSync)>) -> i32 {
1084
    let mut code = 0;
1085
    for (path, r) in results {
1086
        use oxy_core::repo::SubrepoSync::*;
1087
        let line = match r {
1088
            Updated => ui::done("Updated"),
1089
            UpToDate => ui::ok(format!("{} up to date", ui::glyph("✔", "*"))),
1090
            Missing => {
1091
                code = 1;
1092
                ui::warn(format!(
1093
                    "{} missing (pass --init to clone)",
1094
                    ui::glyph("⚠", "!")
1095
                ))
1096
            }
1097
            Dirty => {
1098
                code = 1;
1099
                ui::warn(format!(
1100
                    "{} has uncommitted changes; left as is",
1101
                    ui::glyph("⚠", "!")
1102
                ))
1103
            }
1104
            KeptBookmark { bookmark, newer } => ui::warn(format!(
1105
                "{} checked out at the pin; its bookmark {bookmark} keeps {} the pin lacks",
1106
                ui::glyph("⚠", "!"),
1107
                plural(newer, "newer change")
1108
            )),
1109
            Failed(e) => {
1110
                code = 1;
1111
                ui::err(format!("{} {e}", ui::glyph("✖", "x")))
1112
            }
1113
        };
1114
        println!("{line} {}", ui::path(path));
1115
    }
1116
    code
1117
}
1118
1119
fn pin_json(p: &oxy_core::subrepo::Pin) -> String {
1120
    match p {
1121
        oxy_core::subrepo::Pin::Oxy {
1122
            repo,
1123
            heads,
1124
            bookmark,
1125
        } => json::obj([
1126
            ("kind", json::s("oxy")),
1127
            (
1128
                "repo",
1129
                json::s(&repo.iter().map(|b| format!("{b:02x}")).collect::<String>()),
1130
            ),
1131
            ("heads", json::hashes(heads)),
1132
            ("bookmark", json::s(bookmark)),
1133
        ]),
1134
        oxy_core::subrepo::Pin::Git { sha, branch } => json::obj([
1135
            ("kind", json::s("git")),
1136
            ("sha", json::s(sha)),
1137
            ("branch", json::s(branch)),
1138
        ]),
1139
    }
1140
}
1141
1142
pub fn submodule_status(as_json: bool) -> Result<i32> {
1143
    let repo = open()?;
1144
    let subs = repo.subrepos()?;
1145
    let unpinned = repo.unpinned_modules()?;
1146
    if as_json {
1147
        let pinned = subs.iter().map(|s| {
1148
            json::obj([
1149
                ("path", json::s(&s.path)),
1150
                ("pin", pin_json(&s.pin)),
1151
                ("actual", json::opt(s.actual.as_ref(), pin_json)),
1152
                ("at_pin", json::b(s.actual.as_ref() == Some(&s.pin))),
1153
                ("dirty", json::b(s.dirty)),
1154
                ("committed", json::b(s.committed)),
1155
                ("url", json::opt(s.url.as_deref(), json::s)),
1156
            ])
1157
        });
1158
        // Listed in .oxymodules but never added: no pin at all.
1159
        let unpinned_json = unpinned.iter().map(|(path, m)| {
1160
            json::obj([
1161
                ("path", json::s(path)),
1162
                ("pin", "null".to_string()),
1163
                ("actual", "null".to_string()),
1164
                ("at_pin", json::b(false)),
1165
                ("dirty", json::b(false)),
1166
                ("committed", json::b(false)),
1167
                ("url", json::s(&m.url)),
1168
            ])
1169
        });
1170
        println!("{}", json::arr(pinned.chain(unpinned_json)));
1171
        return Ok(0);
1172
    }
1173
    if subs.is_empty() && unpinned.is_empty() {
1174
        println!("{}", ui::dim("No subrepos."));
1175
    }
1176
    for s in subs {
1177
        let state = match (&s.actual, s.dirty) {
1178
            _ if !s.committed => {
1179
                ui::warn(format!("{} added, not committed yet", ui::glyph("+", "+")))
1180
            }
1181
            (None, _) => ui::warn(format!("{} missing", ui::glyph("⚠", "!"))),
1182
            (Some(a), dirty) if *a == s.pin => {
1183
                if dirty {
1184
                    ui::warn(format!(
1185
                        "{} at pin, uncommitted changes",
1186
                        ui::glyph("✎", "~")
1187
                    ))
1188
                } else {
1189
                    ui::ok(format!("{} at pin", ui::glyph("✔", "*")))
1190
                }
1191
            }
1192
            (Some(a), _) => ui::warn(format!("{} at {}", ui::glyph("↕", "<>"), a.summary())),
1193
        };
1194
        println!(
1195
            "{} {}{}{}{}{state}",
1196
            ui::ok(ui::glyph("⧉", "#")),
1197
            ui::path(&s.path),
1198
            ui::sep(),
1199
            ui::dim(s.pin.summary()),
1200
            ui::sep()
1201
        );
1202
        if let Some(u) = s.url {
1203
            println!("    {} {}", ui::dim(ui::glyph("↳", "->")), ui::dim(u));
1204
        }
1205
    }
1206
    print_unpinned(&unpinned);
1207
    Ok(0)
1208
}
1209
1210
pub fn submodule_remove(path: &str) -> Result<i32> {
1211
    let repo = open()?;
1212
    repo.submodule_remove(path)?;
1213
    println!(
1214
        "{} subrepo {} {}",
1215
        ui::done("Removed"),
1216
        ui::path(path),
1217
        ui::dim("(directory left in place; commit to record the removal)")
1218
    );
1219
    Ok(0)
1220
}
1221
1222
pub fn deepen(remote: &str, depth: u32) -> Result<i32> {
1223
    let repo = open()?;
1224
    if !repo.is_shallow() {
1225
        println!(
1226
            "{}",
1227
            ui::dim("This repository is not shallow; nothing to deepen.")
1228
        );
1229
        return Ok(0);
1230
    }
1231
    let sp = ui::spinner(&format!("Fetching {depth} more levels from {remote}…"));
1232
    let n = repo.deepen(remote, depth)?;
1233
    sp.done(&format!(
1234
        "{} {}; {}",
1235
        ui::done("Deepened by"),
1236
        plural(n, "change"),
1237
        if repo.is_shallow() {
1238
            "still shallow".to_string()
1239
        } else {
1240
            ui::ok("history is now complete")
1241
        }
1242
    ));
1243
    Ok(0)
1244
}
1245
1246
/// `r`, with a note when the remote's own token was refused while another
1247
/// sign-in for its hub would do (oxygen#155).
1248
fn noted<T>(repo: &oxy_core::repo::Repo, remote: &str, r: oxy_core::repo::Result<T>) -> Result<T> {
1249
    r.map_err(|e| {
1250
        let m = e.to_string();
1251
        match repo.stale_token_note(remote, &m) {
1252
            Some(note) => anyhow::anyhow!("{m}\n  note: {note}"),
1253
            None => e.into(),
1254
        }
1255
    })
1256
}