OXYGENHUB
# 37f1914b62a8 oxygen / src / cli / mod.rs
7.0 KiB · Rust · last changed Packed storage: zstd packs with sorted indexes, oxy gc, auto-pack at 4096 loose objects; README 1 month ago source blame history raw
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,
40
    /// Show one change (ChangeId prefix, #hash prefix, or @)
41
    Show { change: String },
42
    /// Move the working copy to the state formed by CHANGE... (default: the bookmark heads)
43
    Checkout {
44
        changes: Vec<String>,
45
        /// Throw away uncommitted working-copy changes
46
        #[arg(long)]
47
        discard: bool,
48
    },
49
    /// Report which conflicts of the working copy's base are resolved by the current files
50
    Resolve,
51
    /// Undo the last operation
52
    Undo,
53
    /// Pack loose objects into a compressed pack
54
    Gc,
55
    /// List operations
56
    #[command(name = "op")]
57
    Op {
58
        #[command(subcommand)]
59
        cmd: OpCmd,
60
    },
61
    /// Manage remotes
62
    Remote {
63
        #[command(subcommand)]
64
        cmd: RemoteCmd,
65
    },
66
    /// Fetch every bookmark of a remote into its tracking references
67
    Fetch { remote: Option<String> },
68
    /// Fetch, then union the remote bookmark into the local one (no merge change)
69
    Pull { remote: Option<String> },
70
    /// Push the current bookmark to a remote
71
    Push {
72
        remote: Option<String>,
73
        /// Allow dropping heads the remote has
74
        #[arg(long)]
75
        force: bool,
76
    },
77
    /// Serve repositories under ROOT over HTTPS (with --tls-cert/--tls-key) or plain HTTP
78
    Serve {
79
        /// Directory with one repository per subdirectory
80
        #[arg(long, default_value = ".")]
81
        root: PathBuf,
82
        #[arg(long, default_value = "127.0.0.1:8890")]
83
        bind: String,
84
        /// Bearer token allowed to push (repeatable); none means open
85
        #[arg(long = "token")]
86
        tokens: Vec<String>,
87
        /// PEM certificate chain; enables HTTPS together with --tls-key
88
        #[arg(long, requires = "tls_key")]
89
        tls_cert: Option<PathBuf>,
90
        /// PEM private key
91
        #[arg(long, requires = "tls_cert")]
92
        tls_key: Option<PathBuf>,
93
    },
94
    /// Manage the signing identity
95
    Identity {
96
        #[command(subcommand)]
97
        cmd: IdentityCmd,
98
    },
99
    /// Verify the signatures on a change (default: every change in the log)
100
    Verify { change: Option<String> },
101
    /// Manage the trust policy (who may update which bookmarks)
102
    Policy {
103
        #[command(subcommand)]
104
        cmd: PolicyCmd,
105
    },
106
    /// Set a configuration key (author.*, core.sign, workspace.bookmark, remote.<name>.url|token|ca)
107
    Config {
108
        key: String,
109
        value: String,
110
        /// Write to the user file (~/.config/oxy/config.toml) instead of this repository
111
        #[arg(long)]
112
        user: bool,
113
    },
114
}
115
116
#[derive(Subcommand)]
117
enum OpCmd {
118
    Log,
119
}
120
121
#[derive(Subcommand)]
122
enum RemoteCmd {
123
    Add { name: String, url: String },
124
    Remove { name: String },
125
    List,
126
}
127
128
#[derive(Subcommand)]
129
enum IdentityCmd {
130
    /// Generate a composite Ed25519 + ML-DSA-65 key pair and make it your author identity
131
    Create {
132
        #[arg(long)]
133
        name: Option<String>,
134
        #[arg(long)]
135
        email: Option<String>,
136
        /// Record the author in this repository only, not in the user config
137
        #[arg(long)]
138
        local: bool,
139
    },
140
    /// Show the configured identity
141
    Show,
142
    /// Write your identity object to a file, to hand to a policy holder
143
    Export { path: Option<PathBuf> },
144
}
145
146
#[derive(Subcommand)]
147
enum PolicyCmd {
148
    /// Create the initial policy with your identity as the only root
149
    Init,
150
    /// Show the current policy
151
    Show,
152
    /// Allow an identity (from `oxy identity export`) to update bookmarks
153
    Grant {
154
        identity_file: PathBuf,
155
        /// Reference pattern the grant applies to (default: every bookmark)
156
        #[arg(long)]
157
        pattern: Option<String>,
158
    },
159
    /// Revoke a key (KeyId prefix of an identity in the policy)
160
    Revoke { key: String },
161
}
162
163
/// Open the repository containing the current directory.
164
fn open() -> Result<Repo> {
165
    Repo::open(&std::env::current_dir()?).context("opening repository")
166
}
167
168
/// Parse arguments and run; returns the process exit code.
169
pub fn run() -> Result<i32> {
170
    let cli = Cli::parse();
171
    match cli.cmd {
172
        Cmd::Init { path } => workspace::init(path),
173
        Cmd::Status => workspace::status(),
174
        Cmd::Commit { message, depends_on } => workspace::commit(&message, &depends_on),
175
        Cmd::Log => workspace::log(),
176
        Cmd::Diff => workspace::diff(),
177
        Cmd::Show { change } => workspace::show(&change),
178
        Cmd::Checkout { changes, discard } => workspace::checkout(&changes, discard),
179
        Cmd::Resolve => workspace::resolve(),
180
        Cmd::Undo => workspace::undo(),
181
        Cmd::Gc => workspace::gc(),
182
        Cmd::Op { cmd: OpCmd::Log } => workspace::op_log(),
183
        Cmd::Clone { url, dest } => sync::clone(&url, dest),
184
        Cmd::Remote { cmd } => match cmd {
185
            RemoteCmd::Add { name, url } => sync::remote_add(&name, &url),
186
            RemoteCmd::Remove { name } => sync::remote_remove(&name),
187
            RemoteCmd::List => sync::remote_list(),
188
        },
189
        Cmd::Fetch { remote } => sync::fetch(remote.as_deref()),
190
        Cmd::Pull { remote } => sync::pull(remote.as_deref()),
191
        Cmd::Push { remote, force } => sync::push(remote.as_deref(), force),
192
        Cmd::Serve { root, bind, tokens, tls_cert, tls_key } => sync::serve(root, &bind, tokens, tls_cert.zip(tls_key)),
193
        Cmd::Identity { cmd } => match cmd {
194
            IdentityCmd::Create { name, email, local } => identity::create(name, email, local),
195
            IdentityCmd::Show => identity::show(),
196
            IdentityCmd::Export { path } => identity::export(path),
197
        },
198
        Cmd::Policy { cmd } => match cmd {
199
            PolicyCmd::Init => identity::policy_init(),
200
            PolicyCmd::Show => identity::policy_show(),
201
            PolicyCmd::Grant { identity_file, pattern } => identity::policy_grant(&identity_file, pattern.as_deref()),
202
            PolicyCmd::Revoke { key } => identity::policy_revoke(&key),
203
        },
204
        Cmd::Verify { change } => identity::verify(change.as_deref()),
205
        Cmd::Config { key, value, user } => identity::config(&key, &value, user),
206
    }
207
}