OXYGENHUB

Coverage / src/cli/mod.rs 58.1%

209 of 360 lines run, 9 of 18 functions · run #369 on main · 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
    Resolve {
302
        /// Machine-readable output: one JSON document on stdout
303
        #[arg(long)]
304
        json: bool,
305
    },
306
    /// Undo the last operation
307
    Undo,
308
    /// Write a checkpoint of the current bookmark's state now
309
    Checkpoint,
310
    /// Print a shell completion script: `oxy completions bash > ~/.local/share/bash-completion/completions/oxy`
311
    Completions {
312
        /// bash, zsh, fish, elvish or powershell
313
        shell: clap_complete::Shell,
314
    },
315
    /// Write man pages (oxy.1 and one per command) into DIR, or print oxy.1
316
    Man {
317
        /// Directory to write every page into, e.g. ~/.local/share/man/man1
318
        #[arg(long)]
319
        dir: Option<PathBuf>,
320
    },
321
    /// Check the identity, configuration and remotes, and say what to do about any problem
322
    Doctor {
323
        /// Do not contact the remotes
324
        #[arg(long)]
325
        offline: bool,
326
        /// Machine-readable output: one JSON document on stdout
327
        #[arg(long)]
328
        json: bool,
329
    },
330
    /// Check the repository's integrity: objects, operation log, references, signatures, checkpoints
331
    Fsck {
332
        /// Machine-readable output: one JSON document on stdout
333
        #[arg(long)]
334
        json: bool,
335
    },
336
    /// Prune unreachable objects older than the grace period and repack everything into one pack
337
    Gc {
338
        /// Ignore the grace period (core.gc-grace) and prune immediately
339
        #[arg(long)]
340
        now: bool,
341
        /// Keep no past working-copy snapshots, only each workspace's current one, so files only they held go (core.snapshot-retention, default 7 days)
342
        #[arg(long)]
343
        expire_snapshots: bool,
344
        /// 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)
345
        #[arg(long, value_name = "NAME")]
346
        remote: Option<String>,
347
    },
348
    /// List operations
349
    #[command(name = "op")]
350
    Op {
351
        #[command(subcommand)]
352
        cmd: OpCmd,
353
    },
354
    /// Take the advisory local lock on a lockable file, making it writable
355
    Lock {
356
        /// The lockable file
357
        path: String,
358
    },
359
    /// Release the local lock and make the file read-only again
360
    Unlock {
361
        /// The locked file
362
        path: String,
363
    },
364
    /// Workspaces: extra checkout directories sharing this repository
365
    Workspace {
366
        #[command(subcommand)]
367
        cmd: WorkspaceCmd,
368
    },
369
    /// Bookmarks: named states; each workspace works on one
370
    Bookmark {
371
        #[command(subcommand)]
372
        cmd: BookmarkCmd,
373
    },
374
    /// Union another bookmark's heads into the current one (no merge change):
375
    /// a bookmark here, or a remote's (`origin/develop`) as last fetched
376
    Merge {
377
        /// The bookmark whose heads to add
378
        bookmark: String,
379
    },
380
    /// Sign in to a hub once, for every repository on it
381
    Auth {
382
        #[command(subcommand)]
383
        cmd: AuthCmd,
384
    },
385
    /// Subrepositories: nested repositories pinned to a state
386
    Submodule {
387
        #[command(subcommand)]
388
        cmd: SubmoduleCmd,
389
    },
390
    /// Witness hubs' transparency logs: cosign each checkpoint that extends
391
    /// the last one cosigned (spec §14.8)
392
    Witness {
393
        #[command(subcommand)]
394
        cmd: WitnessCmd,
395
    },
396
    /// Hubs' transparency logs: check that a hub shows everyone the same
397
    /// history (spec §14.8)
398
    Tlog {
399
        #[command(subcommand)]
400
        cmd: TlogCmd,
401
    },
402
    /// Execution receipts: signed records of what a runner ran on a state
403
    Receipt {
404
        #[command(subcommand)]
405
        cmd: ReceiptCmd,
406
    },
407
    /// Tags: immutable named states
408
    Tag {
409
        #[command(subcommand)]
410
        cmd: TagCmd,
411
    },
412
    /// Manage remotes
413
    Remote {
414
        #[command(subcommand)]
415
        cmd: RemoteCmd,
416
    },
417
    /// Fetch every bookmark of a remote into its tracking references
418
    Fetch {
419
        /// Remote name (default: origin)
420
        remote: Option<String>,
421
        /// Machine-readable output: one JSON document on stdout
422
        #[arg(long)]
423
        json: bool,
424
    },
425
    /// Fetch, then union the remote bookmark into the local one (no merge change)
426
    Pull {
427
        /// Remote name (default: origin)
428
        remote: Option<String>,
429
        /// Machine-readable output: one JSON document on stdout
430
        #[arg(long)]
431
        json: bool,
432
    },
433
    /// Push the current bookmark to a remote
434
    Push {
435
        /// Remote name (default: origin)
436
        remote: Option<String>,
437
        /// Allow dropping heads the remote has
438
        #[arg(long)]
439
        force: bool,
440
        /// Push this bookmark instead of the workspace's current one
441
        #[arg(short = 'b', long)]
442
        bookmark: Option<String>,
443
        /// Delete this bookmark on the remote (never its default bookmark);
444
        /// the local bookmark is kept. `tags/NAME` withdraws a tag there
445
        /// (and here, as `oxy tag delete` does)
446
        #[arg(long, value_name = "NAME", conflicts_with_all = ["bookmark", "force"])]
447
        delete: Option<String>,
448
        /// Machine-readable output: one JSON document on stdout
449
        #[arg(long)]
450
        json: bool,
451
    },
452
    /// Serve repositories under ROOT over HTTPS (with --tls-cert/--tls-key) or plain HTTP
453
    Serve {
454
        /// Directory with one repository per subdirectory
455
        #[arg(long, default_value = ".")]
456
        root: PathBuf,
457
        /// Address and port to listen on
458
        #[arg(long, default_value = "127.0.0.1:8890")]
459
        bind: String,
460
        /// Bearer token allowed to push (repeatable); none means open
461
        #[arg(long = "token")]
462
        tokens: Vec<String>,
463
        /// PEM certificate chain; enables HTTPS together with --tls-key
464
        #[arg(long, requires = "tls_key")]
465
        tls_cert: Option<PathBuf>,
466
        /// PEM private key
467
        #[arg(long, requires = "tls_cert")]
468
        tls_key: Option<PathBuf>,
469
    },
470
    /// Manage the signing identity
471
    Identity {
472
        #[command(subcommand)]
473
        cmd: IdentityCmd,
474
    },
475
    /// Seal changes with your key: named ones, or every change on the current bookmark you can authenticate but have not
476
    Seal {
477
        /// Changes to seal (default: every change on the bookmark you can authenticate and have not sealed)
478
        changes: Vec<String>,
479
        /// Seal every change of a hub proposal and push the seal (your approval)
480
        #[arg(long, conflicts_with = "changes")]
481
        proposal: Option<String>,
482
        /// Approve a job of a hub run that waits for a sealed approval: `--flow https://hub/OWNER/NAME/flow/N --job apply`
483
        #[arg(long, conflicts_with_all = ["changes", "proposal"], requires = "job")]
484
        flow: Option<String>,
485
        /// The job to approve, with --flow
486
        #[arg(long, requires = "flow")]
487
        job: Option<String>,
488
        /// Reject the job instead of approving it, with --flow
489
        #[arg(long, requires = "flow")]
490
        reject: bool,
491
        /// Machine-readable output: one JSON document on stdout
492
        #[arg(long)]
493
        json: bool,
494
    },
495
    /// Hub proposals: show one, or land it from this client
496
    Proposal {
497
        #[command(subcommand)]
498
        cmd: ProposalCmd,
499
    },
500
    /// Verify the signatures on a change (default: every change in the log)
501
    Verify {
502
        /// The change to verify (default: every change in the log)
503
        change: Option<String>,
504
        /// Verify every change a revset selects: `release..main & unsigned()`
505
        #[arg(short = 'r', long, value_name = "REVSET", conflicts_with = "change")]
506
        revset: Option<String>,
507
        /// Machine-readable output: one JSON document on stdout
508
        #[arg(long)]
509
        json: bool,
510
    },
511
    /// Git interoperability
512
    Git {
513
        #[command(subcommand)]
514
        cmd: GitCmd,
515
    },
516
    /// Manage the trust policy (who may update which bookmarks)
517
    Policy {
518
        #[command(subcommand)]
519
        cmd: PolicyCmd,
520
    },
521
    /// 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)
522
    Config {
523
        /// The key, such as author.name or remote.origin.token
524
        key: String,
525
        /// The value to set (none with --unset); `-` reads it from standard input, out of the shell's history
526
        #[arg(required_unless_present = "unset")]
527
        value: Option<String>,
528
        /// Write to the user file (~/.config/oxy/config.toml) instead of this repository
529
        #[arg(long)]
530
        user: bool,
531
        /// Remove the key from this repository's file (or the user file with --user)
532
        #[arg(long)]
533
        unset: bool,
534
    },
535
}
536
537
#[derive(Subcommand)]
538
enum OpCmd {
539
    /// List operations, newest first, with what each changed
540
    Log {
541
        /// Show only the latest N (snapshots folded together count as one)
542
        #[arg(short = 'n', long)]
543
        limit: Option<usize>,
544
        /// Machine-readable output: one JSON document on stdout
545
        #[arg(long)]
546
        json: bool,
547
    },
548
    /// Go back to the bookmarks and working copy as they were after OP (recorded as a new operation, so it can be undone)
549
    Restore {
550
        /// The operation: an ID prefix from oxy op log, @ or @-
551
        op: String,
552
        /// Throw away uncommitted working-copy changes
553
        #[arg(long)]
554
        discard: bool,
555
    },
556
    /// Undo OP alone, keeping every operation after it: the bookmarks it moved go back
557
    Revert {
558
        /// The operation: an ID prefix from oxy op log
559
        op: String,
560
    },
561
}
562
563
#[derive(Subcommand)]
564
enum WorkspaceCmd {
565
    /// Create a workspace at PATH checked out on CHANGE... (default: the bookmark heads)
566
    Add {
567
        /// Directory for the new workspace (must be empty or absent)
568
        path: PathBuf,
569
        /// Changes whose state to check out there
570
        changes: Vec<String>,
571
    },
572
    /// List workspaces and their working-copy changes
573
    List {
574
        /// Machine-readable output: one JSON document on stdout
575
        #[arg(long)]
576
        json: bool,
577
    },
578
    /// Stop tracking a workspace (by id prefix or path); its files stay
579
    Forget {
580
        /// Workspace id prefix or path
581
        workspace: String,
582
    },
583
}
584
585
#[derive(Subcommand)]
586
enum BisectCmd {
587
    /// Begin; remembers where the working copy was
588
    Start,
589
    /// Mark CHANGE... (or the state under test) as good
590
    Good {
591
        /// Changes known to be good (default: the state under test)
592
        changes: Vec<String>,
593
    },
594
    /// Mark CHANGE... (or the state under test) as bad
595
    Bad {
596
        /// Changes known to be bad (default: the state under test)
597
        changes: Vec<String>,
598
    },
599
    /// The state under test cannot be tested; try another
600
    Skip,
601
    /// Run a command for each step: exit 0 good, 125 skip, else bad
602
    Run {
603
        /// Command and arguments to run at each step
604
        #[arg(required = true, trailing_var_arg = true)]
605
        command: Vec<String>,
606
    },
607
    /// Where the search stands
608
    Status {
609
        /// Machine-readable output: one JSON document on stdout
610
        #[arg(long)]
611
        json: bool,
612
    },
613
    /// Restore the working copy and forget the search
614
    Reset,
615
}
616
617
#[derive(Subcommand)]
618
enum BookmarkCmd {
619
    /// Create a bookmark at CHANGE... (default: the working copy's base)
620
    Create {
621
        /// Name of the new bookmark
622
        name: String,
623
        /// Its heads (default: the working copy's base). A state is its
624
        /// heads and what they depend on: a change alone may leave out
625
        /// earlier changes of the current state, which is refused unless
626
        /// --upto or --only says which is meant
627
        changes: Vec<String>,
628
        /// Everything of the current state up to CHANGE..., as a Git
629
        /// branch at a commit would hold
630
        #[arg(long, conflicts_with = "only")]
631
        upto: bool,
632
        /// Just CHANGE... and what they depend on. Other changes to the
633
        /// same files are left out, so a file can come out partly edited;
634
        /// to add one change to a bookmark's work, give the bookmark's
635
        /// heads with it instead: `bookmark set NAME <its heads> CHANGE`
636
        #[arg(long)]
637
        only: bool,
638
    },
639
    /// Point an existing bookmark at CHANGE...
640
    Set {
641
        /// The bookmark to move
642
        name: String,
643
        /// Its new heads. A change alone may leave out changes the
644
        /// bookmark now holds that were recorded before it, which is
645
        /// refused unless --upto or --only says which is meant
646
        changes: Vec<String>,
647
        /// Everything the bookmark holds up to CHANGE..., as a Git branch
648
        /// moved to a commit would hold
649
        #[arg(long, conflicts_with = "only")]
650
        upto: bool,
651
        /// Just CHANGE... and what they depend on. Other changes to the
652
        /// same files are left out, so a file can come out partly edited;
653
        /// to add one change to a bookmark's work, give the bookmark's
654
        /// heads with it instead: `bookmark set NAME <its heads> CHANGE`
655
        #[arg(long)]
656
        only: bool,
657
    },
658
    /// Remove a bookmark; its changes stay
659
    Delete {
660
        /// The bookmark to remove
661
        name: String,
662
    },
663
    /// List bookmarks with their heads and difference from origin
664
    List {
665
        /// Also list remotes' bookmarks that have no local bookmark
666
        #[arg(long, short)]
667
        all: bool,
668
        /// Each head of every bookmark, as `bookmark show` prints them
669
        #[arg(long, short)]
670
        verbose: bool,
671
        /// Machine-readable output: one JSON document on stdout
672
        #[arg(long)]
673
        json: bool,
674
    },
675
    /// A bookmark's heads: each change nothing else on it depends on,
676
    /// newest first. A bookmark name is also a revset for its heads
677
    /// (`oxy log -r NAME`, or `bookmark(NAME)` to mean only the bookmark)
678
    Show {
679
        /// The bookmark, or REMOTE/NAME (default: the current one)
680
        name: Option<String>,
681
        /// Machine-readable output: one JSON document on stdout
682
        #[arg(long)]
683
        json: bool,
684
    },
685
    /// Make NAME this workspace's bookmark and check it out. Uncommitted work stops it; --carry takes the work along, --discard drops it
686
    Switch {
687
        /// The bookmark to work on; one only a remote has (NAME or REMOTE/NAME) is created from it
688
        name: String,
689
        /// Throw away uncommitted working-copy changes
690
        #[arg(long)]
691
        discard: bool,
692
        /// Take uncommitted changes along; refused when NAME has other
693
        /// versions of the files they touch
694
        #[arg(long, conflicts_with = "discard")]
695
        carry: bool,
696
    },
697
}
698
699
#[derive(Subcommand)]
700
enum WitnessCmd {
701
    /// Serve a witness (C2SP tlog-witness: POST /add-checkpoint), signing
702
    /// with this oxy home's key (author.key)
703
    Serve {
704
        /// The witness's name in cosignatures: a host name
705
        #[arg(long)]
706
        name: String,
707
        /// A log to witness, as ORIGIN=FILE: its origin (HOST/log) and its
708
        /// key's identity file (a hub's /.well-known/oxyhub-key); repeatable
709
        #[arg(long = "log", required = true)]
710
        logs: Vec<String>,
711
        /// Where to keep what it cosigned
712
        #[arg(long, default_value = "witness-state")]
713
        state: PathBuf,
714
        /// Address and port to listen on
715
        #[arg(long, default_value = "127.0.0.1:8891")]
716
        bind: String,
717
        /// PEM certificate chain; enables HTTPS together with --tls-key
718
        #[arg(long, requires = "tls_key")]
719
        tls_cert: Option<PathBuf>,
720
        /// PEM private key for --tls-cert
721
        #[arg(long, requires = "tls_cert")]
722
        tls_key: Option<PathBuf>,
723
    },
724
}
725
726
#[derive(Subcommand)]
727
enum TlogCmd {
728
    /// Check a hub's whole log: its checkpoint against the log key and the
729
    /// last checkpoint seen, and every entry against the root it signed;
730
    /// then list this repository's entries (or a key's, or all)
731
    Verify {
732
        /// The hub, as https://oxygenhub.io (default: this checkout's remote)
733
        hub: Option<String>,
734
        /// List the entries a key signed (a KeyId) instead
735
        #[arg(long, conflicts_with = "all")]
736
        key: Option<String>,
737
        /// List every entry
738
        #[arg(long)]
739
        all: bool,
740
        /// Machine-readable output
741
        #[arg(long)]
742
        json: bool,
743
    },
744
}
745
746
#[derive(Subcommand)]
747
enum AuthCmd {
748
    /// Sign in to HUB in the browser (device sign-in) and keep the token for
749
    /// every repository there
750
    Login {
751
        /// The hub, as https://oxygenhub.io or oxygenhub.io (default: this checkout's origin)
752
        hub: Option<String>,
753
        /// Read a token from standard input instead (for CI and scripts)
754
        #[arg(long)]
755
        with_token: bool,
756
        /// PEM file of extra roots to trust (a hub with a self-signed certificate)
757
        #[arg(long)]
758
        ca: Option<PathBuf>,
759
    },
760
    /// List the hubs you are signed in to, checking each token with its hub
761
    Status,
762
    /// Forget HUB's token and revoke it on the hub
763
    Logout {
764
        /// The hub (default: this checkout's origin, or the only one)
765
        hub: Option<String>,
766
    },
767
    /// Keep an agent grant token (from `oh agent grant`) for keyless
768
    /// signing: with `author.signer = keyless`, commits and pushes are
769
    /// signed by fresh keys the hub certifies, in the grantor's name
770
    /// (the token is read from standard input, so it stays out of the
771
    /// shell's history)
772
    Grant {
773
        /// The hub (default: this checkout's origin)
774
        hub: Option<String>,
775
    },
776
}
777
778
#[derive(Subcommand)]
779
enum SubmoduleCmd {
780
    /// Clone URL (Oxygen or Git) into PATH and register it in .oxymodules
781
    Add {
782
        /// URL or path of the Oxygen or Git repository
783
        url: String,
784
        /// Where to put it, relative to the repository root
785
        path: String,
786
        /// Bookmark (Oxygen) or branch (Git) to check out and track
787
        #[arg(long)]
788
        bookmark: Option<String>,
789
    },
790
    /// Bring nested repositories to their pins
791
    Update {
792
        /// Clone nested repositories that are absent
793
        #[arg(long)]
794
        init: bool,
795
        /// Subrepos to update (default: all)
796
        paths: Vec<String>,
797
    },
798
    /// List subrepos with their pins and on-disk state
799
    Status {
800
        /// Machine-readable output: one JSON document on stdout
801
        #[arg(long)]
802
        json: bool,
803
    },
804
    /// Unregister PATH; the directory stays, the node goes with the next commit
805
    Remove {
806
        /// The subrepo to unregister
807
        path: String,
808
    },
809
}
810
811
#[derive(Subcommand)]
812
enum ReceiptCmd {
813
    /// The receipts kept for a state (default: the bookmark heads)
814
    List {
815
        /// Change, bookmark or tag whose state to look at
816
        rev: Option<String>,
817
        /// Machine-readable output: one JSON document on stdout
818
        #[arg(long)]
819
        json: bool,
820
    },
821
    /// One receipt in full, and whether its signature verifies
822
    Show {
823
        /// The receipt (a prefix of its hash, as `list` shows it)
824
        receipt: String,
825
        /// Change, bookmark or tag whose state holds it
826
        #[arg(long)]
827
        rev: Option<String>,
828
        /// Machine-readable output: one JSON document on stdout
829
        #[arg(long)]
830
        json: bool,
831
    },
832
    /// Write a signed receipt to FILE
833
    Export {
834
        /// The receipt (a prefix of its hash, as `list` shows it)
835
        receipt: String,
836
        /// Where to write it
837
        file: String,
838
        /// Change, bookmark or tag whose state holds it
839
        #[arg(long)]
840
        rev: Option<String>,
841
        /// `oxygen`: the runner's signed receipt, as `verify` and `import`
842
        /// read it. `slsa`: SLSA provenance in a DSSE envelope, signed with
843
        /// your key, for in-toto and cosign (`oxy identity export --pem`
844
        /// gives the key they check it with)
845
        #[arg(long, default_value = "oxygen", value_parser = ["oxygen", "slsa"])]
846
        format: String,
847
    },
848
    /// Check a signed receipt in FILE without keeping it
849
    Verify {
850
        /// A signed receipt, as a runner or `export` writes it
851
        file: String,
852
        /// Machine-readable output: one JSON document on stdout
853
        #[arg(long)]
854
        json: bool,
855
    },
856
    /// Verify a signed receipt in FILE and keep it with its state
857
    Import {
858
        /// A signed receipt, as a runner or `export` writes it
859
        file: String,
860
    },
861
}
862
863
#[derive(Subcommand)]
864
enum TagCmd {
865
    /// Tag the state of CHANGE... (default: the bookmark heads)
866
    Create {
867
        /// Name of the tag; tags never move
868
        name: String,
869
        /// Changes whose state to tag (default: the bookmark heads)
870
        changes: Vec<String>,
871
        /// A message recorded with the tag
872
        #[arg(short, long, default_value = "")]
873
        message: String,
874
    },
875
    /// Withdraw a tag: a signed withdrawal, kept beside the tag's creation;
876
    /// `oxy push` sends it to a remote that has the tag (the name is never
877
    /// used again)
878
    Delete {
879
        /// Name of the tag
880
        name: String,
881
        /// Why it is withdrawn, recorded with the withdrawal
882
        #[arg(short, long, default_value = "")]
883
        reason: String,
884
    },
885
    /// List tags and the states they name
886
    List {
887
        /// Machine-readable output: one JSON document on stdout
888
        #[arg(long)]
889
        json: bool,
890
    },
891
}
892
893
#[derive(Subcommand)]
894
enum RemoteCmd {
895
    /// Add a remote
896
    Add {
897
        /// Name to refer to the remote by, such as origin
898
        name: String,
899
        /// 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
900
        url: String,
901
        /// For an s3:// store: the AWS profile to use (default: AWS_PROFILE, else "default")
902
        #[arg(long)]
903
        profile: Option<String>,
904
        /// For an s3:// store: its region (default: the environment, the profile, else asked of S3)
905
        #[arg(long)]
906
        region: Option<String>,
907
        /// For an s3://, gs:// or az:// store: another endpoint, such as an S3-compatible service
908
        #[arg(long)]
909
        endpoint: Option<String>,
910
        /// For a gs:// store: a service account key file, or application default credentials
911
        #[arg(long)]
912
        credentials: Option<String>,
913
    },
914
    /// Check a remote: that it is reachable with the credentials in hand, what it holds, and (for a store) that it behaves
915
    Check {
916
        /// The remote to check
917
        name: String,
918
    },
919
    /// Remove a remote
920
    Remove {
921
        /// The remote to remove
922
        name: String,
923
    },
924
    /// List remotes and their URLs
925
    List {
926
        /// Machine-readable output: one JSON document on stdout
927
        #[arg(long)]
928
        json: bool,
929
    },
930
}
931
932
#[derive(Subcommand)]
933
enum ProposalCmd {
934
    /// Show a proposal by its hub URL
935
    Show {
936
        /// The proposal's URL on the hub
937
        url: String,
938
        /// Machine-readable output: one JSON document on stdout
939
        #[arg(long)]
940
        json: bool,
941
    },
942
    /// Fetch the proposal's changes, set the target bookmark to the projected state, and push with your key
943
    Land {
944
        /// The proposal's URL on the hub
945
        url: String,
946
        /// Land even when the hub reports blockers
947
        #[arg(long)]
948
        force: bool,
949
    },
950
}
951
952
#[derive(Subcommand)]
953
enum IdentityCmd {
954
    /// Generate a composite Ed25519 + ML-DSA-65 key pair and make it your author identity
955
    Create {
956
        /// Your name (default: author.name)
957
        #[arg(long)]
958
        name: Option<String>,
959
        /// Your email (default: author.email)
960
        #[arg(long)]
961
        email: Option<String>,
962
        /// Record the author in this repository only, not in the user config
963
        #[arg(long)]
964
        local: bool,
965
        /// Expire the identity this many days from now
966
        #[arg(long)]
967
        expires: Option<u64>,
968
    },
969
    /// Show the configured identity
970
    Show,
971
    /// Write your identity object to a file, to hand to a policy holder
972
    Export {
973
        /// File to write (default: <key>.identity, or <key>.pem with --pem, in the current directory)
974
        path: Option<PathBuf>,
975
        /// Write the Ed25519 public key as PEM instead, as cosign's --key and
976
        /// other DSSE verifiers take it
977
        #[arg(long)]
978
        pem: bool,
979
    },
980
    /// Sign a host's challenge (hex, at least 16 bytes) to prove you hold your key; prints the signature as hex
981
    SignChallenge {
982
        /// The host's challenge, as hex
983
        challenge: String,
984
        /// Machine-readable output: one JSON document on stdout
985
        #[arg(long)]
986
        json: bool,
987
    },
988
    /// Move to a fresh key pair; the policy follows if it names your key
989
    Rotate,
990
    /// Re-issue your identity with a new expiry (in days from now; 0 = never)
991
    Renew {
992
        /// Days from now until it expires; 0 for never
993
        #[arg(long)]
994
        expires: u64,
995
    },
996
    /// Revoke a key: yours by default, or any key if you hold a policy root
997
    Revoke {
998
        /// KeyId prefix (default: your own key)
999
        key: Option<String>,
1000
        /// Why, recorded in the revocation
1001
        #[arg(long, default_value = "")]
1002
        reason: String,
1003
    },
1004
    /// Keys allowed to sign or seal on your behalf
1005
    Delegate {
1006
        #[command(subcommand)]
1007
        cmd: DelegateCmd,
1008
    },
1009
}
1010
1011
#[derive(Subcommand)]
1012
enum DelegateCmd {
1013
    /// Add a delegate from an exported identity file
1014
    Add {
1015
        /// The delegates' exported identity files: one rotation for all of them
1016
        #[arg(required = true)]
1017
        identity_file: Vec<PathBuf>,
1018
        /// What it may do for you: changes, refs, seal (comma-separated) or all.
1019
        /// An agent gets `changes`; a host that lands for you needs `changes,refs`
1020
        #[arg(long, default_value = "changes")]
1021
        scope: String,
1022
        /// How long for: 30m, 8h, 7d, or a number of days (default: until removed)
1023
        #[arg(long)]
1024
        expires: Option<String>,
1025
    },
1026
    /// Remove a delegate by KeyId prefix
1027
    Remove {
1028
        /// KeyId prefix of the delegate
1029
        key: String,
1030
    },
1031
    /// List delegates
1032
    List,
1033
}
1034
1035
#[derive(Subcommand)]
1036
enum GitCmd {
1037
    /// Export the current bookmark to a Git repository (created bare if absent)
1038
    Export {
1039
        /// Git repository to write to (created bare if absent)
1040
        git_dir: PathBuf,
1041
        /// Git branch to write (default: the bookmark name)
1042
        #[arg(long)]
1043
        branch: Option<String>,
1044
    },
1045
    /// Fetch a branch from a Git remote (path or URL) into the current bookmark
1046
    Fetch {
1047
        /// Git remote: a path or URL
1048
        url: String,
1049
        /// Branch to fetch
1050
        #[arg(long, default_value = "main")]
1051
        branch: String,
1052
        /// Author your own commits (Git author email = your identity's) with your key; sets git.claim
1053
        #[arg(long)]
1054
        claim: bool,
1055
    },
1056
    /// Push the current bookmark to a branch of a Git remote (fetch first)
1057
    Push {
1058
        /// Git remote: a path or URL
1059
        url: String,
1060
        /// Branch to push to
1061
        #[arg(long, default_value = "main")]
1062
        branch: String,
1063
    },
1064
    /// 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
1065
    Recover,
1066
    /// Check this repository's import of a Git branch: every commit's tree must be reproduced exactly
1067
    Verify {
1068
        /// The Git repository the import came from
1069
        git_dir: PathBuf,
1070
        /// Branch that was imported
1071
        #[arg(long, default_value = "main")]
1072
        branch: String,
1073
        /// Rebuild only the last N commits (the others are followed, not checked)
1074
        #[arg(long, value_name = "N")]
1075
        last: Option<usize>,
1076
        /// Stop after N differences (0: check every commit whatever it finds)
1077
        #[arg(long, value_name = "N", default_value_t = 50)]
1078
        max_differences: usize,
1079
    },
1080
    /// Import a Git branch into a new Oxygen repository at DEST
1081
    Import {
1082
        /// Git repository to read
1083
        git_dir: PathBuf,
1084
        /// Directory for the new Oxygen repository
1085
        dest: PathBuf,
1086
        /// Branch to import
1087
        #[arg(long, default_value = "main")]
1088
        branch: String,
1089
        /// Author your own commits (Git author email = your identity's) with your key; sets git.claim
1090
        #[arg(long)]
1091
        claim: bool,
1092
    },
1093
}
1094
1095
#[derive(Subcommand)]
1096
enum PolicyCmd {
1097
    /// Create the initial policy with your identity as the only root
1098
    Init,
1099
    /// Show the current policy
1100
    Show {
1101
        /// Machine-readable output: one JSON document on stdout
1102
        #[arg(long)]
1103
        json: bool,
1104
    },
1105
    /// Allow an identity (from `oxy identity export`) to update bookmarks
1106
    Grant {
1107
        /// The identity to grant, exported with `oxy identity export`
1108
        identity_file: PathBuf,
1109
        /// Reference pattern the grant applies to (default: every bookmark)
1110
        #[arg(long)]
1111
        pattern: Option<String>,
1112
        /// Where to write the proposal when more root signatures are needed
1113
        #[arg(long)]
1114
        out: Option<PathBuf>,
1115
    },
1116
    /// Allow holders of an issuer's certificates for SUBJECT to sign (keyless)
1117
    GrantSubject {
1118
        /// Who: `repo:OWNER/NAME:bookmark:main`, `agent:NAME@ACCOUNT`; `*` matches any run of characters
1119
        subject: String,
1120
        /// The issuer's identity file, e.g. a hub's /.well-known/oxyhub-key
1121
        #[arg(long)]
1122
        issuer: PathBuf,
1123
        /// What its keys may sign: changes, refs, seal
1124
        #[arg(long, default_value = "changes,refs")]
1125
        scope: String,
1126
        /// Reference pattern the grant applies to (default: every bookmark)
1127
        #[arg(long)]
1128
        pattern: Option<String>,
1129
        /// Where to write the proposal when more root signatures are needed
1130
        #[arg(long)]
1131
        out: Option<PathBuf>,
1132
    },
1133
    /// Revoke a key (KeyId prefix of an identity in the policy)
1134
    Revoke {
1135
        /// KeyId prefix of the identity
1136
        key: String,
1137
        /// Where to write the proposal when more root signatures are needed
1138
        #[arg(long)]
1139
        out: Option<PathBuf>,
1140
    },
1141
    /// Manage root keys and the signature threshold for policy updates
1142
    Root {
1143
        #[command(subcommand)]
1144
        cmd: RootCmd,
1145
    },
1146
    /// Add your root signature to a proposal file; applies it once the threshold is met
1147
    Sign {
1148
        /// The proposal file to sign
1149
        proposal: PathBuf,
1150
        /// Where to write the proposal if it still needs signatures (default: in place)
1151
        #[arg(long)]
1152
        out: Option<PathBuf>,
1153
    },
1154
}
1155
1156
#[derive(Subcommand)]
1157
enum RootCmd {
1158
    /// Make an exported identity a root key
1159
    Add {
1160
        /// The identity to make a root, exported with `oxy identity export`
1161
        identity_file: PathBuf,
1162
        /// Where to write the proposal when more root signatures are needed
1163
        #[arg(long)]
1164
        out: Option<PathBuf>,
1165
    },
1166
    /// Remove a root key by KeyId prefix
1167
    Remove {
1168
        /// KeyId prefix of the root key
1169
        key: String,
1170
        /// Where to write the proposal when more root signatures are needed
1171
        #[arg(long)]
1172
        out: Option<PathBuf>,
1173
    },
1174
    /// Require this many root signatures on policy updates
1175
    Threshold {
1176
        /// Number of root signatures
1177
        n: u32,
1178
        /// Where to write the proposal when more root signatures are needed
1179
        #[arg(long)]
1180
        out: Option<PathBuf>,
1181
    },
1182
}
1183
1184
/// Open the repository containing the current directory.
1185122×
fn open() -> Result<Repo> {
1186122×
    let repo = Repo::open(&std::env::current_dir()?).context("opening repository")?;
1187122×
    match AT_OP.get().and_then(|o| o.as_deref()) {
11880×
        Some(op) => Ok(repo.at_op(op)?),
1189122×
        None => Ok(repo),
1190
    }
1191122×
}
1192
1193
/// `oxy man`: every page into `dir`, or the top-level page on stdout.
11942×
fn man(dir: Option<PathBuf>) -> Result<i32> {
1195
    use clap::CommandFactory;
11962×
    let cmd = Cli::command();
11972×
    match dir {
11981×
        Some(dir) => {
11991×
            std::fs::create_dir_all(&dir)?;
12001×
            clap_mangen::generate_to(cmd, &dir)?;
12011×
            let pages = std::fs::read_dir(&dir)?
1202127×
                .filter(|e| {
1203127×
                    e.as_ref()
1204127×
                        .is_ok_and(|e| e.path().extension().is_some_and(|x| x == "1"))
1205127×
                })
12061×
                .count();
12071×
            println!(
1208
                "{}",
12091×
                ui::done(format!("Wrote {pages} man pages to {}", dir.display()))
1210
            );
1211
        }
12121×
        None => clap_mangen::Man::new(cmd).render(&mut std::io::stdout())?,
1213
    }
12142×
    Ok(0)
12152×
}
1216
1217
/// Parse arguments and run; returns the process exit code.
1218152×
pub fn run() -> Result<i32> {
1219
    // The cloud stores (s3://, gs://, az://) for remotes kept on them.
1220152×
    oxy_strata::register();
1221152×
    let cli = Cli::parse();
1222152×
    ui::init(cli.plain);
1223152×
    AT_OP.set(cli.at_op.clone()).ok();
12240×
    match cli.cmd {
12254×
        Cmd::Completions { shell } => {
1226
            use clap::CommandFactory;
12274×
            clap_complete::generate(shell, &mut Cli::command(), "oxy", &mut std::io::stdout());
12284×
            Ok(0)
1229
        }
12302×
        Cmd::Man { dir } => man(dir),
123114×
        Cmd::Init { path, bare, store } => workspace::init(path, bare, store.as_deref()),
12328×
        Cmd::Status { json } => workspace::status(json),
1233
        Cmd::Prompt {
12340×
            format,
12350×
            glyphs,
12360×
            color,
12370×
        } => workspace::prompt(&format, glyphs, color.as_deref()),
1238
        Cmd::Commit {
123926×
            message,
124026×
            file,
124126×
            depends_on,
124226×
            paths,
124326×
            interactive,
124426×
            hunks,
1245
        } => {
124626×
            let message = match (message, file) {
124725×
                (Some(m), _) => m,
12481×
                (None, Some(f)) if f.as_os_str() == "-" => {
12490×
                    let mut s = String::new();
12500×
                    std::io::Read::read_to_string(&mut std::io::stdin(), &mut s)?;
12510×
                    s
1252
                }
12531×
                (None, Some(f)) => std::fs::read_to_string(&f).map_err(|e| {
12540×
                    anyhow::anyhow!("reading the message from {}: {e}", f.display())
12550×
                })?,
12560×
                (None, None) => anyhow::bail!("a commit message is required (-m or -F)"),
1257
            };
125826×
            workspace::commit(&message, &depends_on, &paths, interactive, &hunks)
1259
        }
12602×
        Cmd::Cat { path, change } => workspace::cat(&path, change.as_deref()),
12611×
        Cmd::Root { json } => workspace::root(json),
12621×
        Cmd::Version { json } => workspace::version(json),
12632×
        Cmd::Discard { paths, hunks } => workspace::discard(&paths, &hunks),
12642×
        Cmd::Hunks { json } => workspace::hunks(json),
12650×
        Cmd::Squash { change, into } => workspace::squash(&change, &into),
1266
        Cmd::Rebase {
12670×
            change,
12680×
            onto,
12690×
            into_working_copy,
12700×
        } => workspace::rebase(&change, &onto, into_working_copy),
12710×
        Cmd::Blame { path, json } => workspace::blame(&path, json),
12721×
        Cmd::Bisect { cmd } => match cmd {
12730×
            BisectCmd::Start => workspace::bisect_start(),
12740×
            BisectCmd::Good { changes } => workspace::bisect_mark(Some(true), &changes),
12750×
            BisectCmd::Bad { changes } => workspace::bisect_mark(Some(false), &changes),
12760×
            BisectCmd::Skip => workspace::bisect_mark(None, &[]),
12770×
            BisectCmd::Run { command } => workspace::bisect_run(&command),
12781×
            BisectCmd::Status { json } => workspace::bisect_status(json),
12790×
            BisectCmd::Reset => workspace::bisect_reset(),
1280
        },
1281
        Cmd::Log {
12828×
            revset,
12838×
            limit,
12848×
            verbose,
12858×
            path,
12868×
            lines,
12878×
            json,
12888×
        } => workspace::log(
12898×
            revset.as_deref(),
12908×
            limit,
12918×
            verbose,
12928×
            path.as_deref(),
12938×
            lines.as_deref(),
12948×
            json,
1295
        ),
12961×
        Cmd::Diff { change, json } => workspace::diff(change.as_deref(), json),
1297
        Cmd::Describe {
12984×
            message,
12994×
            change,
13004×
            json,
13014×
        } => match message {
13020×
            Some(m) => workspace::describe(&m, change.as_deref()),
13034×
            None if change.is_some() => {
13040×
                anyhow::bail!(
1305
                    "naming works on the working copy; to change a head's message pass -m"
1306
                )
1307
            }
13084×
            None => workspace::describe_name(json),
1309
        },
1310
        Cmd::Amend {
13110×
            message,
13120×
            into,
13130×
            paths,
13140×
        } => workspace::amend(message.as_deref(), into.as_deref(), &paths),
13150×
        Cmd::Split { change, paths } => workspace::split(&change, &paths),
1316
        Cmd::Abandon {
13170×
            change,
13180×
            revset,
13190×
            yes,
13200×
        } => workspace::abandon(change.as_deref(), revset.as_deref(), yes),
1321
        Cmd::Show {
13223×
            change,
13233×
            revset,
13243×
            stat,
13253×
            json,
13263×
        } => workspace::show(change.as_deref(), revset.as_deref(), stat, json),
1327
        Cmd::Checkout {
13280×
            changes,
13290×
            discard,
13300×
            carry,
13310×
        } => workspace::checkout(&changes, discard, carry),
13321×
        Cmd::Resolve { json } => workspace::resolve(json),
13330×
        Cmd::Undo => workspace::undo(),
1334
        Cmd::Gc {
13352×
            now,
13362×
            expire_snapshots,
13372×
            remote,
13382×
        } => match remote {
13392×
            Some(name) => workspace::gc_remote(&name, now),
13400×
            None => workspace::gc(now, expire_snapshots),
1341
        },
13420×
        Cmd::Fsck { json } => workspace::fsck(json),
13430×
        Cmd::Doctor { offline, json } => workspace::doctor(offline, json),
13440×
        Cmd::Checkpoint => workspace::checkpoint(),
13451×
        Cmd::Op { cmd } => match cmd {
13461×
            OpCmd::Log { limit, json } => workspace::op_log(limit, json),
13470×
            OpCmd::Restore { op, discard } => workspace::op_restore(&op, discard),
13480×
            OpCmd::Revert { op } => workspace::op_revert(&op),
1349
        },
1350
        Cmd::Clone {
13515×
            url,
13525×
            dest,
13535×
            max_blob,
13545×
            sparse,
13555×
            depth,
13565×
            recursive,
13575×
        } => sync::clone(&url, dest, max_blob.as_deref(), &sparse, depth, recursive),
13580×
        Cmd::Deepen { depth, remote } => sync::deepen(&remote, depth),
13590×
        Cmd::Sparse { paths } => workspace::sparse(&paths),
13600×
        Cmd::Lock { path } => workspace::lock(&path),
13610×
        Cmd::Unlock { path } => workspace::unlock(&path),
13621×
        Cmd::Workspace { cmd } => match cmd {
13630×
            WorkspaceCmd::Add { path, changes } => workspace::workspace_add(&path, &changes),
13641×
            WorkspaceCmd::List { json } => workspace::workspace_list(json),
13650×
            WorkspaceCmd::Forget { workspace } => workspace::workspace_forget(&workspace),
1366
        },
136717×
        Cmd::Bookmark { cmd } => match cmd {
1368
            BookmarkCmd::Create {
13696×
                name,
13706×
                changes,
13716×
                upto,
13726×
                only,
13736×
            } => workspace::bookmark_create(&name, &changes, upto, only),
1374
            BookmarkCmd::Set {
13753×
                name,
13763×
                changes,
13773×
                upto,
13783×
                only,
13793×
            } => workspace::bookmark_set(&name, &changes, upto, only),
13800×
            BookmarkCmd::Delete { name } => workspace::bookmark_delete(&name),
13815×
            BookmarkCmd::List { all, verbose, json } => {
13825×
                workspace::bookmark_list(all, verbose, json)
1383
            }
13840×
            BookmarkCmd::Show { name, json } => workspace::bookmark_show(name.as_deref(), json),
1385
            BookmarkCmd::Switch {
13863×
                name,
13873×
                discard,
13883×
                carry,
13893×
            } => workspace::bookmark_switch(&name, discard, carry),
1390
        },
13910×
        Cmd::Merge { bookmark } => workspace::merge(&bookmark),
13920×
        Cmd::Auth { cmd } => match cmd {
1393
            AuthCmd::Login {
13940×
                hub,
13950×
                with_token,
13960×
                ca,
13970×
            } => auth::login(hub.as_deref(), with_token, ca),
13980×
            AuthCmd::Status => auth::status(),
13990×
            AuthCmd::Logout { hub } => auth::logout(hub.as_deref()),
14000×
            AuthCmd::Grant { hub } => auth::grant(hub.as_deref()),
1401
        },
14021×
        Cmd::Submodule { cmd } => match cmd {
1403
            SubmoduleCmd::Add {
14040×
                url,
14050×
                path,
14060×
                bookmark,
14070×
            } => sync::submodule_add(&url, &path, bookmark.as_deref()),
14080×
            SubmoduleCmd::Update { init, paths } => sync::submodule_update(init, &paths),
14091×
            SubmoduleCmd::Status { json } => sync::submodule_status(json),
14100×
            SubmoduleCmd::Remove { path } => sync::submodule_remove(&path),
1411
        },
1412
        Cmd::Witness {
1413
            cmd:
1414
                WitnessCmd::Serve {
14150×
                    name,
14160×
                    logs,
14170×
                    state,
14180×
                    bind,
14190×
                    tls_cert,
14200×
                    tls_key,
1421
                },
14220×
        } => tlog::witness_serve(&name, &logs, state, &bind, tls_cert.zip(tls_key)),
14230×
        Cmd::Tlog { cmd } => match cmd {
1424
            TlogCmd::Verify {
14250×
                hub,
14260×
                key,
14270×
                all,
14280×
                json,
14290×
            } => tlog::verify(hub.as_deref(), key.as_deref(), all, json),
1430
        },
143110×
        Cmd::Receipt { cmd } => match cmd {
14322×
            ReceiptCmd::List { rev, json } => receipt::list(rev.as_deref(), json),
1433
            ReceiptCmd::Show {
14341×
                receipt: r,
14351×
                rev,
14361×
                json,
14371×
            } => receipt::show(&r, rev.as_deref(), json),
1438
            ReceiptCmd::Export {
14393×
                receipt: r,
14403×
                file,
14413×
                rev,
14423×
                format,
14433×
            } => receipt::export(&r, &file, rev.as_deref(), &format),
14442×
            ReceiptCmd::Verify { file, json } => receipt::verify(&file, json),
14452×
            ReceiptCmd::Import { file } => receipt::import(&file),
1446
        },
14473×
        Cmd::Tag { cmd } => match cmd {
1448
            TagCmd::Create {
14492×
                name,
14502×
                changes,
14512×
                message,
14522×
            } => workspace::tag_create(&name, &changes, &message),
14530×
            TagCmd::Delete { name, reason } => workspace::tag_delete(&name, &reason),
14541×
            TagCmd::List { json } => workspace::tag_list(json),
1455
        },
145610×
        Cmd::Remote { cmd } => match cmd {
1457
            RemoteCmd::Add {
14584×
                name,
14594×
                url,
14604×
                profile,
14614×
                region,
14624×
                endpoint,
14634×
                credentials,
14644×
            } => sync::remote_add(
14654×
                &name,
14664×
                &url,
14674×
                &[
14684×
                    ("profile", profile),
14694×
                    ("region", region),
14704×
                    ("endpoint", endpoint),
14714×
                    ("credentials", credentials),
14724×
                ],
1473
            ),
14743×
            RemoteCmd::Check { name } => sync::remote_check(&name),
14751×
            RemoteCmd::Remove { name } => sync::remote_remove(&name),
14762×
            RemoteCmd::List { json } => sync::remote_list(json),
1477
        },
14781×
        Cmd::Fetch { remote, json } => sync::fetch(remote.as_deref(), json),
14794×
        Cmd::Pull { remote, json } => sync::pull(remote.as_deref(), json),
1480
        Cmd::Push {
14817×
            remote,
14827×
            force,
14837×
            bookmark,
14847×
            delete,
14857×
            json,
14867×
        } => match delete {
14870×
            Some(name) => sync::push_delete(remote.as_deref(), &name, json),
14887×
            None => sync::push(remote.as_deref(), force, bookmark.as_deref(), json),
1489
        },
1490
        Cmd::Serve {
14910×
            root,
14920×
            bind,
14930×
            tokens,
14940×
            tls_cert,
14950×
            tls_key,
14960×
        } => sync::serve(root, &bind, tokens, tls_cert.zip(tls_key)),
14973×
        Cmd::Identity { cmd } => match cmd {
1498
            IdentityCmd::Create {
14992×
                name,
15002×
                email,
15012×
                local,
15022×
                expires,
15032×
            } => identity::create(name, email, local, expires),
15040×
            IdentityCmd::Show => identity::show(),
15051×
            IdentityCmd::Export { path, pem } => identity::export(path, pem),
15060×
            IdentityCmd::SignChallenge { challenge, json } => {
15070×
                identity::sign_challenge(&challenge, json)
1508
            }
15090×
            IdentityCmd::Rotate => identity::rotate(),
15100×
            IdentityCmd::Renew { expires } => identity::renew(expires),
15110×
            IdentityCmd::Revoke { key, reason } => identity::revoke(key.as_deref(), &reason),
15120×
            IdentityCmd::Delegate { cmd } => match cmd {
1513
                DelegateCmd::Add {
15140×
                    identity_file,
15150×
                    scope,
15160×
                    expires,
15170×
                } => identity::delegate_add(&identity_file, &scope, expires.as_deref()),
15180×
                DelegateCmd::Remove { key } => identity::delegate_remove(&key),
15190×
                DelegateCmd::List => identity::delegate_list(),
1520
            },
1521
        },
1522
        Cmd::Git {
15230×
            cmd: GitCmd::Export { git_dir, branch },
15240×
        } => sync::git_export(&git_dir, branch.as_deref()),
1525
        Cmd::Git {
15260×
            cmd: GitCmd::Fetch { url, branch, claim },
15270×
        } => sync::git_fetch(&url, &branch, claim),
1528
        Cmd::Git {
15290×
            cmd: GitCmd::Push { url, branch },
15300×
        } => sync::git_push(&url, &branch),
1531
        Cmd::Git {
1532
            cmd: GitCmd::Recover,
15330×
        } => sync::git_recover(),
1534
        Cmd::Git {
1535
            cmd:
1536
                GitCmd::Import {
15370×
                    git_dir,
15380×
                    dest,
15390×
                    branch,
15400×
                    claim,
1541
                },
15420×
        } => sync::git_import(&git_dir, &dest, &branch, claim),
1543
        Cmd::Git {
1544
            cmd:
1545
                GitCmd::Verify {
15460×
                    git_dir,
15470×
                    branch,
15480×
                    last,
15490×
                    max_differences,
1550
                },
15510×
        } => sync::git_verify(&git_dir, &branch, last, max_differences),
15525×
        Cmd::Policy { cmd } => match cmd {
15531×
            PolicyCmd::Init => identity::policy_init(),
15543×
            PolicyCmd::Show { json } => identity::policy_show(json),
1555
            PolicyCmd::Grant {
15560×
                identity_file,
15570×
                pattern,
15580×
                out,
15590×
            } => identity::policy_grant(&identity_file, pattern.as_deref(), out),
1560
            PolicyCmd::GrantSubject {
15611×
                subject,
15621×
                issuer,
15631×
                scope,
15641×
                pattern,
15651×
                out,
15661×
            } => identity::policy_grant_subject(&subject, &issuer, &scope, pattern.as_deref(), out),
15670×
            PolicyCmd::Revoke { key, out } => identity::policy_revoke(&key, out),
15680×
            PolicyCmd::Root { cmd } => match cmd {
15690×
                RootCmd::Add { identity_file, out } => {
15700×
                    identity::policy_root_add(&identity_file, out)
1571
                }
15720×
                RootCmd::Remove { key, out } => identity::policy_root_remove(&key, out),
15730×
                RootCmd::Threshold { n, out } => identity::policy_threshold(n, out),
1574
            },
15750×
            PolicyCmd::Sign { proposal, out } => identity::policy_sign(&proposal, out),
1576
        },
1577
        Cmd::Seal {
15780×
            flow: Some(url),
15790×
            job: Some(job),
15800×
            reject,
15810×
            json,
1582
            ..
15830×
        } => approve::seal(&url, &job, reject, json),
1584
        Cmd::Seal {
15850×
            changes,
15860×
            proposal: Some(url),
15870×
            json,
1588
            ..
15890×
        } if changes.is_empty() => proposal::seal(&url, json),
15900×
        Cmd::Seal { changes, json, .. } => identity::seal(&changes, json),
15910×
        Cmd::Proposal { cmd } => match cmd {
15920×
            ProposalCmd::Show { url, json } => proposal::show(&url, json),
15930×
            ProposalCmd::Land { url, force } => proposal::land(&url, force),
1594
        },
1595
        Cmd::Verify {
15961×
            change,
15971×
            revset,
15981×
            json,
15991×
        } => identity::verify(change.as_deref(), revset.as_deref(), json),
1600
        Cmd::Config {
16011×
            key,
16021×
            value,
16031×
            user,
16041×
            unset,
16051×
        } => identity::config(&key, value.as_deref().unwrap_or(""), user, unset),
1606
    }
1607152×
}
1608
1609
#[cfg(test)]
1610
mod help_tests {
1611
    use super::Cli;
1612
    use clap::CommandFactory;
1613
1614
    /// Every command and every argument explains itself, so `--help`,
1615
    /// completions and the man pages are complete.
1616
    #[test]
16171×
    fn every_command_and_argument_has_help() {
1618127×
        fn walk(cmd: &clap::Command, path: &str, missing: &mut Vec<String>) {
1619127×
            let here = format!("{path} {}", cmd.get_name()).trim().to_string();
1620127×
            if cmd.get_about().is_none() && !path.is_empty() {
16210×
                missing.push(format!("{here} (command)"));
1622127×
            }
1623235×
            for a in cmd.get_arguments() {
1624235×
                let id = a.get_id().as_str();
1625235×
                if id == "help" || id == "version" || (a.is_global_set() && !path.is_empty()) {
16260×
                    continue;
1627235×
                }
1628235×
                if a.get_help().is_none() && a.get_long_help().is_none() {
16290×
                    missing.push(format!("{here} {id}"));
1630235×
                }
1631
            }
1632127×
            for sub in cmd.get_subcommands() {
1633126×
                walk(sub, &here, missing);
1634126×
            }
1635127×
        }
16361×
        let mut missing = Vec::new();
16371×
        walk(&Cli::command(), "", &mut missing);
16381×
        assert!(
16391×
            missing.is_empty(),
1640
            "{} without help:\n{}",
16410×
            missing.len(),
16420×
            missing.join("\n")
1643
        );
16441×
    }
1645
}