OXYGENHUB

Coverage / src/cli/sync.rs 38.8%

366 of 943 lines run, 33 of 146 functions · run #306 on main · run missed

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
115×
pub fn clone(
125×
    url: &str,
135×
    dest: Option<PathBuf>,
145×
    max_blob: Option<&str>,
155×
    sparse: &[String],
165×
    depth: Option<u32>,
175×
    recursive: bool,
185×
) -> Result<i32> {
195×
    let dest = dest.unwrap_or_else(|| {
200×
        let base = url
210×
            .trim_end_matches('/')
220×
            .rsplit('/')
230×
            .next()
240×
            .unwrap_or("repo");
250×
        PathBuf::from(base)
260×
    });
275×
    let max_blob = max_blob.map(oxy_core::repo::parse_size).transpose()?;
285×
    let sp = ui::spinner(&format!("Cloning {url}…"));
295×
    let repo = match depth {
300×
        Some(d) => Repo::clone_shallow(url, &dest, d, max_blob, sparse)?,
315×
        None => Repo::clone_with(url, &dest, max_blob, sparse)?,
32
    };
335×
    let heads = repo.bookmark_heads()?;
345×
    sp.done(&format!(
355×
        "{} into {} {}",
365×
        ui::done("Cloned"),
375×
        ui::path(repo.root().display()),
385×
        ui::dim(format!(
395×
            "({url}, {} with {})",
405×
            ui::bookmark(&repo.config.bookmark),
415×
            plural(heads.len(), "head")
425×
        ))
435×
    ));
445×
    let others = repo.remote_only_bookmarks()?.len();
455×
    if others > 0 {
460×
        println!(
470×
            "  {} {}",
480×
            ui::note(),
490×
            ui::dim(format!(
500×
                "origin has {} more: oxy bookmark list --all, then oxy bookmark switch NAME",
510×
                plural(others, "bookmark")
520×
            ))
530×
        );
545×
    }
555×
    let subrepos = repo.subrepos()?.len();
565×
    if subrepos == 0 {
575×
        return Ok(0);
580×
    }
590×
    if recursive {
600×
        let sp = ui::spinner("Cloning subrepos…");
610×
        let results = repo.submodule_update(true, &[])?;
620×
        sp.clear();
630×
        return Ok(print_subrepo_results(results));
640×
    }
650×
    println!(
66
        "  {} {}",
670×
        ui::note(),
680×
        ui::dim(format!(
69
            "{} not cloned: oxy submodule update --init (or clone with --recursive)",
700×
            plural(subrepos, "subrepo")
71
        ))
72
    );
730×
    Ok(0)
745×
}
75
76
/// After a switch or pull: say when subrepos are not at their pins.
775×
pub fn subrepos_behind(repo: &Repo) {
785×
    let Ok(subs) = repo.subrepos() else {
790×
        return;
80
    };
815×
    let stale = subs
825×
        .iter()
835×
        .filter(|s| s.committed && s.actual.as_ref() != Some(&s.pin))
845×
        .count();
855×
    if stale > 0 {
860×
        println!(
87
            "  {} {}",
880×
            ui::note(),
890×
            ui::dim(format!(
90
                "{} not at {} pin: oxy submodule update --init",
910×
                plural(stale, "subrepo"),
920×
                if stale == 1 { "its" } else { "their" }
93
            ))
94
        );
955×
    }
965×
}
97
98
/// Whether a URL names a store (a bucket or a directory in the store
99
/// layout) rather than a repository.
1002×
fn is_store_url(url: &str) -> bool {
1012×
    ["s3://", "gs://", "az://", "dir://"]
1022×
        .iter()
1035×
        .any(|p| url.starts_with(p))
1042×
}
105
106
/// Percent-encode what would break a query parameter.
1072×
fn encode_param(v: &str) -> String {
1082×
    let mut out = String::with_capacity(v.len());
10913×
    for b in v.bytes() {
11013×
        match b {
11110×
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' | b'/' | b':' => {
11213×
                out.push(b as char)
113
            }
1140×
            _ => out.push_str(&format!("%{b:02X}")),
115
        }
116
    }
1172×
    out
1182×
}
119
120
/// A store URL with settings added as query parameters, which is where a
121
/// store remote keeps them (`s3://b/p?profile=work&region=eu-west-2`).
1224×
fn url_with_settings(url: &str, settings: &[(&str, Option<String>)]) -> Result<String> {
1234×
    let given: Vec<(&str, &str)> = settings
1244×
        .iter()
12516×
        .filter_map(|(k, v)| v.as_deref().map(|v| (*k, v)))
1264×
        .collect();
1274×
    if given.is_empty() {
1282×
        return Ok(url.to_string());
1292×
    }
1302×
    if !is_store_url(url) || url.starts_with("dir://") {
1311×
        let names: Vec<String> = given.iter().map(|(k, _)| format!("--{k}")).collect();
1321×
        anyhow::bail!(
133
            "{} apply to a store remote (s3://, gs://, az://), not to {url}",
1341×
            names.join(", ")
135
        );
1361×
    }
1371×
    let mut out = url.to_string();
1382×
    for (k, v) in given {
1392×
        out.push(if out.contains('?') { '&' } else { '?' });
1402×
        out.push_str(k);
1412×
        out.push('=');
1422×
        out.push_str(&encode_param(v));
143
    }
1441×
    Ok(out)
1454×
}
146
1474×
pub fn remote_add(name: &str, url: &str, settings: &[(&str, Option<String>)]) -> Result<i32> {
1484×
    let mut repo = open()?;
1494×
    let url = url_with_settings(url, settings)?;
1503×
    let url = url.as_str();
1513×
    repo.config.set(
1523×
        &format!("remote.{name}.url"),
1533×
        &oxy_core::protocol::stored_url(url),
1540×
    )?;
1553×
    repo.save_config()?;
1563×
    println!(
157
        "{} remote {} {} {url}",
1583×
        ui::done("Added"),
1593×
        ui::bookmark(name),
1603×
        ui::dim(ui::glyph("→", "->"))
161
    );
1623×
    Ok(0)
1634×
}
164
165
/// `oxy remote check NAME`: reach the remote with the credentials in
166
/// hand, say what it holds, and, for a store, put it through the
167
/// conformance check under a probe prefix (left empty afterwards).
1683×
pub fn remote_check(name: &str) -> Result<i32> {
1693×
    let repo = open()?;
1703×
    let Some(url) = repo.config.remotes.get(name).cloned() else {
1710×
        anyhow::bail!("no remote {name:?}; oxy remote list shows the remotes there are");
172
    };
1733×
    let options = repo.options_for_url(&url);
1743×
    let mine = repo.id()?;
1753×
    let started = std::time::Instant::now();
1763×
    let sp = ui::spinner(&format!("Checking {name}…"));
1773×
    let Some(store) = oxy_core::protocol::open_store(&url, &options) else {
178
        // A hub or another repository: reach it and read its references.
1790×
        let (_, remote) = repo.open_remote_at(&url)?;
1800×
        let id = remote.repo_id()?;
1810×
        let refs = remote.refs()?;
1820×
        sp.done(&format!(
1830×
            "{} {} {}",
1840×
            ui::done("Reachable"),
1850×
            ui::bookmark(name),
1860×
            ui::dim(format!("({url}, {} ms)", started.elapsed().as_millis()))
1870×
        ));
1880×
        describe_holding(id, mine, refs.refs.len(), refs.tags.len(), None);
1890×
        return Ok(0);
190
    };
1913×
    let store = store.with_context(|| format!("opening the store of {name}"))?;
192
    // What it holds, in one listing and two reads.
1933×
    let marker = match store.get("oxygen.toml") {
1942×
        Ok(b) => Some(b),
1951×
        Err(oxy_core::strata::Error::NotFound(_)) => None,
1960×
        Err(e) => {
1970×
            sp.clear();
1980×
            return Err(anyhow::anyhow!("{e}"))
1990×
                .with_context(|| format!("reaching the store of {name}"));
200
        }
201
    };
2023×
    let reached = started.elapsed();
2033×
    let bundles = store.list("bundles/")?.len();
2043×
    let record = store.read_ref("refs")?;
2053×
    sp.done(&format!(
2063×
        "{} {} {}",
2073×
        ui::done("Reachable"),
2083×
        ui::bookmark(name),
2093×
        ui::dim(format!("({url}, {} ms)", reached.as_millis()))
2103×
    ));
2113×
    match marker {
2121×
        None => println!(
213
            "  {} {}",
2141×
            ui::note(),
2151×
            ui::dim("holds no repository yet: oxy push NAME binds it to this one")
216
        ),
217
        Some(_) => {
2182×
            let (id, bookmarks, tags) = match record {
2192×
                Some((bytes, _)) => {
2202×
                    let (id, resp) = oxy_core::protocol::wire::decode_refs(&bytes)?;
2212×
                    (id, resp.refs.len(), resp.tags.len())
222
                }
2230×
                None => (mine, 0, 0),
224
            };
2252×
            describe_holding(id, mine, bookmarks, tags, Some(bundles));
226
        }
227
    }
228
    // Then the behaviour a repository depends on, under a prefix of its own.
2293×
    let sp = ui::spinner("Checking that the store behaves…");
2303×
    let probe = format!("oxy-check-{}", std::process::id());
2313×
    let began = std::time::Instant::now();
2323×
    match oxy_core::strata::conformance(store.as_ref(), &probe) {
2333×
        Ok(passed) => {
2343×
            sp.clear();
23524×
            for p in &passed {
23624×
                println!("  {} {}", ui::ok(ui::glyph("✔", "*")), p);
23724×
            }
2383×
            println!(
239
                "{} {}",
2403×
                ui::done("Behaves"),
2413×
                ui::dim(format!(
242
                    "({} checks in {} ms; nothing left behind)",
2433×
                    passed.len(),
2443×
                    began.elapsed().as_millis()
245
                ))
246
            );
2473×
            Ok(0)
248
        }
2490×
        Err(e) => {
2500×
            sp.clear();
251
            // Leave nothing behind, even after a failed check.
2520×
            if let Ok(left) = store.list(&probe) {
2530×
                for entry in left {
2540×
                    let _ = store.delete(&entry.key);
2550×
                }
2560×
            }
2570×
            println!("{} {}", ui::err(ui::glyph("✘", "x")), e);
2580×
            println!(
259
                "  {} {}",
2600×
                ui::note(),
2610×
                ui::dim("a store that fails this cannot keep a repository safely; check the service and the credentials' permissions")
262
            );
2630×
            Ok(1)
264
        }
265
    }
2663×
}
267
2682×
fn describe_holding(
2692×
    id: [u8; 16],
2702×
    mine: [u8; 16],
2712×
    bookmarks: usize,
2722×
    tags: usize,
2732×
    bundles: Option<usize>,
2742×
) {
27512×
    let short: String = id[..6].iter().map(|b| format!("{b:02x}")).collect();
2762×
    let mut what = format!(
277
        "repository {short}: {} and {}",
2782×
        plural(bookmarks, "bookmark"),
2792×
        plural(tags, "tag")
280
    );
2812×
    if let Some(b) = bundles {
2822×
        what.push_str(&format!(" in {}", plural(b, "bundle")));
2832×
    }
2842×
    if id == mine {
2851×
        println!("  {} {}", ui::note(), ui::dim(format!("holds this {what}")));
2861×
    } else if id == oxy_core::protocol::UNBOUND_ID {
2870×
        println!("  {} {}", ui::note(), ui::dim("holds no repository yet"));
2881×
    } else {
2891×
        println!(
2901×
            "  {} {}",
2911×
            ui::caution(),
2921×
            ui::warn(format!("holds another {what}; a push would be refused"))
2931×
        );
2941×
    }
2952×
}
296
2971×
pub fn remote_remove(name: &str) -> Result<i32> {
2981×
    let mut repo = open()?;
2991×
    repo.remove_remote(name)?;
3001×
    println!("{} remote {}", ui::done("Removed"), ui::bookmark(name));
3011×
    Ok(0)
3021×
}
303
3042×
pub fn remote_list(as_json: bool) -> Result<i32> {
3052×
    let repo = open()?;
3062×
    let view = repo.view()?;
3072×
    if as_json {
3081×
        let items = repo.config.remotes.iter().map(|(name, url)| {
3091×
            let bms = view.remotes.get(name).map(|b| {
3101×
                json::arr(b.iter().map(|(bm, st)| {
3111×
                    let heads = repo
3121×
                        .store()
3131×
                        .get_state(st)
3141×
                        .map(|s| s.heads)
3151×
                        .unwrap_or_default();
3161×
                    json::obj([("name", json::s(bm)), ("heads", json::hashes(&heads))])
3171×
                }))
3181×
            });
3191×
            json::obj([
3201×
                ("name", json::s(name)),
3211×
                ("url", json::s(url)),
322
                // The CA file `remote.NAME.ca` names, for tools that talk to
323
                // the same hub (an editor's forge view, `oh`).
324
                (
3251×
                    "ca",
3261×
                    json::opt(repo.config.remote_cas.get(name), |c| json::s(c)),
327
                ),
3281×
                ("bookmarks", bms.unwrap_or_else(|| "[]".into())),
329
            ])
3301×
        });
3311×
        println!("{}", json::arr(items));
3321×
        return Ok(0);
3331×
    }
3341×
    if repo.config.remotes.is_empty() {
3350×
        println!(
3360×
            "{}",
3370×
            ui::dim("No remotes; add one with oxy remote add <name> <url>.")
3380×
        );
3391×
    }
3401×
    for (name, url) in &repo.config.remotes {
3411×
        println!(
342
            "{} {}{}{}",
3431×
            ui::ok(ui::glyph("⇄", "<>")),
3441×
            ui::bookmark(name),
3451×
            ui::sep(),
3461×
            ui::dim(url)
347
        );
3481×
        if let Some(bms) = view.remotes.get(name) {
3490×
            for (bm, st) in bms {
3500×
                let heads = repo
3510×
                    .store()
3520×
                    .get_state(st)
3530×
                    .map(|s| s.heads.len())
3540×
                    .unwrap_or(0);
3550×
                println!(
356
                    "    {} {}{}{}",
3570×
                    ui::dim(ui::glyph("↳", "->")),
3580×
                    ui::id(format!("{name}/{bm}")),
3590×
                    ui::sep(),
3600×
                    ui::dim(plural(heads, "head"))
361
                );
362
            }
3631×
        }
364
    }
3651×
    Ok(0)
3662×
}
367
368
/// The remote named, else `origin`, else the only remote there is.
36912×
fn remote_name(repo: &oxy_core::repo::Repo, remote: Option<&str>) -> String {
37012×
    remote.map_or_else(|| repo.default_remote(), str::to_string)
37112×
}
372
3731×
pub fn fetch(remote: Option<&str>, as_json: bool) -> Result<i32> {
3741×
    let repo = open()?;
3751×
    let remote = remote_name(&repo, remote);
3761×
    let sp = ui::spinner(&format!("Fetching from {remote}…"));
3771×
    let report = repo.fetch(&remote)?;
3781×
    sp.clear();
3791×
    if as_json {
3801×
        println!(
381
            "{}",
3821×
            json::obj([
3831×
                ("remote", json::s(&remote)),
384
                (
3851×
                    "refs",
3861×
                    json::arr(report.refs.iter().map(|(name, heads, n)| {
3871×
                        let t = repo.tracking(&remote, name).ok().flatten();
3881×
                        json::obj([
3891×
                            ("name", json::s(name)),
3901×
                            ("heads", json::hashes(heads)),
3911×
                            ("new", json::n(*n)),
392
                            (
3931×
                                "ahead",
3941×
                                t.map_or_else(|| "null".into(), |(a, _)| json::n(a)),
395
                            ),
396
                            (
3971×
                                "behind",
3981×
                                t.map_or_else(|| "null".into(), |(_, b)| json::n(b)),
399
                            ),
400
                        ])
4011×
                    }))
402
                ),
4031×
                ("new_tags", json::n(report.new_tags)),
4041×
                ("unverified", json::b(report.unverified)),
405
            ])
406
        );
4071×
        return Ok(0);
4080×
    }
4090×
    if report.refs.is_empty() {
4100×
        println!(
4110×
            "{} {}",
4120×
            ui::done("Fetched"),
4130×
            ui::dim(format!("nothing new from {remote}"))
4140×
        );
4150×
    }
4160×
    for (name, heads, n) in &report.refs {
417
        // What was fetched, then how the local bookmark stands against it:
418
        // "up to date" only when the two are the same (oxygen#56).
4190×
        let fetched =
4200×
            (*n > 0).then(|| format!("{}{}", ui::ok(plural(*n, "new change")), ui::sep()));
4210×
        let them = |k: usize| if k == 1 { "it" } else { "them" };
4220×
        let standing = match repo.tracking(&remote, name).ok().flatten() {
4230×
            None => ui::dim(format!("no local bookmark {name}")),
4240×
            Some((0, 0)) => ui::dim(format!("{name} up to date")),
4250×
            Some((0, b)) if *name == repo.config.bookmark => ui::warn(format!(
426
                "{name} is {} behind: oxy pull to take {}",
4270×
                plural(b, "change"),
4280×
                them(b)
429
            )),
4300×
            Some((0, b)) => ui::warn(format!(
431
                "{name} is {} behind: oxy bookmark switch {name} then oxy pull to take {}",
4320×
                plural(b, "change"),
4330×
                them(b)
434
            )),
4350×
            Some((a, 0)) => ui::dim(format!(
436
                "{name} is {} ahead: oxy push to send {}",
4370×
                plural(a, "change"),
4380×
                them(a)
439
            )),
4400×
            Some((a, b)) => ui::warn(format!(
441
                "{name} and {remote}/{name} have diverged ({a} here, {b} there): oxy pull to merge them"
442
            )),
443
        };
4440×
        let what = format!("{}{standing}", fetched.unwrap_or_default());
4450×
        println!(
446
            "{} {}{}{}{}{what}",
4470×
            ui::done("Fetched"),
4480×
            ui::id(format!("{remote}/{name}")),
4490×
            ui::sep(),
4500×
            ui::dim(plural(heads.len(), "head")),
4510×
            ui::sep()
452
        );
453
    }
4540×
    if report.new_tags > 0 {
4550×
        println!(
4560×
            "  {} {}",
4570×
            ui::note(),
4580×
            ui::dim(format!("{} adopted", plural(report.new_tags, "tag")))
4590×
        );
4600×
    }
4610×
    if report.unverified {
4620×
        eprintln!(
4630×
            "{} {remote} has no trust policy; nothing fetched from it was verified",
4640×
            ui::caution()
4650×
        );
4660×
    }
4670×
    Ok(0)
4681×
}
469
470
/// Pull acts on the current bookmark only: name the others the fetch
471
/// found behind their remote, which is easy to take for the current one
472
/// (oxygen#29).
4733×
fn others_behind(repo: &oxy_core::repo::Repo, remote: &str) {
4743×
    let Ok(bookmarks) = repo.bookmarks() else {
4750×
        return;
476
    };
4774×
    for b in bookmarks {
4784×
        let Some(n) = b.behind.filter(|n| *n > 0) else {
4794×
            continue;
480
        };
4810×
        if b.current {
4820×
            continue;
4830×
        }
4840×
        println!(
485
            "  {} {}",
4860×
            ui::note(),
4870×
            ui::dim(format!(
488
                "{} is {} behind {remote}/{}: oxy bookmark switch {} then oxy pull to take {}",
489
                b.name,
4900×
                plural(n, "change"),
491
                b.name,
492
                b.name,
4930×
                if n == 1 { "it" } else { "them" }
494
            ))
495
        );
496
    }
4973×
}
498
4994×
pub fn pull(remote: Option<&str>, as_json: bool) -> Result<i32> {
5004×
    let repo = open()?;
5014×
    let remote = remote_name(&repo, remote);
5024×
    let sp = ui::spinner(&format!("Pulling from {remote}…"));
5034×
    let r = repo.pull(&remote)?;
5044×
    if as_json {
5051×
        sp.clear();
5061×
        let conflicted = repo
5071×
            .snapshot_status("oxy pull")?
508
            .base_snapshot
5091×
            .is_conflicted();
5101×
        println!(
511
            "{}",
5121×
            json::obj([
5131×
                ("remote", json::s(&remote)),
5141×
                ("bookmark", json::s(&repo.config.bookmark)),
5151×
                ("heads", json::hashes(&r.heads)),
5161×
                ("changed", json::b(r.changed)),
5171×
                ("checked_out", json::b(r.checked_out)),
5181×
                ("conflicted", json::b(conflicted)),
5191×
                ("others", json::arr(r.others.iter().map(|o| json::s(o))))
520
            ])
521
        );
5221×
        return Ok(0);
5233×
    }
5243×
    if !r.changed {
5251×
        sp.done(&format!(
526
            "{} {}{}",
5271×
            ui::ok(ui::glyph("✔", "*")),
5281×
            ui::dim(format!(
529
                "{} is up to date with {remote}.",
530
                repo.config.bookmark
531
            )),
5321×
            if r.checked_out {
5330×
                ui::dim(" The working copy had fallen behind it and is updated.")
534
            } else {
5351×
                String::new()
536
            }
537
        ));
5381×
        others_pulled(&r.others);
5391×
        others_behind(&repo, &remote);
5401×
        if r.checked_out {
5410×
            super::workspace::where_now(&repo);
5421×
        }
5431×
        return Ok(0);
5442×
    }
5452×
    sp.done(&format!(
546
        "{} {} now has {}{}",
5472×
        ui::done("Pulled:"),
5482×
        ui::bookmark(&repo.config.bookmark),
5492×
        plural(r.heads.len(), "head"),
5502×
        ui::dim(if r.checked_out {
5512×
            "; working copy updated"
552
        } else {
5530×
            "; run oxy checkout to update the working copy"
554
        })
555
    ));
5562×
    let snap = repo.snapshot("oxy pull")?;
5572×
    if snap.base_snapshot.is_conflicted() {
5580×
        println!(
5590×
            "  {} {}",
5600×
            ui::caution(),
5610×
            ui::warn("the result has conflicts; see oxy status")
5620×
        );
5632×
    }
5642×
    others_pulled(&r.others);
5652×
    others_behind(&repo, &remote);
5662×
    subrepos_behind(&repo);
5672×
    super::workspace::where_now(&repo);
5682×
    Ok(0)
5694×
}
570
571
/// The other bookmarks a pull moved.
5723×
fn others_pulled(others: &[String]) {
5733×
    if others.is_empty() {
5742×
        return;
5751×
    }
5761×
    let shown: Vec<String> = others.iter().take(8).map(ui::bookmark).collect();
5771×
    let more = others.len().saturating_sub(8);
5781×
    println!(
579
        "  {} {}{}",
5801×
        ui::done("Also updated"),
5811×
        shown.join(", "),
5821×
        if more > 0 {
5830×
            ui::dim(format!(" and {more} more"))
584
        } else {
5851×
            String::new()
586
        }
587
    );
5883×
}
589
590
/// `oxy push [REMOTE] --delete NAME`.
5910×
pub fn push_delete(remote: Option<&str>, name: &str, as_json: bool) -> Result<i32> {
5920×
    let repo = open()?;
5930×
    let remote = remote_name(&repo, remote);
5940×
    let sp = ui::spinner(&format!("Deleting {remote}/{name}…"));
5950×
    let results = repo.push_delete(&remote, name)?;
5960×
    sp.clear();
5970×
    if as_json {
5980×
        println!(
599
            "{}",
6000×
            json::arr(results.iter().map(|r| {
6010×
                json::obj([
6020×
                    ("name", json::s(&r.name)),
6030×
                    ("ok", json::b(r.ok)),
6040×
                    ("reason", json::s(&r.reason)),
6050×
                    ("state", json::s("")),
6060×
                ])
6070×
            }))
608
        );
6090×
        return Ok(if results.iter().all(|r| r.ok) { 0 } else { 1 });
6100×
    }
6110×
    let mut code = 0;
6120×
    for r in results {
6130×
        if r.ok {
6140×
            println!(
6150×
                "{} {}",
6160×
                ui::done("Deleted"),
6170×
                ui::id(format!("{remote}/{}", r.name))
6180×
            );
6190×
        } else {
6200×
            println!(
6210×
                "{} {} {}{}{}",
6220×
                ui::err(ui::glyph("✖", "x")),
6230×
                ui::err("Rejected"),
6240×
                ui::id(format!("{remote}/{}", r.name)),
6250×
                ui::sep(),
6260×
                r.reason
6270×
            );
6280×
            code = 1;
6290×
        }
630
    }
6310×
    Ok(code)
6320×
}
633
6347×
pub fn push(
6357×
    remote: Option<&str>,
6367×
    force: bool,
6377×
    bookmark: Option<&str>,
6387×
    as_json: bool,
6397×
) -> Result<i32> {
6407×
    let repo = open()?;
6417×
    let remote = remote_name(&repo, remote);
6427×
    let sp = ui::spinner(&format!("Pushing to {remote}…"));
6437×
    let bookmark_name = bookmark.map_or_else(|| repo.config.bookmark.clone(), String::from);
644
    // A long push goes in parts: the spinner says which.
6457×
    let results = repo.push_bookmark_with(&remote, &bookmark_name, force, &mut |m| {
6460×
        sp.set(&format!("Pushing to {remote}… {m}"))
6471×
    })?;
6486×
    sp.clear();
6496×
    if as_json {
650
        // The state each accepted bookmark now holds on the remote (its local
651
        // state object), so a caller can look up what the push started, such
652
        // as the checks a forge runs on it.
6531×
        let view = repo.view()?;
6541×
        println!(
655
            "{}",
6561×
            json::arr(results.iter().map(|r| {
6571×
                let state = view
6581×
                    .bookmarks
6591×
                    .get(&r.name)
6601×
                    .filter(|_| r.ok)
6611×
                    .map(|st| st.to_hex())
6621×
                    .unwrap_or_default();
6631×
                json::obj([
6641×
                    ("name", json::s(&r.name)),
6651×
                    ("ok", json::b(r.ok)),
6661×
                    ("reason", json::s(&r.reason)),
6671×
                    ("state", json::s(&state)),
6681×
                ])
6691×
            }))
670
        );
6711×
        return Ok(if results.iter().all(|r| r.ok) { 0 } else { 1 });
6725×
    }
6735×
    let mut code = 0;
6745×
    for r in results {
6755×
        if r.ok {
6765×
            println!(
677
                "{} {}{}{}",
6785×
                ui::done("Pushed"),
6795×
                ui::id(format!("{remote}/{}", r.name)),
6805×
                ui::sep(),
6815×
                ui::dim(if r.reason.is_empty() {
6825×
                    "updated"
683
                } else {
6840×
                    r.reason.as_str()
685
                })
686
            );
6870×
        } else {
6880×
            println!(
6890×
                "{} {} {}{}{}",
6900×
                ui::err(ui::glyph("✖", "x")),
6910×
                ui::err("Rejected"),
6920×
                ui::id(format!("{remote}/{}", r.name)),
6930×
                ui::sep(),
6940×
                r.reason
6950×
            );
6960×
            code = 1;
6970×
        }
698
    }
6995×
    Ok(code)
7007×
}
701
7020×
pub fn serve(
7030×
    root: PathBuf,
7040×
    bind: &str,
7050×
    tokens: Vec<String>,
7060×
    tls: Option<(PathBuf, PathBuf)>,
7070×
) -> Result<i32> {
7080×
    let listener = std::net::TcpListener::bind(bind).with_context(|| format!("binding {bind}"))?;
7090×
    let root = root.canonicalize()?;
7100×
    let tls = tls.map(|(cert, key)| oxy_server::TlsFiles { cert, key });
7110×
    println!(
712
        "Serving repositories under {} on {}://{bind}/<repo>/oxy/v1/ ({})",
7130×
        root.display(),
7140×
        if tls.is_some() { "https" } else { "http" },
7150×
        if tokens.is_empty() {
7160×
            "open: no push tokens configured"
717
        } else {
7180×
            "push requires a token"
719
        }
720
    );
7210×
    if tls.is_none()
7220×
        && !bind.starts_with("127.")
7230×
        && !bind.starts_with("localhost")
7240×
        && !bind.starts_with("[::1]")
7250×
    {
7260×
        eprintln!("warning: serving plain HTTP on a non-loopback address; put a TLS terminator in front or pass --tls-cert/--tls-key");
7270×
    }
7280×
    oxy_server::run_blocking(listener, oxy_server::ServerConfig { root, tokens, tls })?;
7290×
    Ok(0)
7300×
}
731
7320×
pub fn git_export(git_dir: &std::path::Path, branch: Option<&str>) -> Result<i32> {
7330×
    let repo = open()?;
7340×
    let sp = ui::spinner("Exporting to Git…");
7350×
    let r = match branch {
7360×
        Some(b) => repo.git_export_as(git_dir, b)?,
7370×
        None => repo.git_export(git_dir)?,
738
    };
7390×
    sp.clear();
7400×
    println!(
741
        "{} {} to {} as branch {} {}",
7420×
        ui::done("Exported"),
7430×
        plural(r.exported, "new state"),
7440×
        ui::path(git_dir.display()),
7450×
        ui::bookmark(&r.branch),
7460×
        ui::dim(format!(
747
            "(currently {}; {} already exported)",
7480×
            plural(r.heads, "head"),
749
            r.skipped
750
        ))
751
    );
7520×
    Ok(0)
7530×
}
754
7550×
pub fn git_recover() -> Result<i32> {
7560×
    let repo = open()?;
7570×
    let sp = ui::spinner("Reading the store…");
7580×
    let (changes, heads) = repo.recover_git_import(&|m| sp.set(m))?;
7590×
    sp.done(&format!(
7600×
        "{} {} from imported Git commits: {} now has {}",
7610×
        ui::done("Recovered"),
7620×
        plural(changes, "change"),
7630×
        ui::bookmark(&repo.config.bookmark),
7640×
        plural(heads, "head")
7650×
    ));
7660×
    println!(
767
        "  {} {}",
7680×
        ui::note(),
7690×
        ui::dim("carry on with oxy git fetch GIT_DIR --branch BRANCH")
770
    );
7710×
    Ok(0)
7720×
}
773
774
/// `--claim` (oxygen#97): record `git.claim`, so this import and every
775
/// later fetch author the identity's own commits with its key.
7760×
fn claim_commits(repo: &mut Repo) -> Result<()> {
7770×
    repo.config.set("git.claim", "true")?;
7780×
    repo.save_config()?;
7790×
    Ok(())
7800×
}
781
7820×
pub fn git_fetch(url: &str, branch: &str, claim: bool) -> Result<i32> {
7830×
    let mut repo = open()?;
7840×
    if claim {
7850×
        claim_commits(&mut repo)?;
7860×
    }
7870×
    let sp = ui::spinner(&format!("Fetching {url} {branch}…"));
7880×
    let r = repo.git_fetch(url, branch)?;
7890×
    sp.clear();
7900×
    if r.commits == 0 {
7910×
        println!(
7920×
            "{} {}",
7930×
            ui::done("Fetched"),
7940×
            ui::dim(format!("nothing new from {url} {branch}"))
7950×
        );
7960×
    } else {
7970×
        println!(
798
            "{} {} ({} merges) from {} {}{}{} now has {}",
7990×
            ui::done("Fetched"),
8000×
            plural(r.commits, "new commit"),
801
            r.merges,
8020×
            ui::path(url),
8030×
            ui::bookmark(branch),
8040×
            ui::sep(),
8050×
            ui::bookmark(&repo.config.bookmark),
8060×
            plural(r.heads, "head")
807
        );
8080×
        if !repo.snapshot("oxy git fetch")?.changed {
8090×
            println!("  {} {}", ui::note(), ui::dim("working copy updated"));
8100×
        } else {
8110×
            println!(
8120×
                "  {} {}",
8130×
                ui::note(),
8140×
                ui::dim("run oxy checkout to update the working copy")
8150×
            );
8160×
        }
817
    }
8180×
    if r.submodules > 0 {
8190×
        println!(
820
            "  {} {}",
8210×
            ui::note(),
8220×
            ui::dim(format!(
823
                "{} submodule {} imported as subrepos with Git pins",
824
                r.submodules,
8250×
                if r.submodules == 1 {
8260×
                    "entry"
827
                } else {
8280×
                    "entries"
829
                }
830
            ))
831
        );
8320×
    }
8330×
    Ok(0)
8340×
}
835
8360×
pub fn git_push(url: &str, branch: &str) -> Result<i32> {
8370×
    let repo = open()?;
8380×
    let sp = ui::spinner(&format!("Pushing to {url} {branch}…"));
8390×
    let r = repo.git_push(url, branch)?;
8400×
    sp.clear();
8410×
    println!(
842
        "{} {} to {} {} {}",
8430×
        ui::done("Pushed"),
8440×
        plural(r.exported, "new commit"),
8450×
        ui::path(url),
8460×
        ui::bookmark(&r.branch),
8470×
        ui::dim(format!("({} already there)", r.skipped))
848
    );
8490×
    Ok(0)
8500×
}
851
852
/// `oxy git verify`: exit 1 if any commit's tree is not reproduced.
8530×
pub fn git_verify(
8540×
    git_dir: &std::path::Path,
8550×
    branch: &str,
8560×
    last: Option<usize>,
8570×
    max: usize,
8580×
) -> Result<i32> {
8590×
    let repo = open()?;
8600×
    let started = std::time::Instant::now();
8610×
    let sp = ui::spinner(&format!(
8620×
        "Rebuilding every commit of {} {branch}…",
8630×
        git_dir.display()
8640×
    ));
8650×
    let r = repo.verify_git_import_last(git_dir, branch, last, max)?;
8660×
    sp.clear();
8670×
    for (_, what) in &r.mismatches {
8680×
        println!("{} {what}", ui::err(ui::glyph("✖", "x")));
8690×
    }
8700×
    if r.not_yet > 0 {
8710×
        println!(
8720×
            "  {} {}",
8730×
            ui::note(),
8740×
            ui::dim(format!(
8750×
                "{} not imported yet, after the last imported commit: not checked",
8760×
                plural(r.not_yet, "commit")
8770×
            ))
8780×
        );
8790×
    }
8800×
    if r.mismatches.is_empty() {
8810×
        println!(
882
            "{}",
8830×
            ui::done(if r.skipped > 0 {
8840×
                format!(
885
                    "The last {} commits reproduced ({} followed), {} files compared, in {:.1}s",
8860×
                    r.commits - r.skipped,
887
                    r.skipped,
888
                    r.files_compared,
8890×
                    started.elapsed().as_secs_f64()
890
                )
891
            } else {
8920×
                format!(
893
                    "Every commit reproduced: {} commits, {} files compared, in {:.1}s",
894
                    r.commits,
895
                    r.files_compared,
8960×
                    started.elapsed().as_secs_f64()
897
                )
898
            })
899
        );
9000×
        Ok(0)
901
    } else {
902
        // What differs, by kind, the commit taken off: one difference
903
        // carried through history reads as one line.
9040×
        let mut kinds: std::collections::BTreeMap<String, usize> = Default::default();
9050×
        for (_, what) in &r.mismatches {
9060×
            let kind = what.split_once(": ").map_or(what.as_str(), |(_, k)| k);
9070×
            *kinds.entry(kind.to_string()).or_default() += 1;
908
        }
9090×
        let mut kinds: Vec<_> = kinds.into_iter().collect();
9100×
        kinds.sort_by_key(|k| std::cmp::Reverse(k.1));
9110×
        for (kind, n) in kinds.iter().take(10) {
9120×
            println!("  {} {}", ui::dim(format!("{n:>6} ×")), kind);
9130×
        }
9140×
        println!(
915
            "{} differences in {} commits checked{}",
9160×
            r.mismatches.len(),
917
            r.commits,
9180×
            if max > 0 && r.mismatches.len() >= max {
9190×
                format!(" (stopped at {max}; --max-differences 0 checks every commit)")
920
            } else {
9210×
                String::new()
922
            }
923
        );
9240×
        Ok(1)
925
    }
9260×
}
927
9280×
pub fn git_import(
9290×
    git_dir: &std::path::Path,
9300×
    dest: &std::path::Path,
9310×
    branch: &str,
9320×
    claim: bool,
9330×
) -> Result<i32> {
9340×
    let mut repo = Repo::init(dest)?;
9350×
    if claim {
9360×
        claim_commits(&mut repo)?;
9370×
    }
9380×
    let started = std::time::Instant::now();
9390×
    let sp = ui::spinner(&format!("Importing {} {branch}…", git_dir.display()));
9400×
    let r = repo.import_git_with(git_dir, branch, &mut |where_| sp.set(where_))?;
9410×
    sp.clear();
9420×
    println!(
943
        "{} {} ({} merges) from {} {} into {} {}",
9440×
        ui::done("Imported"),
9450×
        plural(r.commits, "commit"),
946
        r.merges,
9470×
        ui::path(git_dir.display()),
9480×
        ui::bookmark(branch),
9490×
        ui::path(repo.root().display()),
9500×
        ui::dim(format!(
951
            "in {:.1?}; {} now has {}",
9520×
            started.elapsed(),
953
            repo.config.bookmark,
9540×
            plural(r.heads, "head")
955
        ))
956
    );
9570×
    if r.submodules > 0 {
9580×
        println!(
959
            "  {} {}",
9600×
            ui::note(),
9610×
            ui::dim(format!(
962
                "{} submodule {} imported as subrepos with Git pins",
963
                r.submodules,
9640×
                if r.submodules == 1 {
9650×
                    "entry"
966
                } else {
9670×
                    "entries"
968
                }
969
            ))
970
        );
9710×
    }
9720×
    Ok(0)
9730×
}
974
9750×
pub fn submodule_add(url: &str, path: &str, bookmark: Option<&str>) -> Result<i32> {
9760×
    let repo = open()?;
9770×
    let sp = ui::spinner(&format!("Cloning {url} into {path}…"));
9780×
    let pin = repo.submodule_add(url, path, bookmark)?;
9790×
    sp.done(&format!(
9800×
        "{} subrepo {} {} {}",
9810×
        ui::done("Added"),
9820×
        ui::path(path),
9830×
        ui::dim(ui::glyph("→", "->")),
9840×
        ui::dim(pin.summary())
9850×
    ));
9860×
    println!(
987
        "  {} {}",
9880×
        ui::note(),
9890×
        ui::dim("recorded in .oxymodules; commit to pin it")
990
    );
9910×
    Ok(0)
9920×
}
993
9940×
pub fn submodule_update(init: bool, paths: &[String]) -> Result<i32> {
9950×
    let repo = open()?;
9960×
    let sp = ui::spinner("Updating subrepos…");
9970×
    let results = repo.submodule_update(init, paths)?;
9980×
    let unpinned: Vec<_> = repo
9990×
        .unpinned_modules()?
10000×
        .into_iter()
10010×
        .filter(|(p, _)| paths.is_empty() || paths.iter().any(|q| q.trim_end_matches('/') == p))
10020×
        .collect();
10030×
    sp.clear();
10040×
    if results.is_empty() && unpinned.is_empty() {
10050×
        println!("{}", ui::dim("No subrepos."));
10060×
    }
10070×
    let code = print_subrepo_results(results);
10080×
    print_unpinned(&unpinned);
10090×
    Ok(if unpinned.is_empty() { code } else { 1 })
10100×
}
1011
1012
/// `.oxymodules` entries never added: nothing to clone, so say how to add
1013
/// them (oxygen#72).
10140×
fn print_unpinned(unpinned: &[(String, oxy_core::subrepo::Module)]) {
10150×
    for (path, m) in unpinned {
10160×
        let url = if m.url.is_empty() { "<url>" } else { &m.url };
10170×
        println!(
1018
            "{} {}",
10190×
            ui::warn(format!(
1020
                "{} in .oxymodules but never added, so no pin",
10210×
                ui::glyph("⚠", "!")
1022
            )),
10230×
            ui::path(path)
1024
        );
10250×
        println!(
1026
            "  {}",
10270×
            ui::dim(format!("add it with: oxy submodule add {url} {path}"))
1028
        );
1029
    }
10300×
}
1031
10320×
fn print_subrepo_results(results: Vec<(String, oxy_core::repo::SubrepoSync)>) -> i32 {
10330×
    let mut code = 0;
10340×
    for (path, r) in results {
1035
        use oxy_core::repo::SubrepoSync::*;
10360×
        let line = match r {
10370×
            Updated => ui::done("Updated"),
10380×
            UpToDate => ui::ok(format!("{} up to date", ui::glyph("✔", "*"))),
1039
            Missing => {
10400×
                code = 1;
10410×
                ui::warn(format!(
1042
                    "{} missing (pass --init to clone)",
10430×
                    ui::glyph("⚠", "!")
1044
                ))
1045
            }
1046
            Dirty => {
10470×
                code = 1;
10480×
                ui::warn(format!(
1049
                    "{} has uncommitted changes; left as is",
10500×
                    ui::glyph("⚠", "!")
1051
                ))
1052
            }
10530×
            KeptBookmark { bookmark, newer } => ui::warn(format!(
1054
                "{} checked out at the pin; its bookmark {bookmark} keeps {} the pin lacks",
10550×
                ui::glyph("⚠", "!"),
10560×
                plural(newer, "newer change")
1057
            )),
10580×
            Failed(e) => {
10590×
                code = 1;
10600×
                ui::err(format!("{} {e}", ui::glyph("✖", "x")))
1061
            }
1062
        };
10630×
        println!("{line} {}", ui::path(path));
1064
    }
10650×
    code
10660×
}
1067
10680×
fn pin_json(p: &oxy_core::subrepo::Pin) -> String {
10690×
    match p {
1070
        oxy_core::subrepo::Pin::Oxy {
10710×
            repo,
10720×
            heads,
10730×
            bookmark,
10740×
        } => json::obj([
10750×
            ("kind", json::s("oxy")),
1076
            (
10770×
                "repo",
10780×
                json::s(&repo.iter().map(|b| format!("{b:02x}")).collect::<String>()),
1079
            ),
10800×
            ("heads", json::hashes(heads)),
10810×
            ("bookmark", json::s(bookmark)),
1082
        ]),
10830×
        oxy_core::subrepo::Pin::Git { sha, branch } => json::obj([
10840×
            ("kind", json::s("git")),
10850×
            ("sha", json::s(sha)),
10860×
            ("branch", json::s(branch)),
10870×
        ]),
1088
    }
10890×
}
1090
10911×
pub fn submodule_status(as_json: bool) -> Result<i32> {
10921×
    let repo = open()?;
10931×
    let subs = repo.subrepos()?;
10941×
    let unpinned = repo.unpinned_modules()?;
10951×
    if as_json {
10961×
        let pinned = subs.iter().map(|s| {
10970×
            json::obj([
10980×
                ("path", json::s(&s.path)),
10990×
                ("pin", pin_json(&s.pin)),
11000×
                ("actual", json::opt(s.actual.as_ref(), pin_json)),
11010×
                ("at_pin", json::b(s.actual.as_ref() == Some(&s.pin))),
11020×
                ("dirty", json::b(s.dirty)),
11030×
                ("committed", json::b(s.committed)),
11040×
                ("url", json::opt(s.url.as_deref(), json::s)),
11050×
            ])
11060×
        });
1107
        // Listed in .oxymodules but never added: no pin at all.
11081×
        let unpinned_json = unpinned.iter().map(|(path, m)| {
11090×
            json::obj([
11100×
                ("path", json::s(path)),
11110×
                ("pin", "null".to_string()),
11120×
                ("actual", "null".to_string()),
11130×
                ("at_pin", json::b(false)),
11140×
                ("dirty", json::b(false)),
11150×
                ("committed", json::b(false)),
11160×
                ("url", json::s(&m.url)),
11170×
            ])
11180×
        });
11191×
        println!("{}", json::arr(pinned.chain(unpinned_json)));
11201×
        return Ok(0);
11210×
    }
11220×
    if subs.is_empty() && unpinned.is_empty() {
11230×
        println!("{}", ui::dim("No subrepos."));
11240×
    }
11250×
    for s in subs {
11260×
        let state = match (&s.actual, s.dirty) {
11270×
            _ if !s.committed => {
11280×
                ui::warn(format!("{} added, not committed yet", ui::glyph("+", "+")))
1129
            }
11300×
            (None, _) => ui::warn(format!("{} missing", ui::glyph("⚠", "!"))),
11310×
            (Some(a), dirty) if *a == s.pin => {
11320×
                if dirty {
11330×
                    ui::warn(format!(
1134
                        "{} at pin, uncommitted changes",
11350×
                        ui::glyph("✎", "~")
1136
                    ))
1137
                } else {
11380×
                    ui::ok(format!("{} at pin", ui::glyph("✔", "*")))
1139
                }
1140
            }
11410×
            (Some(a), _) => ui::warn(format!("{} at {}", ui::glyph("↕", "<>"), a.summary())),
1142
        };
11430×
        println!(
1144
            "{} {}{}{}{}{state}",
11450×
            ui::ok(ui::glyph("⧉", "#")),
11460×
            ui::path(&s.path),
11470×
            ui::sep(),
11480×
            ui::dim(s.pin.summary()),
11490×
            ui::sep()
1150
        );
11510×
        if let Some(u) = s.url {
11520×
            println!("    {} {}", ui::dim(ui::glyph("↳", "->")), ui::dim(u));
11530×
        }
1154
    }
11550×
    print_unpinned(&unpinned);
11560×
    Ok(0)
11571×
}
1158
11590×
pub fn submodule_remove(path: &str) -> Result<i32> {
11600×
    let repo = open()?;
11610×
    repo.submodule_remove(path)?;
11620×
    println!(
1163
        "{} subrepo {} {}",
11640×
        ui::done("Removed"),
11650×
        ui::path(path),
11660×
        ui::dim("(directory left in place; commit to record the removal)")
1167
    );
11680×
    Ok(0)
11690×
}
1170
11710×
pub fn deepen(remote: &str, depth: u32) -> Result<i32> {
11720×
    let repo = open()?;
11730×
    if !repo.is_shallow() {
11740×
        println!(
1175
            "{}",
11760×
            ui::dim("This repository is not shallow; nothing to deepen.")
1177
        );
11780×
        return Ok(0);
11790×
    }
11800×
    let sp = ui::spinner(&format!("Fetching {depth} more levels from {remote}…"));
11810×
    let n = repo.deepen(remote, depth)?;
11820×
    sp.done(&format!(
1183
        "{} {}; {}",
11840×
        ui::done("Deepened by"),
11850×
        plural(n, "change"),
11860×
        if repo.is_shallow() {
11870×
            "still shallow".to_string()
1188
        } else {
11890×
            ui::ok("history is now complete")
1190
        }
1191
    ));
11920×
    Ok(0)
11930×
}