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