OXYGENHUB
# ba160f60219e oxygen / src / cli / mod.rs
7.9 KiB · Rust · last changed Garbage collection: operation-log retention, pruning with grace period, pack rewriting; oxy gc --now 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;
6
mod workspace;
7
8
use anyhow::{Context, Result};
9
use clap::{Parser, Subcommand};
10
use oxy_core::repo::Repo;
11
use std::path::PathBuf;
12
13
#[derive(Parser)]
14
#[command(name = "oxy", version, about = "Oxygen: breathing fresh air into version control")]
15
struct Cli {
16
    #[command(subcommand)]
17
    cmd: Cmd,
18
}
19
20
#[derive(Subcommand)]
21
enum Cmd {
22
    /// Create a repository in PATH (default: current directory)
23
    Init { path: Option<PathBuf> },
24
    /// Clone a repository from a local path or an http(s) URL
25
    Clone { url: String, dest: Option<PathBuf> },
26
    /// Show the working copy: changed paths and conflicts
27
    Status,
28
    /// Record the working copy as a change on the current bookmark
29
    Commit {
30
        #[arg(short, long)]
31
        message: String,
32
        /// Extra explicit dependencies (ChangeId prefix, #hash prefix)
33
        #[arg(long = "depends-on")]
34
        depends_on: Vec<String>,
35
    },
36
    /// List changes reachable from the bookmark and the working copy
37
    Log,
38
    /// Show what the working copy changes against its base
39
    Diff,
614541c3 Andy McCall ↶ 40
    /// Change the message of a head (default: the head the working copy sits on)
41
    Describe {
42
        #[arg(short, long)]
43
        message: String,
44
        change: Option<String>,
45
    },
46
    /// Fold the working copy into the head it sits on
47
    Amend {
48
        #[arg(short, long)]
49
        message: Option<String>,
50
    },
51
    /// Drop a head from the bookmark; its content stays on disk as uncommitted work
52
    Abandon { change: String },
fce44ae9 Andy McCall ↶ 53
    /// Show one change (ChangeId prefix, #hash prefix, or @)
54
    Show { change: String },
55
    /// Move the working copy to the state formed by CHANGE... (default: the bookmark heads)
56
    Checkout {
57
        changes: Vec<String>,
58
        /// Throw away uncommitted working-copy changes
59
        #[arg(long)]
60
        discard: bool,
61
    },
62
    /// Report which conflicts of the working copy's base are resolved by the current files
63
    Resolve,
64
    /// Undo the last operation
65
    Undo,
ba160f60 Andy McCall ↶ 66
    /// Pack loose objects and prune unreachable ones older than the grace period
67
    Gc {
68
        /// Ignore the grace period (core.gc-grace) and prune immediately
69
        #[arg(long)]
70
        now: bool,
71
    },
fce44ae9 Andy McCall ↶ 72
    /// List operations
73
    #[command(name = "op")]
74
    Op {
75
        #[command(subcommand)]
76
        cmd: OpCmd,
77
    },
78
    /// Manage remotes
79
    Remote {
80
        #[command(subcommand)]
81
        cmd: RemoteCmd,
82
    },
83
    /// Fetch every bookmark of a remote into its tracking references
84
    Fetch { remote: Option<String> },
85
    /// Fetch, then union the remote bookmark into the local one (no merge change)
86
    Pull { remote: Option<String> },
87
    /// Push the current bookmark to a remote
88
    Push {
89
        remote: Option<String>,
90
        /// Allow dropping heads the remote has
91
        #[arg(long)]
92
        force: bool,
93
    },
2b5ee371 Andy McCall ↶ 94
    /// Serve repositories under ROOT over HTTPS (with --tls-cert/--tls-key) or plain HTTP
fce44ae9 Andy McCall ↶ 95
    Serve {
96
        /// Directory with one repository per subdirectory
97
        #[arg(long, default_value = ".")]
98
        root: PathBuf,
99
        #[arg(long, default_value = "127.0.0.1:8890")]
100
        bind: String,
101
        /// Bearer token allowed to push (repeatable); none means open
102
        #[arg(long = "token")]
103
        tokens: Vec<String>,
2b5ee371 Andy McCall ↶ 104
        /// PEM certificate chain; enables HTTPS together with --tls-key
105
        #[arg(long, requires = "tls_key")]
106
        tls_cert: Option<PathBuf>,
107
        /// PEM private key
108
        #[arg(long, requires = "tls_cert")]
109
        tls_key: Option<PathBuf>,
fce44ae9 Andy McCall ↶ 110
    },
111
    /// Manage the signing identity
112
    Identity {
113
        #[command(subcommand)]
114
        cmd: IdentityCmd,
115
    },
116
    /// Verify the signatures on a change (default: every change in the log)
117
    Verify { change: Option<String> },
83e0677b Andy McCall ↶ 118
    /// Manage the trust policy (who may update which bookmarks)
119
    Policy {
120
        #[command(subcommand)]
121
        cmd: PolicyCmd,
122
    },
ba160f60 Andy McCall ↶ 123
    /// Set a configuration key (author.*, core.sign, core.op-retention, core.gc-grace, workspace.bookmark, remote.<name>.url|token|ca)
fce44ae9 Andy McCall ↶ 124
    Config {
125
        key: String,
126
        value: String,
127
        /// Write to the user file (~/.config/oxy/config.toml) instead of this repository
128
        #[arg(long)]
129
        user: bool,
130
    },
131
}
132
133
#[derive(Subcommand)]
134
enum OpCmd {
135
    Log,
136
}
137
138
#[derive(Subcommand)]
139
enum RemoteCmd {
140
    Add { name: String, url: String },
141
    Remove { name: String },
142
    List,
143
}
144
145
#[derive(Subcommand)]
146
enum IdentityCmd {
147
    /// Generate a composite Ed25519 + ML-DSA-65 key pair and make it your author identity
148
    Create {
149
        #[arg(long)]
150
        name: Option<String>,
151
        #[arg(long)]
152
        email: Option<String>,
153
        /// Record the author in this repository only, not in the user config
154
        #[arg(long)]
155
        local: bool,
156
    },
157
    /// Show the configured identity
158
    Show,
83e0677b Andy McCall ↶ 159
    /// Write your identity object to a file, to hand to a policy holder
160
    Export { path: Option<PathBuf> },
fce44ae9 Andy McCall ↶ 161
}
162
83e0677b Andy McCall ↶ 163
#[derive(Subcommand)]
164
enum PolicyCmd {
165
    /// Create the initial policy with your identity as the only root
166
    Init,
167
    /// Show the current policy
168
    Show,
169
    /// Allow an identity (from `oxy identity export`) to update bookmarks
170
    Grant {
171
        identity_file: PathBuf,
172
        /// Reference pattern the grant applies to (default: every bookmark)
173
        #[arg(long)]
174
        pattern: Option<String>,
175
    },
176
    /// Revoke a key (KeyId prefix of an identity in the policy)
177
    Revoke { key: String },
178
}
179
fce44ae9 Andy McCall ↶ 180
/// Open the repository containing the current directory.
181
fn open() -> Result<Repo> {
182
    Repo::open(&std::env::current_dir()?).context("opening repository")
183
}
184
185
/// Parse arguments and run; returns the process exit code.
186
pub fn run() -> Result<i32> {
187
    let cli = Cli::parse();
188
    match cli.cmd {
189
        Cmd::Init { path } => workspace::init(path),
190
        Cmd::Status => workspace::status(),
191
        Cmd::Commit { message, depends_on } => workspace::commit(&message, &depends_on),
192
        Cmd::Log => workspace::log(),
193
        Cmd::Diff => workspace::diff(),
614541c3 Andy McCall ↶ 194
        Cmd::Describe { message, change } => workspace::describe(&message, change.as_deref()),
195
        Cmd::Amend { message } => workspace::amend(message.as_deref()),
196
        Cmd::Abandon { change } => workspace::abandon(&change),
fce44ae9 Andy McCall ↶ 197
        Cmd::Show { change } => workspace::show(&change),
198
        Cmd::Checkout { changes, discard } => workspace::checkout(&changes, discard),
199
        Cmd::Resolve => workspace::resolve(),
200
        Cmd::Undo => workspace::undo(),
ba160f60 Andy McCall ↶ 201
        Cmd::Gc { now } => workspace::gc(now),
fce44ae9 Andy McCall ↶ 202
        Cmd::Op { cmd: OpCmd::Log } => workspace::op_log(),
203
        Cmd::Clone { url, dest } => sync::clone(&url, dest),
204
        Cmd::Remote { cmd } => match cmd {
205
            RemoteCmd::Add { name, url } => sync::remote_add(&name, &url),
206
            RemoteCmd::Remove { name } => sync::remote_remove(&name),
207
            RemoteCmd::List => sync::remote_list(),
208
        },
209
        Cmd::Fetch { remote } => sync::fetch(remote.as_deref()),
210
        Cmd::Pull { remote } => sync::pull(remote.as_deref()),
211
        Cmd::Push { remote, force } => sync::push(remote.as_deref(), force),
2b5ee371 Andy McCall ↶ 212
        Cmd::Serve { root, bind, tokens, tls_cert, tls_key } => sync::serve(root, &bind, tokens, tls_cert.zip(tls_key)),
fce44ae9 Andy McCall ↶ 213
        Cmd::Identity { cmd } => match cmd {
214
            IdentityCmd::Create { name, email, local } => identity::create(name, email, local),
215
            IdentityCmd::Show => identity::show(),
83e0677b Andy McCall ↶ 216
            IdentityCmd::Export { path } => identity::export(path),
217
        },
218
        Cmd::Policy { cmd } => match cmd {
219
            PolicyCmd::Init => identity::policy_init(),
220
            PolicyCmd::Show => identity::policy_show(),
221
            PolicyCmd::Grant { identity_file, pattern } => identity::policy_grant(&identity_file, pattern.as_deref()),
222
            PolicyCmd::Revoke { key } => identity::policy_revoke(&key),
fce44ae9 Andy McCall ↶ 223
        },
224
        Cmd::Verify { change } => identity::verify(change.as_deref()),
225
        Cmd::Config { key, value, user } => identity::config(&key, &value, user),
226
    }
227
}