| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! History queries over a repository: the engine's [`History`] on the store, |
| 5 | //! with the store's derived index. |
| 6 | |
| 7 | use braid_engine::{DerivedIndex, DerivedKey, History}; |
| 8 | use braid_format::{Kind, ObjectId, Objects}; |
| 9 | use braid_store::{DerivedIndexFile, Store}; |
| 10 | |
| 11 | use crate::{Error, Repo}; |
| 12 | |
| 13 | /// The derived index of a repository's store, as the engine uses it. |
| 14 | /// |
| 15 | /// Before it records entries, it makes everything this process wrote to the |
| 16 | /// store durable, so that the index never names a record a crash could lose. |
| 17 | #[derive(Clone, Copy, Debug)] |
| 18 | pub struct StoreIndex<'r> { |
| 19 | store: &'r Store, |
| 20 | file: &'r DerivedIndexFile, |
| 21 | } |
| 22 | |
| 23 | impl DerivedIndex for StoreIndex<'_> { |
| 24 | type Error = braid_store::Error; |
| 25 | |
| 26 | fn find(&self, key: &DerivedKey) -> Result<Option<ObjectId>, Self::Error> { |
| 27 | self.file.find(key.as_bytes()) |
| 28 | } |
| 29 | |
| 30 | fn record(&self, entries: &[(DerivedKey, ObjectId)]) -> Result<(), Self::Error> { |
| 31 | self.store.sync()?; |
| 32 | let entries: Vec<([u8; 32], ObjectId)> = entries |
| 33 | .iter() |
| 34 | .map(|(key, record)| (*key.as_bytes(), *record)) |
| 35 | .collect(); |
| 36 | self.file.append(&entries) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /// History queries over a repository's store. |
| 41 | pub type RepoHistory<'r> = History<'r, Store, StoreIndex<'r>>; |
| 42 | |
| 43 | impl Repo { |
| 44 | /// History queries over the store, finding derived data through the |
| 45 | /// store's derived index and computing what is missing. Call |
| 46 | /// [`History::flush`] when done, so that what was computed is kept. |
| 47 | pub fn history(&self) -> RepoHistory<'_> { |
| 48 | History::new( |
| 49 | &self.store, |
| 50 | StoreIndex { |
| 51 | store: &self.store, |
| 52 | file: &self.derived, |
| 53 | }, |
| 54 | ) |
| 55 | } |
| 56 | |
| 57 | /// The changesets the refs point at, without repeats, sorted. |
| 58 | pub fn ref_changesets(&self) -> Result<Vec<ObjectId>, Error> { |
| 59 | let mut heads = Vec::new(); |
| 60 | for (_, id) in self.store.refs()?.iter() { |
| 61 | if matches!(self.store.stat(&id)?, Some((Kind::Changeset, _))) { |
| 62 | heads.push(id); |
| 63 | } |
| 64 | } |
| 65 | heads.sort(); |
| 66 | heads.dedup(); |
| 67 | Ok(heads) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | #[cfg(test)] |
| 72 | mod tests { |
| 73 | use super::*; |
| 74 | use braid_format::{Change, ChangeOp, Signature}; |
| 75 | use braid_store::RefUpdate; |
| 76 | |
| 77 | fn signature() -> Signature { |
| 78 | Signature { |
| 79 | name: b"Ada".to_vec(), |
| 80 | email: b"ada@example.org".to_vec(), |
| 81 | seconds: 1_700_000_000, |
| 82 | offset_minutes: 0, |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | fn change(repo: &Repo, parents: Vec<ObjectId>, content: &str) -> ObjectId { |
| 87 | let blob = repo.store().put(Kind::Blob, content.as_bytes()).unwrap(); |
| 88 | let change = Change { |
| 89 | path: b"f".to_vec(), |
| 90 | op: ChangeOp::File { |
| 91 | blob, |
| 92 | size: content.len() as u64, |
| 93 | }, |
| 94 | }; |
| 95 | braid_engine::commit( |
| 96 | repo.store(), |
| 97 | parents, |
| 98 | vec![change], |
| 99 | signature(), |
| 100 | signature(), |
| 101 | b"change\n".to_vec(), |
| 102 | Vec::new(), |
| 103 | ) |
| 104 | .unwrap() |
| 105 | } |
| 106 | |
| 107 | #[test] |
| 108 | fn derived_data_is_kept_between_processes() { |
| 109 | let dir = tempfile::tempdir().unwrap(); |
| 110 | let repo = Repo::init(dir.path()).unwrap(); |
| 111 | let one = change(&repo, Vec::new(), "1\n"); |
| 112 | let two = change(&repo, vec![one], "1\n2\n"); |
| 113 | repo.store().sync().unwrap(); |
| 114 | repo.store() |
| 115 | .transact("test", "main", |_| { |
| 116 | Ok::<_, std::convert::Infallible>(vec![RefUpdate::new("main", None, Some(two))]) |
| 117 | }) |
| 118 | .unwrap(); |
| 119 | assert_eq!(repo.ref_changesets().unwrap(), [two]); |
| 120 | |
| 121 | let mut history = repo.history(); |
| 122 | let report = history.derive(&[two]).unwrap(); |
| 123 | assert_eq!(report.changesets, 2); |
| 124 | assert_eq!(report.computed, 4); |
| 125 | let blame = history.blame(two, b"f").unwrap().unwrap(); |
| 126 | assert_eq!(blame.origins, [one, two]); |
| 127 | history.flush().unwrap(); |
| 128 | |
| 129 | // Another process finds everything, and the store stays clean. |
| 130 | let again = Repo::open(dir.path()).unwrap(); |
| 131 | let mut history = again.history(); |
| 132 | assert_eq!(history.derive(&[two]).unwrap().computed, 0); |
| 133 | assert_eq!( |
| 134 | history.blame(two, b"f").unwrap().unwrap().origins, |
| 135 | [one, two] |
| 136 | ); |
| 137 | assert_eq!(history.path_log(two, &[b"f"], 10).unwrap(), [two, one]); |
| 138 | assert_eq!(history.computed(), 0); |
| 139 | assert!(again.store().fsck().unwrap().is_clean()); |
| 140 | } |
| 141 | } |