OXYGENHUB
◆ v0.7.0 oxygen / crates / oxy-core / src / repo / config.rs
14.8 KiB · Rust · last changed A push no longer passes a proxy's request limit (closes #133) 11 days ago source blame history raw
98286406 Andy McCall ↶ 1
//! Layered configuration: defaults, the user file, the repository file.
2
3
use super::*;
4
5
/// Effective settings: defaults, then the user file (`~/.config/oxy/config.toml`,
6
/// spec §19), then the repository file (`.oxy/config`). Both files are
7
/// `key = "value"` lines. `save` writes only the repository layer.
8
#[derive(Clone, Debug, PartialEq, Eq)]
9
pub struct Config {
10
    /// Keys set in the repository file (or via `set`), written by `save`.
11
    repo_layer: BTreeMap<String, String>,
12
    pub author_name: String,
13
    pub author_email: String,
14
    /// The KeyId of the author's identity, once `create_identity` has run.
15
    pub author_key: Option<KeyId>,
ff1a64f8 Andy McCall ↶ 16
    /// `author.signer` (or `$OXY_SIGNER`): the key that signs and seals in
17
    /// place of `author.key`, whose identity lists it as a delegate. An
18
    /// agent sets it to its own key, so its changes carry the human as
19
    /// author and the agent's signature (spec §14.2, agent workflows §2).
20
    pub signer_key: Option<KeyId>,
c895bc52 Andy McCall ↶ 21
    /// `author.signer = "keyless"`: sign with fresh keys the hub certifies
22
    /// under the agent's grant, keeping no key on disk (spec §14.7,
23
    /// oxygenhub#241).
24
    pub signer_keyless: bool,
25
    /// `keyless.hub`: the hub that certifies them (default: the default
26
    /// remote's hub).
27
    pub keyless_hub: Option<String>,
98286406 Andy McCall ↶ 28
    /// `core.sign`: sign every change at commit (`true`) or leave signing to
29
    /// push-time seals (`false`, the spec default).
30
    pub sign_changes: bool,
31
    pub bookmark: String,
32
    /// `remote.<name>.url`.
33
    pub remotes: BTreeMap<String, String>,
34
    /// `remote.<name>.token`: bearer token for HTTP pushes.
35
    pub remote_tokens: BTreeMap<String, String>,
2b5ee371 Andy McCall ↶ 36
    /// `remote.<name>.ca`: PEM file of extra roots to trust for that remote.
37
    pub remote_cas: BTreeMap<String, String>,
42ccf504 Andy McCall ↶ 38
    /// `core.remote`: the remote `pull`, `fetch` and `push` use when none is
39
    /// named, for a repository whose remotes include no `origin`.
40
    pub default_remote: Option<String>,
d6e0d5b8 Andy McCall ↶ 41
    /// `git.claim`: a Git import authors the commits whose Git author email
42
    /// is this repository's identity's with its key (oxygen#97).
43
    pub git_claim: bool,
ba160f60 Andy McCall ↶ 44
    /// `core.op-retention`: days for which operations keep their states reachable (spec §12.1, §21).
45
    pub op_retention_days: u64,
46
    /// `core.gc-grace`: days an unreachable object survives, protecting concurrent writers (spec §21).
47
    pub gc_grace_days: u64,
b689779e Andy McCall ↶ 48
    /// `core.snapshot-retention`: days for which past operations keep their
49
    /// working copies reachable (oxygen#98); each workspace's current one is
50
    /// always kept. A file only ever in a working copy goes with it.
51
    pub snapshot_retention_days: u64,
52
    /// `snapshot.max-new-file-size`: a new, untracked file larger than this
53
    /// is left out of working-copy snapshots and named instead (oxygen#98);
54
    /// 0 takes in files of any size.
55
    pub max_new_file: u64,
252e8cb2 Andy McCall ↶ 56
    /// `core.checkpoint-interval`: changes between automatic checkpoints (spec §11).
57
    pub checkpoint_interval: u64,
cb8b8082 Andy McCall ↶ 58
    /// `push.batch-bytes`: about how many bytes of changes and their texts
59
    /// one request of a long push carries (spec §16.3).
60
    pub push_batch_bytes: u64,
b3c5a59a Andy McCall ↶ 61
    /// `filter.max-blob`: blobs larger than this many bytes are fetched lazily (spec §17).
62
    pub max_blob: Option<u64>,
7421309f Andy McCall ↶ 63
    /// `filter.paths`: only these subtrees are materialized (spec §17); empty means everything.
64
    pub sparse_paths: Vec<String>,
6d3eefbc Andy McCall ↶ 65
    /// `hooks.semantic-deps`: run the semantic-deps hook at commit (spec §20).
66
    pub semantic_deps: bool,
47ce8b8e Andy McCall ↶ 67
    /// `revset.<name>`: a saved revset, used as `name()`.
68
    pub revsets: BTreeMap<String, String>,
d069701a Andy McCall ↶ 69
    /// `storage.url`: the store the repository's objects and operation
70
    /// head are kept on (`dir://`, `s3://`, …) instead of `.oxy/objects`
71
    /// (oxygen#68).
72
    pub storage_url: Option<String>,
73
    /// `storage.cache`: the most the local cache in front of that store
74
    /// holds, in bytes.
75
    pub storage_cache: u64,
98286406 Andy McCall ↶ 76
}
77
78
impl Config {
cbe8499a Andy McCall ↶ 79
    /// Whether the repository's own file sets `key` (as against the user
80
    /// config or a default).
81
    pub fn is_repo_set(&self, key: &str) -> bool {
82
        self.repo_layer.contains_key(key)
83
    }
84
98286406 Andy McCall ↶ 85
    pub(super) fn default_for_user() -> Config {
86
        let user = std::env::var("USER").unwrap_or_else(|_| "unknown".into());
87
        Config {
88
            repo_layer: BTreeMap::new(),
89
            author_name: std::env::var("OXY_AUTHOR_NAME").unwrap_or_else(|_| user.clone()),
75167672 Andy McCall 90
            author_email: std::env::var("OXY_AUTHOR_EMAIL")
91
                .unwrap_or_else(|_| format!("{user}@localhost")),
98286406 Andy McCall ↶ 92
            author_key: None,
ff1a64f8 Andy McCall ↶ 93
            signer_key: std::env::var("OXY_SIGNER")
94
                .ok()
95
                .and_then(|v| KeyId::from_hex(&v)),
c895bc52 Andy McCall ↶ 96
            signer_keyless: std::env::var("OXY_SIGNER").is_ok_and(|v| v == "keyless"),
97
            keyless_hub: None,
98286406 Andy McCall ↶ 98
            sign_changes: false,
99
            bookmark: "main".into(),
100
            remotes: BTreeMap::new(),
101
            remote_tokens: BTreeMap::new(),
2b5ee371 Andy McCall ↶ 102
            remote_cas: BTreeMap::new(),
42ccf504 Andy McCall ↶ 103
            default_remote: None,
d6e0d5b8 Andy McCall ↶ 104
            git_claim: false,
ba160f60 Andy McCall ↶ 105
            op_retention_days: 90,
106
            gc_grace_days: 14,
b689779e Andy McCall ↶ 107
            max_new_file: 100 << 20,
108
            snapshot_retention_days: 7,
252e8cb2 Andy McCall ↶ 109
            checkpoint_interval: 256,
d3b27090 Andy McCall ↶ 110
            // Well under the 100 MB a proxy in front of a hub may take
111
            // (oxygen#133).
112
            push_batch_bytes: 64 << 20,
b3c5a59a Andy McCall ↶ 113
            max_blob: None,
7421309f Andy McCall ↶ 114
            sparse_paths: Vec::new(),
6d3eefbc Andy McCall ↶ 115
            semantic_deps: false,
47ce8b8e Andy McCall ↶ 116
            revsets: BTreeMap::new(),
d069701a Andy McCall ↶ 117
            storage_url: None,
118
            storage_cache: DEFAULT_STORAGE_CACHE,
98286406 Andy McCall ↶ 119
        }
120
    }
121
122
    /// Set a key in the repository layer, as `oxy config` does.
123
    pub fn set(&mut self, key: &str, value: &str) -> Result<()> {
124
        self.apply(key, value)?;
125
        if value.is_empty() {
126
            self.repo_layer.remove(key);
127
        } else {
128
            self.repo_layer.insert(key.to_string(), value.to_string());
129
        }
130
        Ok(())
131
    }
132
133
    /// Set a key in the user file without touching any repository.
134
    pub fn set_user(user_path: &Path, key: &str, value: &str) -> Result<()> {
135
        Config::default_for_user().apply(key, value)?;
136
        let mut entries = read_config_file(user_path)?;
137
        if value.is_empty() {
138
            entries.remove(key);
139
        } else {
140
            entries.insert(key.to_string(), value.to_string());
141
        }
142
        if let Some(parent) = user_path.parent() {
143
            fs::create_dir_all(parent)?;
144
        }
145
        write_config_file(user_path, &entries)
146
    }
147
148
    /// Apply a key to the effective values.
149
    pub(super) fn apply(&mut self, key: &str, value: &str) -> Result<()> {
150
        match key {
151
            "author.name" => self.author_name = value.into(),
152
            "author.email" => self.author_email = value.into(),
153
            "author.key" => {
154
                self.author_key = if value.is_empty() {
155
                    None
156
                } else {
75167672 Andy McCall 157
                    Some(KeyId::from_hex(value).ok_or_else(|| {
158
                        RepoError::Msg("author.key must be a 64-character hex KeyId".into())
159
                    })?)
98286406 Andy McCall ↶ 160
                }
161
            }
ff1a64f8 Andy McCall ↶ 162
            "author.signer" => {
c895bc52 Andy McCall ↶ 163
                self.signer_keyless = value == "keyless";
164
                self.signer_key = if value.is_empty() || self.signer_keyless {
ff1a64f8 Andy McCall ↶ 165
                    None
166
                } else {
167
                    Some(KeyId::from_hex(value).ok_or_else(|| {
c895bc52 Andy McCall ↶ 168
                        RepoError::Msg(
169
                            "author.signer must be a 64-character hex KeyId, or \"keyless\"".into(),
170
                        )
ff1a64f8 Andy McCall ↶ 171
                    })?)
172
                }
173
            }
98286406 Andy McCall ↶ 174
            "core.sign" => {
175
                self.sign_changes = match value {
176
                    "change" => true,
c9635448 Andy McCall ↶ 177
                    // Empty: unset, the default.
178
                    "seal" | "" => false,
98286406 Andy McCall ↶ 179
                    _ => return fail("core.sign must be \"change\" or \"seal\""),
180
                }
181
            }
182
            "workspace.bookmark" => self.bookmark = value.into(),
c895bc52 Andy McCall ↶ 183
            "keyless.hub" => self.keyless_hub = (!value.is_empty()).then(|| value.to_string()),
ff1f62fe Andy McCall ↶ 184
            // Read by the credential store from the user file.
185
            "auth.helper" => {}
ba160f60 Andy McCall ↶ 186
            "core.op-retention" => self.op_retention_days = parse_days(value)?,
187
            "core.gc-grace" => self.gc_grace_days = parse_days(value)?,
b689779e Andy McCall ↶ 188
            "snapshot.max-new-file-size" => self.max_new_file = parse_size(value)?,
189
            "core.snapshot-retention" => self.snapshot_retention_days = parse_days(value)?,
75167672 Andy McCall 190
            "filter.max-blob" => {
191
                self.max_blob = if value.is_empty() {
192
                    None
193
                } else {
194
                    Some(parse_size(value)?)
195
                }
196
            }
d069701a Andy McCall ↶ 197
            "storage.url" => {
198
                self.storage_url = (!value.is_empty()).then(|| value.to_string());
199
            }
200
            "storage.cache" => {
201
                self.storage_cache = if value.is_empty() {
202
                    DEFAULT_STORAGE_CACHE
203
                } else {
204
                    parse_size(value)?
205
                }
206
            }
42ccf504 Andy McCall ↶ 207
            "core.remote" => {
208
                self.default_remote = (!value.is_empty()).then(|| value.to_string());
209
            }
6d3eefbc Andy McCall ↶ 210
            "hooks.semantic-deps" => self.semantic_deps = matches!(value, "true" | "1" | "yes"),
d6e0d5b8 Andy McCall ↶ 211
            "git.claim" => self.git_claim = matches!(value, "true" | "1" | "yes"),
7421309f Andy McCall ↶ 212
            "filter.paths" => {
213
                self.sparse_paths = value
214
                    .split(',')
75167672 Andy McCall 215
                    .map(|p| {
216
                        p.trim()
217
                            .trim_start_matches("./")
218
                            .trim_matches('/')
219
                            .to_string()
220
                    })
7421309f Andy McCall ↶ 221
                    .filter(|p| !p.is_empty())
222
                    .collect();
223
            }
252e8cb2 Andy McCall ↶ 224
            "core.checkpoint-interval" => {
75167672 Andy McCall 225
                self.checkpoint_interval = value
226
                    .parse()
227
                    .map_err(|_| RepoError::Msg(format!("expected a number, got {value:?}")))?;
252e8cb2 Andy McCall ↶ 228
            }
cb8b8082 Andy McCall ↶ 229
            "push.batch-bytes" => {
230
                self.push_batch_bytes = value.parse().ok().filter(|n| *n > 0).ok_or_else(|| {
231
                    RepoError::Msg(format!("expected a number of bytes, got {value:?}"))
232
                })?;
233
            }
2b5ee371 Andy McCall ↶ 234
            k if k.starts_with("remote.") => {
235
                let rest = &k["remote.".len()..];
236
                let Some((name, field)) = rest.rsplit_once('.') else {
237
                    return fail(format!("unknown configuration key {key:?}"));
98286406 Andy McCall ↶ 238
                };
239
                if name.is_empty() || name.contains('/') {
240
                    return fail(format!("invalid remote name in {key:?}"));
241
                }
2b5ee371 Andy McCall ↶ 242
                let map = match field {
243
                    "url" => &mut self.remotes,
244
                    "token" => &mut self.remote_tokens,
245
                    "ca" => &mut self.remote_cas,
246
                    _ => return fail(format!("unknown configuration key {key:?}")),
247
                };
98286406 Andy McCall ↶ 248
                if value.is_empty() {
249
                    map.remove(name);
2b5ee371 Andy McCall ↶ 250
                    if field == "url" {
98286406 Andy McCall ↶ 251
                        self.remote_tokens.remove(name);
2b5ee371 Andy McCall ↶ 252
                        self.remote_cas.remove(name);
98286406 Andy McCall ↶ 253
                    }
254
                } else {
255
                    map.insert(name.to_string(), value.into());
256
                }
257
            }
47ce8b8e Andy McCall ↶ 258
            k if k.starts_with("revset.") => {
259
                let name = &k["revset.".len()..];
260
                if name.is_empty()
261
                    || !name
262
                        .chars()
263
                        .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
264
                {
265
                    return fail(format!(
266
                        "a revset's name is letters, digits, _ and -: {key:?}"
267
                    ));
268
                }
269
                if value.is_empty() {
270
                    self.revsets.remove(name);
271
                } else {
272
                    crate::revset::parse(value)
273
                        .map_err(|e| RepoError::Msg(format!("{key} = {value:?}: {e}")))?;
274
                    self.revsets.insert(name.to_string(), value.into());
275
                }
276
            }
98286406 Andy McCall ↶ 277
            _ => return fail(format!("unknown configuration key {key:?}")),
278
        }
279
        Ok(())
280
    }
281
282
    /// Defaults, then the user file if it exists, then the repository file.
283
    pub(super) fn load(user_path: Option<&Path>, repo_path: &Path) -> Result<Config> {
284
        let mut c = Config::default_for_user();
285
        if let Some(u) = user_path {
286
            for (k, v) in read_config_file(u)? {
287
                let _ = c.apply(&k, &v);
288
            }
289
        }
290
        for (k, v) in read_config_file(repo_path)? {
291
            if c.apply(&k, &v).is_ok() {
292
                c.repo_layer.insert(k, v);
293
            }
294
        }
295
        Ok(c)
296
    }
297
298
    pub(super) fn save(&self, path: &Path) -> Result<()> {
299
        write_config_file(path, &self.repo_layer)
300
    }
301
302
    /// Keys explicitly set for this repository.
303
    pub fn repo_layer(&self) -> &BTreeMap<String, String> {
304
        &self.repo_layer
305
    }
306
}
307
b3c5a59a Andy McCall ↶ 308
/// A byte count with an optional `K`, `M`, or `G` suffix (binary units).
d069701a Andy McCall ↶ 309
/// `storage.cache` when unset: 2 GiB.
310
pub const DEFAULT_STORAGE_CACHE: u64 = 2 << 30;
311
b3c5a59a Andy McCall ↶ 312
pub fn parse_size(v: &str) -> Result<u64> {
313
    let v = v.trim();
314
    let (num, mult) = match v.chars().last() {
315
        Some('K' | 'k') => (&v[..v.len() - 1], 1u64 << 10),
316
        Some('M' | 'm') => (&v[..v.len() - 1], 1u64 << 20),
317
        Some('G' | 'g') => (&v[..v.len() - 1], 1u64 << 30),
318
        _ => (v, 1),
319
    };
75167672 Andy McCall 320
    let n: u64 = num
321
        .trim()
322
        .parse()
323
        .map_err(|_| RepoError::Msg(format!("expected a size like 1M, got {v:?}")))?;
b3c5a59a Andy McCall ↶ 324
    Ok(n * mult)
325
}
326
ba160f60 Andy McCall ↶ 327
/// A count of days, optionally suffixed `d`.
328
fn parse_days(v: &str) -> Result<u64> {
75167672 Andy McCall 329
    v.trim_end_matches('d')
330
        .parse()
331
        .map_err(|_| RepoError::Msg(format!("expected a number of days, got {v:?}")))
ba160f60 Andy McCall ↶ 332
}
333
98286406 Andy McCall ↶ 334
/// Read `key = "value"` lines; a missing file is empty.
335
pub fn read_config_file(path: &Path) -> Result<BTreeMap<String, String>> {
336
    let text = match fs::read_to_string(path) {
337
        Ok(t) => t,
338
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(BTreeMap::new()),
339
        Err(e) => return Err(e.into()),
340
    };
341
    let mut out = BTreeMap::new();
342
    for line in text.lines() {
343
        let line = line.trim();
344
        if line.is_empty() || line.starts_with('#') {
345
            continue;
346
        }
75167672 Andy McCall 347
        let Some((k, v)) = line.split_once('=') else {
348
            continue;
349
        };
98286406 Andy McCall ↶ 350
        out.insert(k.trim().to_string(), v.trim().trim_matches('"').to_string());
351
    }
352
    Ok(out)
353
}
354
355
fn write_config_file(path: &Path, entries: &BTreeMap<String, String>) -> Result<()> {
75167672 Andy McCall 356
    let text: String = entries
357
        .iter()
358
        .map(|(k, v)| format!("{k} = \"{v}\"\n"))
359
        .collect();
98286406 Andy McCall ↶ 360
    write_atomic(path, text.as_bytes())
361
}