OXYGENHUB
# 7b9809e67c3d oxygen / crates / oxy-core / src / repo / mod.rs
31.3 KiB · Rust · last changed Bookmarks: any number per repository, per-workspace current bookmark (bookmark create|set|delete|list|switch), oxy merge as union of heads, bookmark names resolve everywhere, push -b (spec 0.8 section 10.1) 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: packs, checkpoints, multiple
5
//! workspaces, multi-signer policy updates, and the `objects`/`checkpoint`
6
//! endpoints. A repository without a policy is unverified and accepts
7
//! unsigned pushes; with one, everything received is checked (spec §14.5). Changes are signed individually when `core.sign` is
8
//! `change`; the default `seal` leaves them unsigned until push exists.
9
10
use crate::attest::{Attest, AttestBody};
11
use crate::diff::{ranges, split_lines, text_edits};
12
use crate::protocol::{open_remote, Bundle, RefResult, RefUpdate, RefsResponse, RemoteOptions, RemoteRef, SignedAttest, SignedPolicy};
13
use crate::hash::{hash_object, ChangeHash, KeyId, ObjectRef, ObjectType};
14
use crate::identity::{verify as verify_signature, Domain, Identity, SecretKey, Signature};
15
use crate::ids::{ChangeId, NodeId, WorkspaceId};
16
use crate::objects::{Blob, Change, ChangeKind, Edit, Hlc, Mode, NodeKind, OpRef, StateObject, TreeOp};
17
use crate::oplog::{Format1Op, Op, View, SNAPSHOT_SUFFIX};
18
use crate::state::{FileContent, Snapshot, State};
19
use crate::store::{Store, StoreError};
20
use crate::tree::OpKey;
21
use rand::RngCore;
22
use std::collections::{BTreeMap, BTreeSet, HashMap};
23
use std::fs;
24
use std::io::Write;
25
use std::path::{Path, PathBuf};
26
27
pub const OXY_DIR: &str = ".oxy";
28
pub const FORMAT_LINE: &str = "oxy repository format 2\n";
29
const FORMAT1_LINE: &str = "oxy repository format 1\n";
30
const BINARY_SNIFF: usize = 8 * 1024;
31
/// Loose objects tolerated before a commit packs them (spec §13.4).
32
pub const AUTOPACK_THRESHOLD: usize = 4096;
33
34
#[derive(Debug, thiserror::Error)]
35
pub enum RepoError {
36
    #[error("io: {0}")]
37
    Io(#[from] std::io::Error),
38
    #[error(transparent)]
39
    Store(#[from] StoreError),
40
    #[error(transparent)]
41
    Object(#[from] crate::objects::ObjectError),
42
    #[error(transparent)]
43
    State(#[from] crate::state::StateError),
44
    #[error("{0}")]
45
    Msg(String),
46
}
47
48
pub type Result<T> = std::result::Result<T, RepoError>;
49
50
fn fail<T>(s: impl Into<String>) -> Result<T> {
51
    Err(RepoError::Msg(s.into()))
52
}
53
54
mod bookmarks;
55
mod checkout;
56
mod checkpoint;
57
mod config;
58
mod gc;
59
mod git;
60
mod history;
61
mod hooks;
62
mod identity;
63
mod policy;
64
mod prompt;
65
mod snapshot;
66
mod sync;
67
mod tags;
68
mod workspace;
69
#[cfg(test)]
70
mod tests;
71
72
pub use bookmarks::{BookmarkInfo, MergeReport};
73
pub use checkout::CheckoutReport;
74
pub use config::{parse_size, read_config_file, Config};
75
pub use gc::GcReport;
76
pub use hooks::{json_array, json_str, HookOutcome};
77
pub use prompt::{PromptInfo, DEFAULT_PROMPT_FORMAT};
78
pub use git::{GitExportReport, GitImportReport};
79
pub use identity::{Rotation, SignatureStatus};
80
pub use policy::PolicyUpdate;
81
pub use snapshot::{added_node_paths, has_conflict_markers, is_binary, referenced_changes, render_change, similarity, ChangeStatus, PathStatus, SnapshotResult};
82
pub use sync::{FetchReport, PullReport};
83
pub use tags::TagInfo;
84
pub use workspace::{SnapshotLock, WorkspaceInfo};
85
86
fn write_atomic(path: &Path, data: &[u8]) -> Result<()> {
87
    let tmp = path.with_extension("tmp");
88
    {
89
        let mut f = fs::File::create(&tmp)?;
90
        f.write_all(data)?;
91
        f.sync_all()?;
92
    }
93
    fs::rename(&tmp, path)?;
94
    Ok(())
95
}
96
97
pub struct LogEntry {
98
    pub hash: ChangeHash,
99
    /// Metadata from the state; the full change may be absent in a shallow repository.
100
    pub change: crate::state::ChangeMeta,
101
    pub is_working_copy: bool,
102
    pub is_head: bool,
103
}
104
105
/// A file as found on disk or in an imported tree.
106
pub(crate) struct FsEntry {
107
    pub kind: NodeKind,
108
    pub exec: bool,
109
    /// File content, or a symlink's target. Empty when `blob` is set.
110
    pub content: Vec<u8>,
111
    /// The stored blob of an opaque file whose size and mtime are unchanged
112
    /// since the last manifest (spec §12.3 stat cache); the content was not
113
    /// read.
114
    pub blob: Option<ObjectRef>,
115
    /// BLAKE3 of the content, carried over from the manifest when the stat
116
    /// is unchanged, so the manifest can be rewritten without rehashing.
117
    pub content_hash: Option<String>,
118
}
119
120
impl FsEntry {
121
    pub(crate) fn new(kind: NodeKind, exec: bool, content: Vec<u8>) -> FsEntry {
122
        FsEntry { kind, exec, content, blob: None, content_hash: None }
123
    }
124
}
125
126
pub struct Repo {
127
    root: PathBuf,
128
    oxy: PathBuf,
129
    store: Store,
130
    workspace: WorkspaceId,
131
    keys_dir: PathBuf,
132
    user_config: PathBuf,
133
    pub config: Config,
134
}
135
136
/// The user's oxy directory: `$OXY_HOME`, else `$XDG_CONFIG_HOME/oxy`, else
137
/// `~/.config/oxy`. It holds `config.toml` (spec §19) and `keys/`.
138
pub fn default_home() -> PathBuf {
139
    if let Ok(h) = std::env::var("OXY_HOME") {
140
        return PathBuf::from(h);
141
    }
142
    if let Ok(x) = std::env::var("XDG_CONFIG_HOME") {
143
        return PathBuf::from(x).join("oxy");
144
    }
145
    let home = std::env::var("HOME").unwrap_or_else(|_| ".".into());
146
    PathBuf::from(home).join(".config").join("oxy")
147
}
148
149
pub fn default_keys_dir() -> PathBuf {
150
    default_home().join("keys")
151
}
152
153
pub fn default_user_config() -> PathBuf {
154
    default_home().join("config.toml")
155
}
156
157
/// Where `create_identity` records the author.
158
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
159
pub enum IdentityScope {
160
    /// The user file: every repository of this user signs with it.
161
    User,
162
    /// This repository only.
163
    Repo,
164
}
165
166
impl Repo {
167
    // ------------------------------------------------------------ lifecycle
168
169
    pub fn init(root: &Path) -> Result<Repo> {
170
        let mut id = [0u8; 16];
171
        rand::rngs::OsRng.fill_bytes(&mut id);
172
        Self::init_with_id(root, id)
173
    }
174
175
    /// Initialize a repository with a given repository ID (clones share the
176
    /// ID of their origin, spec §14.4).
177
    pub fn init_with_id(root: &Path, id: [u8; 16]) -> Result<Repo> {
178
        fs::create_dir_all(root)?;
179
        let root = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());
180
        let oxy = root.join(OXY_DIR);
181
        if oxy.exists() {
182
            return fail(format!("{} is already an oxy repository", root.display()));
183
        }
184
        fs::create_dir_all(oxy.join("ops"))?;
185
        write_atomic(&oxy.join("format"), FORMAT_LINE.as_bytes())?;
186
        write_atomic(&oxy.join("id"), hex::encode(id).as_bytes())?;
187
        let config = Config::load(Some(&default_user_config()), &oxy.join("config"))?;
188
        config.save(&oxy.join("config"))?;
189
        let workspace = WorkspaceId::random();
190
        write_atomic(&oxy.join("workspace"), workspace.to_hex().as_bytes())?;
191
        let store = Store::open(oxy.join("objects"))?;
192
        let repo = Repo { root, oxy, store, workspace, keys_dir: default_keys_dir(), user_config: default_user_config(), config };
193
        repo.register_workspace(&workspace, &repo.root)?;
194
195
        let wc = repo.new_working_copy(vec![], "");
196
        let wc_hash = repo.store.put_change(&wc)?;
197
        let mut view = View::default();
198
        view.bookmarks.insert(repo.config.bookmark.clone(), repo.store.put_state(&StateObject::new(vec![]))?);
199
        view.workspaces.insert(workspace, wc_hash);
200
        repo.record_op(&[], "oxy init", view)?;
201
        Ok(repo)
202
    }
203
204
    /// Open the repository containing `start` (searching upwards).
205
    pub fn open(start: &Path) -> Result<Repo> {
206
        let start = start.canonicalize()?;
207
        let mut dir: Option<&Path> = Some(&start);
208
        while let Some(d) = dir {
209
            let oxy = d.join(OXY_DIR);
210
            if oxy.is_file() {
211
                // A secondary workspace (spec §12.2): the file points at the repository.
212
                let text = fs::read_to_string(&oxy)?;
213
                let Some((shared, workspace)) = workspace::parse_workspace_file(&text) else {
214
                    return fail(format!("{} is not a valid workspace file", oxy.display()));
215
                };
216
                let format = fs::read_to_string(shared.join("format"))?;
217
                if format != FORMAT_LINE {
218
                    return fail(format!("unsupported repository format: {}", format.trim()));
219
                }
220
                let config = Config::load(Some(&default_user_config()), &shared.join("config"))?;
221
                let store = Store::open(shared.join("objects"))?;
222
                let mut repo = Repo { root: d.to_path_buf(), oxy: shared, store, workspace, keys_dir: default_keys_dir(), user_config: default_user_config(), config };
223
                repo.apply_workspace_bookmark();
224
                return Ok(repo);
225
            }
226
            if oxy.is_dir() {
227
                let format = fs::read_to_string(oxy.join("format"))?;
228
                let config = Config::load(Some(&default_user_config()), &oxy.join("config"))?;
229
                let workspace = WorkspaceId::from_hex(fs::read_to_string(oxy.join("workspace"))?.trim())
230
                    .ok_or_else(|| RepoError::Msg("corrupt .oxy/workspace".into()))?;
231
                let store = Store::open(oxy.join("objects"))?;
232
                let mut repo = Repo { root: d.to_path_buf(), oxy, store, workspace, keys_dir: default_keys_dir(), user_config: default_user_config(), config };
233
                repo.apply_workspace_bookmark();
234
                if format == FORMAT1_LINE {
235
                    repo.migrate_format1()?;
236
                } else if format != FORMAT_LINE {
237
                    return fail(format!("unsupported repository format: {}", format.trim()));
238
                }
239
                return Ok(repo);
240
            }
241
            dir = d.parent();
242
        }
243
        fail(format!("no oxy repository found at or above {}", start.display()))
244
    }
245
246
    /// Rewrite a format-1 operation log, whose bookmarks were head lists,
247
    /// into format 2, where each bookmark names a `state` object.
248
    fn migrate_format1(&self) -> Result<()> {
249
        let hex = fs::read_to_string(self.oxy.join("ops").join("head"))?;
250
        let head = ObjectRef::from_hex(hex.trim()).ok_or_else(|| RepoError::Msg("corrupt .oxy/ops/head".into()))?;
251
        // Collect the chain root-first.
252
        let mut chain: Vec<Format1Op> = Vec::new();
253
        let mut cur = Some(head);
254
        while let Some(r) = cur {
255
            let op = Op::decode_format1(&self.store.get_typed(&r, ObjectType::Op)?)?;
256
            cur = op.parents.first().copied();
257
            chain.push(op);
258
        }
259
        chain.reverse();
260
        let mut new_parent: Option<ObjectRef> = None;
261
        for old in chain {
262
            let mut view = View::default();
263
            for (name, heads) in &old.view.bookmarks {
264
                view.bookmarks.insert(name.clone(), self.store.put_state(&StateObject::new(heads.clone()))?);
265
            }
266
            view.workspaces = old.view.workspaces.clone();
267
            let op = Op { parents: new_parent.into_iter().collect(), hlc: old.hlc, command: old.command, view };
268
            new_parent = Some(self.store.put(ObjectType::Op, &op.encode())?);
269
        }
270
        write_atomic(&self.oxy.join("ops").join("head"), new_parent.unwrap().to_hex().as_bytes())?;
271
        write_atomic(&self.oxy.join("format"), FORMAT_LINE.as_bytes())?;
272
        Ok(())
273
    }
274
275
    /// Pack automatically once many loose objects have accumulated.
276
    pub(crate) fn maybe_autopack(&self) -> Result<()> {
277
        if self.store.loose_count()? > AUTOPACK_THRESHOLD {
278
            self.store.pack_loose()?;
279
            self.store.merge_packs()?;
280
        }
281
        Ok(())
282
    }
283
284
    /// The repository ID (spec §14.4). Repositories made before IDs existed get one on first use.
285
    pub fn id(&self) -> Result<[u8; 16]> {
286
        let path = self.oxy.join("id");
287
        match fs::read_to_string(&path) {
288
            Ok(h) => hex::decode(h.trim()).ok().and_then(|v| v.try_into().ok()).ok_or_else(|| RepoError::Msg("corrupt .oxy/id".into())),
289
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
290
                let mut id = [0u8; 16];
291
                rand::rngs::OsRng.fill_bytes(&mut id);
292
                write_atomic(&path, hex::encode(id).as_bytes())?;
293
                Ok(id)
294
            }
295
            Err(e) => Err(e.into()),
296
        }
297
    }
298
299
    pub fn root(&self) -> &Path {
300
        &self.root
301
    }
302
    pub fn store(&self) -> &Store {
303
        &self.store
304
    }
305
    pub fn workspace(&self) -> WorkspaceId {
306
        self.workspace
307
    }
308
309
    /// Use a different user directory for keys and the user config file
310
    /// (tests, or `$OXY_HOME`). Reloads the effective configuration.
311
    pub fn with_home(mut self, dir: impl Into<PathBuf>) -> Repo {
312
        let dir = dir.into();
313
        self.keys_dir = dir.join("keys");
314
        self.user_config = dir.join("config.toml");
315
        if let Ok(c) = Config::load(Some(&self.user_config), &self.oxy.join("config")) {
316
            self.config = c;
317
        }
318
        self.apply_workspace_bookmark();
319
        self
320
    }
321
322
    /// The workspace's current bookmark (spec §10.1) overrides the
323
    /// configured default when `oxy bookmark switch` has recorded one.
324
    fn apply_workspace_bookmark(&mut self) {
325
        if let Ok(b) = fs::read_to_string(self.oxy.join("workspaces").join(self.workspace.to_hex()).join("bookmark")) {
326
            let b = b.trim();
327
            if !b.is_empty() {
328
                self.config.bookmark = b.to_string();
329
            }
330
        }
331
    }
332
333
    pub fn user_config_path(&self) -> &Path {
334
        &self.user_config
335
    }
336
337
    /// Set a key in the user file and refresh the effective configuration.
338
    pub fn set_user_config(&mut self, key: &str, value: &str) -> Result<()> {
339
        Config::set_user(&self.user_config, key, value)?;
340
        self.config = Config::load(Some(&self.user_config), &self.oxy.join("config"))?;
341
        Ok(())
342
    }
343
344
    pub fn save_config(&self) -> Result<()> {
345
        self.config.save(&self.oxy.join("config"))
346
    }
347
348
    fn author(&self) -> KeyId {
349
        self.config.author_key.unwrap_or(ObjectRef::ZERO)
350
    }
351
}
352
353
impl Repo {
354
    fn new_working_copy(&self, deps: Vec<ChangeHash>, message: &str) -> Change {
355
        let mut c = Change {
356
            change_id: ChangeId::random(),
357
            deps,
358
            author: self.author(),
359
            hlc: Hlc::now(),
360
            message: message.into(),
361
            tree_ops: vec![],
362
            edits: vec![],
363
            kind: ChangeKind::WorkingCopy,
364
        };
365
        c.normalize();
366
        c
367
    }
368
369
    // ------------------------------------------------------------ operation log
370
371
    pub fn head_op(&self) -> Result<(ObjectRef, Op)> {
372
        let hex = fs::read_to_string(self.oxy.join("ops").join("head"))?;
373
        let r = ObjectRef::from_hex(hex.trim()).ok_or_else(|| RepoError::Msg("corrupt .oxy/ops/head".into()))?;
374
        let op = Op::decode(&self.store.get_typed(&r, ObjectType::Op)?)?;
375
        Ok((r, op))
376
    }
377
378
    pub fn view(&self) -> Result<View> {
379
        Ok(self.head_op()?.1.view)
380
    }
381
382
    fn record_op(&self, parents: &[ObjectRef], command: &str, view: View) -> Result<ObjectRef> {
383
        let op = Op { parents: parents.to_vec(), hlc: Hlc::now(), command: command.into(), view };
384
        let r = self.store.put(ObjectType::Op, &op.encode())?;
385
        write_atomic(&self.oxy.join("ops").join("head"), r.to_hex().as_bytes())?;
386
        Ok(r)
387
    }
388
389
    pub fn op_log(&self) -> Result<Vec<(ObjectRef, Op)>> {
390
        let mut out = Vec::new();
391
        let (mut r, mut op) = self.head_op()?;
392
        loop {
393
            let next = op.parents.first().copied();
394
            out.push((r, op));
395
            match next {
396
                Some(p) => {
397
                    r = p;
398
                    op = Op::decode(&self.store.get_typed(&r, ObjectType::Op)?)?;
399
                }
400
                None => break,
401
            }
402
        }
403
        Ok(out)
404
    }
405
406
    /// Undo the last user command (spec §12.1). Operations that only recorded
407
    /// a working-copy snapshot are skipped, since the files are still on disk
408
    /// and the next snapshot recreates them.
409
    pub fn undo(&self) -> Result<Op> {
410
        let (_, mut op) = self.head_op()?;
411
        while op.is_snapshot() {
412
            let Some(parent) = op.parents.first() else { break };
413
            op = Op::decode(&self.store.get_typed(parent, ObjectType::Op)?)?;
414
        }
415
        let Some(parent) = op.parents.first() else {
416
            return fail("nothing to undo");
417
        };
418
        let parent_op = Op::decode(&self.store.get_typed(parent, ObjectType::Op)?)?;
419
        write_atomic(&self.oxy.join("ops").join("head"), parent.to_hex().as_bytes())?;
420
        self.drop_manifest();
421
        Ok(parent_op)
422
    }
423
424
    /// The last operation that was not a snapshot.
425
    pub fn last_command(&self) -> Result<Op> {
426
        let (_, mut op) = self.head_op()?;
427
        while op.is_snapshot() {
428
            let Some(parent) = op.parents.first() else { break };
429
            op = Op::decode(&self.store.get_typed(parent, ObjectType::Op)?)?;
430
        }
431
        Ok(op)
432
    }
433
434
    // ------------------------------------------------------------ states
435
436
    /// Load the dependency closure of `heads`, through a checkpoint when one applies.
437
    pub fn load_state(&self, heads: &[ChangeHash]) -> Result<State> {
438
        if let Some(st) = self.load_state_checkpointed(heads)? {
439
            return Ok(st);
440
        }
441
        self.load_state_full(heads)
442
    }
443
444
    /// Load the dependency closure of `heads` change by change.
445
    pub fn load_state_full(&self, heads: &[ChangeHash]) -> Result<State> {
446
        let mut state = State::new();
447
        let mut stack: Vec<ChangeHash> = heads.to_vec();
448
        let mut seen: BTreeSet<ChangeHash> = BTreeSet::new();
449
        while let Some(h) = stack.pop() {
450
            if !seen.insert(h) {
451
                continue;
452
            }
453
            let c = self.store.get_change(&h)?;
454
            stack.extend(c.deps.iter().copied());
455
            state.add_unchecked(c);
456
        }
457
        state.validate()?;
458
        Ok(state)
459
    }
460
461
    pub fn working_copy(&self) -> Result<(ChangeHash, Change)> {
462
        let view = self.view()?;
463
        let h = *view
464
            .workspaces
465
            .get(&self.workspace)
466
            .ok_or_else(|| RepoError::Msg("this workspace is not in the current view".into()))?;
467
        Ok((h, self.store.get_change(&h)?))
468
    }
469
470
    /// Heads of a bookmark's state (spec §10.1).
471
    pub fn heads_of(&self, view: &View, bookmark: &str) -> Result<Vec<ChangeHash>> {
472
        match view.bookmarks.get(bookmark) {
473
            Some(st) => Ok(self.store.get_state(st)?.heads),
474
            None => Ok(vec![]),
475
        }
476
    }
477
478
    pub fn bookmark_heads(&self) -> Result<Vec<ChangeHash>> {
479
        self.heads_of(&self.view()?, &self.config.bookmark)
480
    }
481
482
    // ------------------------------------------------------------ blobs
483
484
    fn blob_for(content: &[u8]) -> Blob {
485
        Blob {
486
            length: content.len() as u64,
487
            chunks: crate::chunk::chunks(content).into_iter().map(|c| hash_object(ObjectType::Chunk, c)).collect(),
488
        }
489
    }
490
491
    pub fn blob_ref(content: &[u8]) -> ObjectRef {
492
        Self::blob_for(content).hash()
493
    }
494
495
    pub fn store_blob(&self, content: &[u8]) -> Result<ObjectRef> {
496
        for c in crate::chunk::chunks(content) {
497
            self.store.put_chunk(c)?;
498
        }
499
        Ok(self.store.put(ObjectType::Blob, &Self::blob_for(content).encode())?)
500
    }
501
502
    /// Read a blob's content, fetching it from the remote that withheld it
503
    /// when it is lazily absent (spec §13.5).
504
    pub fn read_blob(&self, r: &ObjectRef) -> Result<Vec<u8>> {
505
        if !self.store.has(r) && self.lazy_remote(r).is_some() {
506
            self.fetch_lazy_blob(r)?;
507
        }
508
        let blob = Blob::decode(&self.store.get_typed(r, ObjectType::Blob)?)?;
509
        let mut out = Vec::with_capacity(blob.length as usize);
510
        for c in &blob.chunks {
511
            out.extend(self.store.get_typed(c, ObjectType::Chunk)?);
512
        }
513
        Ok(out)
514
    }
515
516
    // ------------------------------------------------------------ working copy
517
518
    /// Attributes from the `.oxyattributes` files in the working directory.
519
    pub fn disk_attributes(&self) -> crate::attributes::Attributes {
520
        let mut files: Vec<(String, Vec<u8>)> = Vec::new();
521
        if let Ok(b) = fs::read(self.root.join(".oxyattributes")) {
522
            files.push((".oxyattributes".into(), b));
523
        }
524
        let mut wb = ignore::WalkBuilder::new(&self.root);
525
        wb.hidden(false).git_ignore(false).git_global(false).git_exclude(false).require_git(false).follow_links(false).add_custom_ignore_filename(".oxyignore");
526
        wb.filter_entry(|e| e.file_name() != OXY_DIR);
527
        for entry in wb.build().flatten() {
528
            if entry.file_name() == ".oxyattributes" && entry.path() != self.root.join(".oxyattributes") {
529
                if let Ok(b) = fs::read(entry.path()) {
530
                    let rel: String = entry.path().strip_prefix(&self.root).unwrap().components().map(|c| c.as_os_str().to_string_lossy().to_string()).collect::<Vec<_>>().join("/");
531
                    files.push((rel, b));
532
                }
533
            }
534
        }
535
        crate::attributes::Attributes::from_files(files.iter().map(|(p, c)| (p.as_str(), c.as_slice())))
536
    }
537
538
    /// Attributes from a materialized state's `.oxyattributes` files.
539
    pub fn state_attributes(snapshot: &Snapshot) -> crate::attributes::Attributes {
540
        crate::attributes::Attributes::from_files(snapshot.files.iter().filter_map(|(p, f)| match &f.content {
541
            FileContent::Text { bytes, .. } if p == ".oxyattributes" || p.ends_with("/.oxyattributes") => Some((p.as_str(), bytes.as_slice())),
542
            _ => None,
543
        }))
544
    }
545
546
    /// Store a blob, raw when the path is `nocompress`.
547
    pub fn store_blob_for(&self, content: &[u8], nocompress: bool) -> Result<ObjectRef> {
548
        if !nocompress {
549
            return self.store_blob(content);
550
        }
551
        for c in crate::chunk::chunks(content) {
552
            self.store.put_chunk_nocompress(c)?;
553
        }
554
        Ok(self.store.put(ObjectType::Blob, &Self::blob_for(content).encode())?)
555
    }
556
557
    fn locks_dir(&self) -> PathBuf {
558
        self.oxy.join("locks")
559
    }
560
561
    /// Whether `path` is locked locally for editing (spec §18, advisory).
562
    pub fn is_locked(&self, path: &str) -> bool {
563
        self.locks_dir().join(path.replace('/', "%2F")).exists()
564
    }
565
566
    /// Take the advisory local lock on a `lockable` file and make it writable.
567
    pub fn lock_path(&self, path: &str) -> Result<()> {
568
        let attrs = self.disk_attributes();
569
        if !attrs.lookup(path).lockable {
570
            return fail(format!("{path} is not marked lockable in .oxyattributes"));
571
        }
572
        fs::create_dir_all(self.locks_dir())?;
573
        write_atomic(&self.locks_dir().join(path.replace('/', "%2F")), std::process::id().to_string().as_bytes())?;
574
        set_readonly(&self.root.join(path), false)?;
575
        Ok(())
576
    }
577
578
    /// Release the local lock and make the file read-only again.
579
    pub fn unlock_path(&self, path: &str) -> Result<()> {
580
        let _ = fs::remove_file(self.locks_dir().join(path.replace('/', "%2F")));
581
        if self.root.join(path).exists() {
582
            set_readonly(&self.root.join(path), true)?;
583
        }
584
        Ok(())
585
    }
586
587
    /// Whether a repository path is inside the sparse filter (spec §17).
588
    pub fn in_scope(&self, path: &str) -> bool {
589
        self.config.sparse_paths.is_empty() || self.config.sparse_paths.iter().any(|p| path == p || path.starts_with(&format!("{p}/")))
590
    }
591
592
    fn walk_working_copy(&self) -> Result<BTreeMap<String, FsEntry>> {
593
        self.walk_working_copy_with(None, &BTreeSet::new())
594
    }
595
596
    /// Walk the working copy; with a manifest, a file whose size and mtime
597
    /// match it is not read: an opaque file yields its known blob, a text
598
    /// file is read but keeps its known hash. Files modified within two
599
    /// seconds of the manifest are never trusted (a racy stat).
600
    ///
601
    /// `.oxyignore` applies to untracked paths only: every path in `tracked`
602
    /// (the base state's files) that exists on disk is included even when
603
    /// an ignore rule matches it, as in Git.
604
    fn walk_working_copy_with(&self, cache: Option<&prompt::Manifest>, tracked: &BTreeSet<String>) -> Result<BTreeMap<String, FsEntry>> {
605
        let mut wb = ignore::WalkBuilder::new(&self.root);
606
        wb.hidden(false)
607
            .git_ignore(false)
608
            .git_global(false)
609
            .git_exclude(false)
610
            .require_git(false)
611
            .follow_links(false)
612
            .add_custom_ignore_filename(".oxyignore");
613
        wb.filter_entry(|e| e.file_name() != OXY_DIR);
614
        let mut out = BTreeMap::new();
615
        for entry in wb.build() {
616
            let entry = entry.map_err(|e| RepoError::Msg(e.to_string()))?;
617
            let path = entry.path();
618
            if path == self.root {
619
                continue;
620
            }
621
            let rel = path.strip_prefix(&self.root).unwrap();
622
            let rel: String = rel.components().map(|c| c.as_os_str().to_string_lossy().to_string()).collect::<Vec<_>>().join("/");
623
            let Some(ft) = entry.file_type() else { continue };
624
            if ft.is_dir() || !self.in_scope(&rel) {
625
                continue;
626
            }
627
            if ft.is_symlink() {
628
                let target = fs::read_link(path)?;
629
                out.insert(rel, FsEntry::new(NodeKind::Symlink, false, target.to_string_lossy().as_bytes().to_vec()));
630
            } else if ft.is_file() {
631
                let meta = entry.metadata().map_err(|e| RepoError::Msg(e.to_string()))?;
632
                let e = self.file_entry(&rel, path, &meta, cache)?;
633
                out.insert(rel, e);
634
            }
635
        }
636
        // Tracked paths the ignore rules hid.
637
        for rel in tracked {
638
            if out.contains_key(rel) || !self.in_scope(rel) {
639
                continue;
640
            }
641
            let path = self.root.join(rel);
642
            let Ok(meta) = fs::symlink_metadata(&path) else { continue };
643
            let ft = meta.file_type();
644
            if ft.is_symlink() {
645
                let target = fs::read_link(&path)?;
646
                out.insert(rel.clone(), FsEntry::new(NodeKind::Symlink, false, target.to_string_lossy().as_bytes().to_vec()));
647
            } else if ft.is_file() {
648
                let e = self.file_entry(rel, &path, &meta, cache)?;
649
                out.insert(rel.clone(), e);
650
            }
651
        }
652
        Ok(out)
653
    }
654
655
    /// The entry for one regular file, read or served from the stat cache.
656
    fn file_entry(&self, rel: &str, path: &Path, meta: &fs::Metadata, cache: Option<&prompt::Manifest>) -> Result<FsEntry> {
657
        let exec = is_executable(meta);
658
        let cached = cache.and_then(|m| {
659
            let e = m.entries.get(rel)?;
660
            let mtime_ns = prompt::mtime_ns(meta);
661
            let racy = mtime_ns + 2_000_000_000 >= m.written_ns;
662
            (e.size == meta.len() && e.mtime_ns == mtime_ns && !racy).then_some(e)
663
        });
664
        Ok(match cached {
665
            Some(e) if e.blob.is_some_and(|b| self.store.has(&b)) => FsEntry { kind: NodeKind::File, exec, content: vec![], blob: e.blob, content_hash: Some(e.hash.clone()) },
666
            Some(e) => FsEntry { kind: NodeKind::File, exec, content: fs::read(path)?, blob: None, content_hash: Some(e.hash.clone()) },
667
            None => FsEntry::new(NodeKind::File, exec, fs::read(path)?),
668
        })
669
    }
670
671
    /// The non-directory paths of a snapshot, for the walker's `tracked` set.
672
    pub(crate) fn tracked_paths(snapshot: &Snapshot) -> BTreeSet<String> {
673
        snapshot.files.iter().filter(|(_, f)| f.kind != NodeKind::Dir).map(|(p, _)| p.clone()).collect()
674
    }
675
}
676
677
impl Repo {
678
    /// Changes reachable from the bookmark and the working copy, newest first.
679
    pub fn log(&self) -> Result<Vec<LogEntry>> {
680
        let (wc_hash, _) = self.working_copy()?;
681
        let mut heads = self.bookmark_heads()?;
682
        heads.push(wc_hash);
683
        let state = self.load_state(&heads)?;
684
        let head_set: BTreeSet<ChangeHash> = self.bookmark_heads()?.into_iter().collect();
685
        let mut entries: Vec<LogEntry> = Vec::new();
686
        for (h, meta) in state.changes() {
687
            entries.push(LogEntry { hash: *h, change: meta.clone(), is_working_copy: *h == wc_hash, is_head: head_set.contains(h) });
688
        }
689
        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)));
690
        Ok(entries)
691
    }
692
693
    /// Resolve a spec to a set of heads: a tag name, the current bookmark's
694
    /// name, or a single change (`resolve_change`).
695
    pub fn resolve_heads(&self, spec: &str) -> Result<Vec<ChangeHash>> {
696
        let view = self.view()?;
697
        if let Some(st) = view.bookmarks.get(spec) {
698
            return Ok(self.store.get_state(st)?.heads);
699
        }
700
        if let Some(heads) = self.tag_heads(spec)? {
701
            return Ok(heads);
702
        }
703
        Ok(vec![self.resolve_change(spec)?])
704
    }
705
706
    /// Resolve a change by ChangeId prefix, `#hash` prefix, `@` (working copy), or bookmark.
707
    pub fn resolve_change(&self, spec: &str) -> Result<ChangeHash> {
708
        if spec == "@" {
709
            return Ok(self.working_copy()?.0);
710
        }
711
        if let Some(st) = self.view()?.bookmarks.get(spec) {
712
            let heads = self.store.get_state(st)?.heads;
713
            return match heads.len() {
714
                1 => Ok(heads[0]),
715
                n => fail(format!("bookmark {spec} has {n} heads; name a change, or use it where a state is expected")),
716
            };
717
        }
718
        if spec.is_empty() || spec == "#" {
719
            return fail("empty change specifier");
720
        }
721
        let entries = self.log()?;
722
        let matches: Vec<&LogEntry> = if let Some(hp) = spec.strip_prefix('#') {
723
            entries.iter().filter(|e| e.hash.starts_with_hex(hp)).collect()
724
        } else {
725
            entries.iter().filter(|e| e.change.change_id.starts_with_hex(spec)).collect()
726
        };
727
        match matches.len() {
728
            1 => Ok(matches[0].hash),
729
            0 => fail(format!("no change matches {spec:?}")),
730
            _ => {
731
                let ids: BTreeSet<ChangeId> = matches.iter().map(|e| e.change.change_id).collect();
732
                if ids.len() == 1 {
733
                    fail(format!("change {spec} is divergent: {} variants", matches.len()))
734
                } else {
735
                    fail(format!("{spec:?} is ambiguous"))
736
                }
737
            }
738
        }
739
    }
740
741
    /// Human-readable description of what a change does, with line content.
742
    pub fn describe_change(&self, hash: &ChangeHash) -> Result<String> {
743
        let change = self.store.get_change(hash)?;
744
        let base = self.load_state(&change.deps)?;
745
        let base_snapshot = base.materialize();
746
        Ok(render_change(&change, &base, &base_snapshot))
747
    }
748
}
749
750
fn remove_any(path: &Path) -> std::io::Result<()> {
751
    let m = fs::symlink_metadata(path)?;
752
    if m.is_dir() { fs::remove_dir_all(path) } else { fs::remove_file(path) }
753
}
754
755
fn set_readonly(path: &Path, readonly: bool) -> std::io::Result<()> {
756
    let mut perms = fs::metadata(path)?.permissions();
757
    perms.set_readonly(readonly);
758
    fs::set_permissions(path, perms)
759
}
760
761
#[cfg(unix)]
762
fn set_executable(path: &Path, exec: bool) -> std::io::Result<()> {
763
    use std::os::unix::fs::PermissionsExt;
764
    let mut perms = fs::metadata(path)?.permissions();
765
    let mode = perms.mode();
766
    let want = if exec { mode | 0o111 } else { mode & !0o111 };
767
    if want != mode {
768
        perms.set_mode(want);
769
        fs::set_permissions(path, perms)?;
770
    }
771
    Ok(())
772
}
773
#[cfg(not(unix))]
774
fn set_executable(_path: &Path, _exec: bool) -> std::io::Result<()> {
775
    Ok(())
776
}
777
778
#[cfg(unix)]
779
fn is_executable(m: &fs::Metadata) -> bool {
780
    use std::os::unix::fs::PermissionsExt;
781
    m.permissions().mode() & 0o111 != 0
782
}
783
#[cfg(not(unix))]
784
fn is_executable(_m: &fs::Metadata) -> bool {
785
    false
786
}
787
788
fn op_ref(k: OpKey) -> OpRef {
789
    OpRef { change: k.0, index: k.1 }
790
}
791
792
fn split_path(path: &str) -> (&str, &str) {
793
    match path.rfind('/') {
794
        Some(i) => (&path[..i], &path[i + 1..]),
795
        None => ("", path),
796
    }
797
}