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