OXYGENHUB
# 6706bdcfb63e oxygen / src / cli / mod.rs
12.2 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
    /// One-line summary for shell prompts; fast and records nothing
54
    Prompt {
55
        /// Placeholders: %b bookmark, %c change id, %h heads, %d dirty (*), %x conflict (!), %A/%D ahead/behind (+n/-n), %a/%B raw counts
56
        #[arg(long, default_value = oxy_core::repo::DEFAULT_PROMPT_FORMAT)]
57
        format: String,
58
    },
59
    /// Record the working copy as a change on the current bookmark
60
    Commit {
61
        #[arg(short, long)]
62
        message: String,
63
        /// Extra explicit dependencies (ChangeId prefix, #hash prefix)
64
        #[arg(long = "depends-on")]
65
        depends_on: Vec<String>,
66
    },
67
    /// List changes reachable from the bookmark and the working copy
68
    Log,
69
    /// Show what the working copy changes against its base
70
    Diff,
71
    /// Change the message of a head (default: the head the working copy sits on)
72
    Describe {
73
        #[arg(short, long)]
74
        message: String,
75
        change: Option<String>,
76
    },
77
    /// Fold the working copy into the head it sits on, or into CHANGE with --into
78
    Amend {
79
        #[arg(short, long)]
80
        message: Option<String>,
81
        /// Amend an older change; the edits must touch only lines it already knows
82
        #[arg(long)]
83
        into: Option<String>,
84
    },
85
    /// Split a head: the named paths become a separate first change
86
    Split {
87
        change: String,
88
        paths: Vec<String>,
89
    },
90
    /// Drop a head from the bookmark; its content stays on disk as uncommitted work
91
    Abandon { change: String },
92
    /// Show one change (ChangeId prefix, #hash prefix, or @)
93
    Show { change: String },
94
    /// Move the working copy to the state formed by CHANGE... (default: the bookmark heads)
95
    Checkout {
96
        changes: Vec<String>,
97
        /// Throw away uncommitted working-copy changes
98
        #[arg(long)]
99
        discard: bool,
100
    },
101
    /// Report which conflicts of the working copy's base are resolved by the current files
102
    Resolve,
103
    /// Undo the last operation
104
    Undo,
105
    /// Write a checkpoint of the current bookmark's state now
106
    Checkpoint,
107
    /// Pack loose objects and prune unreachable ones older than the grace period
108
    Gc {
109
        /// Ignore the grace period (core.gc-grace) and prune immediately
110
        #[arg(long)]
111
        now: bool,
112
    },
113
    /// List operations
114
    #[command(name = "op")]
115
    Op {
116
        #[command(subcommand)]
117
        cmd: OpCmd,
118
    },
119
    /// Take the advisory local lock on a lockable file, making it writable
120
    Lock { path: String },
121
    /// Release the local lock and make the file read-only again
122
    Unlock { path: String },
123
    /// Workspaces: extra checkout directories sharing this repository
124
    Workspace {
125
        #[command(subcommand)]
126
        cmd: WorkspaceCmd,
127
    },
128
    /// Tags: immutable named states
129
    Tag {
130
        #[command(subcommand)]
131
        cmd: TagCmd,
132
    },
133
    /// Manage remotes
134
    Remote {
135
        #[command(subcommand)]
136
        cmd: RemoteCmd,
137
    },
138
    /// Fetch every bookmark of a remote into its tracking references
139
    Fetch { remote: Option<String> },
140
    /// Fetch, then union the remote bookmark into the local one (no merge change)
141
    Pull { remote: Option<String> },
142
    /// Push the current bookmark to a remote
143
    Push {
144
        remote: Option<String>,
145
        /// Allow dropping heads the remote has
146
        #[arg(long)]
147
        force: bool,
148
    },
149
    /// Serve repositories under ROOT over HTTPS (with --tls-cert/--tls-key) or plain HTTP
150
    Serve {
151
        /// Directory with one repository per subdirectory
152
        #[arg(long, default_value = ".")]
153
        root: PathBuf,
154
        #[arg(long, default_value = "127.0.0.1:8890")]
155
        bind: String,
156
        /// Bearer token allowed to push (repeatable); none means open
157
        #[arg(long = "token")]
158
        tokens: Vec<String>,
159
        /// PEM certificate chain; enables HTTPS together with --tls-key
160
        #[arg(long, requires = "tls_key")]
161
        tls_cert: Option<PathBuf>,
162
        /// PEM private key
163
        #[arg(long, requires = "tls_cert")]
164
        tls_key: Option<PathBuf>,
165
    },
166
    /// Manage the signing identity
167
    Identity {
168
        #[command(subcommand)]
169
        cmd: IdentityCmd,
170
    },
171
    /// Verify the signatures on a change (default: every change in the log)
172
    Verify { change: Option<String> },
173
    /// Git interoperability
174
    Git {
175
        #[command(subcommand)]
176
        cmd: GitCmd,
177
    },
178
    /// Manage the trust policy (who may update which bookmarks)
179
    Policy {
180
        #[command(subcommand)]
181
        cmd: PolicyCmd,
182
    },
183
    /// 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)
184
    Config {
185
        key: String,
186
        value: String,
187
        /// Write to the user file (~/.config/oxy/config.toml) instead of this repository
188
        #[arg(long)]
189
        user: bool,
190
    },
191
}
192
193
#[derive(Subcommand)]
194
enum OpCmd {
195
    Log,
196
}
197
198
#[derive(Subcommand)]
199
enum WorkspaceCmd {
200
    /// Create a workspace at PATH checked out on CHANGE... (default: the bookmark heads)
201
    Add { path: PathBuf, changes: Vec<String> },
202
    List,
203
    /// Stop tracking a workspace (by id prefix or path); its files stay
204
    Forget { workspace: String },
205
}
206
207
#[derive(Subcommand)]
208
enum TagCmd {
209
    /// Tag the state of CHANGE... (default: the bookmark heads)
210
    Create {
211
        name: String,
212
        changes: Vec<String>,
213
        #[arg(short, long, default_value = "")]
214
        message: String,
215
    },
216
    List,
217
}
218
219
#[derive(Subcommand)]
220
enum RemoteCmd {
221
    Add { name: String, url: String },
222
    Remove { name: String },
223
    List,
224
}
225
226
#[derive(Subcommand)]
227
enum IdentityCmd {
228
    /// Generate a composite Ed25519 + ML-DSA-65 key pair and make it your author identity
229
    Create {
230
        #[arg(long)]
231
        name: Option<String>,
232
        #[arg(long)]
233
        email: Option<String>,
234
        /// Record the author in this repository only, not in the user config
235
        #[arg(long)]
236
        local: bool,
237
    },
238
    /// Show the configured identity
239
    Show,
240
    /// Write your identity object to a file, to hand to a policy holder
241
    Export { path: Option<PathBuf> },
242
}
243
244
#[derive(Subcommand)]
245
enum GitCmd {
246
    /// Export the current bookmark to a Git repository (created bare if absent)
247
    Export { git_dir: PathBuf },
248
    /// Import a Git branch into a new Oxygen repository at DEST
249
    Import {
250
        git_dir: PathBuf,
251
        dest: PathBuf,
252
        #[arg(long, default_value = "main")]
253
        branch: String,
254
    },
255
}
256
257
#[derive(Subcommand)]
258
enum PolicyCmd {
259
    /// Create the initial policy with your identity as the only root
260
    Init,
261
    /// Show the current policy
262
    Show,
263
    /// Allow an identity (from `oxy identity export`) to update bookmarks
264
    Grant {
265
        identity_file: PathBuf,
266
        /// Reference pattern the grant applies to (default: every bookmark)
267
        #[arg(long)]
268
        pattern: Option<String>,
269
    },
270
    /// Revoke a key (KeyId prefix of an identity in the policy)
271
    Revoke { key: String },
272
}
273
274
/// Open the repository containing the current directory.
275
fn open() -> Result<Repo> {
276
    Repo::open(&std::env::current_dir()?).context("opening repository")
277
}
278
279
/// Parse arguments and run; returns the process exit code.
280
pub fn run() -> Result<i32> {
281
    let cli = Cli::parse();
282
    ui::init(cli.plain);
283
    match cli.cmd {
284
        Cmd::Init { path } => workspace::init(path),
285
        Cmd::Status => workspace::status(),
286
        Cmd::Prompt { format } => workspace::prompt(&format),
287
        Cmd::Commit { message, depends_on } => workspace::commit(&message, &depends_on),
288
        Cmd::Log => workspace::log(),
289
        Cmd::Diff => workspace::diff(),
290
        Cmd::Describe { message, change } => workspace::describe(&message, change.as_deref()),
291
        Cmd::Amend { message, into } => workspace::amend(message.as_deref(), into.as_deref()),
292
        Cmd::Split { change, paths } => workspace::split(&change, &paths),
293
        Cmd::Abandon { change } => workspace::abandon(&change),
294
        Cmd::Show { change } => workspace::show(&change),
295
        Cmd::Checkout { changes, discard } => workspace::checkout(&changes, discard),
296
        Cmd::Resolve => workspace::resolve(),
297
        Cmd::Undo => workspace::undo(),
298
        Cmd::Gc { now } => workspace::gc(now),
299
        Cmd::Checkpoint => workspace::checkpoint(),
300
        Cmd::Op { cmd: OpCmd::Log } => workspace::op_log(),
301
        Cmd::Clone { url, dest, max_blob, sparse, depth } => sync::clone(&url, dest, max_blob.as_deref(), &sparse, depth),
302
        Cmd::Deepen { depth, remote } => sync::deepen(&remote, depth),
303
        Cmd::Sparse { paths } => workspace::sparse(&paths),
304
        Cmd::Lock { path } => workspace::lock(&path),
305
        Cmd::Unlock { path } => workspace::unlock(&path),
306
        Cmd::Workspace { cmd } => match cmd {
307
            WorkspaceCmd::Add { path, changes } => workspace::workspace_add(&path, &changes),
308
            WorkspaceCmd::List => workspace::workspace_list(),
309
            WorkspaceCmd::Forget { workspace } => workspace::workspace_forget(&workspace),
310
        },
311
        Cmd::Tag { cmd } => match cmd {
312
            TagCmd::Create { name, changes, message } => workspace::tag_create(&name, &changes, &message),
313
            TagCmd::List => workspace::tag_list(),
314
        },
315
        Cmd::Remote { cmd } => match cmd {
316
            RemoteCmd::Add { name, url } => sync::remote_add(&name, &url),
317
            RemoteCmd::Remove { name } => sync::remote_remove(&name),
318
            RemoteCmd::List => sync::remote_list(),
319
        },
320
        Cmd::Fetch { remote } => sync::fetch(remote.as_deref()),
321
        Cmd::Pull { remote } => sync::pull(remote.as_deref()),
322
        Cmd::Push { remote, force } => sync::push(remote.as_deref(), force),
323
        Cmd::Serve { root, bind, tokens, tls_cert, tls_key } => sync::serve(root, &bind, tokens, tls_cert.zip(tls_key)),
324
        Cmd::Identity { cmd } => match cmd {
325
            IdentityCmd::Create { name, email, local } => identity::create(name, email, local),
326
            IdentityCmd::Show => identity::show(),
327
            IdentityCmd::Export { path } => identity::export(path),
328
        },
329
        Cmd::Git { cmd: GitCmd::Export { git_dir } } => sync::git_export(&git_dir),
330
        Cmd::Git { cmd: GitCmd::Import { git_dir, dest, branch } } => sync::git_import(&git_dir, &dest, &branch),
331
        Cmd::Policy { cmd } => match cmd {
332
            PolicyCmd::Init => identity::policy_init(),
333
            PolicyCmd::Show => identity::policy_show(),
334
            PolicyCmd::Grant { identity_file, pattern } => identity::policy_grant(&identity_file, pattern.as_deref()),
335
            PolicyCmd::Revoke { key } => identity::policy_revoke(&key),
336
        },
337
        Cmd::Verify { change } => identity::verify(change.as_deref()),
338
        Cmd::Config { key, value, user } => identity::config(&key, &value, user),
339
    }
340
}