OXYGENHUB
# 51d4862a16d8 oxygen / src / cli / mod.rs
56.8 KiB · Rust · last changed oxy witness serve: a transparency-log witness anyone can run 18 days ago source blame history raw
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 change, bookmark, or tag
199
        #[arg(short = 'r', long)]
200
        change: Option<String>,
201
    },
202
    /// Print the repository root, workspace, and bookmark
203
    Root {
204
        /// Machine-readable output: one JSON document on stdout
205
        #[arg(long)]
206
        json: bool,
207
    },
208
    /// Print the version of oxy, its repository format, and protocol
209
    Version {
210
        /// Machine-readable output: one JSON document on stdout
211
        #[arg(long)]
212
        json: bool,
213
    },
214
    /// Restore paths to the working copy's base, or undo chosen hunks
215
    Discard {
216
        /// Paths to restore; none with --hunks, or everything with neither
217
        paths: Vec<String>,
218
        /// Hunk indices as `oxy hunks` numbers them, comma separated
219
        #[arg(long, value_delimiter = ',')]
220
        hunks: Vec<usize>,
221
    },
222
    /// List the working copy's hunks, numbered as commit --hunks expects
223
    Hunks {
224
        /// Machine-readable output: one JSON document on stdout
225
        #[arg(long)]
226
        json: bool,
227
    },
228
    /// With -m, change the message of a head; without, name the working
229
    /// copy for a build: its tag, or the nearest tag plus distance and
230
    /// change, or the bookmark and change, and -dirty
231
    Describe {
232
        /// The new message
233
        #[arg(short, long)]
234
        message: Option<String>,
235
        /// The head to describe (default: the one the working copy sits on)
236
        change: Option<String>,
237
        /// Machine-readable output (naming only)
238
        #[arg(long, conflicts_with = "message")]
239
        json: bool,
240
    },
241
    /// Fold the working copy into the head it sits on, or into CHANGE with --into
242
    Amend {
243
        /// Replace the message too
244
        #[arg(short, long)]
245
        message: Option<String>,
246
        /// Amend an older change; the edits must touch only lines it already knows
247
        #[arg(long)]
248
        into: Option<String>,
249
    },
250
    /// Split a head: the named paths become a separate first change
251
    Split {
252
        /// The head to split
253
        change: String,
254
        /// Paths whose edits become the first change
255
        paths: Vec<String>,
256
    },
257
    /// Drop a change from the bookmark; its content stays on disk as uncommitted work
258
    Abandon {
259
        /// The change to drop (ChangeId prefix, #hash prefix)
260
        #[arg(required_unless_present = "revset")]
261
        change: Option<String>,
262
        /// Drop every change a revset selects, newest first
263
        #[arg(short = 'r', long, value_name = "REVSET", conflicts_with = "change")]
264
        revset: Option<String>,
265
        /// Drop more than five changes without asking
266
        #[arg(long, short = 'y')]
267
        yes: bool,
268
    },
269
    /// Show one change (ChangeId prefix, #hash prefix, or @), or those a revset selects
270
    Show {
271
        /// ChangeId prefix, #hash prefix, or @ for the working copy
272
        #[arg(required_unless_present = "revset")]
273
        change: Option<String>,
274
        /// Show every change a revset selects
275
        #[arg(short = 'r', long, value_name = "REVSET", conflicts_with = "change")]
276
        revset: Option<String>,
277
        /// Machine-readable output: one JSON document on stdout
278
        #[arg(long)]
279
        json: bool,
280
    },
281
    /// Move the working copy to the state formed by CHANGE... (default: the bookmark heads)
282
    Checkout {
283
        /// Changes whose state to check out (default: the bookmark heads)
284
        changes: Vec<String>,
285
        /// Throw away uncommitted working-copy changes
286
        #[arg(long)]
287
        discard: bool,
288
        /// Take uncommitted changes along; refused when the target has
289
        /// other versions of the files they touch
290
        #[arg(long, conflicts_with = "discard")]
291
        carry: bool,
292
    },
293
    /// Report which conflicts of the working copy's base are resolved by the current files
294
    Resolve {
295
        /// Machine-readable output: one JSON document on stdout
296
        #[arg(long)]
297
        json: bool,
298
    },
299
    /// Undo the last operation
300
    Undo,
301
    /// Write a checkpoint of the current bookmark's state now
302
    Checkpoint,
303
    /// Print a shell completion script: `oxy completions bash > ~/.local/share/bash-completion/completions/oxy`
304
    Completions {
305
        /// bash, zsh, fish, elvish or powershell
306
        shell: clap_complete::Shell,
307
    },
308
    /// Write man pages (oxy.1 and one per command) into DIR, or print oxy.1
309
    Man {
310
        /// Directory to write every page into, e.g. ~/.local/share/man/man1
311
        #[arg(long)]
312
        dir: Option<PathBuf>,
313
    },
314
    /// Check the identity, configuration and remotes, and say what to do about any problem
315
    Doctor {
316
        /// Do not contact the remotes
317
        #[arg(long)]
318
        offline: bool,
319
        /// Machine-readable output: one JSON document on stdout
320
        #[arg(long)]
321
        json: bool,
322
    },
323
    /// Check the repository's integrity: objects, operation log, references, signatures, checkpoints
324
    Fsck {
325
        /// Machine-readable output: one JSON document on stdout
326
        #[arg(long)]
327
        json: bool,
328
    },
329
    /// Prune unreachable objects older than the grace period and repack everything into one pack
330
    Gc {
331
        /// Ignore the grace period (core.gc-grace) and prune immediately
332
        #[arg(long)]
333
        now: bool,
334
    },
335
    /// List operations
336
    #[command(name = "op")]
337
    Op {
338
        #[command(subcommand)]
339
        cmd: OpCmd,
340
    },
341
    /// Take the advisory local lock on a lockable file, making it writable
342
    Lock {
343
        /// The lockable file
344
        path: String,
345
    },
346
    /// Release the local lock and make the file read-only again
347
    Unlock {
348
        /// The locked file
349
        path: String,
350
    },
351
    /// Workspaces: extra checkout directories sharing this repository
352
    Workspace {
353
        #[command(subcommand)]
354
        cmd: WorkspaceCmd,
355
    },
356
    /// Bookmarks: named states; each workspace works on one
357
    Bookmark {
358
        #[command(subcommand)]
359
        cmd: BookmarkCmd,
360
    },
361
    /// Union another bookmark's heads into the current one (no merge change)
362
    Merge {
363
        /// The bookmark whose heads to add
364
        bookmark: String,
365
    },
366
    /// Sign in to a hub once, for every repository on it
367
    Auth {
368
        #[command(subcommand)]
369
        cmd: AuthCmd,
370
    },
371
    /// Subrepositories: nested repositories pinned to a state
372
    Submodule {
373
        #[command(subcommand)]
374
        cmd: SubmoduleCmd,
375
    },
376
    /// Witness hubs' transparency logs: cosign each checkpoint that extends
377
    /// the last one cosigned (spec §14.8)
378
    Witness {
379
        #[command(subcommand)]
380
        cmd: WitnessCmd,
381
    },
382
    /// Hubs' transparency logs: check that a hub shows everyone the same
383
    /// history (spec §14.8)
384
    Tlog {
385
        #[command(subcommand)]
386
        cmd: TlogCmd,
387
    },
388
    /// Execution receipts: signed records of what a runner ran on a state
389
    Receipt {
390
        #[command(subcommand)]
391
        cmd: ReceiptCmd,
392
    },
393
    /// Tags: immutable named states
394
    Tag {
395
        #[command(subcommand)]
396
        cmd: TagCmd,
397
    },
398
    /// Manage remotes
399
    Remote {
400
        #[command(subcommand)]
401
        cmd: RemoteCmd,
402
    },
403
    /// Fetch every bookmark of a remote into its tracking references
404
    Fetch {
405
        /// Remote name (default: origin)
406
        remote: Option<String>,
407
        /// Machine-readable output: one JSON document on stdout
408
        #[arg(long)]
409
        json: bool,
410
    },
411
    /// Fetch, then union the remote bookmark into the local one (no merge change)
412
    Pull {
413
        /// Remote name (default: origin)
414
        remote: Option<String>,
415
        /// Machine-readable output: one JSON document on stdout
416
        #[arg(long)]
417
        json: bool,
418
    },
419
    /// Push the current bookmark to a remote
420
    Push {
421
        /// Remote name (default: origin)
422
        remote: Option<String>,
423
        /// Allow dropping heads the remote has
424
        #[arg(long)]
425
        force: bool,
426
        /// Push this bookmark instead of the workspace's current one
427
        #[arg(short = 'b', long)]
428
        bookmark: Option<String>,
429
        /// Delete this bookmark on the remote (never its default bookmark);
430
        /// the local bookmark is kept
431
        #[arg(long, value_name = "NAME", conflicts_with_all = ["bookmark", "force"])]
432
        delete: Option<String>,
433
        /// Machine-readable output: one JSON document on stdout
434
        #[arg(long)]
435
        json: bool,
436
    },
437
    /// Serve repositories under ROOT over HTTPS (with --tls-cert/--tls-key) or plain HTTP
438
    Serve {
439
        /// Directory with one repository per subdirectory
440
        #[arg(long, default_value = ".")]
441
        root: PathBuf,
442
        /// Address and port to listen on
443
        #[arg(long, default_value = "127.0.0.1:8890")]
444
        bind: String,
445
        /// Bearer token allowed to push (repeatable); none means open
446
        #[arg(long = "token")]
447
        tokens: Vec<String>,
448
        /// PEM certificate chain; enables HTTPS together with --tls-key
449
        #[arg(long, requires = "tls_key")]
450
        tls_cert: Option<PathBuf>,
451
        /// PEM private key
452
        #[arg(long, requires = "tls_cert")]
453
        tls_key: Option<PathBuf>,
454
    },
455
    /// Manage the signing identity
456
    Identity {
457
        #[command(subcommand)]
458
        cmd: IdentityCmd,
459
    },
460
    /// Seal changes with your key: named ones, or every change on the current bookmark you can authenticate but have not
461
    Seal {
462
        /// Changes to seal (default: every change on the bookmark you can authenticate and have not sealed)
463
        changes: Vec<String>,
464
        /// Seal every change of a hub proposal and push the seal (your approval)
465
        #[arg(long, conflicts_with = "changes")]
466
        proposal: Option<String>,
467
        /// Approve a job of a hub run that waits for a sealed approval: `--flow https://hub/OWNER/NAME/flow/N --job apply`
468
        #[arg(long, conflicts_with_all = ["changes", "proposal"], requires = "job")]
469
        flow: Option<String>,
470
        /// The job to approve, with --flow
471
        #[arg(long, requires = "flow")]
472
        job: Option<String>,
473
        /// Reject the job instead of approving it, with --flow
474
        #[arg(long, requires = "flow")]
475
        reject: bool,
476
        /// Machine-readable output: one JSON document on stdout
477
        #[arg(long)]
478
        json: bool,
479
    },
480
    /// Hub proposals: show one, or land it from this client
481
    Proposal {
482
        #[command(subcommand)]
483
        cmd: ProposalCmd,
484
    },
485
    /// Verify the signatures on a change (default: every change in the log)
486
    Verify {
487
        /// The change to verify (default: every change in the log)
488
        change: Option<String>,
489
        /// Verify every change a revset selects: `release..main & unsigned()`
490
        #[arg(short = 'r', long, value_name = "REVSET", conflicts_with = "change")]
491
        revset: Option<String>,
492
        /// Machine-readable output: one JSON document on stdout
493
        #[arg(long)]
494
        json: bool,
495
    },
496
    /// Git interoperability
497
    Git {
498
        #[command(subcommand)]
499
        cmd: GitCmd,
500
    },
501
    /// Manage the trust policy (who may update which bookmarks)
502
    Policy {
503
        #[command(subcommand)]
504
        cmd: PolicyCmd,
505
    },
506
    /// Set a configuration key (author.*, core.sign, core.op-retention, core.gc-grace, core.checkpoint-interval, filter.max-blob, filter.paths, workspace.bookmark, remote.<name>.url|token|ca, auth.helper with --user)
507
    Config {
508
        /// The key, such as author.name or remote.origin.token
509
        key: String,
510
        /// The value to set (none with --unset)
511
        #[arg(required_unless_present = "unset")]
512
        value: Option<String>,
513
        /// Write to the user file (~/.config/oxy/config.toml) instead of this repository
514
        #[arg(long)]
515
        user: bool,
516
        /// Remove the key from this repository's file (or the user file with --user)
517
        #[arg(long)]
518
        unset: bool,
519
    },
520
}
521
522
#[derive(Subcommand)]
523
enum OpCmd {
524
    /// List operations, newest first, with what each changed
525
    Log {
526
        /// Show only the latest N (snapshots folded together count as one)
527
        #[arg(short = 'n', long)]
528
        limit: Option<usize>,
529
        /// Machine-readable output: one JSON document on stdout
530
        #[arg(long)]
531
        json: bool,
532
    },
533
    /// Go back to the bookmarks and working copy as they were after OP (recorded as a new operation, so it can be undone)
534
    Restore {
535
        /// The operation: an ID prefix from oxy op log, @ or @-
536
        op: String,
537
        /// Throw away uncommitted working-copy changes
538
        #[arg(long)]
539
        discard: bool,
540
    },
541
    /// Undo OP alone, keeping every operation after it: the bookmarks it moved go back
542
    Revert {
543
        /// The operation: an ID prefix from oxy op log
544
        op: String,
545
    },
546
}
547
548
#[derive(Subcommand)]
549
enum WorkspaceCmd {
550
    /// Create a workspace at PATH checked out on CHANGE... (default: the bookmark heads)
551
    Add {
552
        /// Directory for the new workspace (must be empty or absent)
553
        path: PathBuf,
554
        /// Changes whose state to check out there
555
        changes: Vec<String>,
556
    },
557
    /// List workspaces and their working-copy changes
558
    List {
559
        /// Machine-readable output: one JSON document on stdout
560
        #[arg(long)]
561
        json: bool,
562
    },
563
    /// Stop tracking a workspace (by id prefix or path); its files stay
564
    Forget {
565
        /// Workspace id prefix or path
566
        workspace: String,
567
    },
568
}
569
570
#[derive(Subcommand)]
571
enum BisectCmd {
572
    /// Begin; remembers where the working copy was
573
    Start,
574
    /// Mark CHANGE... (or the state under test) as good
575
    Good {
576
        /// Changes known to be good (default: the state under test)
577
        changes: Vec<String>,
578
    },
579
    /// Mark CHANGE... (or the state under test) as bad
580
    Bad {
581
        /// Changes known to be bad (default: the state under test)
582
        changes: Vec<String>,
583
    },
584
    /// The state under test cannot be tested; try another
585
    Skip,
586
    /// Run a command for each step: exit 0 good, 125 skip, else bad
587
    Run {
588
        /// Command and arguments to run at each step
589
        #[arg(required = true, trailing_var_arg = true)]
590
        command: Vec<String>,
591
    },
592
    /// Where the search stands
593
    Status {
594
        /// Machine-readable output: one JSON document on stdout
595
        #[arg(long)]
596
        json: bool,
597
    },
598
    /// Restore the working copy and forget the search
599
    Reset,
600
}
601
602
#[derive(Subcommand)]
603
enum BookmarkCmd {
604
    /// Create a bookmark at CHANGE... (default: the working copy's base)
605
    Create {
606
        /// Name of the new bookmark
607
        name: String,
608
        /// Its heads (default: the working copy's base). A state is its
609
        /// heads and what they depend on: a change alone may leave out
610
        /// earlier changes of the current state, which is refused unless
611
        /// --upto or --only says which is meant
612
        changes: Vec<String>,
613
        /// Everything of the current state up to CHANGE..., as a Git
614
        /// branch at a commit would hold
615
        #[arg(long, conflicts_with = "only")]
616
        upto: bool,
617
        /// Just CHANGE... and what they depend on
618
        #[arg(long)]
619
        only: bool,
620
    },
621
    /// Point an existing bookmark at CHANGE...
622
    Set {
623
        /// The bookmark to move
624
        name: String,
625
        /// Its new heads. A change alone may leave out changes the
626
        /// bookmark now holds that were recorded before it, which is
627
        /// refused unless --upto or --only says which is meant
628
        changes: Vec<String>,
629
        /// Everything the bookmark holds up to CHANGE..., as a Git branch
630
        /// moved to a commit would hold
631
        #[arg(long, conflicts_with = "only")]
632
        upto: bool,
633
        /// Just CHANGE... and what they depend on
634
        #[arg(long)]
635
        only: bool,
636
    },
637
    /// Remove a bookmark; its changes stay
638
    Delete {
639
        /// The bookmark to remove
640
        name: String,
641
    },
642
    /// List bookmarks with their heads and difference from origin
643
    List {
644
        /// Also list remotes' bookmarks that have no local bookmark
645
        #[arg(long, short)]
646
        all: bool,
647
        /// Each head of every bookmark, as `bookmark show` prints them
648
        #[arg(long, short)]
649
        verbose: bool,
650
        /// Machine-readable output: one JSON document on stdout
651
        #[arg(long)]
652
        json: bool,
653
    },
654
    /// A bookmark's heads: each change nothing else on it depends on,
655
    /// newest first. A bookmark name is also a revset for its heads
656
    /// (`oxy log -r NAME`, or `bookmark(NAME)` to mean only the bookmark)
657
    Show {
658
        /// The bookmark, or REMOTE/NAME (default: the current one)
659
        name: Option<String>,
660
        /// Machine-readable output: one JSON document on stdout
661
        #[arg(long)]
662
        json: bool,
663
    },
664
    /// Make NAME this workspace's bookmark and check it out. Uncommitted work stops it; --carry takes the work along, --discard drops it
665
    Switch {
666
        /// The bookmark to work on; one only a remote has (NAME or REMOTE/NAME) is created from it
667
        name: String,
668
        /// Throw away uncommitted working-copy changes
669
        #[arg(long)]
670
        discard: bool,
671
        /// Take uncommitted changes along; refused when NAME has other
672
        /// versions of the files they touch
673
        #[arg(long, conflicts_with = "discard")]
674
        carry: bool,
675
    },
676
}
677
678
#[derive(Subcommand)]
679
enum WitnessCmd {
680
    /// Serve a witness (C2SP tlog-witness: POST /add-checkpoint), signing
681
    /// with this oxy home's key (author.key)
682
    Serve {
683
        /// The witness's name in cosignatures: a host name
684
        #[arg(long)]
685
        name: String,
686
        /// A log to witness, as ORIGIN=FILE: its origin (HOST/log) and its
687
        /// key's identity file (a hub's /.well-known/oxyhub-key); repeatable
688
        #[arg(long = "log", required = true)]
689
        logs: Vec<String>,
690
        /// Where to keep what it cosigned
691
        #[arg(long, default_value = "witness-state")]
692
        state: PathBuf,
693
        /// Address and port to listen on
694
        #[arg(long, default_value = "127.0.0.1:8891")]
695
        bind: String,
696
        /// PEM certificate chain; enables HTTPS together with --tls-key
697
        #[arg(long, requires = "tls_key")]
698
        tls_cert: Option<PathBuf>,
699
        /// PEM private key for --tls-cert
700
        #[arg(long, requires = "tls_cert")]
701
        tls_key: Option<PathBuf>,
702
    },
703
}
704
705
#[derive(Subcommand)]
706
enum TlogCmd {
707
    /// Check a hub's whole log: its checkpoint against the log key and the
708
    /// last checkpoint seen, and every entry against the root it signed;
709
    /// then list this repository's entries (or a key's, or all)
710
    Verify {
711
        /// The hub, as https://oxygenhub.io (default: this checkout's remote)
712
        hub: Option<String>,
713
        /// List the entries a key signed (a KeyId) instead
714
        #[arg(long, conflicts_with = "all")]
715
        key: Option<String>,
716
        /// List every entry
717
        #[arg(long)]
718
        all: bool,
719
        /// Machine-readable output
720
        #[arg(long)]
721
        json: bool,
722
    },
723
}
724
725
#[derive(Subcommand)]
726
enum AuthCmd {
727
    /// Sign in to HUB in the browser (device sign-in) and keep the token for
728
    /// every repository there
729
    Login {
730
        /// The hub, as https://oxygenhub.io or oxygenhub.io (default: this checkout's origin)
731
        hub: Option<String>,
732
        /// Read a token from standard input instead (for CI and scripts)
733
        #[arg(long)]
734
        with_token: bool,
735
        /// PEM file of extra roots to trust (a hub with a self-signed certificate)
736
        #[arg(long)]
737
        ca: Option<PathBuf>,
738
    },
739
    /// List the hubs you are signed in to, checking each token with its hub
740
    Status,
741
    /// Forget HUB's token and revoke it on the hub
742
    Logout {
743
        /// The hub (default: this checkout's origin, or the only one)
744
        hub: Option<String>,
745
    },
746
    /// Keep an agent grant token (from `oh agent grant`) for keyless
747
    /// signing: with `author.signer = keyless`, commits and pushes are
748
    /// signed by fresh keys the hub certifies, in the grantor's name
749
    /// (the token is read from standard input, so it stays out of the
750
    /// shell's history)
751
    Grant {
752
        /// The hub (default: this checkout's origin)
753
        hub: Option<String>,
754
    },
755
}
756
757
#[derive(Subcommand)]
758
enum SubmoduleCmd {
759
    /// Clone URL (Oxygen or Git) into PATH and register it in .oxymodules
760
    Add {
761
        /// URL or path of the Oxygen or Git repository
762
        url: String,
763
        /// Where to put it, relative to the repository root
764
        path: String,
765
        /// Bookmark (Oxygen) or branch (Git) to check out and track
766
        #[arg(long)]
767
        bookmark: Option<String>,
768
    },
769
    /// Bring nested repositories to their pins
770
    Update {
771
        /// Clone nested repositories that are absent
772
        #[arg(long)]
773
        init: bool,
774
        /// Subrepos to update (default: all)
775
        paths: Vec<String>,
776
    },
777
    /// List subrepos with their pins and on-disk state
778
    Status {
779
        /// Machine-readable output: one JSON document on stdout
780
        #[arg(long)]
781
        json: bool,
782
    },
783
    /// Unregister PATH; the directory stays, the node goes with the next commit
784
    Remove {
785
        /// The subrepo to unregister
786
        path: String,
787
    },
788
}
789
790
#[derive(Subcommand)]
791
enum ReceiptCmd {
792
    /// The receipts kept for a state (default: the bookmark heads)
793
    List {
794
        /// Change, bookmark or tag whose state to look at
795
        rev: Option<String>,
796
        /// Machine-readable output: one JSON document on stdout
797
        #[arg(long)]
798
        json: bool,
799
    },
800
    /// One receipt in full, and whether its signature verifies
801
    Show {
802
        /// The receipt (a prefix of its hash, as `list` shows it)
803
        receipt: String,
804
        /// Change, bookmark or tag whose state holds it
805
        #[arg(long)]
806
        rev: Option<String>,
807
        /// Machine-readable output: one JSON document on stdout
808
        #[arg(long)]
809
        json: bool,
810
    },
811
    /// Write a signed receipt to FILE
812
    Export {
813
        /// The receipt (a prefix of its hash, as `list` shows it)
814
        receipt: String,
815
        /// Where to write it
816
        file: String,
817
        /// Change, bookmark or tag whose state holds it
818
        #[arg(long)]
819
        rev: Option<String>,
820
        /// `oxygen`: the runner's signed receipt, as `verify` and `import`
821
        /// read it. `slsa`: SLSA provenance in a DSSE envelope, signed with
822
        /// your key, for in-toto and cosign (`oxy identity export --pem`
823
        /// gives the key they check it with)
824
        #[arg(long, default_value = "oxygen", value_parser = ["oxygen", "slsa"])]
825
        format: String,
826
    },
827
    /// Check a signed receipt in FILE without keeping it
828
    Verify {
829
        /// A signed receipt, as a runner or `export` writes it
830
        file: String,
831
        /// Machine-readable output: one JSON document on stdout
832
        #[arg(long)]
833
        json: bool,
834
    },
835
    /// Verify a signed receipt in FILE and keep it with its state
836
    Import {
837
        /// A signed receipt, as a runner or `export` writes it
838
        file: String,
839
    },
840
}
841
842
#[derive(Subcommand)]
843
enum TagCmd {
844
    /// Tag the state of CHANGE... (default: the bookmark heads)
845
    Create {
846
        /// Name of the tag; tags never move
847
        name: String,
848
        /// Changes whose state to tag (default: the bookmark heads)
849
        changes: Vec<String>,
850
        /// A message recorded with the tag
851
        #[arg(short, long, default_value = "")]
852
        message: String,
853
    },
854
    /// List tags and the states they name
855
    List {
856
        /// Machine-readable output: one JSON document on stdout
857
        #[arg(long)]
858
        json: bool,
859
    },
860
}
861
862
#[derive(Subcommand)]
863
enum RemoteCmd {
864
    /// Add a remote
865
    Add {
866
        /// Name to refer to the remote by, such as origin
867
        name: String,
868
        /// 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
869
        url: String,
870
        /// For an s3:// store: the AWS profile to use (default: AWS_PROFILE, else "default")
871
        #[arg(long)]
872
        profile: Option<String>,
873
        /// For an s3:// store: its region (default: the environment, the profile, else asked of S3)
874
        #[arg(long)]
875
        region: Option<String>,
876
        /// For an s3://, gs:// or az:// store: another endpoint, such as an S3-compatible service
877
        #[arg(long)]
878
        endpoint: Option<String>,
879
        /// For a gs:// store: a service account key file, or application default credentials
880
        #[arg(long)]
881
        credentials: Option<String>,
882
    },
883
    /// Check a remote: that it is reachable with the credentials in hand, what it holds, and (for a store) that it behaves
884
    Check {
885
        /// The remote to check
886
        name: String,
887
    },
888
    /// Remove a remote
889
    Remove {
890
        /// The remote to remove
891
        name: String,
892
    },
893
    /// List remotes and their URLs
894
    List {
895
        /// Machine-readable output: one JSON document on stdout
896
        #[arg(long)]
897
        json: bool,
898
    },
899
}
900
901
#[derive(Subcommand)]
902
enum ProposalCmd {
903
    /// Show a proposal by its hub URL
904
    Show {
905
        /// The proposal's URL on the hub
906
        url: String,
907
        /// Machine-readable output: one JSON document on stdout
908
        #[arg(long)]
909
        json: bool,
910
    },
911
    /// Fetch the proposal's changes, set the target bookmark to the projected state, and push with your key
912
    Land {
913
        /// The proposal's URL on the hub
914
        url: String,
915
        /// Land even when the hub reports blockers
916
        #[arg(long)]
917
        force: bool,
918
    },
919
}
920
921
#[derive(Subcommand)]
922
enum IdentityCmd {
923
    /// Generate a composite Ed25519 + ML-DSA-65 key pair and make it your author identity
924
    Create {
925
        /// Your name (default: author.name)
926
        #[arg(long)]
927
        name: Option<String>,
928
        /// Your email (default: author.email)
929
        #[arg(long)]
930
        email: Option<String>,
931
        /// Record the author in this repository only, not in the user config
932
        #[arg(long)]
933
        local: bool,
934
        /// Expire the identity this many days from now
935
        #[arg(long)]
936
        expires: Option<u64>,
937
    },
938
    /// Show the configured identity
939
    Show,
940
    /// Write your identity object to a file, to hand to a policy holder
941
    Export {
942
        /// File to write (default: <key>.identity, or <key>.pem with --pem, in the current directory)
943
        path: Option<PathBuf>,
944
        /// Write the Ed25519 public key as PEM instead, as cosign's --key and
945
        /// other DSSE verifiers take it
946
        #[arg(long)]
947
        pem: bool,
948
    },
949
    /// Sign a host's challenge (hex, at least 16 bytes) to prove you hold your key; prints the signature as hex
950
    SignChallenge {
951
        /// The host's challenge, as hex
952
        challenge: String,
953
        /// Machine-readable output: one JSON document on stdout
954
        #[arg(long)]
955
        json: bool,
956
    },
957
    /// Move to a fresh key pair; the policy follows if it names your key
958
    Rotate,
959
    /// Re-issue your identity with a new expiry (in days from now; 0 = never)
960
    Renew {
961
        /// Days from now until it expires; 0 for never
962
        #[arg(long)]
963
        expires: u64,
964
    },
965
    /// Revoke a key: yours by default, or any key if you hold a policy root
966
    Revoke {
967
        /// KeyId prefix (default: your own key)
968
        key: Option<String>,
969
        /// Why, recorded in the revocation
970
        #[arg(long, default_value = "")]
971
        reason: String,
972
    },
973
    /// Keys allowed to sign or seal on your behalf
974
    Delegate {
975
        #[command(subcommand)]
976
        cmd: DelegateCmd,
977
    },
978
}
979
980
#[derive(Subcommand)]
981
enum DelegateCmd {
982
    /// Add a delegate from an exported identity file
983
    Add {
984
        /// The delegates' exported identity files: one rotation for all of them
985
        #[arg(required = true)]
986
        identity_file: Vec<PathBuf>,
987
        /// What it may do for you: changes, refs, seal (comma-separated) or all.
988
        /// An agent gets `changes`; a host that lands for you needs `changes,refs`
989
        #[arg(long, default_value = "changes")]
990
        scope: String,
991
        /// How long for: 30m, 8h, 7d, or a number of days (default: until removed)
992
        #[arg(long)]
993
        expires: Option<String>,
994
    },
995
    /// Remove a delegate by KeyId prefix
996
    Remove {
997
        /// KeyId prefix of the delegate
998
        key: String,
999
    },
1000
    /// List delegates
1001
    List,
1002
}
1003
1004
#[derive(Subcommand)]
1005
enum GitCmd {
1006
    /// Export the current bookmark to a Git repository (created bare if absent)
1007
    Export {
1008
        /// Git repository to write to (created bare if absent)
1009
        git_dir: PathBuf,
1010
        /// Git branch to write (default: the bookmark name)
1011
        #[arg(long)]
1012
        branch: Option<String>,
1013
    },
1014
    /// Fetch a branch from a Git remote (path or URL) into the current bookmark
1015
    Fetch {
1016
        /// Git remote: a path or URL
1017
        url: String,
1018
        /// Branch to fetch
1019
        #[arg(long, default_value = "main")]
1020
        branch: String,
1021
        /// Author your own commits (Git author email = your identity's) with your key; sets git.claim
1022
        #[arg(long)]
1023
        claim: bool,
1024
    },
1025
    /// Push the current bookmark to a branch of a Git remote (fetch first)
1026
    Push {
1027
        /// Git remote: a path or URL
1028
        url: String,
1029
        /// Branch to push to
1030
        #[arg(long, default_value = "main")]
1031
        branch: String,
1032
    },
1033
    /// 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
1034
    Recover,
1035
    /// Check this repository's import of a Git branch: every commit's tree must be reproduced exactly
1036
    Verify {
1037
        /// The Git repository the import came from
1038
        git_dir: PathBuf,
1039
        /// Branch that was imported
1040
        #[arg(long, default_value = "main")]
1041
        branch: String,
1042
    },
1043
    /// Import a Git branch into a new Oxygen repository at DEST
1044
    Import {
1045
        /// Git repository to read
1046
        git_dir: PathBuf,
1047
        /// Directory for the new Oxygen repository
1048
        dest: PathBuf,
1049
        /// Branch to import
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
}
1057
1058
#[derive(Subcommand)]
1059
enum PolicyCmd {
1060
    /// Create the initial policy with your identity as the only root
1061
    Init,
1062
    /// Show the current policy
1063
    Show {
1064
        /// Machine-readable output: one JSON document on stdout
1065
        #[arg(long)]
1066
        json: bool,
1067
    },
1068
    /// Allow an identity (from `oxy identity export`) to update bookmarks
1069
    Grant {
1070
        /// The identity to grant, exported with `oxy identity export`
1071
        identity_file: PathBuf,
1072
        /// Reference pattern the grant applies to (default: every bookmark)
1073
        #[arg(long)]
1074
        pattern: Option<String>,
1075
        /// Where to write the proposal when more root signatures are needed
1076
        #[arg(long)]
1077
        out: Option<PathBuf>,
1078
    },
1079
    /// Allow holders of an issuer's certificates for SUBJECT to sign (keyless)
1080
    GrantSubject {
1081
        /// Who: `repo:OWNER/NAME:bookmark:main`, `agent:NAME@ACCOUNT`; `*` matches any run of characters
1082
        subject: String,
1083
        /// The issuer's identity file, e.g. a hub's /.well-known/oxyhub-key
1084
        #[arg(long)]
1085
        issuer: PathBuf,
1086
        /// What its keys may sign: changes, refs, seal
1087
        #[arg(long, default_value = "changes,refs")]
1088
        scope: String,
1089
        /// Reference pattern the grant applies to (default: every bookmark)
1090
        #[arg(long)]
1091
        pattern: Option<String>,
1092
        /// Where to write the proposal when more root signatures are needed
1093
        #[arg(long)]
1094
        out: Option<PathBuf>,
1095
    },
1096
    /// Revoke a key (KeyId prefix of an identity in the policy)
1097
    Revoke {
1098
        /// KeyId prefix of the identity
1099
        key: String,
1100
        /// Where to write the proposal when more root signatures are needed
1101
        #[arg(long)]
1102
        out: Option<PathBuf>,
1103
    },
1104
    /// Manage root keys and the signature threshold for policy updates
1105
    Root {
1106
        #[command(subcommand)]
1107
        cmd: RootCmd,
1108
    },
1109
    /// Add your root signature to a proposal file; applies it once the threshold is met
1110
    Sign {
1111
        /// The proposal file to sign
1112
        proposal: PathBuf,
1113
        /// Where to write the proposal if it still needs signatures (default: in place)
1114
        #[arg(long)]
1115
        out: Option<PathBuf>,
1116
    },
1117
}
1118
1119
#[derive(Subcommand)]
1120
enum RootCmd {
1121
    /// Make an exported identity a root key
1122
    Add {
1123
        /// The identity to make a root, exported with `oxy identity export`
1124
        identity_file: PathBuf,
1125
        /// Where to write the proposal when more root signatures are needed
1126
        #[arg(long)]
1127
        out: Option<PathBuf>,
1128
    },
1129
    /// Remove a root key by KeyId prefix
1130
    Remove {
1131
        /// KeyId prefix of the root key
1132
        key: String,
1133
        /// Where to write the proposal when more root signatures are needed
1134
        #[arg(long)]
1135
        out: Option<PathBuf>,
1136
    },
1137
    /// Require this many root signatures on policy updates
1138
    Threshold {
1139
        /// Number of root signatures
1140
        n: u32,
1141
        /// Where to write the proposal when more root signatures are needed
1142
        #[arg(long)]
1143
        out: Option<PathBuf>,
1144
    },
1145
}
1146
1147
/// Open the repository containing the current directory.
1148
fn open() -> Result<Repo> {
1149
    let repo = Repo::open(&std::env::current_dir()?).context("opening repository")?;
1150
    match AT_OP.get().and_then(|o| o.as_deref()) {
1151
        Some(op) => Ok(repo.at_op(op)?),
1152
        None => Ok(repo),
1153
    }
1154
}
1155
1156
/// `oxy man`: every page into `dir`, or the top-level page on stdout.
1157
fn man(dir: Option<PathBuf>) -> Result<i32> {
1158
    use clap::CommandFactory;
1159
    let cmd = Cli::command();
1160
    match dir {
1161
        Some(dir) => {
1162
            std::fs::create_dir_all(&dir)?;
1163
            clap_mangen::generate_to(cmd, &dir)?;
1164
            let pages = std::fs::read_dir(&dir)?
1165
                .filter(|e| {
1166
                    e.as_ref()
1167
                        .is_ok_and(|e| e.path().extension().is_some_and(|x| x == "1"))
1168
                })
1169
                .count();
1170
            println!(
1171
                "{}",
1172
                ui::done(format!("Wrote {pages} man pages to {}", dir.display()))
1173
            );
1174
        }
1175
        None => clap_mangen::Man::new(cmd).render(&mut std::io::stdout())?,
1176
    }
1177
    Ok(0)
1178
}
1179
1180
/// Parse arguments and run; returns the process exit code.
1181
pub fn run() -> Result<i32> {
1182
    // The cloud stores (s3://, gs://, az://) for remotes kept on them.
1183
    oxy_strata::register();
1184
    let cli = Cli::parse();
1185
    ui::init(cli.plain);
1186
    AT_OP.set(cli.at_op.clone()).ok();
1187
    match cli.cmd {
1188
        Cmd::Completions { shell } => {
1189
            use clap::CommandFactory;
1190
            clap_complete::generate(shell, &mut Cli::command(), "oxy", &mut std::io::stdout());
1191
            Ok(0)
1192
        }
1193
        Cmd::Man { dir } => man(dir),
1194
        Cmd::Init { path, bare, store } => workspace::init(path, bare, store.as_deref()),
1195
        Cmd::Status { json } => workspace::status(json),
1196
        Cmd::Prompt {
1197
            format,
1198
            glyphs,
1199
            color,
1200
        } => workspace::prompt(&format, glyphs, color.as_deref()),
1201
        Cmd::Commit {
1202
            message,
1203
            file,
1204
            depends_on,
1205
            paths,
1206
            interactive,
1207
            hunks,
1208
        } => {
1209
            let message = match (message, file) {
1210
                (Some(m), _) => m,
1211
                (None, Some(f)) if f.as_os_str() == "-" => {
1212
                    let mut s = String::new();
1213
                    std::io::Read::read_to_string(&mut std::io::stdin(), &mut s)?;
1214
                    s
1215
                }
1216
                (None, Some(f)) => std::fs::read_to_string(&f).map_err(|e| {
1217
                    anyhow::anyhow!("reading the message from {}: {e}", f.display())
1218
                })?,
1219
                (None, None) => anyhow::bail!("a commit message is required (-m or -F)"),
1220
            };
1221
            workspace::commit(&message, &depends_on, &paths, interactive, &hunks)
1222
        }
1223
        Cmd::Cat { path, change } => workspace::cat(&path, change.as_deref()),
1224
        Cmd::Root { json } => workspace::root(json),
1225
        Cmd::Version { json } => workspace::version(json),
1226
        Cmd::Discard { paths, hunks } => workspace::discard(&paths, &hunks),
1227
        Cmd::Hunks { json } => workspace::hunks(json),
1228
        Cmd::Squash { change, into } => workspace::squash(&change, &into),
1229
        Cmd::Rebase {
1230
            change,
1231
            onto,
1232
            into_working_copy,
1233
        } => workspace::rebase(&change, &onto, into_working_copy),
1234
        Cmd::Blame { path, json } => workspace::blame(&path, json),
1235
        Cmd::Bisect { cmd } => match cmd {
1236
            BisectCmd::Start => workspace::bisect_start(),
1237
            BisectCmd::Good { changes } => workspace::bisect_mark(Some(true), &changes),
1238
            BisectCmd::Bad { changes } => workspace::bisect_mark(Some(false), &changes),
1239
            BisectCmd::Skip => workspace::bisect_mark(None, &[]),
1240
            BisectCmd::Run { command } => workspace::bisect_run(&command),
1241
            BisectCmd::Status { json } => workspace::bisect_status(json),
1242
            BisectCmd::Reset => workspace::bisect_reset(),
1243
        },
1244
        Cmd::Log {
1245
            revset,
1246
            limit,
1247
            verbose,
1248
            path,
1249
            lines,
1250
            json,
1251
        } => workspace::log(
1252
            revset.as_deref(),
1253
            limit,
1254
            verbose,
1255
            path.as_deref(),
1256
            lines.as_deref(),
1257
            json,
1258
        ),
1259
        Cmd::Diff { change, json } => workspace::diff(change.as_deref(), json),
1260
        Cmd::Describe {
1261
            message,
1262
            change,
1263
            json,
1264
        } => match message {
1265
            Some(m) => workspace::describe(&m, change.as_deref()),
1266
            None if change.is_some() => {
1267
                anyhow::bail!(
1268
                    "naming works on the working copy; to change a head's message pass -m"
1269
                )
1270
            }
1271
            None => workspace::describe_name(json),
1272
        },
1273
        Cmd::Amend { message, into } => workspace::amend(message.as_deref(), into.as_deref()),
1274
        Cmd::Split { change, paths } => workspace::split(&change, &paths),
1275
        Cmd::Abandon {
1276
            change,
1277
            revset,
1278
            yes,
1279
        } => workspace::abandon(change.as_deref(), revset.as_deref(), yes),
1280
        Cmd::Show {
1281
            change,
1282
            revset,
1283
            json,
1284
        } => workspace::show(change.as_deref(), revset.as_deref(), json),
1285
        Cmd::Checkout {
1286
            changes,
1287
            discard,
1288
            carry,
1289
        } => workspace::checkout(&changes, discard, carry),
1290
        Cmd::Resolve { json } => workspace::resolve(json),
1291
        Cmd::Undo => workspace::undo(),
1292
        Cmd::Gc { now } => workspace::gc(now),
1293
        Cmd::Fsck { json } => workspace::fsck(json),
1294
        Cmd::Doctor { offline, json } => workspace::doctor(offline, json),
1295
        Cmd::Checkpoint => workspace::checkpoint(),
1296
        Cmd::Op { cmd } => match cmd {
1297
            OpCmd::Log { limit, json } => workspace::op_log(limit, json),
1298
            OpCmd::Restore { op, discard } => workspace::op_restore(&op, discard),
1299
            OpCmd::Revert { op } => workspace::op_revert(&op),
1300
        },
1301
        Cmd::Clone {
1302
            url,
1303
            dest,
1304
            max_blob,
1305
            sparse,
1306
            depth,
1307
            recursive,
1308
        } => sync::clone(&url, dest, max_blob.as_deref(), &sparse, depth, recursive),
1309
        Cmd::Deepen { depth, remote } => sync::deepen(&remote, depth),
1310
        Cmd::Sparse { paths } => workspace::sparse(&paths),
1311
        Cmd::Lock { path } => workspace::lock(&path),
1312
        Cmd::Unlock { path } => workspace::unlock(&path),
1313
        Cmd::Workspace { cmd } => match cmd {
1314
            WorkspaceCmd::Add { path, changes } => workspace::workspace_add(&path, &changes),
1315
            WorkspaceCmd::List { json } => workspace::workspace_list(json),
1316
            WorkspaceCmd::Forget { workspace } => workspace::workspace_forget(&workspace),
1317
        },
1318
        Cmd::Bookmark { cmd } => match cmd {
1319
            BookmarkCmd::Create {
1320
                name,
1321
                changes,
1322
                upto,
1323
                only,
1324
            } => workspace::bookmark_create(&name, &changes, upto, only),
1325
            BookmarkCmd::Set {
1326
                name,
1327
                changes,
1328
                upto,
1329
                only,
1330
            } => workspace::bookmark_set(&name, &changes, upto, only),
1331
            BookmarkCmd::Delete { name } => workspace::bookmark_delete(&name),
1332
            BookmarkCmd::List { all, verbose, json } => {
1333
                workspace::bookmark_list(all, verbose, json)
1334
            }
1335
            BookmarkCmd::Show { name, json } => workspace::bookmark_show(name.as_deref(), json),
1336
            BookmarkCmd::Switch {
1337
                name,
1338
                discard,
1339
                carry,
1340
            } => workspace::bookmark_switch(&name, discard, carry),
1341
        },
1342
        Cmd::Merge { bookmark } => workspace::merge(&bookmark),
1343
        Cmd::Auth { cmd } => match cmd {
1344
            AuthCmd::Login {
1345
                hub,
1346
                with_token,
1347
                ca,
1348
            } => auth::login(hub.as_deref(), with_token, ca),
1349
            AuthCmd::Status => auth::status(),
1350
            AuthCmd::Logout { hub } => auth::logout(hub.as_deref()),
1351
            AuthCmd::Grant { hub } => auth::grant(hub.as_deref()),
1352
        },
1353
        Cmd::Submodule { cmd } => match cmd {
1354
            SubmoduleCmd::Add {
1355
                url,
1356
                path,
1357
                bookmark,
1358
            } => sync::submodule_add(&url, &path, bookmark.as_deref()),
1359
            SubmoduleCmd::Update { init, paths } => sync::submodule_update(init, &paths),
1360
            SubmoduleCmd::Status { json } => sync::submodule_status(json),
1361
            SubmoduleCmd::Remove { path } => sync::submodule_remove(&path),
1362
        },
1363
        Cmd::Witness {
1364
            cmd:
1365
                WitnessCmd::Serve {
1366
                    name,
1367
                    logs,
1368
                    state,
1369
                    bind,
1370
                    tls_cert,
1371
                    tls_key,
1372
                },
1373
        } => tlog::witness_serve(&name, &logs, state, &bind, tls_cert.zip(tls_key)),
1374
        Cmd::Tlog { cmd } => match cmd {
1375
            TlogCmd::Verify {
1376
                hub,
1377
                key,
1378
                all,
1379
                json,
1380
            } => tlog::verify(hub.as_deref(), key.as_deref(), all, json),
1381
        },
1382
        Cmd::Receipt { cmd } => match cmd {
1383
            ReceiptCmd::List { rev, json } => receipt::list(rev.as_deref(), json),
1384
            ReceiptCmd::Show {
1385
                receipt: r,
1386
                rev,
1387
                json,
1388
            } => receipt::show(&r, rev.as_deref(), json),
1389
            ReceiptCmd::Export {
1390
                receipt: r,
1391
                file,
1392
                rev,
1393
                format,
1394
            } => receipt::export(&r, &file, rev.as_deref(), &format),
1395
            ReceiptCmd::Verify { file, json } => receipt::verify(&file, json),
1396
            ReceiptCmd::Import { file } => receipt::import(&file),
1397
        },
1398
        Cmd::Tag { cmd } => match cmd {
1399
            TagCmd::Create {
1400
                name,
1401
                changes,
1402
                message,
1403
            } => workspace::tag_create(&name, &changes, &message),
1404
            TagCmd::List { json } => workspace::tag_list(json),
1405
        },
1406
        Cmd::Remote { cmd } => match cmd {
1407
            RemoteCmd::Add {
1408
                name,
1409
                url,
1410
                profile,
1411
                region,
1412
                endpoint,
1413
                credentials,
1414
            } => sync::remote_add(
1415
                &name,
1416
                &url,
1417
                &[
1418
                    ("profile", profile),
1419
                    ("region", region),
1420
                    ("endpoint", endpoint),
1421
                    ("credentials", credentials),
1422
                ],
1423
            ),
1424
            RemoteCmd::Check { name } => sync::remote_check(&name),
1425
            RemoteCmd::Remove { name } => sync::remote_remove(&name),
1426
            RemoteCmd::List { json } => sync::remote_list(json),
1427
        },
1428
        Cmd::Fetch { remote, json } => sync::fetch(remote.as_deref(), json),
1429
        Cmd::Pull { remote, json } => sync::pull(remote.as_deref(), json),
1430
        Cmd::Push {
1431
            remote,
1432
            force,
1433
            bookmark,
1434
            delete,
1435
            json,
1436
        } => match delete {
1437
            Some(name) => sync::push_delete(remote.as_deref(), &name, json),
1438
            None => sync::push(remote.as_deref(), force, bookmark.as_deref(), json),
1439
        },
1440
        Cmd::Serve {
1441
            root,
1442
            bind,
1443
            tokens,
1444
            tls_cert,
1445
            tls_key,
1446
        } => sync::serve(root, &bind, tokens, tls_cert.zip(tls_key)),
1447
        Cmd::Identity { cmd } => match cmd {
1448
            IdentityCmd::Create {
1449
                name,
1450
                email,
1451
                local,
1452
                expires,
1453
            } => identity::create(name, email, local, expires),
1454
            IdentityCmd::Show => identity::show(),
1455
            IdentityCmd::Export { path, pem } => identity::export(path, pem),
1456
            IdentityCmd::SignChallenge { challenge, json } => {
1457
                identity::sign_challenge(&challenge, json)
1458
            }
1459
            IdentityCmd::Rotate => identity::rotate(),
1460
            IdentityCmd::Renew { expires } => identity::renew(expires),
1461
            IdentityCmd::Revoke { key, reason } => identity::revoke(key.as_deref(), &reason),
1462
            IdentityCmd::Delegate { cmd } => match cmd {
1463
                DelegateCmd::Add {
1464
                    identity_file,
1465
                    scope,
1466
                    expires,
1467
                } => identity::delegate_add(&identity_file, &scope, expires.as_deref()),
1468
                DelegateCmd::Remove { key } => identity::delegate_remove(&key),
1469
                DelegateCmd::List => identity::delegate_list(),
1470
            },
1471
        },
1472
        Cmd::Git {
1473
            cmd: GitCmd::Export { git_dir, branch },
1474
        } => sync::git_export(&git_dir, branch.as_deref()),
1475
        Cmd::Git {
1476
            cmd: GitCmd::Fetch { url, branch, claim },
1477
        } => sync::git_fetch(&url, &branch, claim),
1478
        Cmd::Git {
1479
            cmd: GitCmd::Push { url, branch },
1480
        } => sync::git_push(&url, &branch),
1481
        Cmd::Git {
1482
            cmd: GitCmd::Recover,
1483
        } => sync::git_recover(),
1484
        Cmd::Git {
1485
            cmd:
1486
                GitCmd::Import {
1487
                    git_dir,
1488
                    dest,
1489
                    branch,
1490
                    claim,
1491
                },
1492
        } => sync::git_import(&git_dir, &dest, &branch, claim),
1493
        Cmd::Git {
1494
            cmd: GitCmd::Verify { git_dir, branch },
1495
        } => sync::git_verify(&git_dir, &branch),
1496
        Cmd::Policy { cmd } => match cmd {
1497
            PolicyCmd::Init => identity::policy_init(),
1498
            PolicyCmd::Show { json } => identity::policy_show(json),
1499
            PolicyCmd::Grant {
1500
                identity_file,
1501
                pattern,
1502
                out,
1503
            } => identity::policy_grant(&identity_file, pattern.as_deref(), out),
1504
            PolicyCmd::GrantSubject {
1505
                subject,
1506
                issuer,
1507
                scope,
1508
                pattern,
1509
                out,
1510
            } => identity::policy_grant_subject(&subject, &issuer, &scope, pattern.as_deref(), out),
1511
            PolicyCmd::Revoke { key, out } => identity::policy_revoke(&key, out),
1512
            PolicyCmd::Root { cmd } => match cmd {
1513
                RootCmd::Add { identity_file, out } => {
1514
                    identity::policy_root_add(&identity_file, out)
1515
                }
1516
                RootCmd::Remove { key, out } => identity::policy_root_remove(&key, out),
1517
                RootCmd::Threshold { n, out } => identity::policy_threshold(n, out),
1518
            },
1519
            PolicyCmd::Sign { proposal, out } => identity::policy_sign(&proposal, out),
1520
        },
1521
        Cmd::Seal {
1522
            flow: Some(url),
1523
            job: Some(job),
1524
            reject,
1525
            json,
1526
            ..
1527
        } => approve::seal(&url, &job, reject, json),
1528
        Cmd::Seal {
1529
            changes,
1530
            proposal: Some(url),
1531
            json,
1532
            ..
1533
        } if changes.is_empty() => proposal::seal(&url, json),
1534
        Cmd::Seal { changes, json, .. } => identity::seal(&changes, json),
1535
        Cmd::Proposal { cmd } => match cmd {
1536
            ProposalCmd::Show { url, json } => proposal::show(&url, json),
1537
            ProposalCmd::Land { url, force } => proposal::land(&url, force),
1538
        },
1539
        Cmd::Verify {
1540
            change,
1541
            revset,
1542
            json,
1543
        } => identity::verify(change.as_deref(), revset.as_deref(), json),
1544
        Cmd::Config {
1545
            key,
1546
            value,
1547
            user,
1548
            unset,
1549
        } => identity::config(&key, value.as_deref().unwrap_or(""), user, unset),
1550
    }
1551
}
1552
1553
#[cfg(test)]
1554
mod help_tests {
1555
    use super::Cli;
1556
    use clap::CommandFactory;
1557
1558
    /// Every command and every argument explains itself, so `--help`,
1559
    /// completions and the man pages are complete.
1560
    #[test]
1561
    fn every_command_and_argument_has_help() {
1562
        fn walk(cmd: &clap::Command, path: &str, missing: &mut Vec<String>) {
1563
            let here = format!("{path} {}", cmd.get_name()).trim().to_string();
1564
            if cmd.get_about().is_none() && path != "" {
1565
                missing.push(format!("{here} (command)"));
1566
            }
1567
            for a in cmd.get_arguments() {
1568
                let id = a.get_id().as_str();
1569
                if id == "help" || id == "version" || a.is_global_set() && path != "" {
1570
                    continue;
1571
                }
1572
                if a.get_help().is_none() && a.get_long_help().is_none() {
1573
                    missing.push(format!("{here} {id}"));
1574
                }
1575
            }
1576
            for sub in cmd.get_subcommands() {
1577
                walk(sub, &here, missing);
1578
            }
1579
        }
1580
        let mut missing = Vec::new();
1581
        walk(&Cli::command(), "", &mut missing);
1582
        assert!(
1583
            missing.is_empty(),
1584
            "{} without help:\n{}",
1585
            missing.len(),
1586
            missing.join("\n")
1587
        );
1588
    }
1589
}