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