OXYGENHUB
# 61e3bce7ea5b oxygen / src / cli / sync.rs
19.1 KiB · Rust · last changed Status snapshots skip files unchanged since the manifest 1 month 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
) -> Result<i32> {
18
    let dest = dest.unwrap_or_else(|| {
19
        let base = url
20
            .trim_end_matches('/')
21
            .rsplit('/')
22
            .next()
23
            .unwrap_or("repo");
24
        PathBuf::from(base)
25
    });
26
    let max_blob = max_blob.map(oxy_core::repo::parse_size).transpose()?;
27
    let sp = ui::spinner(&format!("Cloning {url}…"));
28
    let repo = match depth {
29
        Some(d) => Repo::clone_shallow(url, &dest, d, max_blob, sparse)?,
30
        None => Repo::clone_with(url, &dest, max_blob, sparse)?,
31
    };
32
    let heads = repo.bookmark_heads()?;
33
    sp.done(&format!(
34
        "{} into {} {}",
35
        ui::done("Cloned"),
36
        ui::path(repo.root().display()),
37
        ui::dim(format!(
38
            "({url}, {} with {})",
39
            ui::bookmark(&repo.config.bookmark),
40
            plural(heads.len(), "head")
41
        ))
42
    ));
43
    Ok(0)
44
}
45
46
pub fn remote_add(name: &str, url: &str) -> Result<i32> {
47
    let mut repo = open()?;
48
    repo.config.set(&format!("remote.{name}.url"), url)?;
49
    repo.save_config()?;
50
    println!(
51
        "{} remote {} {} {url}",
52
        ui::done("Added"),
53
        ui::bookmark(name),
54
        ui::dim(ui::glyph("→", "->"))
55
    );
56
    Ok(0)
57
}
58
59
pub fn remote_remove(name: &str) -> Result<i32> {
60
    let mut repo = open()?;
61
    repo.config.set(&format!("remote.{name}.url"), "")?;
62
    repo.save_config()?;
63
    println!("{} remote {}", ui::done("Removed"), ui::bookmark(name));
64
    Ok(0)
65
}
66
67
pub fn remote_list(as_json: bool) -> Result<i32> {
68
    let repo = open()?;
69
    let view = repo.view()?;
70
    if as_json {
71
        let items = repo.config.remotes.iter().map(|(name, url)| {
72
            let bms = view.remotes.get(name).map(|b| {
73
                json::arr(b.iter().map(|(bm, st)| {
74
                    let heads = repo
75
                        .store()
76
                        .get_state(st)
77
                        .map(|s| s.heads)
78
                        .unwrap_or_default();
79
                    json::obj([("name", json::s(bm)), ("heads", json::hashes(&heads))])
80
                }))
81
            });
82
            json::obj([
83
                ("name", json::s(name)),
84
                ("url", json::s(url)),
85
                ("bookmarks", bms.unwrap_or_else(|| "[]".into())),
86
            ])
87
        });
88
        println!("{}", json::arr(items));
89
        return Ok(0);
90
    }
91
    if repo.config.remotes.is_empty() {
92
        println!(
93
            "{}",
94
            ui::dim("No remotes; add one with oxy remote add <name> <url>.")
95
        );
96
    }
97
    for (name, url) in &repo.config.remotes {
98
        println!(
99
            "{} {}{}{}",
100
            ui::ok(ui::glyph("⇄", "<>")),
101
            ui::bookmark(name),
102
            ui::sep(),
103
            ui::dim(url)
104
        );
105
        if let Some(bms) = view.remotes.get(name) {
106
            for (bm, st) in bms {
107
                let heads = repo
108
                    .store()
109
                    .get_state(st)
110
                    .map(|s| s.heads.len())
111
                    .unwrap_or(0);
112
                println!(
113
                    "    {} {}{}{}",
114
                    ui::dim(ui::glyph("↳", "->")),
115
                    ui::id(format!("{name}/{bm}")),
116
                    ui::sep(),
117
                    ui::dim(plural(heads, "head"))
118
                );
119
            }
120
        }
121
    }
122
    Ok(0)
123
}
124
125
fn remote_name(remote: Option<&str>) -> String {
126
    remote.unwrap_or("origin").to_string()
127
}
128
129
pub fn fetch(remote: Option<&str>, as_json: bool) -> Result<i32> {
130
    let repo = open()?;
131
    let remote = remote_name(remote);
132
    let sp = ui::spinner(&format!("Fetching from {remote}…"));
133
    let report = repo.fetch(&remote)?;
134
    sp.clear();
135
    if as_json {
136
        println!(
137
            "{}",
138
            json::obj([
139
                ("remote", json::s(&remote)),
140
                (
141
                    "refs",
142
                    json::arr(report.refs.iter().map(|(name, heads, n)| json::obj([
143
                        ("name", json::s(name)),
144
                        ("heads", json::hashes(heads)),
145
                        ("new", json::n(*n))
146
                    ])))
147
                ),
148
                ("new_tags", json::n(report.new_tags)),
149
                ("unverified", json::b(report.unverified)),
150
            ])
151
        );
152
        return Ok(0);
153
    }
154
    if report.refs.is_empty() {
155
        println!(
156
            "{} {}",
157
            ui::done("Fetched"),
158
            ui::dim(format!("nothing new from {remote}"))
159
        );
160
    }
161
    for (name, heads, n) in &report.refs {
162
        let what = if *n == 0 {
163
            ui::dim("up to date")
164
        } else {
165
            ui::ok(plural(*n, "new change"))
166
        };
167
        println!(
168
            "{} {}{}{}{}{what}",
169
            ui::done("Fetched"),
170
            ui::id(format!("{remote}/{name}")),
171
            ui::sep(),
172
            ui::dim(plural(heads.len(), "head")),
173
            ui::sep()
174
        );
175
    }
176
    if report.new_tags > 0 {
177
        println!(
178
            "  {} {}",
179
            ui::note(),
180
            ui::dim(format!("{} adopted", plural(report.new_tags, "tag")))
181
        );
182
    }
183
    if report.unverified {
184
        eprintln!(
185
            "{} {remote} has no trust policy; nothing fetched from it was verified",
186
            ui::caution()
187
        );
188
    }
189
    Ok(0)
190
}
191
192
pub fn pull(remote: Option<&str>, as_json: bool) -> Result<i32> {
193
    let repo = open()?;
194
    let remote = remote_name(remote);
195
    let sp = ui::spinner(&format!("Pulling from {remote}…"));
196
    let r = repo.pull(&remote)?;
197
    if as_json {
198
        sp.clear();
199
        let conflicted = repo
200
            .snapshot_status("oxy pull")?
201
            .base_snapshot
202
            .is_conflicted();
203
        println!(
204
            "{}",
205
            json::obj([
206
                ("remote", json::s(&remote)),
207
                ("heads", json::hashes(&r.heads)),
208
                ("changed", json::b(r.changed)),
209
                ("checked_out", json::b(r.checked_out)),
210
                ("conflicted", json::b(conflicted))
211
            ])
212
        );
213
        return Ok(0);
214
    }
215
    if !r.changed {
216
        sp.done(&format!(
217
            "{} {}",
218
            ui::ok(ui::glyph("✔", "*")),
219
            ui::dim("Already up to date.")
220
        ));
221
        return Ok(0);
222
    }
223
    sp.done(&format!(
224
        "{} {} now has {}{}",
225
        ui::done("Pulled:"),
226
        ui::bookmark(&repo.config.bookmark),
227
        plural(r.heads.len(), "head"),
228
        ui::dim(if r.checked_out {
229
            "; working copy updated"
230
        } else {
231
            "; run oxy checkout to update the working copy"
232
        })
233
    ));
234
    let snap = repo.snapshot("oxy pull")?;
235
    if snap.base_snapshot.is_conflicted() {
236
        println!(
237
            "  {} {}",
238
            ui::caution(),
239
            ui::warn("the result has conflicts; see oxy status")
240
        );
241
    }
242
    Ok(0)
243
}
244
245
pub fn push(
246
    remote: Option<&str>,
247
    force: bool,
248
    bookmark: Option<&str>,
249
    as_json: bool,
250
) -> Result<i32> {
251
    let repo = open()?;
252
    let remote = remote_name(remote);
253
    let sp = ui::spinner(&format!("Pushing to {remote}…"));
254
    let results = match bookmark {
255
        Some(b) => repo.push_bookmark(&remote, b, force)?,
256
        None => repo.push(&remote, force)?,
257
    };
258
    sp.clear();
259
    if as_json {
260
        println!(
261
            "{}",
262
            json::arr(results.iter().map(|r| json::obj([
263
                ("name", json::s(&r.name)),
264
                ("ok", json::b(r.ok)),
265
                ("reason", json::s(&r.reason))
266
            ])))
267
        );
268
        return Ok(if results.iter().all(|r| r.ok) { 0 } else { 1 });
269
    }
270
    let mut code = 0;
271
    for r in results {
272
        if r.ok {
273
            println!(
274
                "{} {}{}{}",
275
                ui::done("Pushed"),
276
                ui::id(format!("{remote}/{}", r.name)),
277
                ui::sep(),
278
                ui::dim(if r.reason.is_empty() {
279
                    "updated"
280
                } else {
281
                    r.reason.as_str()
282
                })
283
            );
284
        } else {
285
            println!(
286
                "{} {} {}{}{}",
287
                ui::err(ui::glyph("✖", "x")),
288
                ui::err("Rejected"),
289
                ui::id(format!("{remote}/{}", r.name)),
290
                ui::sep(),
291
                r.reason
292
            );
293
            code = 1;
294
        }
295
    }
296
    Ok(code)
297
}
298
299
pub fn serve(
300
    root: PathBuf,
301
    bind: &str,
302
    tokens: Vec<String>,
303
    tls: Option<(PathBuf, PathBuf)>,
304
) -> Result<i32> {
305
    let listener = std::net::TcpListener::bind(bind).with_context(|| format!("binding {bind}"))?;
306
    let root = root.canonicalize()?;
307
    let tls = tls.map(|(cert, key)| oxy_server::TlsFiles { cert, key });
308
    println!(
309
        "Serving repositories under {} on {}://{bind}/<repo>/oxy/v1/ ({})",
310
        root.display(),
311
        if tls.is_some() { "https" } else { "http" },
312
        if tokens.is_empty() {
313
            "open: no push tokens configured"
314
        } else {
315
            "push requires a token"
316
        }
317
    );
318
    if tls.is_none()
319
        && !bind.starts_with("127.")
320
        && !bind.starts_with("localhost")
321
        && !bind.starts_with("[::1]")
322
    {
323
        eprintln!("warning: serving plain HTTP on a non-loopback address; put a TLS terminator in front or pass --tls-cert/--tls-key");
324
    }
325
    oxy_server::run_blocking(listener, oxy_server::ServerConfig { root, tokens, tls })?;
326
    Ok(0)
327
}
328
329
pub fn git_export(git_dir: &std::path::Path, branch: Option<&str>) -> Result<i32> {
330
    let repo = open()?;
331
    let sp = ui::spinner("Exporting to Git…");
332
    let r = match branch {
333
        Some(b) => repo.git_export_as(git_dir, b)?,
334
        None => repo.git_export(git_dir)?,
335
    };
336
    sp.clear();
337
    println!(
338
        "{} {} to {} as branch {} {}",
339
        ui::done("Exported"),
340
        plural(r.exported, "new state"),
341
        ui::path(git_dir.display()),
342
        ui::bookmark(&r.branch),
343
        ui::dim(format!(
344
            "(currently {}; {} already exported)",
345
            plural(r.heads, "head"),
346
            r.skipped
347
        ))
348
    );
349
    Ok(0)
350
}
351
352
pub fn git_fetch(url: &str, branch: &str) -> Result<i32> {
353
    let repo = open()?;
354
    let sp = ui::spinner(&format!("Fetching {url} {branch}…"));
355
    let r = repo.git_fetch(url, branch)?;
356
    sp.clear();
357
    if r.commits == 0 {
358
        println!(
359
            "{} {}",
360
            ui::done("Fetched"),
361
            ui::dim(format!("nothing new from {url} {branch}"))
362
        );
363
    } else {
364
        println!(
365
            "{} {} ({} merges) from {} {}{}{} now has {}",
366
            ui::done("Fetched"),
367
            plural(r.commits, "new commit"),
368
            r.merges,
369
            ui::path(url),
370
            ui::bookmark(branch),
371
            ui::sep(),
372
            ui::bookmark(&repo.config.bookmark),
373
            plural(r.heads, "head")
374
        );
375
        if !repo.snapshot("oxy git fetch")?.changed {
376
            println!("  {} {}", ui::note(), ui::dim("working copy updated"));
377
        } else {
378
            println!(
379
                "  {} {}",
380
                ui::note(),
381
                ui::dim("run oxy checkout to update the working copy")
382
            );
383
        }
384
    }
385
    if r.submodules > 0 {
386
        println!(
387
            "  {} {}",
388
            ui::note(),
389
            ui::dim(format!(
390
                "{} submodule {} imported as subrepos with Git pins",
391
                r.submodules,
392
                if r.submodules == 1 {
393
                    "entry"
394
                } else {
395
                    "entries"
396
                }
397
            ))
398
        );
399
    }
400
    Ok(0)
401
}
402
403
pub fn git_push(url: &str, branch: &str) -> Result<i32> {
404
    let repo = open()?;
405
    let sp = ui::spinner(&format!("Pushing to {url} {branch}…"));
406
    let r = repo.git_push(url, branch)?;
407
    sp.clear();
408
    println!(
409
        "{} {} to {} {} {}",
410
        ui::done("Pushed"),
411
        plural(r.exported, "new commit"),
412
        ui::path(url),
413
        ui::bookmark(&r.branch),
414
        ui::dim(format!("({} already there)", r.skipped))
415
    );
416
    Ok(0)
417
}
418
419
/// `oxy git verify`: exit 1 if any commit's tree is not reproduced.
420
pub fn git_verify(git_dir: &std::path::Path, branch: &str) -> Result<i32> {
421
    let repo = open()?;
422
    let started = std::time::Instant::now();
423
    let sp = ui::spinner(&format!(
424
        "Rebuilding every commit of {} {branch}…",
425
        git_dir.display()
426
    ));
427
    let r = repo.verify_git_import(git_dir, branch)?;
428
    sp.clear();
429
    for (_, what) in &r.mismatches {
430
        println!("{} {what}", ui::err(ui::glyph("✖", "x")));
431
    }
432
    if r.mismatches.is_empty() {
433
        println!(
434
            "{}",
435
            ui::done(format!(
436
                "Every commit reproduced: {} commits, {} files compared, in {:.1}s",
437
                r.commits,
438
                r.files_compared,
439
                started.elapsed().as_secs_f64()
440
            ))
441
        );
442
        Ok(0)
443
    } else {
444
        println!(
445
            "{} differences in {} commits checked",
446
            r.mismatches.len(),
447
            r.commits
448
        );
449
        Ok(1)
450
    }
451
}
452
453
pub fn git_import(git_dir: &std::path::Path, dest: &std::path::Path, branch: &str) -> Result<i32> {
454
    let repo = Repo::init(dest)?;
455
    let started = std::time::Instant::now();
456
    let sp = ui::spinner(&format!("Importing {} {branch}…", git_dir.display()));
457
    let r = repo.import_git(git_dir, branch)?;
458
    sp.clear();
459
    println!(
460
        "{} {} ({} merges) from {} {} into {} {}",
461
        ui::done("Imported"),
462
        plural(r.commits, "commit"),
463
        r.merges,
464
        ui::path(git_dir.display()),
465
        ui::bookmark(branch),
466
        ui::path(repo.root().display()),
467
        ui::dim(format!(
468
            "in {:.1?}; {} now has {}",
469
            started.elapsed(),
470
            repo.config.bookmark,
471
            plural(r.heads, "head")
472
        ))
473
    );
474
    if r.submodules > 0 {
475
        println!(
476
            "  {} {}",
477
            ui::note(),
478
            ui::dim(format!(
479
                "{} submodule {} imported as subrepos with Git pins",
480
                r.submodules,
481
                if r.submodules == 1 {
482
                    "entry"
483
                } else {
484
                    "entries"
485
                }
486
            ))
487
        );
488
    }
489
    Ok(0)
490
}
491
492
pub fn submodule_add(url: &str, path: &str, bookmark: Option<&str>) -> Result<i32> {
493
    let repo = open()?;
494
    let sp = ui::spinner(&format!("Cloning {url} into {path}…"));
495
    let pin = repo.submodule_add(url, path, bookmark)?;
496
    sp.done(&format!(
497
        "{} subrepo {} {} {}",
498
        ui::done("Added"),
499
        ui::path(path),
500
        ui::dim(ui::glyph("→", "->")),
501
        ui::dim(pin.summary())
502
    ));
503
    println!(
504
        "  {} {}",
505
        ui::note(),
506
        ui::dim("recorded in .oxymodules; commit to pin it")
507
    );
508
    Ok(0)
509
}
510
511
pub fn submodule_update(init: bool, paths: &[String]) -> Result<i32> {
512
    let repo = open()?;
513
    let sp = ui::spinner("Updating subrepos…");
514
    let results = repo.submodule_update(init, paths)?;
515
    sp.clear();
516
    if results.is_empty() {
517
        println!("{}", ui::dim("No subrepos."));
518
    }
519
    let mut code = 0;
520
    for (path, r) in results {
521
        use oxy_core::repo::SubrepoSync::*;
522
        let line = match r {
523
            Updated => ui::done("Updated"),
524
            UpToDate => ui::ok(format!("{} up to date", ui::glyph("✔", "*"))),
525
            Missing => {
526
                code = 1;
527
                ui::warn(format!(
528
                    "{} missing (pass --init to clone)",
529
                    ui::glyph("⚠", "!")
530
                ))
531
            }
532
            Dirty => {
533
                code = 1;
534
                ui::warn(format!(
535
                    "{} has uncommitted changes; left as is",
536
                    ui::glyph("⚠", "!")
537
                ))
538
            }
539
            Failed(e) => {
540
                code = 1;
541
                ui::err(format!("{} {e}", ui::glyph("✖", "x")))
542
            }
543
        };
544
        println!("{line} {}", ui::path(path));
545
    }
546
    Ok(code)
547
}
548
549
fn pin_json(p: &oxy_core::subrepo::Pin) -> String {
550
    match p {
551
        oxy_core::subrepo::Pin::Oxy {
552
            repo,
553
            heads,
554
            bookmark,
555
        } => json::obj([
556
            ("kind", json::s("oxy")),
557
            (
558
                "repo",
559
                json::s(&repo.iter().map(|b| format!("{b:02x}")).collect::<String>()),
560
            ),
561
            ("heads", json::hashes(heads)),
562
            ("bookmark", json::s(bookmark)),
563
        ]),
564
        oxy_core::subrepo::Pin::Git { sha, branch } => json::obj([
565
            ("kind", json::s("git")),
566
            ("sha", json::s(sha)),
567
            ("branch", json::s(branch)),
568
        ]),
569
    }
570
}
571
572
pub fn submodule_status(as_json: bool) -> Result<i32> {
573
    let repo = open()?;
574
    let subs = repo.subrepos()?;
575
    if as_json {
576
        println!(
577
            "{}",
578
            json::arr(subs.iter().map(|s| {
579
                json::obj([
580
                    ("path", json::s(&s.path)),
581
                    ("pin", pin_json(&s.pin)),
582
                    ("actual", json::opt(s.actual.as_ref(), pin_json)),
583
                    ("at_pin", json::b(s.actual.as_ref() == Some(&s.pin))),
584
                    ("dirty", json::b(s.dirty)),
585
                    ("url", json::opt(s.url.as_deref(), json::s)),
586
                ])
587
            }))
588
        );
589
        return Ok(0);
590
    }
591
    if subs.is_empty() {
592
        println!("{}", ui::dim("No subrepos."));
593
    }
594
    for s in subs {
595
        let state = match (&s.actual, s.dirty) {
596
            (None, _) => ui::warn(format!("{} missing", ui::glyph("⚠", "!"))),
597
            (Some(a), dirty) if *a == s.pin => {
598
                if dirty {
599
                    ui::warn(format!(
600
                        "{} at pin, uncommitted changes",
601
                        ui::glyph("✎", "~")
602
                    ))
603
                } else {
604
                    ui::ok(format!("{} at pin", ui::glyph("✔", "*")))
605
                }
606
            }
607
            (Some(a), _) => ui::warn(format!("{} at {}", ui::glyph("↕", "<>"), a.summary())),
608
        };
609
        println!(
610
            "{} {}{}{}{}{state}",
611
            ui::ok(ui::glyph("⧉", "#")),
612
            ui::path(&s.path),
613
            ui::sep(),
614
            ui::dim(s.pin.summary()),
615
            ui::sep()
616
        );
617
        if let Some(u) = s.url {
618
            println!("    {} {}", ui::dim(ui::glyph("↳", "->")), ui::dim(u));
619
        }
620
    }
621
    Ok(0)
622
}
623
624
pub fn submodule_remove(path: &str) -> Result<i32> {
625
    let repo = open()?;
626
    repo.submodule_remove(path)?;
627
    println!(
628
        "{} subrepo {} {}",
629
        ui::done("Removed"),
630
        ui::path(path),
631
        ui::dim("(directory left in place; commit to record the removal)")
632
    );
633
    Ok(0)
634
}
635
636
pub fn deepen(remote: &str, depth: u32) -> Result<i32> {
637
    let repo = open()?;
638
    if !repo.is_shallow() {
639
        println!(
640
            "{}",
641
            ui::dim("This repository is not shallow; nothing to deepen.")
642
        );
643
        return Ok(0);
644
    }
645
    let sp = ui::spinner(&format!("Fetching {depth} more levels from {remote}…"));
646
    let n = repo.deepen(remote, depth)?;
647
    sp.done(&format!(
648
        "{} {}; {}",
649
        ui::ok("Deepened by"),
650
        plural(n, "change"),
651
        if repo.is_shallow() {
652
            "still shallow".to_string()
653
        } else {
654
            ui::ok("history is now complete")
655
        }
656
    ));
657
    Ok(0)
658
}