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