OXYGENHUB
# abaa8d517631 oxygen / crates / oxy-core / src / repo / mod.rs
20.6 KiB · Rust · last changed Windows: oxy's home in APPDATA 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, 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
    // Windows has no HOME as a rule: the roaming profile's application
109
    // data, as Windows programs keep theirs.
110
    #[cfg(windows)]
111
    if let Some(d) = std::env::var_os("APPDATA").or_else(|| std::env::var_os("USERPROFILE")) {
112
        return PathBuf::from(d).join("oxy");
113
    }
114
    let home = std::env::var("HOME").unwrap_or_else(|_| ".".into());
115
    PathBuf::from(home).join(".config").join("oxy")
116
}
117
118
pub fn default_keys_dir() -> PathBuf {
119
    default_home().join("keys")
120
}
121
122
pub fn default_user_config() -> PathBuf {
123
    default_home().join("config.toml")
124
}
125
126
/// Where `create_identity` records the author.
127
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
128
pub enum IdentityScope {
129
    /// The user file: every repository of this user signs with it.
130
    User,
131
    /// This repository only.
132
    Repo,
133
}
134
135
impl Repo {
136
    // ------------------------------------------------------------ lifecycle
137
138
    pub fn init(root: &Path) -> Result<Repo> {
139
        let mut id = [0u8; 16];
140
        rand::rngs::OsRng.fill_bytes(&mut id);
141
        Self::init_with_id(root, id)
142
    }
143
144
    /// Initialize a repository with a given repository ID (clones share the
145
    /// ID of their origin, spec §14.4).
146
    pub fn init_with_id(root: &Path, id: [u8; 16]) -> Result<Repo> {
147
        fs::create_dir_all(root)?;
148
        let root = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());
149
        let oxy = root.join(OXY_DIR);
150
        if oxy.exists() {
151
            return fail(format!("{} is already an oxy repository", root.display()));
152
        }
153
        fs::create_dir_all(oxy.join("ops"))?;
154
        write_atomic(&oxy.join("format"), FORMAT_LINE.as_bytes())?;
155
        write_atomic(&oxy.join("id"), hex::encode(id).as_bytes())?;
156
        let config = Config::load(Some(&default_user_config()), &oxy.join("config"))?;
157
        config.save(&oxy.join("config"))?;
158
        let workspace = WorkspaceId::random();
159
        write_atomic(&oxy.join("workspace"), workspace.to_hex().as_bytes())?;
160
        let store = Store::open(oxy.join("objects"))?;
161
        let repo = Repo { root, oxy, store, workspace, keys_dir: default_keys_dir(), user_config: default_user_config(), config };
162
163
        let wc = repo.new_working_copy(vec![], "");
164
        let wc_hash = repo.store.put_change(&wc)?;
165
        let mut view = View::default();
166
        view.bookmarks.insert(repo.config.bookmark.clone(), repo.store.put_state(&StateObject::new(vec![]))?);
167
        view.workspaces.insert(workspace, wc_hash);
168
        repo.record_op(&[], "oxy init", view)?;
169
        Ok(repo)
170
    }
171
172
    /// Open the repository containing `start` (searching upwards).
173
    pub fn open(start: &Path) -> Result<Repo> {
174
        let start = start.canonicalize()?;
175
        let mut dir: Option<&Path> = Some(&start);
176
        while let Some(d) = dir {
177
            let oxy = d.join(OXY_DIR);
178
            if oxy.is_dir() {
179
                let format = fs::read_to_string(oxy.join("format"))?;
180
                let config = Config::load(Some(&default_user_config()), &oxy.join("config"))?;
181
                let workspace = WorkspaceId::from_hex(fs::read_to_string(oxy.join("workspace"))?.trim())
182
                    .ok_or_else(|| RepoError::Msg("corrupt .oxy/workspace".into()))?;
183
                let store = Store::open(oxy.join("objects"))?;
184
                let repo = Repo { root: d.to_path_buf(), oxy, store, workspace, keys_dir: default_keys_dir(), user_config: default_user_config(), config };
185
                if format == FORMAT1_LINE {
186
                    repo.migrate_format1()?;
187
                } else if format != FORMAT_LINE {
188
                    return fail(format!("unsupported repository format: {}", format.trim()));
189
                }
190
                return Ok(repo);
191
            }
192
            dir = d.parent();
193
        }
194
        fail(format!("no oxy repository found at or above {}", start.display()))
195
    }
196
197
    /// Rewrite a format-1 operation log, whose bookmarks were head lists,
198
    /// into format 2, where each bookmark names a `state` object.
199
    fn migrate_format1(&self) -> Result<()> {
200
        let hex = fs::read_to_string(self.oxy.join("ops").join("head"))?;
201
        let head = ObjectRef::from_hex(hex.trim()).ok_or_else(|| RepoError::Msg("corrupt .oxy/ops/head".into()))?;
202
        // Collect the chain root-first.
203
        let mut chain: Vec<Format1Op> = Vec::new();
204
        let mut cur = Some(head);
205
        while let Some(r) = cur {
206
            let op = Op::decode_format1(&self.store.get_typed(&r, ObjectType::Op)?)?;
207
            cur = op.parents.first().copied();
208
            chain.push(op);
209
        }
210
        chain.reverse();
211
        let mut new_parent: Option<ObjectRef> = None;
212
        for old in chain {
213
            let mut view = View::default();
214
            for (name, heads) in &old.view.bookmarks {
215
                view.bookmarks.insert(name.clone(), self.store.put_state(&StateObject::new(heads.clone()))?);
216
            }
217
            view.workspaces = old.view.workspaces.clone();
218
            let op = Op { parents: new_parent.into_iter().collect(), hlc: old.hlc, command: old.command, view };
219
            new_parent = Some(self.store.put(ObjectType::Op, &op.encode())?);
220
        }
221
        write_atomic(&self.oxy.join("ops").join("head"), new_parent.unwrap().to_hex().as_bytes())?;
222
        write_atomic(&self.oxy.join("format"), FORMAT_LINE.as_bytes())?;
223
        Ok(())
224
    }
225
226
    /// The repository ID (spec §14.4). Repositories made before IDs existed get one on first use.
227
    pub fn id(&self) -> Result<[u8; 16]> {
228
        let path = self.oxy.join("id");
229
        match fs::read_to_string(&path) {
230
            Ok(h) => hex::decode(h.trim()).ok().and_then(|v| v.try_into().ok()).ok_or_else(|| RepoError::Msg("corrupt .oxy/id".into())),
231
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
232
                let mut id = [0u8; 16];
233
                rand::rngs::OsRng.fill_bytes(&mut id);
234
                write_atomic(&path, hex::encode(id).as_bytes())?;
235
                Ok(id)
236
            }
237
            Err(e) => Err(e.into()),
238
        }
239
    }
240
241
    pub fn root(&self) -> &Path {
242
        &self.root
243
    }
244
    pub fn store(&self) -> &Store {
245
        &self.store
246
    }
247
    pub fn workspace(&self) -> WorkspaceId {
248
        self.workspace
249
    }
250
251
    /// Use a different user directory for keys and the user config file
252
    /// (tests, or `$OXY_HOME`). Reloads the effective configuration.
253
    pub fn with_home(mut self, dir: impl Into<PathBuf>) -> Repo {
254
        let dir = dir.into();
255
        self.keys_dir = dir.join("keys");
256
        self.user_config = dir.join("config.toml");
257
        if let Ok(c) = Config::load(Some(&self.user_config), &self.oxy.join("config")) {
258
            self.config = c;
259
        }
260
        self
261
    }
262
263
    pub fn user_config_path(&self) -> &Path {
264
        &self.user_config
265
    }
266
267
    /// Set a key in the user file and refresh the effective configuration.
268
    pub fn set_user_config(&mut self, key: &str, value: &str) -> Result<()> {
269
        Config::set_user(&self.user_config, key, value)?;
270
        self.config = Config::load(Some(&self.user_config), &self.oxy.join("config"))?;
271
        Ok(())
272
    }
273
274
    pub fn save_config(&self) -> Result<()> {
275
        self.config.save(&self.oxy.join("config"))
276
    }
277
278
    fn author(&self) -> KeyId {
279
        self.config.author_key.unwrap_or(ObjectRef::ZERO)
280
    }
281
}
282
283
impl Repo {
284
    fn new_working_copy(&self, deps: Vec<ChangeHash>, message: &str) -> Change {
285
        let mut c = Change {
286
            change_id: ChangeId::random(),
287
            deps,
288
            author: self.author(),
289
            hlc: Hlc::now(),
290
            message: message.into(),
291
            tree_ops: vec![],
292
            edits: vec![],
293
            kind: ChangeKind::WorkingCopy,
294
        };
295
        c.normalize();
296
        c
297
    }
298
299
    // ------------------------------------------------------------ operation log
300
301
    pub fn head_op(&self) -> Result<(ObjectRef, Op)> {
302
        let hex = fs::read_to_string(self.oxy.join("ops").join("head"))?;
303
        let r = ObjectRef::from_hex(hex.trim()).ok_or_else(|| RepoError::Msg("corrupt .oxy/ops/head".into()))?;
304
        let op = Op::decode(&self.store.get_typed(&r, ObjectType::Op)?)?;
305
        Ok((r, op))
306
    }
307
308
    pub fn view(&self) -> Result<View> {
309
        Ok(self.head_op()?.1.view)
310
    }
311
312
    fn record_op(&self, parents: &[ObjectRef], command: &str, view: View) -> Result<ObjectRef> {
313
        let op = Op { parents: parents.to_vec(), hlc: Hlc::now(), command: command.into(), view };
314
        let r = self.store.put(ObjectType::Op, &op.encode())?;
315
        write_atomic(&self.oxy.join("ops").join("head"), r.to_hex().as_bytes())?;
316
        Ok(r)
317
    }
318
319
    pub fn op_log(&self) -> Result<Vec<(ObjectRef, Op)>> {
320
        let mut out = Vec::new();
321
        let (mut r, mut op) = self.head_op()?;
322
        loop {
323
            let next = op.parents.first().copied();
324
            out.push((r, op));
325
            match next {
326
                Some(p) => {
327
                    r = p;
328
                    op = Op::decode(&self.store.get_typed(&r, ObjectType::Op)?)?;
329
                }
330
                None => break,
331
            }
332
        }
333
        Ok(out)
334
    }
335
336
    /// Undo the last user command (spec §12.1). Operations that only recorded
337
    /// a working-copy snapshot are skipped, since the files are still on disk
338
    /// and the next snapshot recreates them.
339
    pub fn undo(&self) -> Result<Op> {
340
        let (_, mut op) = self.head_op()?;
341
        while op.is_snapshot() {
342
            let Some(parent) = op.parents.first() else { break };
343
            op = Op::decode(&self.store.get_typed(parent, ObjectType::Op)?)?;
344
        }
345
        let Some(parent) = op.parents.first() else {
346
            return fail("nothing to undo");
347
        };
348
        let parent_op = Op::decode(&self.store.get_typed(parent, ObjectType::Op)?)?;
349
        write_atomic(&self.oxy.join("ops").join("head"), parent.to_hex().as_bytes())?;
350
        Ok(parent_op)
351
    }
352
353
    /// The last operation that was not a snapshot.
354
    pub fn last_command(&self) -> Result<Op> {
355
        let (_, mut op) = self.head_op()?;
356
        while op.is_snapshot() {
357
            let Some(parent) = op.parents.first() else { break };
358
            op = Op::decode(&self.store.get_typed(parent, ObjectType::Op)?)?;
359
        }
360
        Ok(op)
361
    }
362
363
    // ------------------------------------------------------------ states
364
365
    /// Load the dependency closure of `heads` from the store.
366
    pub fn load_state(&self, heads: &[ChangeHash]) -> Result<State> {
367
        let mut state = State::new();
368
        let mut stack: Vec<ChangeHash> = heads.to_vec();
369
        let mut seen: BTreeSet<ChangeHash> = BTreeSet::new();
370
        while let Some(h) = stack.pop() {
371
            if !seen.insert(h) {
372
                continue;
373
            }
374
            let c = self.store.get_change(&h)?;
375
            stack.extend(c.deps.iter().copied());
376
            state.add_unchecked(c);
377
        }
378
        state.validate()?;
379
        Ok(state)
380
    }
381
382
    pub fn working_copy(&self) -> Result<(ChangeHash, Change)> {
383
        let view = self.view()?;
384
        let h = *view
385
            .workspaces
386
            .get(&self.workspace)
387
            .ok_or_else(|| RepoError::Msg("this workspace is not in the current view".into()))?;
388
        Ok((h, self.store.get_change(&h)?))
389
    }
390
391
    /// Heads of a bookmark's state (spec §10.1).
392
    pub fn heads_of(&self, view: &View, bookmark: &str) -> Result<Vec<ChangeHash>> {
393
        match view.bookmarks.get(bookmark) {
394
            Some(st) => Ok(self.store.get_state(st)?.heads),
395
            None => Ok(vec![]),
396
        }
397
    }
398
399
    pub fn bookmark_heads(&self) -> Result<Vec<ChangeHash>> {
400
        self.heads_of(&self.view()?, &self.config.bookmark)
401
    }
402
403
    // ------------------------------------------------------------ blobs
404
405
    fn blob_for(content: &[u8]) -> Blob {
406
        Blob {
407
            length: content.len() as u64,
408
            chunks: crate::chunk::chunks(content).into_iter().map(|c| hash_object(ObjectType::Chunk, c)).collect(),
409
        }
410
    }
411
412
    pub fn blob_ref(content: &[u8]) -> ObjectRef {
413
        Self::blob_for(content).hash()
414
    }
415
416
    pub fn store_blob(&self, content: &[u8]) -> Result<ObjectRef> {
417
        for c in crate::chunk::chunks(content) {
418
            self.store.put_chunk(c)?;
419
        }
420
        Ok(self.store.put(ObjectType::Blob, &Self::blob_for(content).encode())?)
421
    }
422
423
    pub fn read_blob(&self, r: &ObjectRef) -> Result<Vec<u8>> {
424
        let blob = Blob::decode(&self.store.get_typed(r, ObjectType::Blob)?)?;
425
        let mut out = Vec::with_capacity(blob.length as usize);
426
        for c in &blob.chunks {
427
            out.extend(self.store.get_typed(c, ObjectType::Chunk)?);
428
        }
429
        Ok(out)
430
    }
431
432
    // ------------------------------------------------------------ working copy
433
434
    fn walk_working_copy(&self) -> Result<BTreeMap<String, FsEntry>> {
435
        let mut wb = ignore::WalkBuilder::new(&self.root);
436
        wb.hidden(false)
437
            .git_ignore(false)
438
            .git_global(false)
439
            .git_exclude(false)
440
            .require_git(false)
441
            .follow_links(false)
442
            .add_custom_ignore_filename(".oxyignore");
443
        wb.filter_entry(|e| e.file_name() != OXY_DIR);
444
        let mut out = BTreeMap::new();
445
        for entry in wb.build() {
446
            let entry = entry.map_err(|e| RepoError::Msg(e.to_string()))?;
447
            let path = entry.path();
448
            if path == self.root {
449
                continue;
450
            }
451
            let rel = path.strip_prefix(&self.root).unwrap();
452
            let rel: String = rel.components().map(|c| c.as_os_str().to_string_lossy().to_string()).collect::<Vec<_>>().join("/");
453
            let Some(ft) = entry.file_type() else { continue };
454
            if ft.is_dir() {
455
                continue;
456
            }
457
            if ft.is_symlink() {
458
                let target = fs::read_link(path)?;
459
                out.insert(rel, FsEntry { kind: NodeKind::Symlink, exec: false, content: target.to_string_lossy().as_bytes().to_vec() });
460
            } else if ft.is_file() {
461
                let exec = is_executable(&entry.metadata().map_err(|e| RepoError::Msg(e.to_string()))?);
462
                out.insert(rel, FsEntry { kind: NodeKind::File, exec, content: fs::read(path)? });
463
            }
464
        }
465
        Ok(out)
466
    }
467
}
468
469
impl Repo {
470
    /// Changes reachable from the bookmark and the working copy, newest first.
471
    pub fn log(&self) -> Result<Vec<LogEntry>> {
472
        let (wc_hash, _) = self.working_copy()?;
473
        let mut heads = self.bookmark_heads()?;
474
        heads.push(wc_hash);
475
        let state = self.load_state(&heads)?;
476
        let head_set: BTreeSet<ChangeHash> = self.bookmark_heads()?.into_iter().collect();
477
        let mut entries: Vec<LogEntry> = state
478
            .changes()
479
            .map(|(h, c)| LogEntry { hash: *h, change: c.clone(), is_working_copy: *h == wc_hash, is_head: head_set.contains(h) })
480
            .collect();
481
        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)));
482
        Ok(entries)
483
    }
484
485
    /// Resolve a change by ChangeId prefix, `#hash` prefix, `@` (working copy), or bookmark.
486
    pub fn resolve_change(&self, spec: &str) -> Result<ChangeHash> {
487
        if spec == "@" {
488
            return Ok(self.working_copy()?.0);
489
        }
490
        if spec.is_empty() || spec == "#" {
491
            return fail("empty change specifier");
492
        }
493
        let entries = self.log()?;
494
        let matches: Vec<&LogEntry> = if let Some(hp) = spec.strip_prefix('#') {
495
            entries.iter().filter(|e| e.hash.starts_with_hex(hp)).collect()
496
        } else {
497
            entries.iter().filter(|e| e.change.change_id.starts_with_hex(spec)).collect()
498
        };
499
        match matches.len() {
500
            1 => Ok(matches[0].hash),
501
            0 => fail(format!("no change matches {spec:?}")),
502
            _ => {
503
                let ids: BTreeSet<ChangeId> = matches.iter().map(|e| e.change.change_id).collect();
504
                if ids.len() == 1 {
505
                    fail(format!("change {spec} is divergent: {} variants", matches.len()))
506
                } else {
507
                    fail(format!("{spec:?} is ambiguous"))
508
                }
509
            }
510
        }
511
    }
512
513
    /// Human-readable description of what a change does, with line content.
514
    pub fn describe_change(&self, hash: &ChangeHash) -> Result<String> {
515
        let change = self.store.get_change(hash)?;
516
        let base = self.load_state(&change.deps)?;
517
        let base_snapshot = base.materialize();
518
        Ok(render_change(&change, &base, &base_snapshot))
519
    }
520
}
521
522
fn remove_any(path: &Path) -> std::io::Result<()> {
523
    let m = fs::symlink_metadata(path)?;
524
    if m.is_dir() { fs::remove_dir_all(path) } else { fs::remove_file(path) }
525
}
526
527
#[cfg(unix)]
528
fn set_executable(path: &Path, exec: bool) -> std::io::Result<()> {
529
    use std::os::unix::fs::PermissionsExt;
530
    let mut perms = fs::metadata(path)?.permissions();
531
    let mode = perms.mode();
532
    let want = if exec { mode | 0o111 } else { mode & !0o111 };
533
    if want != mode {
534
        perms.set_mode(want);
535
        fs::set_permissions(path, perms)?;
536
    }
537
    Ok(())
538
}
539
#[cfg(not(unix))]
540
fn set_executable(_path: &Path, _exec: bool) -> std::io::Result<()> {
541
    Ok(())
542
}
543
544
#[cfg(unix)]
545
fn is_executable(m: &fs::Metadata) -> bool {
546
    use std::os::unix::fs::PermissionsExt;
547
    m.permissions().mode() & 0o111 != 0
548
}
549
#[cfg(not(unix))]
550
fn is_executable(_m: &fs::Metadata) -> bool {
551
    false
552
}
553
554
fn op_ref(k: OpKey) -> OpRef {
555
    OpRef { change: k.0, index: k.1 }
556
}
557
558
fn split_path(path: &str) -> (&str, &str) {
559
    match path.rfind('/') {
560
        Some(i) => (&path[..i], &path[i + 1..]),
561
        None => ("", path),
562
    }
563
}