OXYGENHUB
# d3a6a9b37d7e oxygen / src / cli / sync.rs
18.1 KiB · Rust · last changed Unbound hosted repositories adopt the first pusher's id 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.snapshot("oxy pull")?.base_snapshot.is_conflicted();
200
        println!(
201
            "{}",
202
            json::obj([
203
                ("remote", json::s(&remote)),
204
                ("heads", json::hashes(&r.heads)),
205
                ("changed", json::b(r.changed)),
206
                ("checked_out", json::b(r.checked_out)),
207
                ("conflicted", json::b(conflicted))
208
            ])
209
        );
210
        return Ok(0);
211
    }
212
    if !r.changed {
213
        sp.done(&format!(
214
            "{} {}",
215
            ui::ok(ui::glyph("✔", "*")),
216
            ui::dim("Already up to date.")
217
        ));
218
        return Ok(0);
219
    }
220
    sp.done(&format!(
221
        "{} {} now has {}{}",
222
        ui::done("Pulled:"),
223
        ui::bookmark(&repo.config.bookmark),
224
        plural(r.heads.len(), "head"),
225
        ui::dim(if r.checked_out {
226
            "; working copy updated"
227
        } else {
228
            "; run oxy checkout to update the working copy"
229
        })
230
    ));
231
    let snap = repo.snapshot("oxy pull")?;
232
    if snap.base_snapshot.is_conflicted() {
233
        println!(
234
            "  {} {}",
235
            ui::caution(),
236
            ui::warn("the result has conflicts; see oxy status")
237
        );
238
    }
239
    Ok(0)
240
}
241
242
pub fn push(
243
    remote: Option<&str>,
244
    force: bool,
245
    bookmark: Option<&str>,
246
    as_json: bool,
247
) -> Result<i32> {
248
    let repo = open()?;
249
    let remote = remote_name(remote);
250
    let sp = ui::spinner(&format!("Pushing to {remote}…"));
251
    let results = match bookmark {
252
        Some(b) => repo.push_bookmark(&remote, b, force)?,
253
        None => repo.push(&remote, force)?,
254
    };
255
    sp.clear();
256
    if as_json {
257
        println!(
258
            "{}",
259
            json::arr(results.iter().map(|r| json::obj([
260
                ("name", json::s(&r.name)),
261
                ("ok", json::b(r.ok)),
262
                ("reason", json::s(&r.reason))
263
            ])))
264
        );
265
        return Ok(if results.iter().all(|r| r.ok) { 0 } else { 1 });
266
    }
267
    let mut code = 0;
268
    for r in results {
269
        if r.ok {
270
            println!(
271
                "{} {}{}{}",
272
                ui::done("Pushed"),
273
                ui::id(format!("{remote}/{}", r.name)),
274
                ui::sep(),
275
                ui::dim(if r.reason.is_empty() {
276
                    "updated"
277
                } else {
278
                    r.reason.as_str()
279
                })
280
            );
281
        } else {
282
            println!(
283
                "{} {} {}{}{}",
284
                ui::err(ui::glyph("✖", "x")),
285
                ui::err("Rejected"),
286
                ui::id(format!("{remote}/{}", r.name)),
287
                ui::sep(),
288
                r.reason
289
            );
290
            code = 1;
291
        }
292
    }
293
    Ok(code)
294
}
295
296
pub fn serve(
297
    root: PathBuf,
298
    bind: &str,
299
    tokens: Vec<String>,
300
    tls: Option<(PathBuf, PathBuf)>,
301
) -> Result<i32> {
302
    let listener = std::net::TcpListener::bind(bind).with_context(|| format!("binding {bind}"))?;
303
    let root = root.canonicalize()?;
304
    let tls = tls.map(|(cert, key)| oxy_server::TlsFiles { cert, key });
305
    println!(
306
        "Serving repositories under {} on {}://{bind}/<repo>/oxy/v1/ ({})",
307
        root.display(),
308
        if tls.is_some() { "https" } else { "http" },
309
        if tokens.is_empty() {
310
            "open: no push tokens configured"
311
        } else {
312
            "push requires a token"
313
        }
314
    );
315
    if tls.is_none()
316
        && !bind.starts_with("127.")
317
        && !bind.starts_with("localhost")
318
        && !bind.starts_with("[::1]")
319
    {
320
        eprintln!("warning: serving plain HTTP on a non-loopback address; put a TLS terminator in front or pass --tls-cert/--tls-key");
321
    }
322
    oxy_server::run_blocking(listener, oxy_server::ServerConfig { root, tokens, tls })?;
323
    Ok(0)
324
}
325
326
pub fn git_export(git_dir: &std::path::Path, branch: Option<&str>) -> Result<i32> {
327
    let repo = open()?;
328
    let sp = ui::spinner("Exporting to Git…");
329
    let r = match branch {
330
        Some(b) => repo.git_export_as(git_dir, b)?,
331
        None => repo.git_export(git_dir)?,
332
    };
333
    sp.clear();
334
    println!(
335
        "{} {} to {} as branch {} {}",
336
        ui::done("Exported"),
337
        plural(r.exported, "new state"),
338
        ui::path(git_dir.display()),
339
        ui::bookmark(&r.branch),
340
        ui::dim(format!(
341
            "(currently {}; {} already exported)",
342
            plural(r.heads, "head"),
343
            r.skipped
344
        ))
345
    );
346
    Ok(0)
347
}
348
349
pub fn git_fetch(url: &str, branch: &str) -> Result<i32> {
350
    let repo = open()?;
351
    let sp = ui::spinner(&format!("Fetching {url} {branch}…"));
352
    let r = repo.git_fetch(url, branch)?;
353
    sp.clear();
354
    if r.commits == 0 {
355
        println!(
356
            "{} {}",
357
            ui::done("Fetched"),
358
            ui::dim(format!("nothing new from {url} {branch}"))
359
        );
360
    } else {
361
        println!(
362
            "{} {} ({} merges) from {} {}{}{} now has {}",
363
            ui::done("Fetched"),
364
            plural(r.commits, "new commit"),
365
            r.merges,
366
            ui::path(url),
367
            ui::bookmark(branch),
368
            ui::sep(),
369
            ui::bookmark(&repo.config.bookmark),
370
            plural(r.heads, "head")
371
        );
372
        if !repo.snapshot("oxy git fetch")?.changed {
373
            println!("  {} {}", ui::note(), ui::dim("working copy updated"));
374
        } else {
375
            println!(
376
                "  {} {}",
377
                ui::note(),
378
                ui::dim("run oxy checkout to update the working copy")
379
            );
380
        }
381
    }
382
    if r.submodules > 0 {
383
        println!(
384
            "  {} {}",
385
            ui::note(),
386
            ui::dim(format!(
387
                "{} submodule {} imported as subrepos with Git pins",
388
                r.submodules,
389
                if r.submodules == 1 {
390
                    "entry"
391
                } else {
392
                    "entries"
393
                }
394
            ))
395
        );
396
    }
397
    Ok(0)
398
}
399
400
pub fn git_push(url: &str, branch: &str) -> Result<i32> {
401
    let repo = open()?;
402
    let sp = ui::spinner(&format!("Pushing to {url} {branch}…"));
403
    let r = repo.git_push(url, branch)?;
404
    sp.clear();
405
    println!(
406
        "{} {} to {} {} {}",
407
        ui::done("Pushed"),
408
        plural(r.exported, "new commit"),
409
        ui::path(url),
410
        ui::bookmark(&r.branch),
411
        ui::dim(format!("({} already there)", r.skipped))
412
    );
413
    Ok(0)
414
}
415
416
pub fn git_import(git_dir: &std::path::Path, dest: &std::path::Path, branch: &str) -> Result<i32> {
417
    let repo = Repo::init(dest)?;
418
    let started = std::time::Instant::now();
419
    let sp = ui::spinner(&format!("Importing {} {branch}…", git_dir.display()));
420
    let r = repo.import_git(git_dir, branch)?;
421
    sp.clear();
422
    println!(
423
        "{} {} ({} merges) from {} {} into {} {}",
424
        ui::done("Imported"),
425
        plural(r.commits, "commit"),
426
        r.merges,
427
        ui::path(git_dir.display()),
428
        ui::bookmark(branch),
429
        ui::path(repo.root().display()),
430
        ui::dim(format!(
431
            "in {:.1?}; {} now has {}",
432
            started.elapsed(),
433
            repo.config.bookmark,
434
            plural(r.heads, "head")
435
        ))
436
    );
437
    if r.submodules > 0 {
438
        println!(
439
            "  {} {}",
440
            ui::note(),
441
            ui::dim(format!(
442
                "{} submodule {} imported as subrepos with Git pins",
443
                r.submodules,
444
                if r.submodules == 1 {
445
                    "entry"
446
                } else {
447
                    "entries"
448
                }
449
            ))
450
        );
451
    }
452
    Ok(0)
453
}
454
455
pub fn submodule_add(url: &str, path: &str, bookmark: Option<&str>) -> Result<i32> {
456
    let repo = open()?;
457
    let sp = ui::spinner(&format!("Cloning {url} into {path}…"));
458
    let pin = repo.submodule_add(url, path, bookmark)?;
459
    sp.done(&format!(
460
        "{} subrepo {} {} {}",
461
        ui::done("Added"),
462
        ui::path(path),
463
        ui::dim(ui::glyph("→", "->")),
464
        ui::dim(pin.summary())
465
    ));
466
    println!(
467
        "  {} {}",
468
        ui::note(),
469
        ui::dim("recorded in .oxymodules; commit to pin it")
470
    );
471
    Ok(0)
472
}
473
474
pub fn submodule_update(init: bool, paths: &[String]) -> Result<i32> {
475
    let repo = open()?;
476
    let sp = ui::spinner("Updating subrepos…");
477
    let results = repo.submodule_update(init, paths)?;
478
    sp.clear();
479
    if results.is_empty() {
480
        println!("{}", ui::dim("No subrepos."));
481
    }
482
    let mut code = 0;
483
    for (path, r) in results {
484
        use oxy_core::repo::SubrepoSync::*;
485
        let line = match r {
486
            Updated => ui::done("Updated"),
487
            UpToDate => ui::ok(format!("{} up to date", ui::glyph("✔", "*"))),
488
            Missing => {
489
                code = 1;
490
                ui::warn(format!(
491
                    "{} missing (pass --init to clone)",
492
                    ui::glyph("⚠", "!")
493
                ))
494
            }
495
            Dirty => {
496
                code = 1;
497
                ui::warn(format!(
498
                    "{} has uncommitted changes; left as is",
499
                    ui::glyph("⚠", "!")
500
                ))
501
            }
502
            Failed(e) => {
503
                code = 1;
504
                ui::err(format!("{} {e}", ui::glyph("✖", "x")))
505
            }
506
        };
507
        println!("{line} {}", ui::path(path));
508
    }
509
    Ok(code)
510
}
511
512
fn pin_json(p: &oxy_core::subrepo::Pin) -> String {
513
    match p {
514
        oxy_core::subrepo::Pin::Oxy {
515
            repo,
516
            heads,
517
            bookmark,
518
        } => json::obj([
519
            ("kind", json::s("oxy")),
520
            (
521
                "repo",
522
                json::s(&repo.iter().map(|b| format!("{b:02x}")).collect::<String>()),
523
            ),
524
            ("heads", json::hashes(heads)),
525
            ("bookmark", json::s(bookmark)),
526
        ]),
527
        oxy_core::subrepo::Pin::Git { sha, branch } => json::obj([
528
            ("kind", json::s("git")),
529
            ("sha", json::s(sha)),
530
            ("branch", json::s(branch)),
531
        ]),
532
    }
533
}
534
535
pub fn submodule_status(as_json: bool) -> Result<i32> {
536
    let repo = open()?;
537
    let subs = repo.subrepos()?;
538
    if as_json {
539
        println!(
540
            "{}",
541
            json::arr(subs.iter().map(|s| {
542
                json::obj([
543
                    ("path", json::s(&s.path)),
544
                    ("pin", pin_json(&s.pin)),
545
                    ("actual", json::opt(s.actual.as_ref(), pin_json)),
546
                    ("at_pin", json::b(s.actual.as_ref() == Some(&s.pin))),
547
                    ("dirty", json::b(s.dirty)),
548
                    ("url", json::opt(s.url.as_deref(), json::s)),
549
                ])
550
            }))
551
        );
552
        return Ok(0);
553
    }
554
    if subs.is_empty() {
555
        println!("{}", ui::dim("No subrepos."));
556
    }
557
    for s in subs {
558
        let state = match (&s.actual, s.dirty) {
559
            (None, _) => ui::warn(format!("{} missing", ui::glyph("⚠", "!"))),
560
            (Some(a), dirty) if *a == s.pin => {
561
                if dirty {
562
                    ui::warn(format!(
563
                        "{} at pin, uncommitted changes",
564
                        ui::glyph("✎", "~")
565
                    ))
566
                } else {
567
                    ui::ok(format!("{} at pin", ui::glyph("✔", "*")))
568
                }
569
            }
570
            (Some(a), _) => ui::warn(format!("{} at {}", ui::glyph("↕", "<>"), a.summary())),
571
        };
572
        println!(
573
            "{} {}{}{}{}{state}",
574
            ui::ok(ui::glyph("⧉", "#")),
575
            ui::path(&s.path),
576
            ui::sep(),
577
            ui::dim(s.pin.summary()),
578
            ui::sep()
579
        );
580
        if let Some(u) = s.url {
581
            println!("    {} {}", ui::dim(ui::glyph("↳", "->")), ui::dim(u));
582
        }
583
    }
584
    Ok(0)
585
}
586
587
pub fn submodule_remove(path: &str) -> Result<i32> {
588
    let repo = open()?;
589
    repo.submodule_remove(path)?;
590
    println!(
591
        "{} subrepo {} {}",
592
        ui::done("Removed"),
593
        ui::path(path),
594
        ui::dim("(directory left in place; commit to record the removal)")
595
    );
596
    Ok(0)
597
}
598
599
pub fn deepen(remote: &str, depth: u32) -> Result<i32> {
600
    let repo = open()?;
601
    if !repo.is_shallow() {
602
        println!(
603
            "{}",
604
            ui::dim("This repository is not shallow; nothing to deepen.")
605
        );
606
        return Ok(0);
607
    }
608
    let sp = ui::spinner(&format!("Fetching {depth} more levels from {remote}…"));
609
    let n = repo.deepen(remote, depth)?;
610
    sp.done(&format!(
611
        "{} {}; {}",
612
        ui::ok("Deepened by"),
613
        plural(n, "change"),
614
        if repo.is_shallow() {
615
            "still shallow".to_string()
616
        } else {
617
            ui::ok("history is now complete")
618
        }
619
    ));
620
    Ok(0)
621
}