OXYGENHUB

Coverage / src/cli/mod.rs 58.5%

213 of 364 lines run, 9 of 18 functions · run #398 on subrepo-pin-state · run missed

1
//! Command-line definitions and dispatch.
2
3
mod approve;
4
mod auth;
5
mod format;
6
mod identity;
7
mod json;
8
mod proposal;
9
mod receipt;
10
mod sync;
11
mod tlog;
12
pub mod ui;
13
mod workspace;
14
15
use anyhow::{Context, Result};
16
use clap::{Parser, Subcommand};
17
use oxy_core::repo::Repo;
18
use std::path::PathBuf;
19
20
#[derive(Parser)]
21
#[command(
22
    name = "oxy",
23
    version = env!("OXY_VERSION_LONG"),
24
    about = "Oxygen: breathing fresh air into version control"
25
)]
26
struct Cli {
27
    /// Plain output: no colour, no spinner (also implied by NO_COLOR or a non-terminal)
28
    #[arg(long, global = true)]
29
    plain: bool,
30
    /// Read the repository as it was after operation OP (an ID from oxy op log, @ or @-); commands that would change it are refused
31
    #[arg(long, global = true, value_name = "OP")]
32
    at_op: Option<String>,
33
    #[command(subcommand)]
34
    cmd: Cmd,
35
}
36
37
/// `--at-op`, for `open` to apply.
38
static AT_OP: std::sync::OnceLock<Option<String>> = std::sync::OnceLock::new();
39
40
#[derive(Subcommand)]
41
enum Cmd {
42
    /// Create a repository in PATH (default: current directory)
43
    Init {
44
        /// Directory to create the repository in (default: the current directory)
45
        path: Option<PathBuf>,
46
        /// No working copy: an object store and references only, for hosting
47
        #[arg(long)]
48
        bare: bool,
49
        /// Keep the objects and the operation log on this store
50
        /// (dir://PATH, s3://BUCKET/PREFIX, gs://…, az://…) instead of
51
        /// in .oxy, with a local cache in front (storage.cache, 2 GiB)
52
        #[arg(long, value_name = "URL")]
53
        store: Option<String>,
54
    },
55
    /// Clone a repository from a local path or an http(s) URL
56
    Clone {
57
        /// Local path or http(s) URL of the repository
58
        url: String,
59
        /// Directory to clone into (default: the last part of the URL)
60
        dest: Option<PathBuf>,
61
        /// Leave blobs larger than this (e.g. 1M) to be fetched on first read
62
        #[arg(long)]
63
        max_blob: Option<String>,
64
        /// Materialize only these paths (repeatable); implies lazy blobs
65
        #[arg(long = "sparse")]
66
        sparse: Vec<String>,
67
        /// Shallow clone: fetch only this many levels of history below the heads
68
        #[arg(long)]
69
        depth: Option<u32>,
70
        /// Clone the subrepos too (as oxy submodule update --init)
71
        #[arg(long)]
72
        recursive: bool,
73
    },
74
    /// Fetch more history below a shallow clone's boundary
75
    Deepen {
76
        /// Levels of history to add below the boundary
77
        #[arg(default_value_t = 10)]
78
        depth: u32,
79
        /// Remote to fetch the history from
80
        #[arg(long, default_value = "origin")]
81
        remote: String,
82
    },
83
    /// Show or set the sparse filter (paths to materialize); re-checks out the bookmark
84
    Sparse {
85
        /// Paths to materialize; none prints the current filter
86
        paths: Vec<String>,
87
    },
88
    /// Show the working copy: changed paths and conflicts
89
    Status {
90
        /// Machine-readable output: paths, and every conflict with its kind and contributing changes
91
        #[arg(long)]
92
        json: bool,
93
    },
94
    /// One-line summary for shell prompts; fast and records nothing
95
    Prompt {
96
        /// Placeholders: %b bookmark, %c change id, %h heads, %d dirty (*), %x conflict (!), %A/%D ahead/behind (+n/-n), %a/%B raw counts, %s state glyph, %U/%O ahead/behind arrows, %i icon
97
        #[arg(long, default_value = oxy_core::repo::DEFAULT_PROMPT_FORMAT)]
98
        format: String,
99
        /// Glyph format (%i %b %c %s%U%O) instead of the plain default
100
        #[arg(long)]
101
        glyphs: bool,
102
        /// Colour by state (cyan clean, yellow dirty, red conflicted): raw escapes, or guarded for a bash or zsh PS1
103
        #[arg(long, value_parser = ["raw", "bash", "zsh"])]
104
        color: Option<String>,
105
    },
106
    /// Record the working copy as a change on the current bookmark
107
    Commit {
108
        /// The change's message: a subject line, then optionally a blank line and a body
109
        #[arg(short, long, required_unless_present = "file", conflicts_with = "file")]
110
        message: Option<String>,
111
        /// Read the message from FILE (`-` for standard input)
112
        #[arg(short = 'F', long, value_name = "FILE")]
113
        file: Option<PathBuf>,
114
        /// Extra explicit dependencies (ChangeId prefix, #hash prefix)
115
        #[arg(long = "depends-on")]
116
        depends_on: Vec<String>,
117
        /// Commit only these paths; the rest stays as uncommitted work
118
        paths: Vec<String>,
119
        /// Choose hunks one by one
120
        #[arg(short, long)]
121
        interactive: bool,
122
        /// Commit these hunks (as `oxy hunks` numbers them), comma separated
123
        #[arg(long, value_delimiter = ',')]
124
        hunks: Vec<usize>,
125
    },
126
    /// Fold CHANGE into one of its dependencies (keeps that change's id)
127
    Squash {
128
        /// The change to fold (ChangeId prefix, #hash prefix)
129
        change: String,
130
        /// The dependency that absorbs it
131
        #[arg(long)]
132
        into: String,
133
    },
134
    /// Re-express CHANGE against another base state
135
    Rebase {
136
        /// The change to re-express (ChangeId prefix, #hash prefix)
137
        change: String,
138
        /// The changes, bookmark or tag whose state becomes the new base
139
        #[arg(long, required = true, num_args = 1..)]
140
        onto: Vec<String>,
141
        /// Where the rebase would be conflicted, merge the target into the
142
        /// current bookmark instead, leaving the conflict in the working
143
        /// copy to fix and commit
144
        #[arg(long)]
145
        into_working_copy: bool,
146
    },
147
    /// Show which change introduced each line of a file
148
    Blame {
149
        /// File to annotate, relative to the repository root
150
        path: String,
151
        /// Machine-readable output: one JSON document on stdout
152
        #[arg(long)]
153
        json: bool,
154
    },
155
    /// Find the change that introduced a regression
156
    Bisect {
157
        #[command(subcommand)]
158
        cmd: BisectCmd,
159
    },
160
    /// List changes reachable from the bookmark and the working copy
161
    Log {
162
        /// Show the changes a revset selects: `main` (a bookmark name means its
163
        /// heads; `bookmark(main)` only ever the bookmark), `main..feature &
164
        /// author(andy)`, `touches("src/**") & date(-7d..)` (see
165
        /// docs/revset_query_language.md)
166
        #[arg(short = 'r', long, value_name = "REVSET", conflicts_with_all = ["path", "lines"])]
167
        revset: Option<String>,
168
        /// Show at most this many changes
169
        #[arg(short = 'n', long)]
170
        limit: Option<usize>,
171
        /// Show full messages, dependencies, and signatures
172
        #[arg(short, long)]
173
        verbose: bool,
174
        /// Only changes touching this path (renames followed)
175
        #[arg(long)]
176
        path: Option<String>,
177
        /// Only changes that wrote or deleted lines START to END of PATH
178
        /// (numbered as in the working copy's base), or deleted lines from
179
        /// between them: the history of a function
180
        #[arg(short = 'L', value_name = "START,END:PATH", conflicts_with = "path")]
181
        lines: Option<String>,
182
        /// Machine-readable output: one JSON document on stdout
183
        #[arg(long)]
184
        json: bool,
185
    },
186
    /// Show what the working copy changes against its base
187
    Diff {
188
        /// A change instead of the working copy
189
        change: Option<String>,
190
        /// Machine-readable output: one JSON document on stdout
191
        #[arg(long)]
192
        json: bool,
193
    },
194
    /// Print a file as it is in a state (default: the working copy's base)
195
    Cat {
196
        /// File to print, relative to the repository root
197
        path: String,
198
        /// A bookmark or tag (its state), or a change: a change means its
199
        /// dependency closure, the change and what it depends on, so files
200
        /// only later changes touched are absent or at older versions
201
        #[arg(short = 'r', long)]
202
        change: Option<String>,
203
    },
204
    /// Print the repository root, workspace, and bookmark
205
    Root {
206
        /// Machine-readable output: one JSON document on stdout
207
        #[arg(long)]
208
        json: bool,
209
    },
210
    /// Print the version of oxy, its repository format, and protocol
211
    Version {
212
        /// Machine-readable output: one JSON document on stdout
213
        #[arg(long)]
214
        json: bool,
215
    },
216
    /// Restore paths to the working copy's base, or undo chosen hunks
217
    Discard {
218
        /// Paths to restore; none with --hunks, or everything with neither
219
        paths: Vec<String>,
220
        /// Hunk indices as `oxy hunks` numbers them, comma separated
221
        #[arg(long, value_delimiter = ',')]
222
        hunks: Vec<usize>,
223
    },
224
    /// List the working copy's hunks, numbered as commit --hunks expects
225
    Hunks {
226
        /// Machine-readable output: one JSON document on stdout
227
        #[arg(long)]
228
        json: bool,
229
    },
230
    /// With -m, change the message of a head; without, name the working
231
    /// copy for a build: its tag, or the nearest tag plus distance and
232
    /// change, or the bookmark and change, and -dirty
233
    Describe {
234
        /// The new message
235
        #[arg(short, long)]
236
        message: Option<String>,
237
        /// The head to describe (default: the one the working copy sits on)
238
        change: Option<String>,
239
        /// Machine-readable output (naming only)
240
        #[arg(long, conflicts_with = "message")]
241
        json: bool,
242
    },
243
    /// Fold the working copy into the head it sits on, or into CHANGE with --into
244
    Amend {
245
        /// Replace the message too
246
        #[arg(short, long)]
247
        message: Option<String>,
248
        /// Amend an older change; the edits must touch only lines it already knows
249
        #[arg(long, conflicts_with = "paths")]
250
        into: Option<String>,
251
        /// Fold in only these paths; the rest stays as uncommitted work
252
        paths: Vec<String>,
253
    },
254
    /// Split a head: the named paths become a separate first change
255
    Split {
256
        /// The head to split
257
        change: String,
258
        /// Paths whose edits become the first change
259
        paths: Vec<String>,
260
    },
261
    /// Drop a change from the bookmark; its content stays on disk as uncommitted work
262
    Abandon {
263
        /// The change to drop (ChangeId prefix, #hash prefix)
264
        #[arg(required_unless_present = "revset")]
265
        change: Option<String>,
266
        /// Drop every change a revset selects, newest first
267
        #[arg(short = 'r', long, value_name = "REVSET", conflicts_with = "change")]
268
        revset: Option<String>,
269
        /// Drop more than five changes without asking
270
        #[arg(long, short = 'y')]
271
        yes: bool,
272
    },
273
    /// Show one change (ChangeId prefix, #hash prefix, or @), or those a revset selects
274
    Show {
275
        /// ChangeId prefix, #hash prefix, or @ for the working copy
276
        #[arg(required_unless_present = "revset")]
277
        change: Option<String>,
278
        /// Show every change a revset selects
279
        #[arg(short = 'r', long, value_name = "REVSET", conflicts_with = "change")]
280
        revset: Option<String>,
281
        /// The files it touched with lines added and removed, instead of the diff
282
        #[arg(long)]
283
        stat: bool,
284
        /// Machine-readable output: one JSON document on stdout
285
        #[arg(long)]
286
        json: bool,
287
    },
288
    /// Move the working copy to the state formed by CHANGE... (default: the bookmark heads)
289
    Checkout {
290
        /// Changes whose state to check out (default: the bookmark heads)
291
        changes: Vec<String>,
292
        /// Throw away uncommitted working-copy changes
293
        #[arg(long)]
294
        discard: bool,
295
        /// Take uncommitted changes along; refused when the target has
296
        /// other versions of the files they touch
297
        #[arg(long, conflicts_with = "discard")]
298
        carry: bool,
299
    },
300
    /// Report which conflicts of the working copy's base are resolved by the current files;
301
    /// with paths, settle their replace conflicts (binary files, subrepo pins) with what the
302
    /// working copy holds there
303
    Resolve {
304
        /// Opaque files or subrepos whose replace conflict to settle with their current content;
305
        /// with --both, the text files whose conflicts to settle (all of them when none)
306
        paths: Vec<String>,
307
        /// Settle each adjacent insertion (two changes adding lines at the same place) by keeping
308
        /// every side, earliest change first; refused where the sides changed the same lines
309
        #[arg(long)]
310
        both: bool,
311
        /// With --both, the latest change first
312
        #[arg(long, requires = "both")]
313
        reverse: bool,
314
        /// Machine-readable output: one JSON document on stdout
315
        #[arg(long)]
316
        json: bool,
317
    },
318
    /// Undo the last operation
319
    Undo,
320
    /// Write a checkpoint of the current bookmark's state now
321
    Checkpoint,
322
    /// Print a shell completion script: `oxy completions bash > ~/.local/share/bash-completion/completions/oxy`
323
    Completions {
324
        /// bash, zsh, fish, elvish or powershell
325
        shell: clap_complete::Shell,
326
    },
327
    /// Write man pages (oxy.1 and one per command) into DIR, or print oxy.1
328
    Man {
329
        /// Directory to write every page into, e.g. ~/.local/share/man/man1
330
        #[arg(long)]
331
        dir: Option<PathBuf>,
332
    },
333
    /// Check the identity, configuration and remotes, and say what to do about any problem
334
    Doctor {
335
        /// Do not contact the remotes
336
        #[arg(long)]
337
        offline: bool,
338
        /// Machine-readable output: one JSON document on stdout
339
        #[arg(long)]
340
        json: bool,
341
    },
342
    /// Check the repository's integrity: objects, operation log, references, signatures, checkpoints
343
    Fsck {
344
        /// Machine-readable output: one JSON document on stdout
345
        #[arg(long)]
346
        json: bool,
347
    },
348
    /// Prune unreachable objects older than the grace period and repack everything into one pack
349
    Gc {
350
        /// Ignore the grace period (core.gc-grace) and prune immediately
351
        #[arg(long)]
352
        now: bool,
353
        /// Keep no past working-copy snapshots, only each workspace's current one, so files only they held go (core.snapshot-retention, default 7 days)
354
        #[arg(long)]
355
        expire_snapshots: bool,
356
        /// Instead: fold the bundles of a remote kept on a store into one, and remove the ones a gc superseded 7 days ago (or now, with --now)
357
        #[arg(long, value_name = "NAME")]
358
        remote: Option<String>,
359
    },
360
    /// List operations
361
    #[command(name = "op")]
362
    Op {
363
        #[command(subcommand)]
364
        cmd: OpCmd,
365
    },
366
    /// Take the advisory local lock on a lockable file, making it writable
367
    Lock {
368
        /// The lockable file
369
        path: String,
370
    },
371
    /// Release the local lock and make the file read-only again
372
    Unlock {
373
        /// The locked file
374
        path: String,
375
    },
376
    /// Workspaces: extra checkout directories sharing this repository
377
    Workspace {
378
        #[command(subcommand)]
379
        cmd: WorkspaceCmd,
380
    },
381
    /// Bookmarks: named states; each workspace works on one
382
    Bookmark {
383
        #[command(subcommand)]
384
        cmd: BookmarkCmd,
385
    },
386
    /// Union another bookmark's heads into the current one (no merge change):
387
    /// a bookmark here, or a remote's (`origin/develop`) as last fetched
388
    Merge {
389
        /// The bookmark whose heads to add
390
        bookmark: String,
391
    },
392
    /// Sign in to a hub once, for every repository on it
393
    Auth {
394
        #[command(subcommand)]
395
        cmd: AuthCmd,
396
    },
397
    /// Subrepositories: nested repositories pinned to a state
398
    Submodule {
399
        #[command(subcommand)]
400
        cmd: SubmoduleCmd,
401
    },
402
    /// Witness hubs' transparency logs: cosign each checkpoint that extends
403
    /// the last one cosigned (spec §14.8)
404
    Witness {
405
        #[command(subcommand)]
406
        cmd: WitnessCmd,
407
    },
408
    /// Hubs' transparency logs: check that a hub shows everyone the same
409
    /// history (spec §14.8)
410
    Tlog {
411
        #[command(subcommand)]
412
        cmd: TlogCmd,
413
    },
414
    /// Execution receipts: signed records of what a runner ran on a state
415
    Receipt {
416
        #[command(subcommand)]
417
        cmd: ReceiptCmd,
418
    },
419
    /// Tags: immutable named states
420
    Tag {
421
        #[command(subcommand)]
422
        cmd: TagCmd,
423
    },
424
    /// Manage remotes
425
    Remote {
426
        #[command(subcommand)]
427
        cmd: RemoteCmd,
428
    },
429
    /// Fetch every bookmark of a remote into its tracking references
430
    Fetch {
431
        /// Remote name (default: origin)
432
        remote: Option<String>,
433
        /// Machine-readable output: one JSON document on stdout
434
        #[arg(long)]
435
        json: bool,
436
    },
437
    /// Fetch, then union the remote bookmark into the local one (no merge change)
438
    Pull {
439
        /// Remote name (default: origin)
440
        remote: Option<String>,
441
        /// Machine-readable output: one JSON document on stdout
442
        #[arg(long)]
443
        json: bool,
444
    },
445
    /// Push the current bookmark to a remote
446
    Push {
447
        /// Remote name (default: origin)
448
        remote: Option<String>,
449
        /// Allow dropping heads the remote has
450
        #[arg(long)]
451
        force: bool,
452
        /// Push this bookmark instead of the workspace's current one
453
        #[arg(short = 'b', long)]
454
        bookmark: Option<String>,
455
        /// Delete this bookmark on the remote (never its default bookmark);
456
        /// the local bookmark is kept. `tags/NAME` withdraws a tag there
457
        /// (and here, as `oxy tag delete` does)
458
        #[arg(long, value_name = "NAME", conflicts_with_all = ["bookmark", "force"])]
459
        delete: Option<String>,
460
        /// Machine-readable output: one JSON document on stdout
461
        #[arg(long)]
462
        json: bool,
463
    },
464
    /// Serve repositories under ROOT over HTTPS (with --tls-cert/--tls-key) or plain HTTP
465
    Serve {
466
        /// Directory with one repository per subdirectory
467
        #[arg(long, default_value = ".")]
468
        root: PathBuf,
469
        /// Address and port to listen on
470
        #[arg(long, default_value = "127.0.0.1:8890")]
471
        bind: String,
472
        /// Bearer token allowed to push (repeatable); none means open
473
        #[arg(long = "token")]
474
        tokens: Vec<String>,
475
        /// PEM certificate chain; enables HTTPS together with --tls-key
476
        #[arg(long, requires = "tls_key")]
477
        tls_cert: Option<PathBuf>,
478
        /// PEM private key
479
        #[arg(long, requires = "tls_cert")]
480
        tls_key: Option<PathBuf>,
481
    },
482
    /// Manage the signing identity
483
    Identity {
484
        #[command(subcommand)]
485
        cmd: IdentityCmd,
486
    },
487
    /// Seal changes with your key: named ones, or every change on the current bookmark you can authenticate but have not
488
    Seal {
489
        /// Changes to seal (default: every change on the bookmark you can authenticate and have not sealed)
490
        changes: Vec<String>,
491
        /// Seal every change of a hub proposal and push the seal (your approval)
492
        #[arg(long, conflicts_with = "changes")]
493
        proposal: Option<String>,
494
        /// Approve a job of a hub run that waits for a sealed approval: `--flow https://hub/OWNER/NAME/flow/N --job apply`
495
        #[arg(long, conflicts_with_all = ["changes", "proposal"], requires = "job")]
496
        flow: Option<String>,
497
        /// The job to approve, with --flow
498
        #[arg(long, requires = "flow")]
499
        job: Option<String>,
500
        /// Reject the job instead of approving it, with --flow
501
        #[arg(long, requires = "flow")]
502
        reject: bool,
503
        /// Machine-readable output: one JSON document on stdout
504
        #[arg(long)]
505
        json: bool,
506
    },
507
    /// Hub proposals: show one, or land it from this client
508
    Proposal {
509
        #[command(subcommand)]
510
        cmd: ProposalCmd,
511
    },
512
    /// Verify the signatures on a change (default: every change in the log)
513
    Verify {
514
        /// The change to verify (default: every change in the log)
515
        change: Option<String>,
516
        /// Verify every change a revset selects: `release..main & unsigned()`
517
        #[arg(short = 'r', long, value_name = "REVSET", conflicts_with = "change")]
518
        revset: Option<String>,
519
        /// Machine-readable output: one JSON document on stdout
520
        #[arg(long)]
521
        json: bool,
522
    },
523
    /// Git interoperability
524
    Git {
525
        #[command(subcommand)]
526
        cmd: GitCmd,
527
    },
528
    /// Manage the trust policy (who may update which bookmarks)
529
    Policy {
530
        #[command(subcommand)]
531
        cmd: PolicyCmd,
532
    },
533
    /// Set a configuration key (author.*, core.sign, core.op-retention, core.gc-grace, core.checkpoint-interval, filter.max-blob, filter.paths, storage.cache, workspace.bookmark, remote.<name>.url|token|ca, auth.helper with --user)
534
    Config {
535
        /// The key, such as author.name or remote.origin.token
536
        key: String,
537
        /// The value to set (none with --unset); `-` reads it from standard input, out of the shell's history
538
        #[arg(required_unless_present = "unset")]
539
        value: Option<String>,
540
        /// Write to the user file (~/.config/oxy/config.toml) instead of this repository
541
        #[arg(long)]
542
        user: bool,
543
        /// Remove the key from this repository's file (or the user file with --user)
544
        #[arg(long)]
545
        unset: bool,
546
    },
547
}
548
549
#[derive(Subcommand)]
550
enum OpCmd {
551
    /// List operations, newest first, with what each changed
552
    Log {
553
        /// Show only the latest N (snapshots folded together count as one)
554
        #[arg(short = 'n', long)]
555
        limit: Option<usize>,
556
        /// Machine-readable output: one JSON document on stdout
557
        #[arg(long)]
558
        json: bool,
559
    },
560
    /// Go back to the bookmarks and working copy as they were after OP (recorded as a new operation, so it can be undone)
561
    Restore {
562
        /// The operation: an ID prefix from oxy op log, @ or @-
563
        op: String,
564
        /// Throw away uncommitted working-copy changes
565
        #[arg(long)]
566
        discard: bool,
567
    },
568
    /// Undo OP alone, keeping every operation after it: the bookmarks it moved go back
569
    Revert {
570
        /// The operation: an ID prefix from oxy op log
571
        op: String,
572
    },
573
}
574
575
#[derive(Subcommand)]
576
enum WorkspaceCmd {
577
    /// Create a workspace at PATH checked out on CHANGE... (default: the bookmark heads)
578
    Add {
579
        /// Directory for the new workspace (must be empty or absent)
580
        path: PathBuf,
581
        /// Changes whose state to check out there
582
        changes: Vec<String>,
583
    },
584
    /// List workspaces and their working-copy changes
585
    List {
586
        /// Machine-readable output: one JSON document on stdout
587
        #[arg(long)]
588
        json: bool,
589
    },
590
    /// Stop tracking a workspace (by id prefix or path); its files stay
591
    Forget {
592
        /// Workspace id prefix or path
593
        workspace: String,
594
    },
595
}
596
597
#[derive(Subcommand)]
598
enum BisectCmd {
599
    /// Begin; remembers where the working copy was
600
    Start,
601
    /// Mark CHANGE... (or the state under test) as good
602
    Good {
603
        /// Changes known to be good (default: the state under test)
604
        changes: Vec<String>,
605
    },
606
    /// Mark CHANGE... (or the state under test) as bad
607
    Bad {
608
        /// Changes known to be bad (default: the state under test)
609
        changes: Vec<String>,
610
    },
611
    /// The state under test cannot be tested; try another
612
    Skip,
613
    /// Run a command for each step: exit 0 good, 125 skip, else bad
614
    Run {
615
        /// Command and arguments to run at each step
616
        #[arg(required = true, trailing_var_arg = true)]
617
        command: Vec<String>,
618
    },
619
    /// Where the search stands
620
    Status {
621
        /// Machine-readable output: one JSON document on stdout
622
        #[arg(long)]
623
        json: bool,
624
    },
625
    /// Restore the working copy and forget the search
626
    Reset,
627
}
628
629
#[derive(Subcommand)]
630
enum BookmarkCmd {
631
    /// Create a bookmark at CHANGE... (default: the working copy's base)
632
    Create {
633
        /// Name of the new bookmark
634
        name: String,
635
        /// Its heads (default: the working copy's base). A state is its
636
        /// heads and what they depend on: a change alone may leave out
637
        /// earlier changes of the current state, which is refused unless
638
        /// --upto or --only says which is meant
639
        changes: Vec<String>,
640
        /// Everything of the current state up to CHANGE..., as a Git
641
        /// branch at a commit would hold
642
        #[arg(long, conflicts_with = "only")]
643
        upto: bool,
644
        /// Just CHANGE... and what they depend on. Other changes to the
645
        /// same files are left out, so a file can come out partly edited;
646
        /// to add one change to a bookmark's work, give the bookmark's
647
        /// heads with it instead: `bookmark set NAME <its heads> CHANGE`
648
        #[arg(long)]
649
        only: bool,
650
    },
651
    /// Point an existing bookmark at CHANGE...
652
    Set {
653
        /// The bookmark to move
654
        name: String,
655
        /// Its new heads. A change alone may leave out changes the
656
        /// bookmark now holds that were recorded before it, which is
657
        /// refused unless --upto or --only says which is meant
658
        changes: Vec<String>,
659
        /// Everything the bookmark holds up to CHANGE..., as a Git branch
660
        /// moved to a commit would hold
661
        #[arg(long, conflicts_with = "only")]
662
        upto: bool,
663
        /// Just CHANGE... and what they depend on. Other changes to the
664
        /// same files are left out, so a file can come out partly edited;
665
        /// to add one change to a bookmark's work, give the bookmark's
666
        /// heads with it instead: `bookmark set NAME <its heads> CHANGE`
667
        #[arg(long)]
668
        only: bool,
669
    },
670
    /// Remove a bookmark; its changes stay
671
    Delete {
672
        /// The bookmark to remove
673
        name: String,
674
    },
675
    /// List bookmarks with their heads and difference from origin
676
    List {
677
        /// Also list remotes' bookmarks that have no local bookmark
678
        #[arg(long, short)]
679
        all: bool,
680
        /// Each head of every bookmark, as `bookmark show` prints them
681
        #[arg(long, short)]
682
        verbose: bool,
683
        /// Machine-readable output: one JSON document on stdout
684
        #[arg(long)]
685
        json: bool,
686
    },
687
    /// A bookmark's heads: each change nothing else on it depends on,
688
    /// newest first. A bookmark name is also a revset for its heads
689
    /// (`oxy log -r NAME`, or `bookmark(NAME)` to mean only the bookmark)
690
    Show {
691
        /// The bookmark, or REMOTE/NAME (default: the current one)
692
        name: Option<String>,
693
        /// Machine-readable output: one JSON document on stdout
694
        #[arg(long)]
695
        json: bool,
696
    },
697
    /// Make NAME this workspace's bookmark and check it out. Uncommitted work stops it; --carry takes the work along, --discard drops it
698
    Switch {
699
        /// The bookmark to work on; one only a remote has (NAME or REMOTE/NAME) is created from it
700
        name: String,
701
        /// Throw away uncommitted working-copy changes
702
        #[arg(long)]
703
        discard: bool,
704
        /// Take uncommitted changes along; refused when NAME has other
705
        /// versions of the files they touch
706
        #[arg(long, conflicts_with = "discard")]
707
        carry: bool,
708
    },
709
}
710
711
#[derive(Subcommand)]
712
enum WitnessCmd {
713
    /// Serve a witness (C2SP tlog-witness: POST /add-checkpoint), signing
714
    /// with this oxy home's key (author.key)
715
    Serve {
716
        /// The witness's name in cosignatures: a host name
717
        #[arg(long)]
718
        name: String,
719
        /// A log to witness, as ORIGIN=FILE: its origin (HOST/log) and its
720
        /// key's identity file (a hub's /.well-known/oxyhub-key); repeatable
721
        #[arg(long = "log", required = true)]
722
        logs: Vec<String>,
723
        /// Where to keep what it cosigned
724
        #[arg(long, default_value = "witness-state")]
725
        state: PathBuf,
726
        /// Address and port to listen on
727
        #[arg(long, default_value = "127.0.0.1:8891")]
728
        bind: String,
729
        /// PEM certificate chain; enables HTTPS together with --tls-key
730
        #[arg(long, requires = "tls_key")]
731
        tls_cert: Option<PathBuf>,
732
        /// PEM private key for --tls-cert
733
        #[arg(long, requires = "tls_cert")]
734
        tls_key: Option<PathBuf>,
735
    },
736
}
737
738
#[derive(Subcommand)]
739
enum TlogCmd {
740
    /// Check a hub's whole log: its checkpoint against the log key and the
741
    /// last checkpoint seen, and every entry against the root it signed;
742
    /// then list this repository's entries (or a key's, or all)
743
    Verify {
744
        /// The hub, as https://oxygenhub.io (default: this checkout's remote)
745
        hub: Option<String>,
746
        /// List the entries a key signed (a KeyId) instead
747
        #[arg(long, conflicts_with = "all")]
748
        key: Option<String>,
749
        /// List every entry
750
        #[arg(long)]
751
        all: bool,
752
        /// Machine-readable output
753
        #[arg(long)]
754
        json: bool,
755
    },
756
}
757
758
#[derive(Subcommand)]
759
enum AuthCmd {
760
    /// Sign in to HUB in the browser (device sign-in) and keep the token for
761
    /// every repository there
762
    Login {
763
        /// The hub, as https://oxygenhub.io or oxygenhub.io (default: this checkout's origin)
764
        hub: Option<String>,
765
        /// Read a token from standard input instead (for CI and scripts)
766
        #[arg(long)]
767
        with_token: bool,
768
        /// PEM file of extra roots to trust (a hub with a self-signed certificate)
769
        #[arg(long)]
770
        ca: Option<PathBuf>,
771
    },
772
    /// List the hubs you are signed in to, checking each token with its hub
773
    Status,
774
    /// Forget HUB's token and revoke it on the hub
775
    Logout {
776
        /// The hub (default: this checkout's origin, or the only one)
777
        hub: Option<String>,
778
    },
779
    /// Keep an agent grant token (from `oh agent grant`) for keyless
780
    /// signing: with `author.signer = keyless`, commits and pushes are
781
    /// signed by fresh keys the hub certifies, in the grantor's name
782
    /// (the token is read from standard input, so it stays out of the
783
    /// shell's history)
784
    Grant {
785
        /// The hub (default: this checkout's origin)
786
        hub: Option<String>,
787
    },
788
}
789
790
#[derive(Subcommand)]
791
enum SubmoduleCmd {
792
    /// Clone URL (Oxygen or Git) into PATH and register it in .oxymodules
793
    Add {
794
        /// URL or path of the Oxygen or Git repository
795
        url: String,
796
        /// Where to put it, relative to the repository root
797
        path: String,
798
        /// Bookmark (Oxygen) or branch (Git) to check out and track
799
        #[arg(long)]
800
        bookmark: Option<String>,
801
    },
802
    /// Bring nested repositories to their pins
803
    Update {
804
        /// Clone nested repositories that are absent
805
        #[arg(long)]
806
        init: bool,
807
        /// Subrepos to update (default: all)
808
        paths: Vec<String>,
809
    },
810
    /// List subrepos with their pins and on-disk state
811
    Status {
812
        /// Machine-readable output: one JSON document on stdout
813
        #[arg(long)]
814
        json: bool,
815
    },
816
    /// Unregister PATH; the directory stays, the node goes with the next commit
817
    Remove {
818
        /// The subrepo to unregister
819
        path: String,
820
    },
821
}
822
823
#[derive(Subcommand)]
824
enum ReceiptCmd {
825
    /// The receipts kept for a state (default: the bookmark heads)
826
    List {
827
        /// Change, bookmark or tag whose state to look at
828
        rev: Option<String>,
829
        /// Machine-readable output: one JSON document on stdout
830
        #[arg(long)]
831
        json: bool,
832
    },
833
    /// One receipt in full, and whether its signature verifies
834
    Show {
835
        /// The receipt (a prefix of its hash, as `list` shows it)
836
        receipt: String,
837
        /// Change, bookmark or tag whose state holds it
838
        #[arg(long)]
839
        rev: Option<String>,
840
        /// Machine-readable output: one JSON document on stdout
841
        #[arg(long)]
842
        json: bool,
843
    },
844
    /// Write a signed receipt to FILE
845
    Export {
846
        /// The receipt (a prefix of its hash, as `list` shows it)
847
        receipt: String,
848
        /// Where to write it
849
        file: String,
850
        /// Change, bookmark or tag whose state holds it
851
        #[arg(long)]
852
        rev: Option<String>,
853
        /// `oxygen`: the runner's signed receipt, as `verify` and `import`
854
        /// read it. `slsa`: SLSA provenance in a DSSE envelope, signed with
855
        /// your key, for in-toto and cosign (`oxy identity export --pem`
856
        /// gives the key they check it with)
857
        #[arg(long, default_value = "oxygen", value_parser = ["oxygen", "slsa"])]
858
        format: String,
859
    },
860
    /// Check a signed receipt in FILE without keeping it
861
    Verify {
862
        /// A signed receipt, as a runner or `export` writes it
863
        file: String,
864
        /// Machine-readable output: one JSON document on stdout
865
        #[arg(long)]
866
        json: bool,
867
    },
868
    /// Verify a signed receipt in FILE and keep it with its state
869
    Import {
870
        /// A signed receipt, as a runner or `export` writes it
871
        file: String,
872
    },
873
}
874
875
#[derive(Subcommand)]
876
enum TagCmd {
877
    /// Tag the state of CHANGE... (default: the bookmark heads)
878
    Create {
879
        /// Name of the tag; tags never move
880
        name: String,
881
        /// Changes whose state to tag (default: the bookmark heads)
882
        changes: Vec<String>,
883
        /// A message recorded with the tag
884
        #[arg(short, long, default_value = "")]
885
        message: String,
886
    },
887
    /// Withdraw a tag: a signed withdrawal, kept beside the tag's creation;
888
    /// `oxy push` sends it to a remote that has the tag (the name is never
889
    /// used again)
890
    Delete {
891
        /// Name of the tag
892
        name: String,
893
        /// Why it is withdrawn, recorded with the withdrawal
894
        #[arg(short, long, default_value = "")]
895
        reason: String,
896
    },
897
    /// List tags and the states they name
898
    List {
899
        /// Machine-readable output: one JSON document on stdout
900
        #[arg(long)]
901
        json: bool,
902
    },
903
}
904
905
#[derive(Subcommand)]
906
enum RemoteCmd {
907
    /// Add a remote
908
    Add {
909
        /// Name to refer to the remote by, such as origin
910
        name: String,
911
        /// A local path or the http(s) URL of a hub repository; or a store the repository is kept on: s3://BUCKET/PREFIX, gs://BUCKET/PREFIX, az://ACCOUNT/CONTAINER/PREFIX, dir://PATH
912
        url: String,
913
        /// For an s3:// store: the AWS profile to use (default: AWS_PROFILE, else "default")
914
        #[arg(long)]
915
        profile: Option<String>,
916
        /// For an s3:// store: its region (default: the environment, the profile, else asked of S3)
917
        #[arg(long)]
918
        region: Option<String>,
919
        /// For an s3://, gs:// or az:// store: another endpoint, such as an S3-compatible service
920
        #[arg(long)]
921
        endpoint: Option<String>,
922
        /// For a gs:// store: a service account key file, or application default credentials
923
        #[arg(long)]
924
        credentials: Option<String>,
925
    },
926
    /// Check a remote: that it is reachable with the credentials in hand, what it holds, and (for a store) that it behaves
927
    Check {
928
        /// The remote to check
929
        name: String,
930
    },
931
    /// Remove a remote
932
    Remove {
933
        /// The remote to remove
934
        name: String,
935
    },
936
    /// List remotes and their URLs
937
    List {
938
        /// Machine-readable output: one JSON document on stdout
939
        #[arg(long)]
940
        json: bool,
941
    },
942
}
943
944
#[derive(Subcommand)]
945
enum ProposalCmd {
946
    /// Show a proposal by its hub URL
947
    Show {
948
        /// The proposal's URL on the hub
949
        url: String,
950
        /// Machine-readable output: one JSON document on stdout
951
        #[arg(long)]
952
        json: bool,
953
    },
954
    /// Fetch the proposal's changes, set the target bookmark to the projected state, and push with your key
955
    Land {
956
        /// The proposal's URL on the hub
957
        url: String,
958
        /// Land even when the hub reports blockers
959
        #[arg(long)]
960
        force: bool,
961
    },
962
}
963
964
#[derive(Subcommand)]
965
enum IdentityCmd {
966
    /// Generate a composite Ed25519 + ML-DSA-65 key pair and make it your author identity
967
    Create {
968
        /// Your name (default: author.name)
969
        #[arg(long)]
970
        name: Option<String>,
971
        /// Your email (default: author.email)
972
        #[arg(long)]
973
        email: Option<String>,
974
        /// Record the author in this repository only, not in the user config
975
        #[arg(long)]
976
        local: bool,
977
        /// Expire the identity this many days from now
978
        #[arg(long)]
979
        expires: Option<u64>,
980
    },
981
    /// Show the configured identity
982
    Show,
983
    /// Write your identity object to a file, to hand to a policy holder
984
    Export {
985
        /// File to write (default: <key>.identity, or <key>.pem with --pem, in the current directory)
986
        path: Option<PathBuf>,
987
        /// Write the Ed25519 public key as PEM instead, as cosign's --key and
988
        /// other DSSE verifiers take it
989
        #[arg(long)]
990
        pem: bool,
991
    },
992
    /// Sign a host's challenge (hex, at least 16 bytes) to prove you hold your key; prints the signature as hex
993
    SignChallenge {
994
        /// The host's challenge, as hex
995
        challenge: String,
996
        /// Machine-readable output: one JSON document on stdout
997
        #[arg(long)]
998
        json: bool,
999
    },
1000
    /// Move to a fresh key pair; the policy follows if it names your key
1001
    Rotate,
1002
    /// Re-issue your identity with a new expiry (in days from now; 0 = never)
1003
    Renew {
1004
        /// Days from now until it expires; 0 for never
1005
        #[arg(long)]
1006
        expires: u64,
1007
    },
1008
    /// Revoke a key: yours by default, or any key if you hold a policy root
1009
    Revoke {
1010
        /// KeyId prefix (default: your own key)
1011
        key: Option<String>,
1012
        /// Why, recorded in the revocation
1013
        #[arg(long, default_value = "")]
1014
        reason: String,
1015
    },
1016
    /// Keys allowed to sign or seal on your behalf
1017
    Delegate {
1018
        #[command(subcommand)]
1019
        cmd: DelegateCmd,
1020
    },
1021
}
1022
1023
#[derive(Subcommand)]
1024
enum DelegateCmd {
1025
    /// Add a delegate from an exported identity file
1026
    Add {
1027
        /// The delegates' exported identity files: one rotation for all of them
1028
        #[arg(required = true)]
1029
        identity_file: Vec<PathBuf>,
1030
        /// What it may do for you: changes, refs, seal (comma-separated) or all.
1031
        /// An agent gets `changes`; a host that lands for you needs `changes,refs`
1032
        #[arg(long, default_value = "changes")]
1033
        scope: String,
1034
        /// How long for: 30m, 8h, 7d, or a number of days (default: until removed)
1035
        #[arg(long)]
1036
        expires: Option<String>,
1037
    },
1038
    /// Remove a delegate by KeyId prefix
1039
    Remove {
1040
        /// KeyId prefix of the delegate
1041
        key: String,
1042
    },
1043
    /// List delegates
1044
    List,
1045
}
1046
1047
#[derive(Subcommand)]
1048
enum GitCmd {
1049
    /// Export the current bookmark to a Git repository (created bare if absent)
1050
    Export {
1051
        /// Git repository to write to (created bare if absent)
1052
        git_dir: PathBuf,
1053
        /// Git branch to write (default: the bookmark name)
1054
        #[arg(long)]
1055
        branch: Option<String>,
1056
    },
1057
    /// Fetch a branch from a Git remote (path or URL) into the current bookmark
1058
    Fetch {
1059
        /// Git remote: a path or URL
1060
        url: String,
1061
        /// Branch to fetch
1062
        #[arg(long, default_value = "main")]
1063
        branch: String,
1064
        /// Author your own commits (Git author email = your identity's) with your key; sets git.claim
1065
        #[arg(long)]
1066
        claim: bool,
1067
    },
1068
    /// Push the current bookmark to a branch of a Git remote (fetch first)
1069
    Push {
1070
        /// Git remote: a path or URL
1071
        url: String,
1072
        /// Branch to push to
1073
        #[arg(long, default_value = "main")]
1074
        branch: String,
1075
    },
1076
    /// Recover an import that stopped before it moved the bookmark: point the bookmark at every imported commit's change in the store, so oxy git fetch carries on from there
1077
    Recover,
1078
    /// Check this repository's import of a Git branch: every commit's tree must be reproduced exactly
1079
    Verify {
1080
        /// The Git repository the import came from
1081
        git_dir: PathBuf,
1082
        /// Branch that was imported
1083
        #[arg(long, default_value = "main")]
1084
        branch: String,
1085
        /// Rebuild only the last N commits (the others are followed, not checked)
1086
        #[arg(long, value_name = "N")]
1087
        last: Option<usize>,
1088
        /// Stop after N differences (0: check every commit whatever it finds)
1089
        #[arg(long, value_name = "N", default_value_t = 50)]
1090
        max_differences: usize,
1091
    },
1092
    /// Import a Git branch into a new Oxygen repository at DEST
1093
    Import {
1094
        /// Git repository to read
1095
        git_dir: PathBuf,
1096
        /// Directory for the new Oxygen repository
1097
        dest: PathBuf,
1098
        /// Branch to import
1099
        #[arg(long, default_value = "main")]
1100
        branch: String,
1101
        /// Author your own commits (Git author email = your identity's) with your key; sets git.claim
1102
        #[arg(long)]
1103
        claim: bool,
1104
    },
1105
}
1106
1107
#[derive(Subcommand)]
1108
enum PolicyCmd {
1109
    /// Create the initial policy with your identity as the only root
1110
    Init,
1111
    /// Show the current policy
1112
    Show {
1113
        /// Machine-readable output: one JSON document on stdout
1114
        #[arg(long)]
1115
        json: bool,
1116
    },
1117
    /// Allow an identity (from `oxy identity export`) to update bookmarks
1118
    Grant {
1119
        /// The identity to grant, exported with `oxy identity export`
1120
        identity_file: PathBuf,
1121
        /// Reference pattern the grant applies to (default: every bookmark)
1122
        #[arg(long)]
1123
        pattern: Option<String>,
1124
        /// Where to write the proposal when more root signatures are needed
1125
        #[arg(long)]
1126
        out: Option<PathBuf>,
1127
    },
1128
    /// Allow holders of an issuer's certificates for SUBJECT to sign (keyless)
1129
    GrantSubject {
1130
        /// Who: `repo:OWNER/NAME:bookmark:main`, `agent:NAME@ACCOUNT`; `*` matches any run of characters
1131
        subject: String,
1132
        /// The issuer's identity file, e.g. a hub's /.well-known/oxyhub-key
1133
        #[arg(long)]
1134
        issuer: PathBuf,
1135
        /// What its keys may sign: changes, refs, seal
1136
        #[arg(long, default_value = "changes,refs")]
1137
        scope: String,
1138
        /// Reference pattern the grant applies to (default: every bookmark)
1139
        #[arg(long)]
1140
        pattern: Option<String>,
1141
        /// Where to write the proposal when more root signatures are needed
1142
        #[arg(long)]
1143
        out: Option<PathBuf>,
1144
    },
1145
    /// Revoke a key (KeyId prefix of an identity in the policy)
1146
    Revoke {
1147
        /// KeyId prefix of the identity
1148
        key: String,
1149
        /// Where to write the proposal when more root signatures are needed
1150
        #[arg(long)]
1151
        out: Option<PathBuf>,
1152
    },
1153
    /// Manage root keys and the signature threshold for policy updates
1154
    Root {
1155
        #[command(subcommand)]
1156
        cmd: RootCmd,
1157
    },
1158
    /// Add your root signature to a proposal file; applies it once the threshold is met
1159
    Sign {
1160
        /// The proposal file to sign
1161
        proposal: PathBuf,
1162
        /// Where to write the proposal if it still needs signatures (default: in place)
1163
        #[arg(long)]
1164
        out: Option<PathBuf>,
1165
    },
1166
}
1167
1168
#[derive(Subcommand)]
1169
enum RootCmd {
1170
    /// Make an exported identity a root key
1171
    Add {
1172
        /// The identity to make a root, exported with `oxy identity export`
1173
        identity_file: PathBuf,
1174
        /// Where to write the proposal when more root signatures are needed
1175
        #[arg(long)]
1176
        out: Option<PathBuf>,
1177
    },
1178
    /// Remove a root key by KeyId prefix
1179
    Remove {
1180
        /// KeyId prefix of the root key
1181
        key: String,
1182
        /// Where to write the proposal when more root signatures are needed
1183
        #[arg(long)]
1184
        out: Option<PathBuf>,
1185
    },
1186
    /// Require this many root signatures on policy updates
1187
    Threshold {
1188
        /// Number of root signatures
1189
        n: u32,
1190
        /// Where to write the proposal when more root signatures are needed
1191
        #[arg(long)]
1192
        out: Option<PathBuf>,
1193
    },
1194
}
1195
1196
/// Open the repository containing the current directory.
1197122×
fn open() -> Result<Repo> {
1198122×
    let repo = Repo::open(&std::env::current_dir()?).context("opening repository")?;
1199122×
    match AT_OP.get().and_then(|o| o.as_deref()) {
12000×
        Some(op) => Ok(repo.at_op(op)?),
1201122×
        None => Ok(repo),
1202
    }
1203122×
}
1204
1205
/// `oxy man`: every page into `dir`, or the top-level page on stdout.
12062×
fn man(dir: Option<PathBuf>) -> Result<i32> {
1207
    use clap::CommandFactory;
12082×
    let cmd = Cli::command();
12092×
    match dir {
12101×
        Some(dir) => {
12111×
            std::fs::create_dir_all(&dir)?;
12121×
            clap_mangen::generate_to(cmd, &dir)?;
12131×
            let pages = std::fs::read_dir(&dir)?
1214127×
                .filter(|e| {
1215127×
                    e.as_ref()
1216127×
                        .is_ok_and(|e| e.path().extension().is_some_and(|x| x == "1"))
1217127×
                })
12181×
                .count();
12191×
            println!(
1220
                "{}",
12211×
                ui::done(format!("Wrote {pages} man pages to {}", dir.display()))
1222
            );
1223
        }
12241×
        None => clap_mangen::Man::new(cmd).render(&mut std::io::stdout())?,
1225
    }
12262×
    Ok(0)
12272×
}
1228
1229
/// Parse arguments and run; returns the process exit code.
1230152×
pub fn run() -> Result<i32> {
1231
    // The cloud stores (s3://, gs://, az://) for remotes kept on them.
1232152×
    oxy_strata::register();
1233152×
    let cli = Cli::parse();
1234152×
    ui::init(cli.plain);
1235152×
    AT_OP.set(cli.at_op.clone()).ok();
12360×
    match cli.cmd {
12374×
        Cmd::Completions { shell } => {
1238
            use clap::CommandFactory;
12394×
            clap_complete::generate(shell, &mut Cli::command(), "oxy", &mut std::io::stdout());
12404×
            Ok(0)
1241
        }
12422×
        Cmd::Man { dir } => man(dir),
124314×
        Cmd::Init { path, bare, store } => workspace::init(path, bare, store.as_deref()),
12448×
        Cmd::Status { json } => workspace::status(json),
1245
        Cmd::Prompt {
12460×
            format,
12470×
            glyphs,
12480×
            color,
12490×
        } => workspace::prompt(&format, glyphs, color.as_deref()),
1250
        Cmd::Commit {
125126×
            message,
125226×
            file,
125326×
            depends_on,
125426×
            paths,
125526×
            interactive,
125626×
            hunks,
1257
        } => {
125826×
            let message = match (message, file) {
125925×
                (Some(m), _) => m,
12601×
                (None, Some(f)) if f.as_os_str() == "-" => {
12610×
                    let mut s = String::new();
12620×
                    std::io::Read::read_to_string(&mut std::io::stdin(), &mut s)?;
12630×
                    s
1264
                }
12651×
                (None, Some(f)) => std::fs::read_to_string(&f).map_err(|e| {
12660×
                    anyhow::anyhow!("reading the message from {}: {e}", f.display())
12670×
                })?,
12680×
                (None, None) => anyhow::bail!("a commit message is required (-m or -F)"),
1269
            };
127026×
            workspace::commit(&message, &depends_on, &paths, interactive, &hunks)
1271
        }
12722×
        Cmd::Cat { path, change } => workspace::cat(&path, change.as_deref()),
12731×
        Cmd::Root { json } => workspace::root(json),
12741×
        Cmd::Version { json } => workspace::version(json),
12752×
        Cmd::Discard { paths, hunks } => workspace::discard(&paths, &hunks),
12762×
        Cmd::Hunks { json } => workspace::hunks(json),
12770×
        Cmd::Squash { change, into } => workspace::squash(&change, &into),
1278
        Cmd::Rebase {
12790×
            change,
12800×
            onto,
12810×
            into_working_copy,
12820×
        } => workspace::rebase(&change, &onto, into_working_copy),
12830×
        Cmd::Blame { path, json } => workspace::blame(&path, json),
12841×
        Cmd::Bisect { cmd } => match cmd {
12850×
            BisectCmd::Start => workspace::bisect_start(),
12860×
            BisectCmd::Good { changes } => workspace::bisect_mark(Some(true), &changes),
12870×
            BisectCmd::Bad { changes } => workspace::bisect_mark(Some(false), &changes),
12880×
            BisectCmd::Skip => workspace::bisect_mark(None, &[]),
12890×
            BisectCmd::Run { command } => workspace::bisect_run(&command),
12901×
            BisectCmd::Status { json } => workspace::bisect_status(json),
12910×
            BisectCmd::Reset => workspace::bisect_reset(),
1292
        },
1293
        Cmd::Log {
12948×
            revset,
12958×
            limit,
12968×
            verbose,
12978×
            path,
12988×
            lines,
12998×
            json,
13008×
        } => workspace::log(
13018×
            revset.as_deref(),
13028×
            limit,
13038×
            verbose,
13048×
            path.as_deref(),
13058×
            lines.as_deref(),
13068×
            json,
1307
        ),
13081×
        Cmd::Diff { change, json } => workspace::diff(change.as_deref(), json),
1309
        Cmd::Describe {
13104×
            message,
13114×
            change,
13124×
            json,
13134×
        } => match message {
13140×
            Some(m) => workspace::describe(&m, change.as_deref()),
13154×
            None if change.is_some() => {
13160×
                anyhow::bail!(
1317
                    "naming works on the working copy; to change a head's message pass -m"
1318
                )
1319
            }
13204×
            None => workspace::describe_name(json),
1321
        },
1322
        Cmd::Amend {
13230×
            message,
13240×
            into,
13250×
            paths,
13260×
        } => workspace::amend(message.as_deref(), into.as_deref(), &paths),
13270×
        Cmd::Split { change, paths } => workspace::split(&change, &paths),
1328
        Cmd::Abandon {
13290×
            change,
13300×
            revset,
13310×
            yes,
13320×
        } => workspace::abandon(change.as_deref(), revset.as_deref(), yes),
1333
        Cmd::Show {
13343×
            change,
13353×
            revset,
13363×
            stat,
13373×
            json,
13383×
        } => workspace::show(change.as_deref(), revset.as_deref(), stat, json),
1339
        Cmd::Checkout {
13400×
            changes,
13410×
            discard,
13420×
            carry,
13430×
        } => workspace::checkout(&changes, discard, carry),
1344
        Cmd::Resolve {
13451×
            paths,
13461×
            both,
13471×
            reverse,
13481×
            json,
13491×
        } => workspace::resolve(&paths, both, reverse, json),
13500×
        Cmd::Undo => workspace::undo(),
1351
        Cmd::Gc {
13522×
            now,
13532×
            expire_snapshots,
13542×
            remote,
13552×
        } => match remote {
13562×
            Some(name) => workspace::gc_remote(&name, now),
13570×
            None => workspace::gc(now, expire_snapshots),
1358
        },
13590×
        Cmd::Fsck { json } => workspace::fsck(json),
13600×
        Cmd::Doctor { offline, json } => workspace::doctor(offline, json),
13610×
        Cmd::Checkpoint => workspace::checkpoint(),
13621×
        Cmd::Op { cmd } => match cmd {
13631×
            OpCmd::Log { limit, json } => workspace::op_log(limit, json),
13640×
            OpCmd::Restore { op, discard } => workspace::op_restore(&op, discard),
13650×
            OpCmd::Revert { op } => workspace::op_revert(&op),
1366
        },
1367
        Cmd::Clone {
13685×
            url,
13695×
            dest,
13705×
            max_blob,
13715×
            sparse,
13725×
            depth,
13735×
            recursive,
13745×
        } => sync::clone(&url, dest, max_blob.as_deref(), &sparse, depth, recursive),
13750×
        Cmd::Deepen { depth, remote } => sync::deepen(&remote, depth),
13760×
        Cmd::Sparse { paths } => workspace::sparse(&paths),
13770×
        Cmd::Lock { path } => workspace::lock(&path),
13780×
        Cmd::Unlock { path } => workspace::unlock(&path),
13791×
        Cmd::Workspace { cmd } => match cmd {
13800×
            WorkspaceCmd::Add { path, changes } => workspace::workspace_add(&path, &changes),
13811×
            WorkspaceCmd::List { json } => workspace::workspace_list(json),
13820×
            WorkspaceCmd::Forget { workspace } => workspace::workspace_forget(&workspace),
1383
        },
138417×
        Cmd::Bookmark { cmd } => match cmd {
1385
            BookmarkCmd::Create {
13866×
                name,
13876×
                changes,
13886×
                upto,
13896×
                only,
13906×
            } => workspace::bookmark_create(&name, &changes, upto, only),
1391
            BookmarkCmd::Set {
13923×
                name,
13933×
                changes,
13943×
                upto,
13953×
                only,
13963×
            } => workspace::bookmark_set(&name, &changes, upto, only),
13970×
            BookmarkCmd::Delete { name } => workspace::bookmark_delete(&name),
13985×
            BookmarkCmd::List { all, verbose, json } => {
13995×
                workspace::bookmark_list(all, verbose, json)
1400
            }
14010×
            BookmarkCmd::Show { name, json } => workspace::bookmark_show(name.as_deref(), json),
1402
            BookmarkCmd::Switch {
14033×
                name,
14043×
                discard,
14053×
                carry,
14063×
            } => workspace::bookmark_switch(&name, discard, carry),
1407
        },
14080×
        Cmd::Merge { bookmark } => workspace::merge(&bookmark),
14090×
        Cmd::Auth { cmd } => match cmd {
1410
            AuthCmd::Login {
14110×
                hub,
14120×
                with_token,
14130×
                ca,
14140×
            } => auth::login(hub.as_deref(), with_token, ca),
14150×
            AuthCmd::Status => auth::status(),
14160×
            AuthCmd::Logout { hub } => auth::logout(hub.as_deref()),
14170×
            AuthCmd::Grant { hub } => auth::grant(hub.as_deref()),
1418
        },
14191×
        Cmd::Submodule { cmd } => match cmd {
1420
            SubmoduleCmd::Add {
14210×
                url,
14220×
                path,
14230×
                bookmark,
14240×
            } => sync::submodule_add(&url, &path, bookmark.as_deref()),
14250×
            SubmoduleCmd::Update { init, paths } => sync::submodule_update(init, &paths),
14261×
            SubmoduleCmd::Status { json } => sync::submodule_status(json),
14270×
            SubmoduleCmd::Remove { path } => sync::submodule_remove(&path),
1428
        },
1429
        Cmd::Witness {
1430
            cmd:
1431
                WitnessCmd::Serve {
14320×
                    name,
14330×
                    logs,
14340×
                    state,
14350×
                    bind,
14360×
                    tls_cert,
14370×
                    tls_key,
1438
                },
14390×
        } => tlog::witness_serve(&name, &logs, state, &bind, tls_cert.zip(tls_key)),
14400×
        Cmd::Tlog { cmd } => match cmd {
1441
            TlogCmd::Verify {
14420×
                hub,
14430×
                key,
14440×
                all,
14450×
                json,
14460×
            } => tlog::verify(hub.as_deref(), key.as_deref(), all, json),
1447
        },
144810×
        Cmd::Receipt { cmd } => match cmd {
14492×
            ReceiptCmd::List { rev, json } => receipt::list(rev.as_deref(), json),
1450
            ReceiptCmd::Show {
14511×
                receipt: r,
14521×
                rev,
14531×
                json,
14541×
            } => receipt::show(&r, rev.as_deref(), json),
1455
            ReceiptCmd::Export {
14563×
                receipt: r,
14573×
                file,
14583×
                rev,
14593×
                format,
14603×
            } => receipt::export(&r, &file, rev.as_deref(), &format),
14612×
            ReceiptCmd::Verify { file, json } => receipt::verify(&file, json),
14622×
            ReceiptCmd::Import { file } => receipt::import(&file),
1463
        },
14643×
        Cmd::Tag { cmd } => match cmd {
1465
            TagCmd::Create {
14662×
                name,
14672×
                changes,
14682×
                message,
14692×
            } => workspace::tag_create(&name, &changes, &message),
14700×
            TagCmd::Delete { name, reason } => workspace::tag_delete(&name, &reason),
14711×
            TagCmd::List { json } => workspace::tag_list(json),
1472
        },
147310×
        Cmd::Remote { cmd } => match cmd {
1474
            RemoteCmd::Add {
14754×
                name,
14764×
                url,
14774×
                profile,
14784×
                region,
14794×
                endpoint,
14804×
                credentials,
14814×
            } => sync::remote_add(
14824×
                &name,
14834×
                &url,
14844×
                &[
14854×
                    ("profile", profile),
14864×
                    ("region", region),
14874×
                    ("endpoint", endpoint),
14884×
                    ("credentials", credentials),
14894×
                ],
1490
            ),
14913×
            RemoteCmd::Check { name } => sync::remote_check(&name),
14921×
            RemoteCmd::Remove { name } => sync::remote_remove(&name),
14932×
            RemoteCmd::List { json } => sync::remote_list(json),
1494
        },
14951×
        Cmd::Fetch { remote, json } => sync::fetch(remote.as_deref(), json),
14964×
        Cmd::Pull { remote, json } => sync::pull(remote.as_deref(), json),
1497
        Cmd::Push {
14987×
            remote,
14997×
            force,
15007×
            bookmark,
15017×
            delete,
15027×
            json,
15037×
        } => match delete {
15040×
            Some(name) => sync::push_delete(remote.as_deref(), &name, json),
15057×
            None => sync::push(remote.as_deref(), force, bookmark.as_deref(), json),
1506
        },
1507
        Cmd::Serve {
15080×
            root,
15090×
            bind,
15100×
            tokens,
15110×
            tls_cert,
15120×
            tls_key,
15130×
        } => sync::serve(root, &bind, tokens, tls_cert.zip(tls_key)),
15143×
        Cmd::Identity { cmd } => match cmd {
1515
            IdentityCmd::Create {
15162×
                name,
15172×
                email,
15182×
                local,
15192×
                expires,
15202×
            } => identity::create(name, email, local, expires),
15210×
            IdentityCmd::Show => identity::show(),
15221×
            IdentityCmd::Export { path, pem } => identity::export(path, pem),
15230×
            IdentityCmd::SignChallenge { challenge, json } => {
15240×
                identity::sign_challenge(&challenge, json)
1525
            }
15260×
            IdentityCmd::Rotate => identity::rotate(),
15270×
            IdentityCmd::Renew { expires } => identity::renew(expires),
15280×
            IdentityCmd::Revoke { key, reason } => identity::revoke(key.as_deref(), &reason),
15290×
            IdentityCmd::Delegate { cmd } => match cmd {
1530
                DelegateCmd::Add {
15310×
                    identity_file,
15320×
                    scope,
15330×
                    expires,
15340×
                } => identity::delegate_add(&identity_file, &scope, expires.as_deref()),
15350×
                DelegateCmd::Remove { key } => identity::delegate_remove(&key),
15360×
                DelegateCmd::List => identity::delegate_list(),
1537
            },
1538
        },
1539
        Cmd::Git {
15400×
            cmd: GitCmd::Export { git_dir, branch },
15410×
        } => sync::git_export(&git_dir, branch.as_deref()),
1542
        Cmd::Git {
15430×
            cmd: GitCmd::Fetch { url, branch, claim },
15440×
        } => sync::git_fetch(&url, &branch, claim),
1545
        Cmd::Git {
15460×
            cmd: GitCmd::Push { url, branch },
15470×
        } => sync::git_push(&url, &branch),
1548
        Cmd::Git {
1549
            cmd: GitCmd::Recover,
15500×
        } => sync::git_recover(),
1551
        Cmd::Git {
1552
            cmd:
1553
                GitCmd::Import {
15540×
                    git_dir,
15550×
                    dest,
15560×
                    branch,
15570×
                    claim,
1558
                },
15590×
        } => sync::git_import(&git_dir, &dest, &branch, claim),
1560
        Cmd::Git {
1561
            cmd:
1562
                GitCmd::Verify {
15630×
                    git_dir,
15640×
                    branch,
15650×
                    last,
15660×
                    max_differences,
1567
                },
15680×
        } => sync::git_verify(&git_dir, &branch, last, max_differences),
15695×
        Cmd::Policy { cmd } => match cmd {
15701×
            PolicyCmd::Init => identity::policy_init(),
15713×
            PolicyCmd::Show { json } => identity::policy_show(json),
1572
            PolicyCmd::Grant {
15730×
                identity_file,
15740×
                pattern,
15750×
                out,
15760×
            } => identity::policy_grant(&identity_file, pattern.as_deref(), out),
1577
            PolicyCmd::GrantSubject {
15781×
                subject,
15791×
                issuer,
15801×
                scope,
15811×
                pattern,
15821×
                out,
15831×
            } => identity::policy_grant_subject(&subject, &issuer, &scope, pattern.as_deref(), out),
15840×
            PolicyCmd::Revoke { key, out } => identity::policy_revoke(&key, out),
15850×
            PolicyCmd::Root { cmd } => match cmd {
15860×
                RootCmd::Add { identity_file, out } => {
15870×
                    identity::policy_root_add(&identity_file, out)
1588
                }
15890×
                RootCmd::Remove { key, out } => identity::policy_root_remove(&key, out),
15900×
                RootCmd::Threshold { n, out } => identity::policy_threshold(n, out),
1591
            },
15920×
            PolicyCmd::Sign { proposal, out } => identity::policy_sign(&proposal, out),
1593
        },
1594
        Cmd::Seal {
15950×
            flow: Some(url),
15960×
            job: Some(job),
15970×
            reject,
15980×
            json,
1599
            ..
16000×
        } => approve::seal(&url, &job, reject, json),
1601
        Cmd::Seal {
16020×
            changes,
16030×
            proposal: Some(url),
16040×
            json,
1605
            ..
16060×
        } if changes.is_empty() => proposal::seal(&url, json),
16070×
        Cmd::Seal { changes, json, .. } => identity::seal(&changes, json),
16080×
        Cmd::Proposal { cmd } => match cmd {
16090×
            ProposalCmd::Show { url, json } => proposal::show(&url, json),
16100×
            ProposalCmd::Land { url, force } => proposal::land(&url, force),
1611
        },
1612
        Cmd::Verify {
16131×
            change,
16141×
            revset,
16151×
            json,
16161×
        } => identity::verify(change.as_deref(), revset.as_deref(), json),
1617
        Cmd::Config {
16181×
            key,
16191×
            value,
16201×
            user,
16211×
            unset,
16221×
        } => identity::config(&key, value.as_deref().unwrap_or(""), user, unset),
1623
    }
1624152×
}
1625
1626
#[cfg(test)]
1627
mod help_tests {
1628
    use super::Cli;
1629
    use clap::CommandFactory;
1630
1631
    /// Every command and every argument explains itself, so `--help`,
1632
    /// completions and the man pages are complete.
1633
    #[test]
16341×
    fn every_command_and_argument_has_help() {
1635127×
        fn walk(cmd: &clap::Command, path: &str, missing: &mut Vec<String>) {
1636127×
            let here = format!("{path} {}", cmd.get_name()).trim().to_string();
1637127×
            if cmd.get_about().is_none() && !path.is_empty() {
16380×
                missing.push(format!("{here} (command)"));
1639127×
            }
1640238×
            for a in cmd.get_arguments() {
1641238×
                let id = a.get_id().as_str();
1642238×
                if id == "help" || id == "version" || (a.is_global_set() && !path.is_empty()) {
16430×
                    continue;
1644238×
                }
1645238×
                if a.get_help().is_none() && a.get_long_help().is_none() {
16460×
                    missing.push(format!("{here} {id}"));
1647238×
                }
1648
            }
1649127×
            for sub in cmd.get_subcommands() {
1650126×
                walk(sub, &here, missing);
1651126×
            }
1652127×
        }
16531×
        let mut missing = Vec::new();
16541×
        walk(&Cli::command(), "", &mut missing);
16551×
        assert!(
16561×
            missing.is_empty(),
1657
            "{} without help:\n{}",
16580×
            missing.len(),
16590×
            missing.join("\n")
1660
        );
16611×
    }
1662
}