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