OXYGENHUB
# 5259a3af8ee6 oxygen / src / cli / mod.rs
9.8 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>,
b3c5a59a Andy McCall ↶ 38
    },
7421309f Andy McCall ↶ 39
    /// Show or set the sparse filter (paths to materialize); re-checks out the bookmark
40
    Sparse { paths: Vec<String> },
fce44ae9 Andy McCall ↶ 41
    /// Show the working copy: changed paths and conflicts
42
    Status,
135212de Andy McCall ↶ 43
    /// One-line summary for shell prompts; fast and records nothing
44
    Prompt {
45
        /// Placeholders: %b bookmark, %c change id, %h heads, %d dirty (*), %x conflict (!), %A/%D ahead/behind (+n/-n), %a/%B raw counts
46
        #[arg(long, default_value = oxy_core::repo::DEFAULT_PROMPT_FORMAT)]
47
        format: String,
48
    },
fce44ae9 Andy McCall ↶ 49
    /// Record the working copy as a change on the current bookmark
50
    Commit {
51
        #[arg(short, long)]
52
        message: String,
53
        /// Extra explicit dependencies (ChangeId prefix, #hash prefix)
54
        #[arg(long = "depends-on")]
55
        depends_on: Vec<String>,
56
    },
57
    /// List changes reachable from the bookmark and the working copy
58
    Log,
59
    /// Show what the working copy changes against its base
60
    Diff,
614541c3 Andy McCall ↶ 61
    /// Change the message of a head (default: the head the working copy sits on)
62
    Describe {
63
        #[arg(short, long)]
64
        message: String,
65
        change: Option<String>,
66
    },
67
    /// Fold the working copy into the head it sits on
68
    Amend {
69
        #[arg(short, long)]
70
        message: Option<String>,
71
    },
72
    /// Drop a head from the bookmark; its content stays on disk as uncommitted work
73
    Abandon { change: String },
fce44ae9 Andy McCall ↶ 74
    /// Show one change (ChangeId prefix, #hash prefix, or @)
75
    Show { change: String },
76
    /// Move the working copy to the state formed by CHANGE... (default: the bookmark heads)
77
    Checkout {
78
        changes: Vec<String>,
79
        /// Throw away uncommitted working-copy changes
80
        #[arg(long)]
81
        discard: bool,
82
    },
83
    /// Report which conflicts of the working copy's base are resolved by the current files
84
    Resolve,
85
    /// Undo the last operation
86
    Undo,
252e8cb2 Andy McCall ↶ 87
    /// Write a checkpoint of the current bookmark's state now
88
    Checkpoint,
ba160f60 Andy McCall ↶ 89
    /// Pack loose objects and prune unreachable ones older than the grace period
90
    Gc {
91
        /// Ignore the grace period (core.gc-grace) and prune immediately
92
        #[arg(long)]
93
        now: bool,
94
    },
fce44ae9 Andy McCall ↶ 95
    /// List operations
96
    #[command(name = "op")]
97
    Op {
98
        #[command(subcommand)]
99
        cmd: OpCmd,
100
    },
101
    /// Manage remotes
102
    Remote {
103
        #[command(subcommand)]
104
        cmd: RemoteCmd,
105
    },
106
    /// Fetch every bookmark of a remote into its tracking references
107
    Fetch { remote: Option<String> },
108
    /// Fetch, then union the remote bookmark into the local one (no merge change)
109
    Pull { remote: Option<String> },
110
    /// Push the current bookmark to a remote
111
    Push {
112
        remote: Option<String>,
113
        /// Allow dropping heads the remote has
114
        #[arg(long)]
115
        force: bool,
116
    },
2b5ee371 Andy McCall ↶ 117
    /// Serve repositories under ROOT over HTTPS (with --tls-cert/--tls-key) or plain HTTP
fce44ae9 Andy McCall ↶ 118
    Serve {
119
        /// Directory with one repository per subdirectory
120
        #[arg(long, default_value = ".")]
121
        root: PathBuf,
122
        #[arg(long, default_value = "127.0.0.1:8890")]
123
        bind: String,
124
        /// Bearer token allowed to push (repeatable); none means open
125
        #[arg(long = "token")]
126
        tokens: Vec<String>,
2b5ee371 Andy McCall ↶ 127
        /// PEM certificate chain; enables HTTPS together with --tls-key
128
        #[arg(long, requires = "tls_key")]
129
        tls_cert: Option<PathBuf>,
130
        /// PEM private key
131
        #[arg(long, requires = "tls_cert")]
132
        tls_key: Option<PathBuf>,
fce44ae9 Andy McCall ↶ 133
    },
134
    /// Manage the signing identity
135
    Identity {
136
        #[command(subcommand)]
137
        cmd: IdentityCmd,
138
    },
139
    /// Verify the signatures on a change (default: every change in the log)
140
    Verify { change: Option<String> },
79ab8528 Andy McCall ↶ 141
    /// Git interoperability
142
    Git {
143
        #[command(subcommand)]
144
        cmd: GitCmd,
145
    },
83e0677b Andy McCall ↶ 146
    /// Manage the trust policy (who may update which bookmarks)
147
    Policy {
148
        #[command(subcommand)]
149
        cmd: PolicyCmd,
150
    },
7421309f Andy McCall ↶ 151
    /// 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 ↶ 152
    Config {
153
        key: String,
154
        value: String,
155
        /// Write to the user file (~/.config/oxy/config.toml) instead of this repository
156
        #[arg(long)]
157
        user: bool,
158
    },
159
}
160
161
#[derive(Subcommand)]
162
enum OpCmd {
163
    Log,
164
}
165
166
#[derive(Subcommand)]
167
enum RemoteCmd {
168
    Add { name: String, url: String },
169
    Remove { name: String },
170
    List,
171
}
172
173
#[derive(Subcommand)]
174
enum IdentityCmd {
175
    /// Generate a composite Ed25519 + ML-DSA-65 key pair and make it your author identity
176
    Create {
177
        #[arg(long)]
178
        name: Option<String>,
179
        #[arg(long)]
180
        email: Option<String>,
181
        /// Record the author in this repository only, not in the user config
182
        #[arg(long)]
183
        local: bool,
184
    },
185
    /// Show the configured identity
186
    Show,
83e0677b Andy McCall ↶ 187
    /// Write your identity object to a file, to hand to a policy holder
188
    Export { path: Option<PathBuf> },
fce44ae9 Andy McCall ↶ 189
}
190
83e0677b Andy McCall ↶ 191
#[derive(Subcommand)]
79ab8528 Andy McCall ↶ 192
enum GitCmd {
193
    /// Export the current bookmark to a Git repository (created bare if absent)
194
    Export { git_dir: PathBuf },
c3069537 Andy McCall ↶ 195
    /// Import a Git branch into a new Oxygen repository at DEST
196
    Import {
197
        git_dir: PathBuf,
198
        dest: PathBuf,
199
        #[arg(long, default_value = "main")]
200
        branch: String,
201
    },
79ab8528 Andy McCall ↶ 202
}
203
204
#[derive(Subcommand)]
83e0677b Andy McCall ↶ 205
enum PolicyCmd {
206
    /// Create the initial policy with your identity as the only root
207
    Init,
208
    /// Show the current policy
209
    Show,
210
    /// Allow an identity (from `oxy identity export`) to update bookmarks
211
    Grant {
212
        identity_file: PathBuf,
213
        /// Reference pattern the grant applies to (default: every bookmark)
214
        #[arg(long)]
215
        pattern: Option<String>,
216
    },
217
    /// Revoke a key (KeyId prefix of an identity in the policy)
218
    Revoke { key: String },
219
}
220
fce44ae9 Andy McCall ↶ 221
/// Open the repository containing the current directory.
222
fn open() -> Result<Repo> {
223
    Repo::open(&std::env::current_dir()?).context("opening repository")
224
}
225
226
/// Parse arguments and run; returns the process exit code.
227
pub fn run() -> Result<i32> {
228
    let cli = Cli::parse();
135212de Andy McCall ↶ 229
    ui::init(cli.plain);
fce44ae9 Andy McCall ↶ 230
    match cli.cmd {
231
        Cmd::Init { path } => workspace::init(path),
232
        Cmd::Status => workspace::status(),
135212de Andy McCall ↶ 233
        Cmd::Prompt { format } => workspace::prompt(&format),
fce44ae9 Andy McCall ↶ 234
        Cmd::Commit { message, depends_on } => workspace::commit(&message, &depends_on),
235
        Cmd::Log => workspace::log(),
236
        Cmd::Diff => workspace::diff(),
614541c3 Andy McCall ↶ 237
        Cmd::Describe { message, change } => workspace::describe(&message, change.as_deref()),
238
        Cmd::Amend { message } => workspace::amend(message.as_deref()),
239
        Cmd::Abandon { change } => workspace::abandon(&change),
fce44ae9 Andy McCall ↶ 240
        Cmd::Show { change } => workspace::show(&change),
241
        Cmd::Checkout { changes, discard } => workspace::checkout(&changes, discard),
242
        Cmd::Resolve => workspace::resolve(),
243
        Cmd::Undo => workspace::undo(),
ba160f60 Andy McCall ↶ 244
        Cmd::Gc { now } => workspace::gc(now),
252e8cb2 Andy McCall ↶ 245
        Cmd::Checkpoint => workspace::checkpoint(),
fce44ae9 Andy McCall ↶ 246
        Cmd::Op { cmd: OpCmd::Log } => workspace::op_log(),
7421309f Andy McCall ↶ 247
        Cmd::Clone { url, dest, max_blob, sparse } => sync::clone(&url, dest, max_blob.as_deref(), &sparse),
248
        Cmd::Sparse { paths } => workspace::sparse(&paths),
fce44ae9 Andy McCall ↶ 249
        Cmd::Remote { cmd } => match cmd {
250
            RemoteCmd::Add { name, url } => sync::remote_add(&name, &url),
251
            RemoteCmd::Remove { name } => sync::remote_remove(&name),
252
            RemoteCmd::List => sync::remote_list(),
253
        },
254
        Cmd::Fetch { remote } => sync::fetch(remote.as_deref()),
255
        Cmd::Pull { remote } => sync::pull(remote.as_deref()),
256
        Cmd::Push { remote, force } => sync::push(remote.as_deref(), force),
2b5ee371 Andy McCall ↶ 257
        Cmd::Serve { root, bind, tokens, tls_cert, tls_key } => sync::serve(root, &bind, tokens, tls_cert.zip(tls_key)),
fce44ae9 Andy McCall ↶ 258
        Cmd::Identity { cmd } => match cmd {
259
            IdentityCmd::Create { name, email, local } => identity::create(name, email, local),
260
            IdentityCmd::Show => identity::show(),
83e0677b Andy McCall ↶ 261
            IdentityCmd::Export { path } => identity::export(path),
262
        },
79ab8528 Andy McCall ↶ 263
        Cmd::Git { cmd: GitCmd::Export { git_dir } } => sync::git_export(&git_dir),
c3069537 Andy McCall ↶ 264
        Cmd::Git { cmd: GitCmd::Import { git_dir, dest, branch } } => sync::git_import(&git_dir, &dest, &branch),
83e0677b Andy McCall ↶ 265
        Cmd::Policy { cmd } => match cmd {
266
            PolicyCmd::Init => identity::policy_init(),
267
            PolicyCmd::Show => identity::policy_show(),
268
            PolicyCmd::Grant { identity_file, pattern } => identity::policy_grant(&identity_file, pattern.as_deref()),
269
            PolicyCmd::Revoke { key } => identity::policy_revoke(&key),
fce44ae9 Andy McCall ↶ 270
        },
271
        Cmd::Verify { change } => identity::verify(change.as_deref()),
272
        Cmd::Config { key, value, user } => identity::config(&key, &value, user),
273
    }
274
}