OXYGENHUB
◆ agents-in-readme oxygen / src / cli / mod.rs
50.4 KiB · Rust · last changed A rotation follows you everywhere; config --unset; delegate add takes several files 1 month ago source blame history raw
1
//! Command-line definitions and dispatch.
2
3
mod auth;
4
mod format;
5
mod identity;
6
mod json;
7
mod proposal;
8
mod receipt;
9
mod sync;
10
pub mod ui;
11
mod workspace;
12
13
use anyhow::{Context, Result};
14
use clap::{Parser, Subcommand};
15
use oxy_core::repo::Repo;
16
use std::path::PathBuf;
17
18
#[derive(Parser)]
19
#[command(
20
    name = "oxy",
21
    version = env!("OXY_VERSION_LONG"),
22
    about = "Oxygen: breathing fresh air into version control"
23
)]
24
struct Cli {
25
    /// Plain output: no colour, no spinner (also implied by NO_COLOR or a non-terminal)
26
    #[arg(long, global = true)]
27
    plain: bool,
28
    /// Read the repository as it was after operation OP (an ID from oxy op log, @ or @-); commands that would change it are refused
29
    #[arg(long, global = true, value_name = "OP")]
30
    at_op: Option<String>,
31
    #[command(subcommand)]
32
    cmd: Cmd,
33
}
34
35
/// `--at-op`, for `open` to apply.
36
static AT_OP: std::sync::OnceLock<Option<String>> = std::sync::OnceLock::new();
37
38
#[derive(Subcommand)]
39
enum Cmd {
40
    /// Create a repository in PATH (default: current directory)
41
    Init {
42
        /// Directory to create the repository in (default: the current directory)
43
        path: Option<PathBuf>,
44
        /// No working copy: an object store and references only, for hosting
45
        #[arg(long)]
46
        bare: bool,
47
    },
48
    /// Clone a repository from a local path or an http(s) URL
49
    Clone {
50
        /// Local path or http(s) URL of the repository
51
        url: String,
52
        /// Directory to clone into (default: the last part of the URL)
53
        dest: Option<PathBuf>,
54
        /// Leave blobs larger than this (e.g. 1M) to be fetched on first read
55
        #[arg(long)]
56
        max_blob: Option<String>,
57
        /// Materialize only these paths (repeatable); implies lazy blobs
58
        #[arg(long = "sparse")]
59
        sparse: Vec<String>,
60
        /// Shallow clone: fetch only this many levels of history below the heads
61
        #[arg(long)]
62
        depth: Option<u32>,
63
        /// Clone the subrepos too (as oxy submodule update --init)
64
        #[arg(long)]
65
        recursive: bool,
66
    },
67
    /// Fetch more history below a shallow clone's boundary
68
    Deepen {
69
        /// Levels of history to add below the boundary
70
        #[arg(default_value_t = 10)]
71
        depth: u32,
72
        /// Remote to fetch the history from
73
        #[arg(long, default_value = "origin")]
74
        remote: String,
75
    },
76
    /// Show or set the sparse filter (paths to materialize); re-checks out the bookmark
77
    Sparse {
78
        /// Paths to materialize; none prints the current filter
79
        paths: Vec<String>,
80
    },
81
    /// Show the working copy: changed paths and conflicts
82
    Status {
83
        /// Machine-readable output: paths, and every conflict with its kind and contributing changes
84
        #[arg(long)]
85
        json: bool,
86
    },
87
    /// One-line summary for shell prompts; fast and records nothing
88
    Prompt {
89
        /// 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
90
        #[arg(long, default_value = oxy_core::repo::DEFAULT_PROMPT_FORMAT)]
91
        format: String,
92
        /// Glyph format (%i %b %c %s%U%O) instead of the plain default
93
        #[arg(long)]
94
        glyphs: bool,
95
        /// Colour by state (cyan clean, yellow dirty, red conflicted): raw escapes, or guarded for a bash or zsh PS1
96
        #[arg(long, value_parser = ["raw", "bash", "zsh"])]
97
        color: Option<String>,
98
    },
99
    /// Record the working copy as a change on the current bookmark
100
    Commit {
101
        /// The change's message: a subject line, then optionally a blank line and a body
102
        #[arg(short, long, required_unless_present = "file", conflicts_with = "file")]
103
        message: Option<String>,
104
        /// Read the message from FILE (`-` for standard input)
105
        #[arg(short = 'F', long, value_name = "FILE")]
106
        file: Option<PathBuf>,
107
        /// Extra explicit dependencies (ChangeId prefix, #hash prefix)
108
        #[arg(long = "depends-on")]
109
        depends_on: Vec<String>,
110
        /// Commit only these paths; the rest stays as uncommitted work
111
        paths: Vec<String>,
112
        /// Choose hunks one by one
113
        #[arg(short, long)]
114
        interactive: bool,
115
        /// Commit these hunks (as `oxy hunks` numbers them), comma separated
116
        #[arg(long, value_delimiter = ',')]
117
        hunks: Vec<usize>,
118
    },
119
    /// Fold CHANGE into one of its dependencies (keeps that change's id)
120
    Squash {
121
        /// The change to fold (ChangeId prefix, #hash prefix)
122
        change: String,
123
        /// The dependency that absorbs it
124
        #[arg(long)]
125
        into: String,
126
    },
127
    /// Re-express CHANGE against another base state
128
    Rebase {
129
        /// The change to re-express (ChangeId prefix, #hash prefix)
130
        change: String,
131
        /// The changes, bookmark or tag whose state becomes the new base
132
        #[arg(long, required = true, num_args = 1..)]
133
        onto: Vec<String>,
134
        /// Where the rebase would be conflicted, merge the target into the
135
        /// current bookmark instead, leaving the conflict in the working
136
        /// copy to fix and commit
137
        #[arg(long)]
138
        into_working_copy: bool,
139
    },
140
    /// Show which change introduced each line of a file
141
    Blame {
142
        /// File to annotate, relative to the repository root
143
        path: String,
144
        /// Machine-readable output: one JSON document on stdout
145
        #[arg(long)]
146
        json: bool,
147
    },
148
    /// Find the change that introduced a regression
149
    Bisect {
150
        #[command(subcommand)]
151
        cmd: BisectCmd,
152
    },
153
    /// List changes reachable from the bookmark and the working copy
154
    Log {
155
        /// Show the changes a revset selects: `main..feature & author(andy)`,
156
        /// `touches("src/**") & date(-7d..)` (see docs/revset_query_language.md)
157
        #[arg(short = 'r', long, value_name = "REVSET", conflicts_with_all = ["path", "lines"])]
158
        revset: Option<String>,
159
        /// Show at most this many changes
160
        #[arg(short = 'n', long)]
161
        limit: Option<usize>,
162
        /// Show full messages, dependencies, and signatures
163
        #[arg(short, long)]
164
        verbose: bool,
165
        /// Only changes touching this path (renames followed)
166
        #[arg(long)]
167
        path: Option<String>,
168
        /// Only changes that wrote or deleted lines START to END of PATH
169
        /// (numbered as in the working copy's base), or deleted lines from
170
        /// between them: the history of a function
171
        #[arg(short = 'L', value_name = "START,END:PATH", conflicts_with = "path")]
172
        lines: Option<String>,
173
        /// Machine-readable output: one JSON document on stdout
174
        #[arg(long)]
175
        json: bool,
176
    },
177
    /// Show what the working copy changes against its base
178
    Diff {
179
        /// A change instead of the working copy
180
        change: Option<String>,
181
        /// Machine-readable output: one JSON document on stdout
182
        #[arg(long)]
183
        json: bool,
184
    },
185
    /// Print a file as it is in a state (default: the working copy's base)
186
    Cat {
187
        /// File to print, relative to the repository root
188
        path: String,
189
        /// A change, bookmark, or tag
190
        #[arg(short = 'r', long)]
191
        change: Option<String>,
192
    },
193
    /// Print the repository root, workspace, and bookmark
194
    Root {
195
        /// Machine-readable output: one JSON document on stdout
196
        #[arg(long)]
197
        json: bool,
198
    },
199
    /// Print the version of oxy, its repository format, and protocol
200
    Version {
201
        /// Machine-readable output: one JSON document on stdout
202
        #[arg(long)]
203
        json: bool,
204
    },
205
    /// Restore paths to the working copy's base, or undo chosen hunks
206
    Discard {
207
        /// Paths to restore; none with --hunks, or everything with neither
208
        paths: Vec<String>,
209
        /// Hunk indices as `oxy hunks` numbers them, comma separated
210
        #[arg(long, value_delimiter = ',')]
211
        hunks: Vec<usize>,
212
    },
213
    /// List the working copy's hunks, numbered as commit --hunks expects
214
    Hunks {
215
        /// Machine-readable output: one JSON document on stdout
216
        #[arg(long)]
217
        json: bool,
218
    },
219
    /// With -m, change the message of a head; without, name the working
220
    /// copy for a build: its tag, or the nearest tag plus distance and
221
    /// change, or the bookmark and change, and -dirty
222
    Describe {
223
        /// The new message
224
        #[arg(short, long)]
225
        message: Option<String>,
226
        /// The head to describe (default: the one the working copy sits on)
227
        change: Option<String>,
228
        /// Machine-readable output (naming only)
229
        #[arg(long, conflicts_with = "message")]
230
        json: bool,
231
    },
232
    /// Fold the working copy into the head it sits on, or into CHANGE with --into
233
    Amend {
234
        /// Replace the message too
235
        #[arg(short, long)]
236
        message: Option<String>,
237
        /// Amend an older change; the edits must touch only lines it already knows
238
        #[arg(long)]
239
        into: Option<String>,
240
    },
241
    /// Split a head: the named paths become a separate first change
242
    Split {
243
        /// The head to split
244
        change: String,
245
        /// Paths whose edits become the first change
246
        paths: Vec<String>,
247
    },
248
    /// Drop a change from the bookmark; its content stays on disk as uncommitted work
249
    Abandon {
250
        /// The change to drop (ChangeId prefix, #hash prefix)
251
        #[arg(required_unless_present = "revset")]
252
        change: Option<String>,
253
        /// Drop every change a revset selects, newest first
254
        #[arg(short = 'r', long, value_name = "REVSET", conflicts_with = "change")]
255
        revset: Option<String>,
256
        /// Drop more than five changes without asking
257
        #[arg(long, short = 'y')]
258
        yes: bool,
259
    },
260
    /// Show one change (ChangeId prefix, #hash prefix, or @), or those a revset selects
261
    Show {
262
        /// ChangeId prefix, #hash prefix, or @ for the working copy
263
        #[arg(required_unless_present = "revset")]
264
        change: Option<String>,
265
        /// Show every change a revset selects
266
        #[arg(short = 'r', long, value_name = "REVSET", conflicts_with = "change")]
267
        revset: Option<String>,
268
        /// Machine-readable output: one JSON document on stdout
269
        #[arg(long)]
270
        json: bool,
271
    },
272
    /// Move the working copy to the state formed by CHANGE... (default: the bookmark heads)
273
    Checkout {
274
        /// Changes whose state to check out (default: the bookmark heads)
275
        changes: Vec<String>,
276
        /// Throw away uncommitted working-copy changes
277
        #[arg(long)]
278
        discard: bool,
279
        /// Take uncommitted changes along; refused when the target has
280
        /// other versions of the files they touch
281
        #[arg(long, conflicts_with = "discard")]
282
        carry: bool,
283
    },
284
    /// Report which conflicts of the working copy's base are resolved by the current files
285
    Resolve {
286
        /// Machine-readable output: one JSON document on stdout
287
        #[arg(long)]
288
        json: bool,
289
    },
290
    /// Undo the last operation
291
    Undo,
292
    /// Write a checkpoint of the current bookmark's state now
293
    Checkpoint,
294
    /// Print a shell completion script: `oxy completions bash > ~/.local/share/bash-completion/completions/oxy`
295
    Completions {
296
        /// bash, zsh, fish, elvish or powershell
297
        shell: clap_complete::Shell,
298
    },
299
    /// Write man pages (oxy.1 and one per command) into DIR, or print oxy.1
300
    Man {
301
        /// Directory to write every page into, e.g. ~/.local/share/man/man1
302
        #[arg(long)]
303
        dir: Option<PathBuf>,
304
    },
305
    /// Check the identity, configuration and remotes, and say what to do about any problem
306
    Doctor {
307
        /// Do not contact the remotes
308
        #[arg(long)]
309
        offline: bool,
310
        /// Machine-readable output: one JSON document on stdout
311
        #[arg(long)]
312
        json: bool,
313
    },
314
    /// Check the repository's integrity: objects, operation log, references, signatures, checkpoints
315
    Fsck {
316
        /// Machine-readable output: one JSON document on stdout
317
        #[arg(long)]
318
        json: bool,
319
    },
320
    /// Prune unreachable objects older than the grace period and repack everything into one pack
321
    Gc {
322
        /// Ignore the grace period (core.gc-grace) and prune immediately
323
        #[arg(long)]
324
        now: bool,
325
        /// 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)
326
        #[arg(long, value_name = "NAME")]
327
        remote: Option<String>,
328
    },
329
    /// List operations
330
    #[command(name = "op")]
331
    Op {
332
        #[command(subcommand)]
333
        cmd: OpCmd,
334
    },
335
    /// Take the advisory local lock on a lockable file, making it writable
336
    Lock {
337
        /// The lockable file
338
        path: String,
339
    },
340
    /// Release the local lock and make the file read-only again
341
    Unlock {
342
        /// The locked file
343
        path: String,
344
    },
345
    /// Workspaces: extra checkout directories sharing this repository
346
    Workspace {
347
        #[command(subcommand)]
348
        cmd: WorkspaceCmd,
349
    },
350
    /// Bookmarks: named states; each workspace works on one
351
    Bookmark {
352
        #[command(subcommand)]
353
        cmd: BookmarkCmd,
354
    },
355
    /// Union another bookmark's heads into the current one (no merge change)
356
    Merge {
357
        /// The bookmark whose heads to add
358
        bookmark: String,
359
    },
360
    /// Sign in to a hub once, for every repository on it
361
    Auth {
362
        #[command(subcommand)]
363
        cmd: AuthCmd,
364
    },
365
    /// Subrepositories: nested repositories pinned to a state
366
    Submodule {
367
        #[command(subcommand)]
368
        cmd: SubmoduleCmd,
369
    },
370
    /// Execution receipts: signed records of what a runner ran on a state
371
    Receipt {
372
        #[command(subcommand)]
373
        cmd: ReceiptCmd,
374
    },
375
    /// Tags: immutable named states
376
    Tag {
377
        #[command(subcommand)]
378
        cmd: TagCmd,
379
    },
380
    /// Manage remotes
381
    Remote {
382
        #[command(subcommand)]
383
        cmd: RemoteCmd,
384
    },
385
    /// Fetch every bookmark of a remote into its tracking references
386
    Fetch {
387
        /// Remote name (default: origin)
388
        remote: Option<String>,
389
        /// Machine-readable output: one JSON document on stdout
390
        #[arg(long)]
391
        json: bool,
392
    },
393
    /// Fetch, then union the remote bookmark into the local one (no merge change)
394
    Pull {
395
        /// Remote name (default: origin)
396
        remote: Option<String>,
397
        /// Machine-readable output: one JSON document on stdout
398
        #[arg(long)]
399
        json: bool,
400
    },
401
    /// Push the current bookmark to a remote
402
    Push {
403
        /// Remote name (default: origin)
404
        remote: Option<String>,
405
        /// Allow dropping heads the remote has
406
        #[arg(long)]
407
        force: bool,
408
        /// Push this bookmark instead of the workspace's current one
409
        #[arg(short = 'b', long)]
410
        bookmark: Option<String>,
411
        /// Delete this bookmark on the remote (never its default bookmark);
412
        /// the local bookmark is kept
413
        #[arg(long, value_name = "NAME", conflicts_with_all = ["bookmark", "force"])]
414
        delete: Option<String>,
415
        /// Machine-readable output: one JSON document on stdout
416
        #[arg(long)]
417
        json: bool,
418
    },
419
    /// Serve repositories under ROOT over HTTPS (with --tls-cert/--tls-key) or plain HTTP
420
    Serve {
421
        /// Directory with one repository per subdirectory
422
        #[arg(long, default_value = ".")]
423
        root: PathBuf,
424
        /// Address and port to listen on
425
        #[arg(long, default_value = "127.0.0.1:8890")]
426
        bind: String,
427
        /// Bearer token allowed to push (repeatable); none means open
428
        #[arg(long = "token")]
429
        tokens: Vec<String>,
430
        /// PEM certificate chain; enables HTTPS together with --tls-key
431
        #[arg(long, requires = "tls_key")]
432
        tls_cert: Option<PathBuf>,
433
        /// PEM private key
434
        #[arg(long, requires = "tls_cert")]
435
        tls_key: Option<PathBuf>,
436
    },
437
    /// Manage the signing identity
438
    Identity {
439
        #[command(subcommand)]
440
        cmd: IdentityCmd,
441
    },
442
    /// Seal changes with your key: named ones, or every change on the current bookmark you can authenticate but have not
443
    Seal {
444
        /// Changes to seal (default: every change on the bookmark you can authenticate and have not sealed)
445
        changes: Vec<String>,
446
        /// Seal every change of a hub proposal and push the seal (your approval)
447
        #[arg(long, conflicts_with = "changes")]
448
        proposal: Option<String>,
449
        /// Machine-readable output: one JSON document on stdout
450
        #[arg(long)]
451
        json: bool,
452
    },
453
    /// Hub proposals: show one, or land it from this client
454
    Proposal {
455
        #[command(subcommand)]
456
        cmd: ProposalCmd,
457
    },
458
    /// Verify the signatures on a change (default: every change in the log)
459
    Verify {
460
        /// The change to verify (default: every change in the log)
461
        change: Option<String>,
462
        /// Verify every change a revset selects: `release..main & unsigned()`
463
        #[arg(short = 'r', long, value_name = "REVSET", conflicts_with = "change")]
464
        revset: Option<String>,
465
        /// Machine-readable output: one JSON document on stdout
466
        #[arg(long)]
467
        json: bool,
468
    },
469
    /// Git interoperability
470
    Git {
471
        #[command(subcommand)]
472
        cmd: GitCmd,
473
    },
474
    /// Manage the trust policy (who may update which bookmarks)
475
    Policy {
476
        #[command(subcommand)]
477
        cmd: PolicyCmd,
478
    },
479
    /// 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)
480
    Config {
481
        /// The key, such as author.name or remote.origin.token
482
        key: String,
483
        /// The value to set (none with --unset)
484
        #[arg(required_unless_present = "unset")]
485
        value: Option<String>,
486
        /// Write to the user file (~/.config/oxy/config.toml) instead of this repository
487
        #[arg(long)]
488
        user: bool,
489
        /// Remove the key from this repository's file (or the user file with --user)
490
        #[arg(long)]
491
        unset: bool,
492
    },
493
}
494
495
#[derive(Subcommand)]
496
enum OpCmd {
497
    /// List operations, newest first, with what each changed
498
    Log {
499
        /// Show only the latest N (snapshots folded together count as one)
500
        #[arg(short = 'n', long)]
501
        limit: Option<usize>,
502
        /// Machine-readable output: one JSON document on stdout
503
        #[arg(long)]
504
        json: bool,
505
    },
506
    /// Go back to the bookmarks and working copy as they were after OP (recorded as a new operation, so it can be undone)
507
    Restore {
508
        /// The operation: an ID prefix from oxy op log, @ or @-
509
        op: String,
510
        /// Throw away uncommitted working-copy changes
511
        #[arg(long)]
512
        discard: bool,
513
    },
514
    /// Undo OP alone, keeping every operation after it: the bookmarks it moved go back
515
    Revert {
516
        /// The operation: an ID prefix from oxy op log
517
        op: String,
518
    },
519
}
520
521
#[derive(Subcommand)]
522
enum WorkspaceCmd {
523
    /// Create a workspace at PATH checked out on CHANGE... (default: the bookmark heads)
524
    Add {
525
        /// Directory for the new workspace (must be empty or absent)
526
        path: PathBuf,
527
        /// Changes whose state to check out there
528
        changes: Vec<String>,
529
    },
530
    /// List workspaces and their working-copy changes
531
    List {
532
        /// Machine-readable output: one JSON document on stdout
533
        #[arg(long)]
534
        json: bool,
535
    },
536
    /// Stop tracking a workspace (by id prefix or path); its files stay
537
    Forget {
538
        /// Workspace id prefix or path
539
        workspace: String,
540
    },
541
}
542
543
#[derive(Subcommand)]
544
enum BisectCmd {
545
    /// Begin; remembers where the working copy was
546
    Start,
547
    /// Mark CHANGE... (or the state under test) as good
548
    Good {
549
        /// Changes known to be good (default: the state under test)
550
        changes: Vec<String>,
551
    },
552
    /// Mark CHANGE... (or the state under test) as bad
553
    Bad {
554
        /// Changes known to be bad (default: the state under test)
555
        changes: Vec<String>,
556
    },
557
    /// The state under test cannot be tested; try another
558
    Skip,
559
    /// Run a command for each step: exit 0 good, 125 skip, else bad
560
    Run {
561
        /// Command and arguments to run at each step
562
        #[arg(required = true, trailing_var_arg = true)]
563
        command: Vec<String>,
564
    },
565
    /// Where the search stands
566
    Status {
567
        /// Machine-readable output: one JSON document on stdout
568
        #[arg(long)]
569
        json: bool,
570
    },
571
    /// Restore the working copy and forget the search
572
    Reset,
573
}
574
575
#[derive(Subcommand)]
576
enum BookmarkCmd {
577
    /// Create a bookmark at CHANGE... (default: the working copy's base)
578
    Create {
579
        /// Name of the new bookmark
580
        name: String,
581
        /// Its heads (default: the working copy's base). A state is its
582
        /// heads and what they depend on: a change alone may leave out
583
        /// earlier changes of the current state, which is refused unless
584
        /// --upto or --only says which is meant
585
        changes: Vec<String>,
586
        /// Everything of the current state up to CHANGE..., as a Git
587
        /// branch at a commit would hold
588
        #[arg(long, conflicts_with = "only")]
589
        upto: bool,
590
        /// Just CHANGE... and what they depend on
591
        #[arg(long)]
592
        only: bool,
593
    },
594
    /// Point an existing bookmark at CHANGE...
595
    Set {
596
        /// The bookmark to move
597
        name: String,
598
        /// Its new heads. A change alone may leave out changes the
599
        /// bookmark now holds that were recorded before it, which is
600
        /// refused unless --upto or --only says which is meant
601
        changes: Vec<String>,
602
        /// Everything the bookmark holds up to CHANGE..., as a Git branch
603
        /// moved to a commit would hold
604
        #[arg(long, conflicts_with = "only")]
605
        upto: bool,
606
        /// Just CHANGE... and what they depend on
607
        #[arg(long)]
608
        only: bool,
609
    },
610
    /// Remove a bookmark; its changes stay
611
    Delete {
612
        /// The bookmark to remove
613
        name: String,
614
    },
615
    /// List bookmarks with their heads and difference from origin
616
    List {
617
        /// Also list remotes' bookmarks that have no local bookmark
618
        #[arg(long, short)]
619
        all: bool,
620
        /// Machine-readable output: one JSON document on stdout
621
        #[arg(long)]
622
        json: bool,
623
    },
624
    /// Make NAME this workspace's bookmark and check it out. Uncommitted work stops it; --carry takes the work along, --discard drops it
625
    Switch {
626
        /// The bookmark to work on; one only a remote has (NAME or REMOTE/NAME) is created from it
627
        name: String,
628
        /// Throw away uncommitted working-copy changes
629
        #[arg(long)]
630
        discard: bool,
631
        /// Take uncommitted changes along; refused when NAME has other
632
        /// versions of the files they touch
633
        #[arg(long, conflicts_with = "discard")]
634
        carry: bool,
635
    },
636
}
637
638
#[derive(Subcommand)]
639
enum AuthCmd {
640
    /// Sign in to HUB in the browser (device sign-in) and keep the token for
641
    /// every repository there
642
    Login {
643
        /// The hub, as https://oxygenhub.io or oxygenhub.io (default: this checkout's origin)
644
        hub: Option<String>,
645
        /// Read a token from standard input instead (for CI and scripts)
646
        #[arg(long)]
647
        with_token: bool,
648
        /// PEM file of extra roots to trust (a hub with a self-signed certificate)
649
        #[arg(long)]
650
        ca: Option<PathBuf>,
651
    },
652
    /// List the hubs you are signed in to, checking each token with its hub
653
    Status,
654
    /// Forget HUB's token and revoke it on the hub
655
    Logout {
656
        /// The hub (default: this checkout's origin, or the only one)
657
        hub: Option<String>,
658
    },
659
}
660
661
#[derive(Subcommand)]
662
enum SubmoduleCmd {
663
    /// Clone URL (Oxygen or Git) into PATH and register it in .oxymodules
664
    Add {
665
        /// URL or path of the Oxygen or Git repository
666
        url: String,
667
        /// Where to put it, relative to the repository root
668
        path: String,
669
        /// Bookmark (Oxygen) or branch (Git) to check out and track
670
        #[arg(long)]
671
        bookmark: Option<String>,
672
    },
673
    /// Bring nested repositories to their pins
674
    Update {
675
        /// Clone nested repositories that are absent
676
        #[arg(long)]
677
        init: bool,
678
        /// Subrepos to update (default: all)
679
        paths: Vec<String>,
680
    },
681
    /// List subrepos with their pins and on-disk state
682
    Status {
683
        /// Machine-readable output: one JSON document on stdout
684
        #[arg(long)]
685
        json: bool,
686
    },
687
    /// Unregister PATH; the directory stays, the node goes with the next commit
688
    Remove {
689
        /// The subrepo to unregister
690
        path: String,
691
    },
692
}
693
694
#[derive(Subcommand)]
695
enum ReceiptCmd {
696
    /// The receipts kept for a state (default: the bookmark heads)
697
    List {
698
        /// Change, bookmark or tag whose state to look at
699
        rev: Option<String>,
700
        /// Machine-readable output: one JSON document on stdout
701
        #[arg(long)]
702
        json: bool,
703
    },
704
    /// One receipt in full, and whether its signature verifies
705
    Show {
706
        /// The receipt (a prefix of its hash, as `list` shows it)
707
        receipt: String,
708
        /// Change, bookmark or tag whose state holds it
709
        #[arg(long)]
710
        rev: Option<String>,
711
        /// Machine-readable output: one JSON document on stdout
712
        #[arg(long)]
713
        json: bool,
714
    },
715
    /// Write a signed receipt to FILE
716
    Export {
717
        /// The receipt (a prefix of its hash, as `list` shows it)
718
        receipt: String,
719
        /// Where to write it
720
        file: String,
721
        /// Change, bookmark or tag whose state holds it
722
        #[arg(long)]
723
        rev: Option<String>,
724
    },
725
    /// Check a signed receipt in FILE without keeping it
726
    Verify {
727
        /// A signed receipt, as a runner or `export` writes it
728
        file: String,
729
        /// Machine-readable output: one JSON document on stdout
730
        #[arg(long)]
731
        json: bool,
732
    },
733
    /// Verify a signed receipt in FILE and keep it with its state
734
    Import {
735
        /// A signed receipt, as a runner or `export` writes it
736
        file: String,
737
    },
738
}
739
740
#[derive(Subcommand)]
741
enum TagCmd {
742
    /// Tag the state of CHANGE... (default: the bookmark heads)
743
    Create {
744
        /// Name of the tag; tags never move
745
        name: String,
746
        /// Changes whose state to tag (default: the bookmark heads)
747
        changes: Vec<String>,
748
        /// A message recorded with the tag
749
        #[arg(short, long, default_value = "")]
750
        message: String,
751
    },
752
    /// List tags and the states they name
753
    List {
754
        /// Machine-readable output: one JSON document on stdout
755
        #[arg(long)]
756
        json: bool,
757
    },
758
}
759
760
#[derive(Subcommand)]
761
enum RemoteCmd {
762
    /// Add a remote
763
    Add {
764
        /// Name to refer to the remote by, such as origin
765
        name: String,
766
        /// 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
767
        url: String,
768
        /// For an s3:// store: the AWS profile to use (default: AWS_PROFILE, else "default")
769
        #[arg(long)]
770
        profile: Option<String>,
771
        /// For an s3:// store: its region (default: the environment, the profile, else asked of S3)
772
        #[arg(long)]
773
        region: Option<String>,
774
        /// For an s3://, gs:// or az:// store: another endpoint, such as an S3-compatible service
775
        #[arg(long)]
776
        endpoint: Option<String>,
777
        /// For a gs:// store: a service account key file, or application default credentials
778
        #[arg(long)]
779
        credentials: Option<String>,
780
    },
781
    /// Check a remote: that it is reachable with the credentials in hand, what it holds, and (for a store) that it behaves
782
    Check {
783
        /// The remote to check
784
        name: String,
785
    },
786
    /// Remove a remote
787
    Remove {
788
        /// The remote to remove
789
        name: String,
790
    },
791
    /// List remotes and their URLs
792
    List {
793
        /// Machine-readable output: one JSON document on stdout
794
        #[arg(long)]
795
        json: bool,
796
    },
797
}
798
799
#[derive(Subcommand)]
800
enum ProposalCmd {
801
    /// Show a proposal by its hub URL
802
    Show {
803
        /// The proposal's URL on the hub
804
        url: String,
805
        /// Machine-readable output: one JSON document on stdout
806
        #[arg(long)]
807
        json: bool,
808
    },
809
    /// Fetch the proposal's changes, set the target bookmark to the projected state, and push with your key
810
    Land {
811
        /// The proposal's URL on the hub
812
        url: String,
813
        /// Land even when the hub reports blockers
814
        #[arg(long)]
815
        force: bool,
816
    },
817
}
818
819
#[derive(Subcommand)]
820
enum IdentityCmd {
821
    /// Generate a composite Ed25519 + ML-DSA-65 key pair and make it your author identity
822
    Create {
823
        /// Your name (default: author.name)
824
        #[arg(long)]
825
        name: Option<String>,
826
        /// Your email (default: author.email)
827
        #[arg(long)]
828
        email: Option<String>,
829
        /// Record the author in this repository only, not in the user config
830
        #[arg(long)]
831
        local: bool,
832
        /// Expire the identity this many days from now
833
        #[arg(long)]
834
        expires: Option<u64>,
835
    },
836
    /// Show the configured identity
837
    Show,
838
    /// Write your identity object to a file, to hand to a policy holder
839
    Export {
840
        /// File to write (default: <key>.identity in the current directory)
841
        path: Option<PathBuf>,
842
    },
843
    /// Sign a host's challenge (hex, at least 16 bytes) to prove you hold your key; prints the signature as hex
844
    SignChallenge {
845
        /// The host's challenge, as hex
846
        challenge: String,
847
        /// Machine-readable output: one JSON document on stdout
848
        #[arg(long)]
849
        json: bool,
850
    },
851
    /// Move to a fresh key pair; the policy follows if it names your key
852
    Rotate,
853
    /// Re-issue your identity with a new expiry (in days from now; 0 = never)
854
    Renew {
855
        /// Days from now until it expires; 0 for never
856
        #[arg(long)]
857
        expires: u64,
858
    },
859
    /// Revoke a key: yours by default, or any key if you hold a policy root
860
    Revoke {
861
        /// KeyId prefix (default: your own key)
862
        key: Option<String>,
863
        /// Why, recorded in the revocation
864
        #[arg(long, default_value = "")]
865
        reason: String,
866
    },
867
    /// Keys allowed to sign or seal on your behalf
868
    Delegate {
869
        #[command(subcommand)]
870
        cmd: DelegateCmd,
871
    },
872
}
873
874
#[derive(Subcommand)]
875
enum DelegateCmd {
876
    /// Add a delegate from an exported identity file
877
    Add {
878
        /// The delegates' exported identity files: one rotation for all of them
879
        #[arg(required = true)]
880
        identity_file: Vec<PathBuf>,
881
        /// What it may do for you: changes, refs, seal (comma-separated) or all.
882
        /// An agent gets `changes`; a host that lands for you needs `changes,refs`
883
        #[arg(long, default_value = "changes")]
884
        scope: String,
885
        /// How long for: 30m, 8h, 7d, or a number of days (default: until removed)
886
        #[arg(long)]
887
        expires: Option<String>,
888
    },
889
    /// Remove a delegate by KeyId prefix
890
    Remove {
891
        /// KeyId prefix of the delegate
892
        key: String,
893
    },
894
    /// List delegates
895
    List,
896
}
897
898
#[derive(Subcommand)]
899
enum GitCmd {
900
    /// Export the current bookmark to a Git repository (created bare if absent)
901
    Export {
902
        /// Git repository to write to (created bare if absent)
903
        git_dir: PathBuf,
904
        /// Git branch to write (default: the bookmark name)
905
        #[arg(long)]
906
        branch: Option<String>,
907
    },
908
    /// Fetch a branch from a Git remote (path or URL) into the current bookmark
909
    Fetch {
910
        /// Git remote: a path or URL
911
        url: String,
912
        /// Branch to fetch
913
        #[arg(long, default_value = "main")]
914
        branch: String,
915
    },
916
    /// Push the current bookmark to a branch of a Git remote (fetch first)
917
    Push {
918
        /// Git remote: a path or URL
919
        url: String,
920
        /// Branch to push to
921
        #[arg(long, default_value = "main")]
922
        branch: String,
923
    },
924
    /// Check this repository's import of a Git branch: every commit's tree must be reproduced exactly
925
    Verify {
926
        /// The Git repository the import came from
927
        git_dir: PathBuf,
928
        /// Branch that was imported
929
        #[arg(long, default_value = "main")]
930
        branch: String,
931
        /// Rebuild only the last N commits (the others are followed, not checked)
932
        #[arg(long, value_name = "N")]
933
        last: Option<usize>,
934
    },
935
    /// Import a Git branch into a new Oxygen repository at DEST
936
    Import {
937
        /// Git repository to read
938
        git_dir: PathBuf,
939
        /// Directory for the new Oxygen repository
940
        dest: PathBuf,
941
        /// Branch to import
942
        #[arg(long, default_value = "main")]
943
        branch: String,
944
    },
945
}
946
947
#[derive(Subcommand)]
948
enum PolicyCmd {
949
    /// Create the initial policy with your identity as the only root
950
    Init,
951
    /// Show the current policy
952
    Show {
953
        /// Machine-readable output: one JSON document on stdout
954
        #[arg(long)]
955
        json: bool,
956
    },
957
    /// Allow an identity (from `oxy identity export`) to update bookmarks
958
    Grant {
959
        /// The identity to grant, exported with `oxy identity export`
960
        identity_file: PathBuf,
961
        /// Reference pattern the grant applies to (default: every bookmark)
962
        #[arg(long)]
963
        pattern: Option<String>,
964
        /// Where to write the proposal when more root signatures are needed
965
        #[arg(long)]
966
        out: Option<PathBuf>,
967
    },
968
    /// Revoke a key (KeyId prefix of an identity in the policy)
969
    Revoke {
970
        /// KeyId prefix of the identity
971
        key: String,
972
        /// Where to write the proposal when more root signatures are needed
973
        #[arg(long)]
974
        out: Option<PathBuf>,
975
    },
976
    /// Manage root keys and the signature threshold for policy updates
977
    Root {
978
        #[command(subcommand)]
979
        cmd: RootCmd,
980
    },
981
    /// Add your root signature to a proposal file; applies it once the threshold is met
982
    Sign {
983
        /// The proposal file to sign
984
        proposal: PathBuf,
985
        /// Where to write the proposal if it still needs signatures (default: in place)
986
        #[arg(long)]
987
        out: Option<PathBuf>,
988
    },
989
}
990
991
#[derive(Subcommand)]
992
enum RootCmd {
993
    /// Make an exported identity a root key
994
    Add {
995
        /// The identity to make a root, exported with `oxy identity export`
996
        identity_file: PathBuf,
997
        /// Where to write the proposal when more root signatures are needed
998
        #[arg(long)]
999
        out: Option<PathBuf>,
1000
    },
1001
    /// Remove a root key by KeyId prefix
1002
    Remove {
1003
        /// KeyId prefix of the root key
1004
        key: String,
1005
        /// Where to write the proposal when more root signatures are needed
1006
        #[arg(long)]
1007
        out: Option<PathBuf>,
1008
    },
1009
    /// Require this many root signatures on policy updates
1010
    Threshold {
1011
        /// Number of root signatures
1012
        n: u32,
1013
        /// Where to write the proposal when more root signatures are needed
1014
        #[arg(long)]
1015
        out: Option<PathBuf>,
1016
    },
1017
}
1018
1019
/// Open the repository containing the current directory.
1020
fn open() -> Result<Repo> {
1021
    let repo = Repo::open(&std::env::current_dir()?).context("opening repository")?;
1022
    match AT_OP.get().and_then(|o| o.as_deref()) {
1023
        Some(op) => Ok(repo.at_op(op)?),
1024
        None => Ok(repo),
1025
    }
1026
}
1027
1028
/// `oxy man`: every page into `dir`, or the top-level page on stdout.
1029
fn man(dir: Option<PathBuf>) -> Result<i32> {
1030
    use clap::CommandFactory;
1031
    let cmd = Cli::command();
1032
    match dir {
1033
        Some(dir) => {
1034
            std::fs::create_dir_all(&dir)?;
1035
            clap_mangen::generate_to(cmd, &dir)?;
1036
            let pages = std::fs::read_dir(&dir)?
1037
                .filter(|e| {
1038
                    e.as_ref()
1039
                        .is_ok_and(|e| e.path().extension().is_some_and(|x| x == "1"))
1040
                })
1041
                .count();
1042
            println!(
1043
                "{}",
1044
                ui::done(format!("Wrote {pages} man pages to {}", dir.display()))
1045
            );
1046
        }
1047
        None => clap_mangen::Man::new(cmd).render(&mut std::io::stdout())?,
1048
    }
1049
    Ok(0)
1050
}
1051
1052
/// Parse arguments and run; returns the process exit code.
1053
pub fn run() -> Result<i32> {
1054
    // The cloud stores (s3://, gs://, az://) for remotes kept on them.
1055
    oxy_strata::register();
1056
    let cli = Cli::parse();
1057
    ui::init(cli.plain);
1058
    AT_OP.set(cli.at_op.clone()).ok();
1059
    match cli.cmd {
1060
        Cmd::Completions { shell } => {
1061
            use clap::CommandFactory;
1062
            clap_complete::generate(shell, &mut Cli::command(), "oxy", &mut std::io::stdout());
1063
            Ok(0)
1064
        }
1065
        Cmd::Man { dir } => man(dir),
1066
        Cmd::Init { path, bare } => workspace::init(path, bare),
1067
        Cmd::Status { json } => workspace::status(json),
1068
        Cmd::Prompt {
1069
            format,
1070
            glyphs,
1071
            color,
1072
        } => workspace::prompt(&format, glyphs, color.as_deref()),
1073
        Cmd::Commit {
1074
            message,
1075
            file,
1076
            depends_on,
1077
            paths,
1078
            interactive,
1079
            hunks,
1080
        } => {
1081
            let message = match (message, file) {
1082
                (Some(m), _) => m,
1083
                (None, Some(f)) if f.as_os_str() == "-" => {
1084
                    let mut s = String::new();
1085
                    std::io::Read::read_to_string(&mut std::io::stdin(), &mut s)?;
1086
                    s
1087
                }
1088
                (None, Some(f)) => std::fs::read_to_string(&f).map_err(|e| {
1089
                    anyhow::anyhow!("reading the message from {}: {e}", f.display())
1090
                })?,
1091
                (None, None) => anyhow::bail!("a commit message is required (-m or -F)"),
1092
            };
1093
            workspace::commit(&message, &depends_on, &paths, interactive, &hunks)
1094
        }
1095
        Cmd::Cat { path, change } => workspace::cat(&path, change.as_deref()),
1096
        Cmd::Root { json } => workspace::root(json),
1097
        Cmd::Version { json } => workspace::version(json),
1098
        Cmd::Discard { paths, hunks } => workspace::discard(&paths, &hunks),
1099
        Cmd::Hunks { json } => workspace::hunks(json),
1100
        Cmd::Squash { change, into } => workspace::squash(&change, &into),
1101
        Cmd::Rebase {
1102
            change,
1103
            onto,
1104
            into_working_copy,
1105
        } => workspace::rebase(&change, &onto, into_working_copy),
1106
        Cmd::Blame { path, json } => workspace::blame(&path, json),
1107
        Cmd::Bisect { cmd } => match cmd {
1108
            BisectCmd::Start => workspace::bisect_start(),
1109
            BisectCmd::Good { changes } => workspace::bisect_mark(Some(true), &changes),
1110
            BisectCmd::Bad { changes } => workspace::bisect_mark(Some(false), &changes),
1111
            BisectCmd::Skip => workspace::bisect_mark(None, &[]),
1112
            BisectCmd::Run { command } => workspace::bisect_run(&command),
1113
            BisectCmd::Status { json } => workspace::bisect_status(json),
1114
            BisectCmd::Reset => workspace::bisect_reset(),
1115
        },
1116
        Cmd::Log {
1117
            revset,
1118
            limit,
1119
            verbose,
1120
            path,
1121
            lines,
1122
            json,
1123
        } => workspace::log(
1124
            revset.as_deref(),
1125
            limit,
1126
            verbose,
1127
            path.as_deref(),
1128
            lines.as_deref(),
1129
            json,
1130
        ),
1131
        Cmd::Diff { change, json } => workspace::diff(change.as_deref(), json),
1132
        Cmd::Describe {
1133
            message,
1134
            change,
1135
            json,
1136
        } => match message {
1137
            Some(m) => workspace::describe(&m, change.as_deref()),
1138
            None if change.is_some() => {
1139
                anyhow::bail!(
1140
                    "naming works on the working copy; to change a head's message pass -m"
1141
                )
1142
            }
1143
            None => workspace::describe_name(json),
1144
        },
1145
        Cmd::Amend { message, into } => workspace::amend(message.as_deref(), into.as_deref()),
1146
        Cmd::Split { change, paths } => workspace::split(&change, &paths),
1147
        Cmd::Abandon {
1148
            change,
1149
            revset,
1150
            yes,
1151
        } => workspace::abandon(change.as_deref(), revset.as_deref(), yes),
1152
        Cmd::Show {
1153
            change,
1154
            revset,
1155
            json,
1156
        } => workspace::show(change.as_deref(), revset.as_deref(), json),
1157
        Cmd::Checkout {
1158
            changes,
1159
            discard,
1160
            carry,
1161
        } => workspace::checkout(&changes, discard, carry),
1162
        Cmd::Resolve { json } => workspace::resolve(json),
1163
        Cmd::Undo => workspace::undo(),
1164
        Cmd::Gc { now, remote } => match remote {
1165
            Some(name) => workspace::gc_remote(&name, now),
1166
            None => workspace::gc(now),
1167
        },
1168
        Cmd::Fsck { json } => workspace::fsck(json),
1169
        Cmd::Doctor { offline, json } => workspace::doctor(offline, json),
1170
        Cmd::Checkpoint => workspace::checkpoint(),
1171
        Cmd::Op { cmd } => match cmd {
1172
            OpCmd::Log { limit, json } => workspace::op_log(limit, json),
1173
            OpCmd::Restore { op, discard } => workspace::op_restore(&op, discard),
1174
            OpCmd::Revert { op } => workspace::op_revert(&op),
1175
        },
1176
        Cmd::Clone {
1177
            url,
1178
            dest,
1179
            max_blob,
1180
            sparse,
1181
            depth,
1182
            recursive,
1183
        } => sync::clone(&url, dest, max_blob.as_deref(), &sparse, depth, recursive),
1184
        Cmd::Deepen { depth, remote } => sync::deepen(&remote, depth),
1185
        Cmd::Sparse { paths } => workspace::sparse(&paths),
1186
        Cmd::Lock { path } => workspace::lock(&path),
1187
        Cmd::Unlock { path } => workspace::unlock(&path),
1188
        Cmd::Workspace { cmd } => match cmd {
1189
            WorkspaceCmd::Add { path, changes } => workspace::workspace_add(&path, &changes),
1190
            WorkspaceCmd::List { json } => workspace::workspace_list(json),
1191
            WorkspaceCmd::Forget { workspace } => workspace::workspace_forget(&workspace),
1192
        },
1193
        Cmd::Bookmark { cmd } => match cmd {
1194
            BookmarkCmd::Create {
1195
                name,
1196
                changes,
1197
                upto,
1198
                only,
1199
            } => workspace::bookmark_create(&name, &changes, upto, only),
1200
            BookmarkCmd::Set {
1201
                name,
1202
                changes,
1203
                upto,
1204
                only,
1205
            } => workspace::bookmark_set(&name, &changes, upto, only),
1206
            BookmarkCmd::Delete { name } => workspace::bookmark_delete(&name),
1207
            BookmarkCmd::List { all, json } => workspace::bookmark_list(all, json),
1208
            BookmarkCmd::Switch {
1209
                name,
1210
                discard,
1211
                carry,
1212
            } => workspace::bookmark_switch(&name, discard, carry),
1213
        },
1214
        Cmd::Merge { bookmark } => workspace::merge(&bookmark),
1215
        Cmd::Auth { cmd } => match cmd {
1216
            AuthCmd::Login {
1217
                hub,
1218
                with_token,
1219
                ca,
1220
            } => auth::login(hub.as_deref(), with_token, ca),
1221
            AuthCmd::Status => auth::status(),
1222
            AuthCmd::Logout { hub } => auth::logout(hub.as_deref()),
1223
        },
1224
        Cmd::Submodule { cmd } => match cmd {
1225
            SubmoduleCmd::Add {
1226
                url,
1227
                path,
1228
                bookmark,
1229
            } => sync::submodule_add(&url, &path, bookmark.as_deref()),
1230
            SubmoduleCmd::Update { init, paths } => sync::submodule_update(init, &paths),
1231
            SubmoduleCmd::Status { json } => sync::submodule_status(json),
1232
            SubmoduleCmd::Remove { path } => sync::submodule_remove(&path),
1233
        },
1234
        Cmd::Receipt { cmd } => match cmd {
1235
            ReceiptCmd::List { rev, json } => receipt::list(rev.as_deref(), json),
1236
            ReceiptCmd::Show {
1237
                receipt: r,
1238
                rev,
1239
                json,
1240
            } => receipt::show(&r, rev.as_deref(), json),
1241
            ReceiptCmd::Export {
1242
                receipt: r,
1243
                file,
1244
                rev,
1245
            } => receipt::export(&r, &file, rev.as_deref()),
1246
            ReceiptCmd::Verify { file, json } => receipt::verify(&file, json),
1247
            ReceiptCmd::Import { file } => receipt::import(&file),
1248
        },
1249
        Cmd::Tag { cmd } => match cmd {
1250
            TagCmd::Create {
1251
                name,
1252
                changes,
1253
                message,
1254
            } => workspace::tag_create(&name, &changes, &message),
1255
            TagCmd::List { json } => workspace::tag_list(json),
1256
        },
1257
        Cmd::Remote { cmd } => match cmd {
1258
            RemoteCmd::Add {
1259
                name,
1260
                url,
1261
                profile,
1262
                region,
1263
                endpoint,
1264
                credentials,
1265
            } => sync::remote_add(
1266
                &name,
1267
                &url,
1268
                &[
1269
                    ("profile", profile),
1270
                    ("region", region),
1271
                    ("endpoint", endpoint),
1272
                    ("credentials", credentials),
1273
                ],
1274
            ),
1275
            RemoteCmd::Check { name } => sync::remote_check(&name),
1276
            RemoteCmd::Remove { name } => sync::remote_remove(&name),
1277
            RemoteCmd::List { json } => sync::remote_list(json),
1278
        },
1279
        Cmd::Fetch { remote, json } => sync::fetch(remote.as_deref(), json),
1280
        Cmd::Pull { remote, json } => sync::pull(remote.as_deref(), json),
1281
        Cmd::Push {
1282
            remote,
1283
            force,
1284
            bookmark,
1285
            delete,
1286
            json,
1287
        } => match delete {
1288
            Some(name) => sync::push_delete(remote.as_deref(), &name, json),
1289
            None => sync::push(remote.as_deref(), force, bookmark.as_deref(), json),
1290
        },
1291
        Cmd::Serve {
1292
            root,
1293
            bind,
1294
            tokens,
1295
            tls_cert,
1296
            tls_key,
1297
        } => sync::serve(root, &bind, tokens, tls_cert.zip(tls_key)),
1298
        Cmd::Identity { cmd } => match cmd {
1299
            IdentityCmd::Create {
1300
                name,
1301
                email,
1302
                local,
1303
                expires,
1304
            } => identity::create(name, email, local, expires),
1305
            IdentityCmd::Show => identity::show(),
1306
            IdentityCmd::Export { path } => identity::export(path),
1307
            IdentityCmd::SignChallenge { challenge, json } => {
1308
                identity::sign_challenge(&challenge, json)
1309
            }
1310
            IdentityCmd::Rotate => identity::rotate(),
1311
            IdentityCmd::Renew { expires } => identity::renew(expires),
1312
            IdentityCmd::Revoke { key, reason } => identity::revoke(key.as_deref(), &reason),
1313
            IdentityCmd::Delegate { cmd } => match cmd {
1314
                DelegateCmd::Add {
1315
                    identity_file,
1316
                    scope,
1317
                    expires,
1318
                } => identity::delegate_add(&identity_file, &scope, expires.as_deref()),
1319
                DelegateCmd::Remove { key } => identity::delegate_remove(&key),
1320
                DelegateCmd::List => identity::delegate_list(),
1321
            },
1322
        },
1323
        Cmd::Git {
1324
            cmd: GitCmd::Export { git_dir, branch },
1325
        } => sync::git_export(&git_dir, branch.as_deref()),
1326
        Cmd::Git {
1327
            cmd: GitCmd::Fetch { url, branch },
1328
        } => sync::git_fetch(&url, &branch),
1329
        Cmd::Git {
1330
            cmd: GitCmd::Push { url, branch },
1331
        } => sync::git_push(&url, &branch),
1332
        Cmd::Git {
1333
            cmd:
1334
                GitCmd::Import {
1335
                    git_dir,
1336
                    dest,
1337
                    branch,
1338
                },
1339
        } => sync::git_import(&git_dir, &dest, &branch),
1340
        Cmd::Git {
1341
            cmd:
1342
                GitCmd::Verify {
1343
                    git_dir,
1344
                    branch,
1345
                    last,
1346
                },
1347
        } => sync::git_verify(&git_dir, &branch, last),
1348
        Cmd::Policy { cmd } => match cmd {
1349
            PolicyCmd::Init => identity::policy_init(),
1350
            PolicyCmd::Show { json } => identity::policy_show(json),
1351
            PolicyCmd::Grant {
1352
                identity_file,
1353
                pattern,
1354
                out,
1355
            } => identity::policy_grant(&identity_file, pattern.as_deref(), out),
1356
            PolicyCmd::Revoke { key, out } => identity::policy_revoke(&key, out),
1357
            PolicyCmd::Root { cmd } => match cmd {
1358
                RootCmd::Add { identity_file, out } => {
1359
                    identity::policy_root_add(&identity_file, out)
1360
                }
1361
                RootCmd::Remove { key, out } => identity::policy_root_remove(&key, out),
1362
                RootCmd::Threshold { n, out } => identity::policy_threshold(n, out),
1363
            },
1364
            PolicyCmd::Sign { proposal, out } => identity::policy_sign(&proposal, out),
1365
        },
1366
        Cmd::Seal {
1367
            changes,
1368
            proposal: Some(url),
1369
            json,
1370
        } if changes.is_empty() => proposal::seal(&url, json),
1371
        Cmd::Seal { changes, json, .. } => identity::seal(&changes, json),
1372
        Cmd::Proposal { cmd } => match cmd {
1373
            ProposalCmd::Show { url, json } => proposal::show(&url, json),
1374
            ProposalCmd::Land { url, force } => proposal::land(&url, force),
1375
        },
1376
        Cmd::Verify {
1377
            change,
1378
            revset,
1379
            json,
1380
        } => identity::verify(change.as_deref(), revset.as_deref(), json),
1381
        Cmd::Config {
1382
            key,
1383
            value,
1384
            user,
1385
            unset,
1386
        } => identity::config(&key, value.as_deref().unwrap_or(""), user, unset),
1387
    }
1388
}
1389
1390
#[cfg(test)]
1391
mod help_tests {
1392
    use super::Cli;
1393
    use clap::CommandFactory;
1394
1395
    /// Every command and every argument explains itself, so `--help`,
1396
    /// completions and the man pages are complete.
1397
    #[test]
1398
    fn every_command_and_argument_has_help() {
1399
        fn walk(cmd: &clap::Command, path: &str, missing: &mut Vec<String>) {
1400
            let here = format!("{path} {}", cmd.get_name()).trim().to_string();
1401
            if cmd.get_about().is_none() && !path.is_empty() {
1402
                missing.push(format!("{here} (command)"));
1403
            }
1404
            for a in cmd.get_arguments() {
1405
                let id = a.get_id().as_str();
1406
                if id == "help" || id == "version" || (a.is_global_set() && !path.is_empty()) {
1407
                    continue;
1408
                }
1409
                if a.get_help().is_none() && a.get_long_help().is_none() {
1410
                    missing.push(format!("{here} {id}"));
1411
                }
1412
            }
1413
            for sub in cmd.get_subcommands() {
1414
                walk(sub, &here, missing);
1415
            }
1416
        }
1417
        let mut missing = Vec::new();
1418
        walk(&Cli::command(), "", &mut missing);
1419
        assert!(
1420
            missing.is_empty(),
1421
            "{} without help:\n{}",
1422
            missing.len(),
1423
            missing.join("\n")
1424
        );
1425
    }
1426
}