OXYGENHUB
# 2b5ee371060e oxygen / crates / oxy-core / src / repo / mod.rs
20.3 KiB · Rust · last changed TLS: oxy serve --tls-cert/--tls-key via rustls; remote.<name>.ca and OXY_CA for self-hosted roots 1 month ago source blame history raw
1
//! A repository on disk: the `.oxy` directory, one workspace, the operation
2
//! log, and the working-copy change (spec §12, §13.1).
3
//!
4
//! Not yet implemented from the spec: trust policy (every remote is
5
//! "unverified", so unsigned pushes are accepted), packs, checkpoints,
6
//! multiple workspaces, and network transports (only local paths). Changes are signed individually when `core.sign` is
7
//! `change`; the default `seal` leaves them unsigned until push exists.
8
9
use crate::attest::{Attest, AttestBody};
10
use crate::diff::{ranges, split_lines, text_edits};
11
use crate::protocol::{open_remote, Bundle, RefResult, RefUpdate, RemoteOptions, RemoteRef, SignedAttest};
12
use crate::hash::{hash_object, ChangeHash, KeyId, ObjectRef, ObjectType};
13
use crate::identity::{verify as verify_signature, Domain, Identity, SecretKey, Signature};
14
use crate::ids::{ChangeId, NodeId, WorkspaceId};
15
use crate::objects::{Blob, Change, ChangeKind, Edit, Hlc, Mode, NodeKind, OpRef, StateObject, TreeOp};
16
use crate::oplog::{Format1Op, Op, View, SNAPSHOT_SUFFIX};
17
use crate::state::{FileContent, Snapshot, State};
18
use crate::store::{Store, StoreError};
19
use crate::tree::OpKey;
20
use rand::RngCore;
21
use std::collections::{BTreeMap, BTreeSet, HashMap};
22
use std::fs;
23
use std::io::Write;
24
use std::path::{Path, PathBuf};
25
26
pub const OXY_DIR: &str = ".oxy";
27
pub const FORMAT_LINE: &str = "oxy repository format 2\n";
28
const FORMAT1_LINE: &str = "oxy repository format 1\n";
29
const BINARY_SNIFF: usize = 8 * 1024;
30
31
#[derive(Debug, thiserror::Error)]
32
pub enum RepoError {
33
    #[error("io: {0}")]
34
    Io(#[from] std::io::Error),
35
    #[error(transparent)]
36
    Store(#[from] StoreError),
37
    #[error(transparent)]
38
    Object(#[from] crate::objects::ObjectError),
39
    #[error(transparent)]
40
    State(#[from] crate::state::StateError),
41
    #[error("{0}")]
42
    Msg(String),
43
}
44
45
pub type Result<T> = std::result::Result<T, RepoError>;
46
47
fn fail<T>(s: impl Into<String>) -> Result<T> {
48
    Err(RepoError::Msg(s.into()))
49
}
50
51
mod checkout;
52
mod config;
53
mod identity;
54
mod snapshot;
55
mod sync;
56
#[cfg(test)]
57
mod tests;
58
59
pub use checkout::CheckoutReport;
60
pub use config::{read_config_file, Config};
61
pub use identity::SignatureStatus;
62
pub use snapshot::{added_node_paths, has_conflict_markers, is_binary, referenced_changes, render_change, ChangeStatus, PathStatus, SnapshotResult};
63
pub use sync::PullReport;
64
65
fn write_atomic(path: &Path, data: &[u8]) -> Result<()> {
66
    let tmp = path.with_extension("tmp");
67
    {
68
        let mut f = fs::File::create(&tmp)?;
69
        f.write_all(data)?;
70
        f.sync_all()?;
71
    }
72
    fs::rename(&tmp, path)?;
73
    Ok(())
74
}
75
76
pub struct LogEntry {
77
    pub hash: ChangeHash,
78
    pub change: Change,
79
    pub is_working_copy: bool,
80
    pub is_head: bool,
81
}
82
83
struct FsEntry {
84
    kind: NodeKind,
85
    exec: bool,
86
    content: Vec<u8>,
87
}
88
89
pub struct Repo {
90
    root: PathBuf,
91
    oxy: PathBuf,
92
    store: Store,
93
    workspace: WorkspaceId,
94
    keys_dir: PathBuf,
95
    user_config: PathBuf,
96
    pub config: Config,
97
}
98
99
/// The user's oxy directory: `$OXY_HOME`, else `$XDG_CONFIG_HOME/oxy`, else
100
/// `~/.config/oxy`. It holds `config.toml` (spec §19) and `keys/`.
101
pub fn default_home() -> PathBuf {
102
    if let Ok(h) = std::env::var("OXY_HOME") {
103
        return PathBuf::from(h);
104
    }
105
    if let Ok(x) = std::env::var("XDG_CONFIG_HOME") {
106
        return PathBuf::from(x).join("oxy");
107
    }
108
    let home = std::env::var("HOME").unwrap_or_else(|_| ".".into());
109
    PathBuf::from(home).join(".config").join("oxy")
110
}
111
112
pub fn default_keys_dir() -> PathBuf {
113
    default_home().join("keys")
114
}
115
116
pub fn default_user_config() -> PathBuf {
117
    default_home().join("config.toml")
118
}
119
120
/// Where `create_identity` records the author.
121
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
122
pub enum IdentityScope {
123
    /// The user file: every repository of this user signs with it.
124
    User,
125
    /// This repository only.
126
    Repo,
127
}
128
129
impl Repo {
130
    // ------------------------------------------------------------ lifecycle
131
132
    pub fn init(root: &Path) -> Result<Repo> {
133
        let mut id = [0u8; 16];
134
        rand::rngs::OsRng.fill_bytes(&mut id);
135
        Self::init_with_id(root, id)
136
    }
137
138
    /// Initialize a repository with a given repository ID (clones share the
139
    /// ID of their origin, spec §14.4).
140
    pub fn init_with_id(root: &Path, id: [u8; 16]) -> Result<Repo> {
141
        fs::create_dir_all(root)?;
142
        let root = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());
143
        let oxy = root.join(OXY_DIR);
144
        if oxy.exists() {
145
            return fail(format!("{} is already an oxy repository", root.display()));
146
        }
147
        fs::create_dir_all(oxy.join("ops"))?;
148
        write_atomic(&oxy.join("format"), FORMAT_LINE.as_bytes())?;
149
        write_atomic(&oxy.join("id"), hex::encode(id).as_bytes())?;
150
        let config = Config::load(Some(&default_user_config()), &oxy.join("config"))?;
151
        config.save(&oxy.join("config"))?;
152
        let workspace = WorkspaceId::random();
153
        write_atomic(&oxy.join("workspace"), workspace.to_hex().as_bytes())?;
154
        let store = Store::open(oxy.join("objects"))?;
155
        let repo = Repo { root, oxy, store, workspace, keys_dir: default_keys_dir(), user_config: default_user_config(), config };
156
157
        let wc = repo.new_working_copy(vec![], "");
158
        let wc_hash = repo.store.put_change(&wc)?;
159
        let mut view = View::default();
160
        view.bookmarks.insert(repo.config.bookmark.clone(), repo.store.put_state(&StateObject::new(vec![]))?);
161
        view.workspaces.insert(workspace, wc_hash);
162
        repo.record_op(&[], "oxy init", view)?;
163
        Ok(repo)
164
    }
165
166
    /// Open the repository containing `start` (searching upwards).
167
    pub fn open(start: &Path) -> Result<Repo> {
168
        let start = start.canonicalize()?;
169
        let mut dir: Option<&Path> = Some(&start);
170
        while let Some(d) = dir {
171
            let oxy = d.join(OXY_DIR);
172
            if oxy.is_dir() {
173
                let format = fs::read_to_string(oxy.join("format"))?;
174
                let config = Config::load(Some(&default_user_config()), &oxy.join("config"))?;
175
                let workspace = WorkspaceId::from_hex(fs::read_to_string(oxy.join("workspace"))?.trim())
176
                    .ok_or_else(|| RepoError::Msg("corrupt .oxy/workspace".into()))?;
177
                let store = Store::open(oxy.join("objects"))?;
178
                let repo = Repo { root: d.to_path_buf(), oxy, store, workspace, keys_dir: default_keys_dir(), user_config: default_user_config(), config };
179
                if format == FORMAT1_LINE {
180
                    repo.migrate_format1()?;
181
                } else if format != FORMAT_LINE {
182
                    return fail(format!("unsupported repository format: {}", format.trim()));
183
                }
184
                return Ok(repo);
185
            }
186
            dir = d.parent();
187
        }
188
        fail(format!("no oxy repository found at or above {}", start.display()))
189
    }
190
191
    /// Rewrite a format-1 operation log, whose bookmarks were head lists,
192
    /// into format 2, where each bookmark names a `state` object.
193
    fn migrate_format1(&self) -> Result<()> {
194
        let hex = fs::read_to_string(self.oxy.join("ops").join("head"))?;
195
        let head = ObjectRef::from_hex(hex.trim()).ok_or_else(|| RepoError::Msg("corrupt .oxy/ops/head".into()))?;
196
        // Collect the chain root-first.
197
        let mut chain: Vec<Format1Op> = Vec::new();
198
        let mut cur = Some(head);
199
        while let Some(r) = cur {
200
            let op = Op::decode_format1(&self.store.get_typed(&r, ObjectType::Op)?)?;
201
            cur = op.parents.first().copied();
202
            chain.push(op);
203
        }
204
        chain.reverse();
205
        let mut new_parent: Option<ObjectRef> = None;
206
        for old in chain {
207
            let mut view = View::default();
208
            for (name, heads) in &old.view.bookmarks {
209
                view.bookmarks.insert(name.clone(), self.store.put_state(&StateObject::new(heads.clone()))?);
210
            }
211
            view.workspaces = old.view.workspaces.clone();
212
            let op = Op { parents: new_parent.into_iter().collect(), hlc: old.hlc, command: old.command, view };
213
            new_parent = Some(self.store.put(ObjectType::Op, &op.encode())?);
214
        }
215
        write_atomic(&self.oxy.join("ops").join("head"), new_parent.unwrap().to_hex().as_bytes())?;
216
        write_atomic(&self.oxy.join("format"), FORMAT_LINE.as_bytes())?;
217
        Ok(())
218
    }
219
220
    /// The repository ID (spec §14.4). Repositories made before IDs existed get one on first use.
221
    pub fn id(&self) -> Result<[u8; 16]> {
222
        let path = self.oxy.join("id");
223
        match fs::read_to_string(&path) {
224
            Ok(h) => hex::decode(h.trim()).ok().and_then(|v| v.try_into().ok()).ok_or_else(|| RepoError::Msg("corrupt .oxy/id".into())),
225
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
226
                let mut id = [0u8; 16];
227
                rand::rngs::OsRng.fill_bytes(&mut id);
228
                write_atomic(&path, hex::encode(id).as_bytes())?;
229
                Ok(id)
230
            }
231
            Err(e) => Err(e.into()),
232
        }
233
    }
234
235
    pub fn root(&self) -> &Path {
236
        &self.root
237
    }
238
    pub fn store(&self) -> &Store {
239
        &self.store
240
    }
241
    pub fn workspace(&self) -> WorkspaceId {
242
        self.workspace
243
    }
244
245
    /// Use a different user directory for keys and the user config file
246
    /// (tests, or `$OXY_HOME`). Reloads the effective configuration.
247
    pub fn with_home(mut self, dir: impl Into<PathBuf>) -> Repo {
248
        let dir = dir.into();
249
        self.keys_dir = dir.join("keys");
250
        self.user_config = dir.join("config.toml");
251
        if let Ok(c) = Config::load(Some(&self.user_config), &self.oxy.join("config")) {
252
            self.config = c;
253
        }
254
        self
255
    }
256
257
    pub fn user_config_path(&self) -> &Path {
258
        &self.user_config
259
    }
260
261
    /// Set a key in the user file and refresh the effective configuration.
262
    pub fn set_user_config(&mut self, key: &str, value: &str) -> Result<()> {
263
        Config::set_user(&self.user_config, key, value)?;
264
        self.config = Config::load(Some(&self.user_config), &self.oxy.join("config"))?;
265
        Ok(())
266
    }
267
268
    pub fn save_config(&self) -> Result<()> {
269
        self.config.save(&self.oxy.join("config"))
270
    }
271
272
    fn author(&self) -> KeyId {
273
        self.config.author_key.unwrap_or(ObjectRef::ZERO)
274
    }
275
}
276
277
impl Repo {
278
    fn new_working_copy(&self, deps: Vec<ChangeHash>, message: &str) -> Change {
279
        let mut c = Change {
280
            change_id: ChangeId::random(),
281
            deps,
282
            author: self.author(),
283
            hlc: Hlc::now(),
284
            message: message.into(),
285
            tree_ops: vec![],
286
            edits: vec![],
287
            kind: ChangeKind::WorkingCopy,
288
        };
289
        c.normalize();
290
        c
291
    }
292
293
    // ------------------------------------------------------------ operation log
294
295
    pub fn head_op(&self) -> Result<(ObjectRef, Op)> {
296
        let hex = fs::read_to_string(self.oxy.join("ops").join("head"))?;
297
        let r = ObjectRef::from_hex(hex.trim()).ok_or_else(|| RepoError::Msg("corrupt .oxy/ops/head".into()))?;
298
        let op = Op::decode(&self.store.get_typed(&r, ObjectType::Op)?)?;
299
        Ok((r, op))
300
    }
301
302
    pub fn view(&self) -> Result<View> {
303
        Ok(self.head_op()?.1.view)
304
    }
305
306
    fn record_op(&self, parents: &[ObjectRef], command: &str, view: View) -> Result<ObjectRef> {
307
        let op = Op { parents: parents.to_vec(), hlc: Hlc::now(), command: command.into(), view };
308
        let r = self.store.put(ObjectType::Op, &op.encode())?;
309
        write_atomic(&self.oxy.join("ops").join("head"), r.to_hex().as_bytes())?;
310
        Ok(r)
311
    }
312
313
    pub fn op_log(&self) -> Result<Vec<(ObjectRef, Op)>> {
314
        let mut out = Vec::new();
315
        let (mut r, mut op) = self.head_op()?;
316
        loop {
317
            let next = op.parents.first().copied();
318
            out.push((r, op));
319
            match next {
320
                Some(p) => {
321
                    r = p;
322
                    op = Op::decode(&self.store.get_typed(&r, ObjectType::Op)?)?;
323
                }
324
                None => break,
325
            }
326
        }
327
        Ok(out)
328
    }
329
330
    /// Undo the last user command (spec §12.1). Operations that only recorded
331
    /// a working-copy snapshot are skipped, since the files are still on disk
332
    /// and the next snapshot recreates them.
333
    pub fn undo(&self) -> Result<Op> {
334
        let (_, mut op) = self.head_op()?;
335
        while op.is_snapshot() {
336
            let Some(parent) = op.parents.first() else { break };
337
            op = Op::decode(&self.store.get_typed(parent, ObjectType::Op)?)?;
338
        }
339
        let Some(parent) = op.parents.first() else {
340
            return fail("nothing to undo");
341
        };
342
        let parent_op = Op::decode(&self.store.get_typed(parent, ObjectType::Op)?)?;
343
        write_atomic(&self.oxy.join("ops").join("head"), parent.to_hex().as_bytes())?;
344
        Ok(parent_op)
345
    }
346
347
    /// The last operation that was not a snapshot.
348
    pub fn last_command(&self) -> Result<Op> {
349
        let (_, mut op) = self.head_op()?;
350
        while op.is_snapshot() {
351
            let Some(parent) = op.parents.first() else { break };
352
            op = Op::decode(&self.store.get_typed(parent, ObjectType::Op)?)?;
353
        }
354
        Ok(op)
355
    }
356
357
    // ------------------------------------------------------------ states
358
359
    /// Load the dependency closure of `heads` from the store.
360
    pub fn load_state(&self, heads: &[ChangeHash]) -> Result<State> {
361
        let mut state = State::new();
362
        let mut stack: Vec<ChangeHash> = heads.to_vec();
363
        let mut seen: BTreeSet<ChangeHash> = BTreeSet::new();
364
        while let Some(h) = stack.pop() {
365
            if !seen.insert(h) {
366
                continue;
367
            }
368
            let c = self.store.get_change(&h)?;
369
            stack.extend(c.deps.iter().copied());
370
            state.add_unchecked(c);
371
        }
372
        state.validate()?;
373
        Ok(state)
374
    }
375
376
    pub fn working_copy(&self) -> Result<(ChangeHash, Change)> {
377
        let view = self.view()?;
378
        let h = *view
379
            .workspaces
380
            .get(&self.workspace)
381
            .ok_or_else(|| RepoError::Msg("this workspace is not in the current view".into()))?;
382
        Ok((h, self.store.get_change(&h)?))
383
    }
384
385
    /// Heads of a bookmark's state (spec §10.1).
386
    pub fn heads_of(&self, view: &View, bookmark: &str) -> Result<Vec<ChangeHash>> {
387
        match view.bookmarks.get(bookmark) {
388
            Some(st) => Ok(self.store.get_state(st)?.heads),
389
            None => Ok(vec![]),
390
        }
391
    }
392
393
    pub fn bookmark_heads(&self) -> Result<Vec<ChangeHash>> {
394
        self.heads_of(&self.view()?, &self.config.bookmark)
395
    }
396
397
    // ------------------------------------------------------------ blobs
398
399
    fn blob_for(content: &[u8]) -> Blob {
400
        Blob {
401
            length: content.len() as u64,
402
            chunks: crate::chunk::chunks(content).into_iter().map(|c| hash_object(ObjectType::Chunk, c)).collect(),
403
        }
404
    }
405
406
    pub fn blob_ref(content: &[u8]) -> ObjectRef {
407
        Self::blob_for(content).hash()
408
    }
409
410
    pub fn store_blob(&self, content: &[u8]) -> Result<ObjectRef> {
411
        for c in crate::chunk::chunks(content) {
412
            self.store.put_chunk(c)?;
413
        }
414
        Ok(self.store.put(ObjectType::Blob, &Self::blob_for(content).encode())?)
415
    }
416
417
    pub fn read_blob(&self, r: &ObjectRef) -> Result<Vec<u8>> {
418
        let blob = Blob::decode(&self.store.get_typed(r, ObjectType::Blob)?)?;
419
        let mut out = Vec::with_capacity(blob.length as usize);
420
        for c in &blob.chunks {
421
            out.extend(self.store.get_typed(c, ObjectType::Chunk)?);
422
        }
423
        Ok(out)
424
    }
425
426
    // ------------------------------------------------------------ working copy
427
428
    fn walk_working_copy(&self) -> Result<BTreeMap<String, FsEntry>> {
429
        let mut wb = ignore::WalkBuilder::new(&self.root);
430
        wb.hidden(false)
431
            .git_ignore(false)
432
            .git_global(false)
433
            .git_exclude(false)
434
            .require_git(false)
435
            .follow_links(false)
436
            .add_custom_ignore_filename(".oxyignore");
437
        wb.filter_entry(|e| e.file_name() != OXY_DIR);
438
        let mut out = BTreeMap::new();
439
        for entry in wb.build() {
440
            let entry = entry.map_err(|e| RepoError::Msg(e.to_string()))?;
441
            let path = entry.path();
442
            if path == self.root {
443
                continue;
444
            }
445
            let rel = path.strip_prefix(&self.root).unwrap();
446
            let rel: String = rel.components().map(|c| c.as_os_str().to_string_lossy().to_string()).collect::<Vec<_>>().join("/");
447
            let Some(ft) = entry.file_type() else { continue };
448
            if ft.is_dir() {
449
                continue;
450
            }
451
            if ft.is_symlink() {
452
                let target = fs::read_link(path)?;
453
                out.insert(rel, FsEntry { kind: NodeKind::Symlink, exec: false, content: target.to_string_lossy().as_bytes().to_vec() });
454
            } else if ft.is_file() {
455
                let exec = is_executable(&entry.metadata().map_err(|e| RepoError::Msg(e.to_string()))?);
456
                out.insert(rel, FsEntry { kind: NodeKind::File, exec, content: fs::read(path)? });
457
            }
458
        }
459
        Ok(out)
460
    }
461
}
462
463
impl Repo {
464
    /// Changes reachable from the bookmark and the working copy, newest first.
465
    pub fn log(&self) -> Result<Vec<LogEntry>> {
466
        let (wc_hash, _) = self.working_copy()?;
467
        let mut heads = self.bookmark_heads()?;
468
        heads.push(wc_hash);
469
        let state = self.load_state(&heads)?;
470
        let head_set: BTreeSet<ChangeHash> = self.bookmark_heads()?.into_iter().collect();
471
        let mut entries: Vec<LogEntry> = state
472
            .changes()
473
            .map(|(h, c)| LogEntry { hash: *h, change: c.clone(), is_working_copy: *h == wc_hash, is_head: head_set.contains(h) })
474
            .collect();
475
        entries.sort_by(|a, b| b.is_working_copy.cmp(&a.is_working_copy).then(b.change.hlc.cmp(&a.change.hlc)).then(b.hash.cmp(&a.hash)));
476
        Ok(entries)
477
    }
478
479
    /// Resolve a change by ChangeId prefix, `#hash` prefix, `@` (working copy), or bookmark.
480
    pub fn resolve_change(&self, spec: &str) -> Result<ChangeHash> {
481
        if spec == "@" {
482
            return Ok(self.working_copy()?.0);
483
        }
484
        if spec.is_empty() || spec == "#" {
485
            return fail("empty change specifier");
486
        }
487
        let entries = self.log()?;
488
        let matches: Vec<&LogEntry> = if let Some(hp) = spec.strip_prefix('#') {
489
            entries.iter().filter(|e| e.hash.starts_with_hex(hp)).collect()
490
        } else {
491
            entries.iter().filter(|e| e.change.change_id.starts_with_hex(spec)).collect()
492
        };
493
        match matches.len() {
494
            1 => Ok(matches[0].hash),
495
            0 => fail(format!("no change matches {spec:?}")),
496
            _ => {
497
                let ids: BTreeSet<ChangeId> = matches.iter().map(|e| e.change.change_id).collect();
498
                if ids.len() == 1 {
499
                    fail(format!("change {spec} is divergent: {} variants", matches.len()))
500
                } else {
501
                    fail(format!("{spec:?} is ambiguous"))
502
                }
503
            }
504
        }
505
    }
506
507
    /// Human-readable description of what a change does, with line content.
508
    pub fn describe_change(&self, hash: &ChangeHash) -> Result<String> {
509
        let change = self.store.get_change(hash)?;
510
        let base = self.load_state(&change.deps)?;
511
        let base_snapshot = base.materialize();
512
        Ok(render_change(&change, &base, &base_snapshot))
513
    }
514
}
515
516
fn remove_any(path: &Path) -> std::io::Result<()> {
517
    let m = fs::symlink_metadata(path)?;
518
    if m.is_dir() { fs::remove_dir_all(path) } else { fs::remove_file(path) }
519
}
520
521
#[cfg(unix)]
522
fn set_executable(path: &Path, exec: bool) -> std::io::Result<()> {
523
    use std::os::unix::fs::PermissionsExt;
524
    let mut perms = fs::metadata(path)?.permissions();
525
    let mode = perms.mode();
526
    let want = if exec { mode | 0o111 } else { mode & !0o111 };
527
    if want != mode {
528
        perms.set_mode(want);
529
        fs::set_permissions(path, perms)?;
530
    }
531
    Ok(())
532
}
533
#[cfg(not(unix))]
534
fn set_executable(_path: &Path, _exec: bool) -> std::io::Result<()> {
535
    Ok(())
536
}
537
538
#[cfg(unix)]
539
fn is_executable(m: &fs::Metadata) -> bool {
540
    use std::os::unix::fs::PermissionsExt;
541
    m.permissions().mode() & 0o111 != 0
542
}
543
#[cfg(not(unix))]
544
fn is_executable(_m: &fs::Metadata) -> bool {
545
    false
546
}
547
548
fn op_ref(k: OpKey) -> OpRef {
549
    OpRef { change: k.0, index: k.1 }
550
}
551
552
fn split_path(path: &str) -> (&str, &str) {
553
    match path.rfind('/') {
554
        Some(i) => (&path[..i], &path[i + 1..]),
555
        None => ("", path),
556
    }
557
}