OXYGENHUB
# 79ab8528ff62 oxygen / src / cli / mod.rs
8.2 KiB · Rust · last changed Git bridge, export side: one commit per bookmark state via git fast-import, Change-Id/Oxy-Hash/Oxy-State trailers, incremental marks 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> },
79ab8528 Andy McCall ↶ 118
    /// Git interoperability
119
    Git {
120
        #[command(subcommand)]
121
        cmd: GitCmd,
122
    },
83e0677b Andy McCall ↶ 123
    /// Manage the trust policy (who may update which bookmarks)
124
    Policy {
125
        #[command(subcommand)]
126
        cmd: PolicyCmd,
127
    },
ba160f60 Andy McCall ↶ 128
    /// Set a configuration key (author.*, core.sign, core.op-retention, core.gc-grace, workspace.bookmark, remote.<name>.url|token|ca)
fce44ae9 Andy McCall ↶ 129
    Config {
130
        key: String,
131
        value: String,
132
        /// Write to the user file (~/.config/oxy/config.toml) instead of this repository
133
        #[arg(long)]
134
        user: bool,
135
    },
136
}
137
138
#[derive(Subcommand)]
139
enum OpCmd {
140
    Log,
141
}
142
143
#[derive(Subcommand)]
144
enum RemoteCmd {
145
    Add { name: String, url: String },
146
    Remove { name: String },
147
    List,
148
}
149
150
#[derive(Subcommand)]
151
enum IdentityCmd {
152
    /// Generate a composite Ed25519 + ML-DSA-65 key pair and make it your author identity
153
    Create {
154
        #[arg(long)]
155
        name: Option<String>,
156
        #[arg(long)]
157
        email: Option<String>,
158
        /// Record the author in this repository only, not in the user config
159
        #[arg(long)]
160
        local: bool,
161
    },
162
    /// Show the configured identity
163
    Show,
83e0677b Andy McCall ↶ 164
    /// Write your identity object to a file, to hand to a policy holder
165
    Export { path: Option<PathBuf> },
fce44ae9 Andy McCall ↶ 166
}
167
83e0677b Andy McCall ↶ 168
#[derive(Subcommand)]
79ab8528 Andy McCall ↶ 169
enum GitCmd {
170
    /// Export the current bookmark to a Git repository (created bare if absent)
171
    Export { git_dir: PathBuf },
172
}
173
174
#[derive(Subcommand)]
83e0677b Andy McCall ↶ 175
enum PolicyCmd {
176
    /// Create the initial policy with your identity as the only root
177
    Init,
178
    /// Show the current policy
179
    Show,
180
    /// Allow an identity (from `oxy identity export`) to update bookmarks
181
    Grant {
182
        identity_file: PathBuf,
183
        /// Reference pattern the grant applies to (default: every bookmark)
184
        #[arg(long)]
185
        pattern: Option<String>,
186
    },
187
    /// Revoke a key (KeyId prefix of an identity in the policy)
188
    Revoke { key: String },
189
}
190
fce44ae9 Andy McCall ↶ 191
/// Open the repository containing the current directory.
192
fn open() -> Result<Repo> {
193
    Repo::open(&std::env::current_dir()?).context("opening repository")
194
}
195
196
/// Parse arguments and run; returns the process exit code.
197
pub fn run() -> Result<i32> {
198
    let cli = Cli::parse();
199
    match cli.cmd {
200
        Cmd::Init { path } => workspace::init(path),
201
        Cmd::Status => workspace::status(),
202
        Cmd::Commit { message, depends_on } => workspace::commit(&message, &depends_on),
203
        Cmd::Log => workspace::log(),
204
        Cmd::Diff => workspace::diff(),
614541c3 Andy McCall ↶ 205
        Cmd::Describe { message, change } => workspace::describe(&message, change.as_deref()),
206
        Cmd::Amend { message } => workspace::amend(message.as_deref()),
207
        Cmd::Abandon { change } => workspace::abandon(&change),
fce44ae9 Andy McCall ↶ 208
        Cmd::Show { change } => workspace::show(&change),
209
        Cmd::Checkout { changes, discard } => workspace::checkout(&changes, discard),
210
        Cmd::Resolve => workspace::resolve(),
211
        Cmd::Undo => workspace::undo(),
ba160f60 Andy McCall ↶ 212
        Cmd::Gc { now } => workspace::gc(now),
fce44ae9 Andy McCall ↶ 213
        Cmd::Op { cmd: OpCmd::Log } => workspace::op_log(),
214
        Cmd::Clone { url, dest } => sync::clone(&url, dest),
215
        Cmd::Remote { cmd } => match cmd {
216
            RemoteCmd::Add { name, url } => sync::remote_add(&name, &url),
217
            RemoteCmd::Remove { name } => sync::remote_remove(&name),
218
            RemoteCmd::List => sync::remote_list(),
219
        },
220
        Cmd::Fetch { remote } => sync::fetch(remote.as_deref()),
221
        Cmd::Pull { remote } => sync::pull(remote.as_deref()),
222
        Cmd::Push { remote, force } => sync::push(remote.as_deref(), force),
2b5ee371 Andy McCall ↶ 223
        Cmd::Serve { root, bind, tokens, tls_cert, tls_key } => sync::serve(root, &bind, tokens, tls_cert.zip(tls_key)),
fce44ae9 Andy McCall ↶ 224
        Cmd::Identity { cmd } => match cmd {
225
            IdentityCmd::Create { name, email, local } => identity::create(name, email, local),
226
            IdentityCmd::Show => identity::show(),
83e0677b Andy McCall ↶ 227
            IdentityCmd::Export { path } => identity::export(path),
228
        },
79ab8528 Andy McCall ↶ 229
        Cmd::Git { cmd: GitCmd::Export { git_dir } } => sync::git_export(&git_dir),
83e0677b Andy McCall ↶ 230
        Cmd::Policy { cmd } => match cmd {
231
            PolicyCmd::Init => identity::policy_init(),
232
            PolicyCmd::Show => identity::policy_show(),
233
            PolicyCmd::Grant { identity_file, pattern } => identity::policy_grant(&identity_file, pattern.as_deref()),
234
            PolicyCmd::Revoke { key } => identity::policy_revoke(&key),
fce44ae9 Andy McCall ↶ 235
        },
236
        Cmd::Verify { change } => identity::verify(change.as_deref()),
237
        Cmd::Config { key, value, user } => identity::config(&key, &value, user),
238
    }
239
}