OXYGENHUB
# 86fe29fd62ea oxygen / src / cli / mod.rs
16.3 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
1
//! Command-line definitions and dispatch.
2
3
mod format;
4
mod identity;
5
mod sync;
6
pub mod ui;
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 {
17
    /// Plain output: no colour, no spinner (also implied by NO_COLOR or a non-terminal)
18
    #[arg(long, global = true)]
19
    plain: bool,
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
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>,
35
        /// Materialize only these paths (repeatable); implies lazy blobs
36
        #[arg(long = "sparse")]
37
        sparse: Vec<String>,
38
        /// Shallow clone: fetch only this many levels of history below the heads
39
        #[arg(long)]
40
        depth: Option<u32>,
41
    },
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
    },
49
    /// Show or set the sparse filter (paths to materialize); re-checks out the bookmark
50
    Sparse { paths: Vec<String> },
51
    /// Show the working copy: changed paths and conflicts
52
    Status {
53
        /// Machine-readable output: paths, and every conflict with its kind and contributing changes
54
        #[arg(long)]
55
        json: bool,
56
    },
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
    },
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
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
    },
80
    /// Show what the working copy changes against its base
81
    Diff,
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
    },
88
    /// Fold the working copy into the head it sits on, or into CHANGE with --into
89
    Amend {
90
        #[arg(short, long)]
91
        message: Option<String>,
92
        /// Amend an older change; the edits must touch only lines it already knows
93
        #[arg(long)]
94
        into: Option<String>,
95
    },
96
    /// Split a head: the named paths become a separate first change
97
    Split {
98
        change: String,
99
        paths: Vec<String>,
100
    },
101
    /// Drop a head from the bookmark; its content stays on disk as uncommitted work
102
    Abandon { change: String },
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,
116
    /// Write a checkpoint of the current bookmark's state now
117
    Checkpoint,
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
    },
124
    /// List operations
125
    #[command(name = "op")]
126
    Op {
127
        #[command(subcommand)]
128
        cmd: OpCmd,
129
    },
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 },
134
    /// Workspaces: extra checkout directories sharing this repository
135
    Workspace {
136
        #[command(subcommand)]
137
        cmd: WorkspaceCmd,
138
    },
139
    /// Tags: immutable named states
140
    Tag {
141
        #[command(subcommand)]
142
        cmd: TagCmd,
143
    },
144
    /// Manage remotes
145
    Remote {
146
        #[command(subcommand)]
147
        cmd: RemoteCmd,
148
    },
149
    /// Fetch every bookmark of a remote into its tracking references
150
    Fetch { remote: Option<String> },
151
    /// Fetch, then union the remote bookmark into the local one (no merge change)
152
    Pull { remote: Option<String> },
153
    /// Push the current bookmark to a remote
154
    Push {
155
        remote: Option<String>,
156
        /// Allow dropping heads the remote has
157
        #[arg(long)]
158
        force: bool,
159
    },
160
    /// Serve repositories under ROOT over HTTPS (with --tls-cert/--tls-key) or plain HTTP
161
    Serve {
162
        /// Directory with one repository per subdirectory
163
        #[arg(long, default_value = ".")]
164
        root: PathBuf,
165
        #[arg(long, default_value = "127.0.0.1:8890")]
166
        bind: String,
167
        /// Bearer token allowed to push (repeatable); none means open
168
        #[arg(long = "token")]
169
        tokens: Vec<String>,
170
        /// PEM certificate chain; enables HTTPS together with --tls-key
171
        #[arg(long, requires = "tls_key")]
172
        tls_cert: Option<PathBuf>,
173
        /// PEM private key
174
        #[arg(long, requires = "tls_cert")]
175
        tls_key: Option<PathBuf>,
176
    },
177
    /// Manage the signing identity
178
    Identity {
179
        #[command(subcommand)]
180
        cmd: IdentityCmd,
181
    },
182
    /// Verify the signatures on a change (default: every change in the log)
183
    Verify { change: Option<String> },
184
    /// Git interoperability
185
    Git {
186
        #[command(subcommand)]
187
        cmd: GitCmd,
188
    },
189
    /// Manage the trust policy (who may update which bookmarks)
190
    Policy {
191
        #[command(subcommand)]
192
        cmd: PolicyCmd,
193
    },
194
    /// 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)
195
    Config {
196
        key: String,
197
        value: String,
198
        /// Write to the user file (~/.config/oxy/config.toml) instead of this repository
199
        #[arg(long)]
200
        user: bool,
201
    },
202
}
203
204
#[derive(Subcommand)]
205
enum OpCmd {
206
    Log,
207
}
208
209
#[derive(Subcommand)]
210
enum WorkspaceCmd {
211
    /// Create a workspace at PATH checked out on CHANGE... (default: the bookmark heads)
212
    Add { path: PathBuf, changes: Vec<String> },
213
    List,
214
    /// Stop tracking a workspace (by id prefix or path); its files stay
215
    Forget { workspace: String },
216
}
217
218
#[derive(Subcommand)]
219
enum TagCmd {
220
    /// Tag the state of CHANGE... (default: the bookmark heads)
221
    Create {
222
        name: String,
223
        changes: Vec<String>,
224
        #[arg(short, long, default_value = "")]
225
        message: String,
226
    },
227
    List,
228
}
229
230
#[derive(Subcommand)]
231
enum RemoteCmd {
232
    Add { name: String, url: String },
233
    Remove { name: String },
234
    List,
235
}
236
237
#[derive(Subcommand)]
238
enum IdentityCmd {
239
    /// Generate a composite Ed25519 + ML-DSA-65 key pair and make it your author identity
240
    Create {
241
        #[arg(long)]
242
        name: Option<String>,
243
        #[arg(long)]
244
        email: Option<String>,
245
        /// Record the author in this repository only, not in the user config
246
        #[arg(long)]
247
        local: bool,
248
        /// Expire the identity this many days from now
249
        #[arg(long)]
250
        expires: Option<u64>,
251
    },
252
    /// Show the configured identity
253
    Show,
254
    /// Write your identity object to a file, to hand to a policy holder
255
    Export { path: Option<PathBuf> },
256
    /// Move to a fresh key pair; the policy follows if it names your key
257
    Rotate,
258
    /// Re-issue your identity with a new expiry (in days from now; 0 = never)
259
    Renew {
260
        #[arg(long)]
261
        expires: u64,
262
    },
263
    /// Revoke a key: yours by default, or any key if you hold a policy root
264
    Revoke {
265
        /// KeyId prefix (default: your own key)
266
        key: Option<String>,
267
        #[arg(long, default_value = "")]
268
        reason: String,
269
    },
270
    /// Keys allowed to sign or seal on your behalf
271
    Delegate {
272
        #[command(subcommand)]
273
        cmd: DelegateCmd,
274
    },
275
}
276
277
#[derive(Subcommand)]
278
enum DelegateCmd {
279
    /// Add a delegate from an exported identity file
280
    Add { identity_file: PathBuf },
281
    /// Remove a delegate by KeyId prefix
282
    Remove { key: String },
283
    /// List delegates
284
    List,
285
}
286
287
#[derive(Subcommand)]
288
enum GitCmd {
289
    /// Export the current bookmark to a Git repository (created bare if absent)
290
    Export {
291
        git_dir: PathBuf,
292
        /// Git branch to write (default: the bookmark name)
293
        #[arg(long)]
294
        branch: Option<String>,
295
    },
296
    /// Fetch a branch from a Git remote (path or URL) into the current bookmark
297
    Fetch {
298
        url: String,
299
        #[arg(long, default_value = "main")]
300
        branch: String,
301
    },
302
    /// Push the current bookmark to a branch of a Git remote (fetch first)
303
    Push {
304
        url: String,
305
        #[arg(long, default_value = "main")]
306
        branch: String,
307
    },
308
    /// Import a Git branch into a new Oxygen repository at DEST
309
    Import {
310
        git_dir: PathBuf,
311
        dest: PathBuf,
312
        #[arg(long, default_value = "main")]
313
        branch: String,
314
    },
315
}
316
317
#[derive(Subcommand)]
318
enum PolicyCmd {
319
    /// Create the initial policy with your identity as the only root
320
    Init,
321
    /// Show the current policy
322
    Show,
323
    /// Allow an identity (from `oxy identity export`) to update bookmarks
324
    Grant {
325
        identity_file: PathBuf,
326
        /// Reference pattern the grant applies to (default: every bookmark)
327
        #[arg(long)]
328
        pattern: Option<String>,
329
        /// Where to write the proposal when more root signatures are needed
330
        #[arg(long)]
331
        out: Option<PathBuf>,
332
    },
333
    /// Revoke a key (KeyId prefix of an identity in the policy)
334
    Revoke {
335
        key: String,
336
        /// Where to write the proposal when more root signatures are needed
337
        #[arg(long)]
338
        out: Option<PathBuf>,
339
    },
340
    /// Manage root keys and the signature threshold for policy updates
341
    Root {
342
        #[command(subcommand)]
343
        cmd: RootCmd,
344
    },
345
    /// Add your root signature to a proposal file; applies it once the threshold is met
346
    Sign {
347
        proposal: PathBuf,
348
        /// Where to write the proposal if it still needs signatures (default: in place)
349
        #[arg(long)]
350
        out: Option<PathBuf>,
351
    },
352
}
353
354
#[derive(Subcommand)]
355
enum RootCmd {
356
    /// Make an exported identity a root key
357
    Add {
358
        identity_file: PathBuf,
359
        #[arg(long)]
360
        out: Option<PathBuf>,
361
    },
362
    /// Remove a root key by KeyId prefix
363
    Remove {
364
        key: String,
365
        #[arg(long)]
366
        out: Option<PathBuf>,
367
    },
368
    /// Require this many root signatures on policy updates
369
    Threshold {
370
        n: u32,
371
        #[arg(long)]
372
        out: Option<PathBuf>,
373
    },
374
}
375
376
/// Open the repository containing the current directory.
377
fn open() -> Result<Repo> {
378
    Repo::open(&std::env::current_dir()?).context("opening repository")
379
}
380
381
/// Parse arguments and run; returns the process exit code.
382
pub fn run() -> Result<i32> {
383
    let cli = Cli::parse();
384
    ui::init(cli.plain);
385
    match cli.cmd {
386
        Cmd::Init { path } => workspace::init(path),
387
        Cmd::Status { json } => workspace::status(json),
388
        Cmd::Prompt { format } => workspace::prompt(&format),
389
        Cmd::Commit { message, depends_on } => workspace::commit(&message, &depends_on),
390
        Cmd::Log { limit, verbose } => workspace::log(limit, verbose),
391
        Cmd::Diff => workspace::diff(),
392
        Cmd::Describe { message, change } => workspace::describe(&message, change.as_deref()),
393
        Cmd::Amend { message, into } => workspace::amend(message.as_deref(), into.as_deref()),
394
        Cmd::Split { change, paths } => workspace::split(&change, &paths),
395
        Cmd::Abandon { change } => workspace::abandon(&change),
396
        Cmd::Show { change } => workspace::show(&change),
397
        Cmd::Checkout { changes, discard } => workspace::checkout(&changes, discard),
398
        Cmd::Resolve => workspace::resolve(),
399
        Cmd::Undo => workspace::undo(),
400
        Cmd::Gc { now } => workspace::gc(now),
401
        Cmd::Checkpoint => workspace::checkpoint(),
402
        Cmd::Op { cmd: OpCmd::Log } => workspace::op_log(),
403
        Cmd::Clone { url, dest, max_blob, sparse, depth } => sync::clone(&url, dest, max_blob.as_deref(), &sparse, depth),
404
        Cmd::Deepen { depth, remote } => sync::deepen(&remote, depth),
405
        Cmd::Sparse { paths } => workspace::sparse(&paths),
406
        Cmd::Lock { path } => workspace::lock(&path),
407
        Cmd::Unlock { path } => workspace::unlock(&path),
408
        Cmd::Workspace { cmd } => match cmd {
409
            WorkspaceCmd::Add { path, changes } => workspace::workspace_add(&path, &changes),
410
            WorkspaceCmd::List => workspace::workspace_list(),
411
            WorkspaceCmd::Forget { workspace } => workspace::workspace_forget(&workspace),
412
        },
413
        Cmd::Tag { cmd } => match cmd {
414
            TagCmd::Create { name, changes, message } => workspace::tag_create(&name, &changes, &message),
415
            TagCmd::List => workspace::tag_list(),
416
        },
417
        Cmd::Remote { cmd } => match cmd {
418
            RemoteCmd::Add { name, url } => sync::remote_add(&name, &url),
419
            RemoteCmd::Remove { name } => sync::remote_remove(&name),
420
            RemoteCmd::List => sync::remote_list(),
421
        },
422
        Cmd::Fetch { remote } => sync::fetch(remote.as_deref()),
423
        Cmd::Pull { remote } => sync::pull(remote.as_deref()),
424
        Cmd::Push { remote, force } => sync::push(remote.as_deref(), force),
425
        Cmd::Serve { root, bind, tokens, tls_cert, tls_key } => sync::serve(root, &bind, tokens, tls_cert.zip(tls_key)),
426
        Cmd::Identity { cmd } => match cmd {
427
            IdentityCmd::Create { name, email, local, expires } => identity::create(name, email, local, expires),
428
            IdentityCmd::Show => identity::show(),
429
            IdentityCmd::Export { path } => identity::export(path),
430
            IdentityCmd::Rotate => identity::rotate(),
431
            IdentityCmd::Renew { expires } => identity::renew(expires),
432
            IdentityCmd::Revoke { key, reason } => identity::revoke(key.as_deref(), &reason),
433
            IdentityCmd::Delegate { cmd } => match cmd {
434
                DelegateCmd::Add { identity_file } => identity::delegate_add(&identity_file),
435
                DelegateCmd::Remove { key } => identity::delegate_remove(&key),
436
                DelegateCmd::List => identity::delegate_list(),
437
            },
438
        },
439
        Cmd::Git { cmd: GitCmd::Export { git_dir, branch } } => sync::git_export(&git_dir, branch.as_deref()),
440
        Cmd::Git { cmd: GitCmd::Fetch { url, branch } } => sync::git_fetch(&url, &branch),
441
        Cmd::Git { cmd: GitCmd::Push { url, branch } } => sync::git_push(&url, &branch),
442
        Cmd::Git { cmd: GitCmd::Import { git_dir, dest, branch } } => sync::git_import(&git_dir, &dest, &branch),
443
        Cmd::Policy { cmd } => match cmd {
444
            PolicyCmd::Init => identity::policy_init(),
445
            PolicyCmd::Show => identity::policy_show(),
446
            PolicyCmd::Grant { identity_file, pattern, out } => identity::policy_grant(&identity_file, pattern.as_deref(), out),
447
            PolicyCmd::Revoke { key, out } => identity::policy_revoke(&key, out),
448
            PolicyCmd::Root { cmd } => match cmd {
449
                RootCmd::Add { identity_file, out } => identity::policy_root_add(&identity_file, out),
450
                RootCmd::Remove { key, out } => identity::policy_root_remove(&key, out),
451
                RootCmd::Threshold { n, out } => identity::policy_threshold(n, out),
452
            },
453
            PolicyCmd::Sign { proposal, out } => identity::policy_sign(&proposal, out),
454
        },
455
        Cmd::Verify { change } => identity::verify(change.as_deref()),
456
        Cmd::Config { key, value, user } => identity::config(&key, &value, user),
457
    }
458
}