OXYGENHUB
# 7b9809e67c3d oxygen / src / cli / mod.rs
17.9 KiB · Rust · last changed Bookmarks: any number per repository, per-workspace current bookmark (bookmark create|set|delete|list|switch), oxy merge as union of heads, bookmark names resolve everywhere, push -b (spec 0.8 section 10.1) 1 month ago source blame history raw
fce44ae9 Andy McCall ↶ 1
//! Command-line definitions and dispatch.
2
3
mod format;
4
mod identity;
5
mod sync;
135212de Andy McCall ↶ 6
pub mod ui;
fce44ae9 Andy McCall ↶ 7
mod workspace;
8
9
use anyhow::{Context, Result};
10
use clap::{Parser, Subcommand};
11
use oxy_core::repo::Repo;
12
use std::path::PathBuf;
13
14
#[derive(Parser)]
15
#[command(name = "oxy", version, about = "Oxygen: breathing fresh air into version control")]
16
struct Cli {
135212de Andy McCall ↶ 17
    /// Plain output: no colour, no spinner (also implied by NO_COLOR or a non-terminal)
18
    #[arg(long, global = true)]
19
    plain: bool,
fce44ae9 Andy McCall ↶ 20
    #[command(subcommand)]
21
    cmd: Cmd,
22
}
23
24
#[derive(Subcommand)]
25
enum Cmd {
26
    /// Create a repository in PATH (default: current directory)
27
    Init { path: Option<PathBuf> },
28
    /// Clone a repository from a local path or an http(s) URL
b3c5a59a Andy McCall ↶ 29
    Clone {
30
        url: String,
31
        dest: Option<PathBuf>,
32
        /// Leave blobs larger than this (e.g. 1M) to be fetched on first read
33
        #[arg(long)]
34
        max_blob: Option<String>,
7421309f Andy McCall ↶ 35
        /// Materialize only these paths (repeatable); implies lazy blobs
36
        #[arg(long = "sparse")]
37
        sparse: Vec<String>,
2863456a Andy McCall ↶ 38
        /// Shallow clone: fetch only this many levels of history below the heads
39
        #[arg(long)]
40
        depth: Option<u32>,
b3c5a59a Andy McCall ↶ 41
    },
2863456a Andy McCall ↶ 42
    /// Fetch more history below a shallow clone's boundary
43
    Deepen {
44
        #[arg(default_value_t = 10)]
45
        depth: u32,
46
        #[arg(long, default_value = "origin")]
47
        remote: String,
48
    },
7421309f Andy McCall ↶ 49
    /// Show or set the sparse filter (paths to materialize); re-checks out the bookmark
50
    Sparse { paths: Vec<String> },
fce44ae9 Andy McCall ↶ 51
    /// Show the working copy: changed paths and conflicts
50a046e4 Andy McCall ↶ 52
    Status {
53
        /// Machine-readable output: paths, and every conflict with its kind and contributing changes
54
        #[arg(long)]
55
        json: bool,
56
    },
135212de Andy McCall ↶ 57
    /// One-line summary for shell prompts; fast and records nothing
58
    Prompt {
59
        /// Placeholders: %b bookmark, %c change id, %h heads, %d dirty (*), %x conflict (!), %A/%D ahead/behind (+n/-n), %a/%B raw counts
60
        #[arg(long, default_value = oxy_core::repo::DEFAULT_PROMPT_FORMAT)]
61
        format: String,
62
    },
fce44ae9 Andy McCall ↶ 63
    /// Record the working copy as a change on the current bookmark
64
    Commit {
65
        #[arg(short, long)]
66
        message: String,
67
        /// Extra explicit dependencies (ChangeId prefix, #hash prefix)
68
        #[arg(long = "depends-on")]
69
        depends_on: Vec<String>,
70
    },
71
    /// List changes reachable from the bookmark and the working copy
94084087 Andy McCall ↶ 72
    Log {
73
        /// Show at most this many changes
74
        #[arg(short = 'n', long)]
75
        limit: Option<usize>,
76
        /// Show full messages, dependencies, and signatures
77
        #[arg(short, long)]
78
        verbose: bool,
79
    },
fce44ae9 Andy McCall ↶ 80
    /// Show what the working copy changes against its base
81
    Diff,
614541c3 Andy McCall ↶ 82
    /// Change the message of a head (default: the head the working copy sits on)
83
    Describe {
84
        #[arg(short, long)]
85
        message: String,
86
        change: Option<String>,
87
    },
23b217d7 Andy McCall ↶ 88
    /// Fold the working copy into the head it sits on, or into CHANGE with --into
614541c3 Andy McCall ↶ 89
    Amend {
90
        #[arg(short, long)]
91
        message: Option<String>,
23b217d7 Andy McCall ↶ 92
        /// Amend an older change; the edits must touch only lines it already knows
93
        #[arg(long)]
94
        into: Option<String>,
614541c3 Andy McCall ↶ 95
    },
23b217d7 Andy McCall ↶ 96
    /// Split a head: the named paths become a separate first change
97
    Split {
98
        change: String,
99
        paths: Vec<String>,
100
    },
614541c3 Andy McCall ↶ 101
    /// Drop a head from the bookmark; its content stays on disk as uncommitted work
102
    Abandon { change: String },
fce44ae9 Andy McCall ↶ 103
    /// Show one change (ChangeId prefix, #hash prefix, or @)
104
    Show { change: String },
105
    /// Move the working copy to the state formed by CHANGE... (default: the bookmark heads)
106
    Checkout {
107
        changes: Vec<String>,
108
        /// Throw away uncommitted working-copy changes
109
        #[arg(long)]
110
        discard: bool,
111
    },
112
    /// Report which conflicts of the working copy's base are resolved by the current files
113
    Resolve,
114
    /// Undo the last operation
115
    Undo,
252e8cb2 Andy McCall ↶ 116
    /// Write a checkpoint of the current bookmark's state now
117
    Checkpoint,
ba160f60 Andy McCall ↶ 118
    /// Pack loose objects and prune unreachable ones older than the grace period
119
    Gc {
120
        /// Ignore the grace period (core.gc-grace) and prune immediately
121
        #[arg(long)]
122
        now: bool,
123
    },
fce44ae9 Andy McCall ↶ 124
    /// List operations
125
    #[command(name = "op")]
126
    Op {
127
        #[command(subcommand)]
128
        cmd: OpCmd,
129
    },
cdb5beba Andy McCall ↶ 130
    /// Take the advisory local lock on a lockable file, making it writable
131
    Lock { path: String },
132
    /// Release the local lock and make the file read-only again
133
    Unlock { path: String },
4e88e774 Andy McCall ↶ 134
    /// Workspaces: extra checkout directories sharing this repository
135
    Workspace {
136
        #[command(subcommand)]
137
        cmd: WorkspaceCmd,
138
    },
7b9809e6 Andy McCall ↶ 139
    /// Bookmarks: named states; each workspace works on one
140
    Bookmark {
141
        #[command(subcommand)]
142
        cmd: BookmarkCmd,
143
    },
144
    /// Union another bookmark's heads into the current one (no merge change)
145
    Merge { bookmark: String },
132b884b Andy McCall ↶ 146
    /// Tags: immutable named states
147
    Tag {
148
        #[command(subcommand)]
149
        cmd: TagCmd,
150
    },
fce44ae9 Andy McCall ↶ 151
    /// Manage remotes
152
    Remote {
153
        #[command(subcommand)]
154
        cmd: RemoteCmd,
155
    },
156
    /// Fetch every bookmark of a remote into its tracking references
157
    Fetch { remote: Option<String> },
158
    /// Fetch, then union the remote bookmark into the local one (no merge change)
159
    Pull { remote: Option<String> },
160
    /// Push the current bookmark to a remote
161
    Push {
162
        remote: Option<String>,
163
        /// Allow dropping heads the remote has
164
        #[arg(long)]
165
        force: bool,
7b9809e6 Andy McCall ↶ 166
        /// Push this bookmark instead of the workspace's current one
167
        #[arg(short = 'b', long)]
168
        bookmark: Option<String>,
fce44ae9 Andy McCall ↶ 169
    },
2b5ee371 Andy McCall ↶ 170
    /// Serve repositories under ROOT over HTTPS (with --tls-cert/--tls-key) or plain HTTP
fce44ae9 Andy McCall ↶ 171
    Serve {
172
        /// Directory with one repository per subdirectory
173
        #[arg(long, default_value = ".")]
174
        root: PathBuf,
175
        #[arg(long, default_value = "127.0.0.1:8890")]
176
        bind: String,
177
        /// Bearer token allowed to push (repeatable); none means open
178
        #[arg(long = "token")]
179
        tokens: Vec<String>,
2b5ee371 Andy McCall ↶ 180
        /// PEM certificate chain; enables HTTPS together with --tls-key
181
        #[arg(long, requires = "tls_key")]
182
        tls_cert: Option<PathBuf>,
183
        /// PEM private key
184
        #[arg(long, requires = "tls_cert")]
185
        tls_key: Option<PathBuf>,
fce44ae9 Andy McCall ↶ 186
    },
187
    /// Manage the signing identity
188
    Identity {
189
        #[command(subcommand)]
190
        cmd: IdentityCmd,
191
    },
192
    /// Verify the signatures on a change (default: every change in the log)
193
    Verify { change: Option<String> },
79ab8528 Andy McCall ↶ 194
    /// Git interoperability
195
    Git {
196
        #[command(subcommand)]
197
        cmd: GitCmd,
198
    },
83e0677b Andy McCall ↶ 199
    /// Manage the trust policy (who may update which bookmarks)
200
    Policy {
201
        #[command(subcommand)]
202
        cmd: PolicyCmd,
203
    },
7421309f Andy McCall ↶ 204
    /// 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)
fce44ae9 Andy McCall ↶ 205
    Config {
206
        key: String,
207
        value: String,
208
        /// Write to the user file (~/.config/oxy/config.toml) instead of this repository
209
        #[arg(long)]
210
        user: bool,
211
    },
212
}
213
214
#[derive(Subcommand)]
215
enum OpCmd {
216
    Log,
217
}
218
219
#[derive(Subcommand)]
4e88e774 Andy McCall ↶ 220
enum WorkspaceCmd {
221
    /// Create a workspace at PATH checked out on CHANGE... (default: the bookmark heads)
222
    Add { path: PathBuf, changes: Vec<String> },
223
    List,
224
    /// Stop tracking a workspace (by id prefix or path); its files stay
225
    Forget { workspace: String },
226
}
227
228
#[derive(Subcommand)]
7b9809e6 Andy McCall ↶ 229
enum BookmarkCmd {
230
    /// Create a bookmark at CHANGE... (default: the working copy's base)
231
    Create { name: String, changes: Vec<String> },
232
    /// Point an existing bookmark at CHANGE...
233
    Set { name: String, changes: Vec<String> },
234
    /// Remove a bookmark; its changes stay
235
    Delete { name: String },
236
    /// List bookmarks with their heads and difference from origin
237
    List,
238
    /// Make NAME this workspace's bookmark and check it out
239
    Switch {
240
        name: String,
241
        /// Throw away uncommitted working-copy changes
242
        #[arg(long)]
243
        discard: bool,
244
    },
245
}
246
247
#[derive(Subcommand)]
132b884b Andy McCall ↶ 248
enum TagCmd {
249
    /// Tag the state of CHANGE... (default: the bookmark heads)
250
    Create {
251
        name: String,
252
        changes: Vec<String>,
253
        #[arg(short, long, default_value = "")]
254
        message: String,
255
    },
256
    List,
257
}
258
259
#[derive(Subcommand)]
fce44ae9 Andy McCall ↶ 260
enum RemoteCmd {
261
    Add { name: String, url: String },
262
    Remove { name: String },
263
    List,
264
}
265
266
#[derive(Subcommand)]
267
enum IdentityCmd {
268
    /// Generate a composite Ed25519 + ML-DSA-65 key pair and make it your author identity
269
    Create {
270
        #[arg(long)]
271
        name: Option<String>,
272
        #[arg(long)]
273
        email: Option<String>,
274
        /// Record the author in this repository only, not in the user config
275
        #[arg(long)]
276
        local: bool,
28fc23d7 Andy McCall ↶ 277
        /// Expire the identity this many days from now
278
        #[arg(long)]
279
        expires: Option<u64>,
fce44ae9 Andy McCall ↶ 280
    },
281
    /// Show the configured identity
282
    Show,
83e0677b Andy McCall ↶ 283
    /// Write your identity object to a file, to hand to a policy holder
284
    Export { path: Option<PathBuf> },
28fc23d7 Andy McCall ↶ 285
    /// Move to a fresh key pair; the policy follows if it names your key
286
    Rotate,
287
    /// Re-issue your identity with a new expiry (in days from now; 0 = never)
288
    Renew {
289
        #[arg(long)]
290
        expires: u64,
291
    },
292
    /// Revoke a key: yours by default, or any key if you hold a policy root
293
    Revoke {
294
        /// KeyId prefix (default: your own key)
295
        key: Option<String>,
296
        #[arg(long, default_value = "")]
297
        reason: String,
298
    },
299
    /// Keys allowed to sign or seal on your behalf
300
    Delegate {
301
        #[command(subcommand)]
302
        cmd: DelegateCmd,
303
    },
304
}
305
306
#[derive(Subcommand)]
307
enum DelegateCmd {
308
    /// Add a delegate from an exported identity file
309
    Add { identity_file: PathBuf },
310
    /// Remove a delegate by KeyId prefix
311
    Remove { key: String },
312
    /// List delegates
313
    List,
fce44ae9 Andy McCall ↶ 314
}
315
83e0677b Andy McCall ↶ 316
#[derive(Subcommand)]
79ab8528 Andy McCall ↶ 317
enum GitCmd {
318
    /// Export the current bookmark to a Git repository (created bare if absent)
30ea9c1e Andy McCall ↶ 319
    Export {
320
        git_dir: PathBuf,
321
        /// Git branch to write (default: the bookmark name)
322
        #[arg(long)]
323
        branch: Option<String>,
324
    },
325
    /// Fetch a branch from a Git remote (path or URL) into the current bookmark
326
    Fetch {
327
        url: String,
328
        #[arg(long, default_value = "main")]
329
        branch: String,
330
    },
331
    /// Push the current bookmark to a branch of a Git remote (fetch first)
332
    Push {
333
        url: String,
334
        #[arg(long, default_value = "main")]
335
        branch: String,
336
    },
c3069537 Andy McCall ↶ 337
    /// Import a Git branch into a new Oxygen repository at DEST
338
    Import {
339
        git_dir: PathBuf,
340
        dest: PathBuf,
341
        #[arg(long, default_value = "main")]
342
        branch: String,
343
    },
79ab8528 Andy McCall ↶ 344
}
345
346
#[derive(Subcommand)]
83e0677b Andy McCall ↶ 347
enum PolicyCmd {
348
    /// Create the initial policy with your identity as the only root
349
    Init,
350
    /// Show the current policy
351
    Show,
352
    /// Allow an identity (from `oxy identity export`) to update bookmarks
353
    Grant {
354
        identity_file: PathBuf,
355
        /// Reference pattern the grant applies to (default: every bookmark)
356
        #[arg(long)]
357
        pattern: Option<String>,
28fc23d7 Andy McCall ↶ 358
        /// Where to write the proposal when more root signatures are needed
359
        #[arg(long)]
360
        out: Option<PathBuf>,
83e0677b Andy McCall ↶ 361
    },
362
    /// Revoke a key (KeyId prefix of an identity in the policy)
28fc23d7 Andy McCall ↶ 363
    Revoke {
364
        key: String,
365
        /// Where to write the proposal when more root signatures are needed
366
        #[arg(long)]
367
        out: Option<PathBuf>,
368
    },
369
    /// Manage root keys and the signature threshold for policy updates
370
    Root {
371
        #[command(subcommand)]
372
        cmd: RootCmd,
373
    },
374
    /// Add your root signature to a proposal file; applies it once the threshold is met
375
    Sign {
376
        proposal: PathBuf,
377
        /// Where to write the proposal if it still needs signatures (default: in place)
378
        #[arg(long)]
379
        out: Option<PathBuf>,
380
    },
381
}
382
383
#[derive(Subcommand)]
384
enum RootCmd {
385
    /// Make an exported identity a root key
386
    Add {
387
        identity_file: PathBuf,
388
        #[arg(long)]
389
        out: Option<PathBuf>,
390
    },
391
    /// Remove a root key by KeyId prefix
392
    Remove {
393
        key: String,
394
        #[arg(long)]
395
        out: Option<PathBuf>,
396
    },
397
    /// Require this many root signatures on policy updates
398
    Threshold {
399
        n: u32,
400
        #[arg(long)]
401
        out: Option<PathBuf>,
402
    },
83e0677b Andy McCall ↶ 403
}
404
fce44ae9 Andy McCall ↶ 405
/// Open the repository containing the current directory.
406
fn open() -> Result<Repo> {
407
    Repo::open(&std::env::current_dir()?).context("opening repository")
408
}
409
410
/// Parse arguments and run; returns the process exit code.
411
pub fn run() -> Result<i32> {
412
    let cli = Cli::parse();
135212de Andy McCall ↶ 413
    ui::init(cli.plain);
fce44ae9 Andy McCall ↶ 414
    match cli.cmd {
415
        Cmd::Init { path } => workspace::init(path),
50a046e4 Andy McCall ↶ 416
        Cmd::Status { json } => workspace::status(json),
135212de Andy McCall ↶ 417
        Cmd::Prompt { format } => workspace::prompt(&format),
fce44ae9 Andy McCall ↶ 418
        Cmd::Commit { message, depends_on } => workspace::commit(&message, &depends_on),
94084087 Andy McCall ↶ 419
        Cmd::Log { limit, verbose } => workspace::log(limit, verbose),
fce44ae9 Andy McCall ↶ 420
        Cmd::Diff => workspace::diff(),
614541c3 Andy McCall ↶ 421
        Cmd::Describe { message, change } => workspace::describe(&message, change.as_deref()),
23b217d7 Andy McCall ↶ 422
        Cmd::Amend { message, into } => workspace::amend(message.as_deref(), into.as_deref()),
423
        Cmd::Split { change, paths } => workspace::split(&change, &paths),
614541c3 Andy McCall ↶ 424
        Cmd::Abandon { change } => workspace::abandon(&change),
fce44ae9 Andy McCall ↶ 425
        Cmd::Show { change } => workspace::show(&change),
426
        Cmd::Checkout { changes, discard } => workspace::checkout(&changes, discard),
427
        Cmd::Resolve => workspace::resolve(),
428
        Cmd::Undo => workspace::undo(),
ba160f60 Andy McCall ↶ 429
        Cmd::Gc { now } => workspace::gc(now),
252e8cb2 Andy McCall ↶ 430
        Cmd::Checkpoint => workspace::checkpoint(),
fce44ae9 Andy McCall ↶ 431
        Cmd::Op { cmd: OpCmd::Log } => workspace::op_log(),
2863456a Andy McCall ↶ 432
        Cmd::Clone { url, dest, max_blob, sparse, depth } => sync::clone(&url, dest, max_blob.as_deref(), &sparse, depth),
433
        Cmd::Deepen { depth, remote } => sync::deepen(&remote, depth),
7421309f Andy McCall ↶ 434
        Cmd::Sparse { paths } => workspace::sparse(&paths),
cdb5beba Andy McCall ↶ 435
        Cmd::Lock { path } => workspace::lock(&path),
436
        Cmd::Unlock { path } => workspace::unlock(&path),
4e88e774 Andy McCall ↶ 437
        Cmd::Workspace { cmd } => match cmd {
438
            WorkspaceCmd::Add { path, changes } => workspace::workspace_add(&path, &changes),
439
            WorkspaceCmd::List => workspace::workspace_list(),
440
            WorkspaceCmd::Forget { workspace } => workspace::workspace_forget(&workspace),
441
        },
7b9809e6 Andy McCall ↶ 442
        Cmd::Bookmark { cmd } => match cmd {
443
            BookmarkCmd::Create { name, changes } => workspace::bookmark_create(&name, &changes),
444
            BookmarkCmd::Set { name, changes } => workspace::bookmark_set(&name, &changes),
445
            BookmarkCmd::Delete { name } => workspace::bookmark_delete(&name),
446
            BookmarkCmd::List => workspace::bookmark_list(),
447
            BookmarkCmd::Switch { name, discard } => workspace::bookmark_switch(&name, discard),
448
        },
449
        Cmd::Merge { bookmark } => workspace::merge(&bookmark),
132b884b Andy McCall ↶ 450
        Cmd::Tag { cmd } => match cmd {
451
            TagCmd::Create { name, changes, message } => workspace::tag_create(&name, &changes, &message),
452
            TagCmd::List => workspace::tag_list(),
453
        },
fce44ae9 Andy McCall ↶ 454
        Cmd::Remote { cmd } => match cmd {
455
            RemoteCmd::Add { name, url } => sync::remote_add(&name, &url),
456
            RemoteCmd::Remove { name } => sync::remote_remove(&name),
457
            RemoteCmd::List => sync::remote_list(),
458
        },
459
        Cmd::Fetch { remote } => sync::fetch(remote.as_deref()),
460
        Cmd::Pull { remote } => sync::pull(remote.as_deref()),
7b9809e6 Andy McCall ↶ 461
        Cmd::Push { remote, force, bookmark } => sync::push(remote.as_deref(), force, bookmark.as_deref()),
2b5ee371 Andy McCall ↶ 462
        Cmd::Serve { root, bind, tokens, tls_cert, tls_key } => sync::serve(root, &bind, tokens, tls_cert.zip(tls_key)),
fce44ae9 Andy McCall ↶ 463
        Cmd::Identity { cmd } => match cmd {
28fc23d7 Andy McCall ↶ 464
            IdentityCmd::Create { name, email, local, expires } => identity::create(name, email, local, expires),
fce44ae9 Andy McCall ↶ 465
            IdentityCmd::Show => identity::show(),
83e0677b Andy McCall ↶ 466
            IdentityCmd::Export { path } => identity::export(path),
28fc23d7 Andy McCall ↶ 467
            IdentityCmd::Rotate => identity::rotate(),
468
            IdentityCmd::Renew { expires } => identity::renew(expires),
469
            IdentityCmd::Revoke { key, reason } => identity::revoke(key.as_deref(), &reason),
470
            IdentityCmd::Delegate { cmd } => match cmd {
471
                DelegateCmd::Add { identity_file } => identity::delegate_add(&identity_file),
472
                DelegateCmd::Remove { key } => identity::delegate_remove(&key),
473
                DelegateCmd::List => identity::delegate_list(),
474
            },
83e0677b Andy McCall ↶ 475
        },
30ea9c1e Andy McCall ↶ 476
        Cmd::Git { cmd: GitCmd::Export { git_dir, branch } } => sync::git_export(&git_dir, branch.as_deref()),
477
        Cmd::Git { cmd: GitCmd::Fetch { url, branch } } => sync::git_fetch(&url, &branch),
478
        Cmd::Git { cmd: GitCmd::Push { url, branch } } => sync::git_push(&url, &branch),
c3069537 Andy McCall ↶ 479
        Cmd::Git { cmd: GitCmd::Import { git_dir, dest, branch } } => sync::git_import(&git_dir, &dest, &branch),
83e0677b Andy McCall ↶ 480
        Cmd::Policy { cmd } => match cmd {
481
            PolicyCmd::Init => identity::policy_init(),
482
            PolicyCmd::Show => identity::policy_show(),
28fc23d7 Andy McCall ↶ 483
            PolicyCmd::Grant { identity_file, pattern, out } => identity::policy_grant(&identity_file, pattern.as_deref(), out),
484
            PolicyCmd::Revoke { key, out } => identity::policy_revoke(&key, out),
485
            PolicyCmd::Root { cmd } => match cmd {
486
                RootCmd::Add { identity_file, out } => identity::policy_root_add(&identity_file, out),
487
                RootCmd::Remove { key, out } => identity::policy_root_remove(&key, out),
488
                RootCmd::Threshold { n, out } => identity::policy_threshold(n, out),
489
            },
490
            PolicyCmd::Sign { proposal, out } => identity::policy_sign(&proposal, out),
fce44ae9 Andy McCall ↶ 491
        },
492
        Cmd::Verify { change } => identity::verify(change.as_deref()),
493
        Cmd::Config { key, value, user } => identity::config(&key, &value, user),
494
    }
495
}