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