diff --git a/apps/origin-src/src/index.rs b/apps/origin-src/src/index.rs index 9927bf3d..413016f6 100644 --- a/apps/origin-src/src/index.rs +++ b/apps/origin-src/src/index.rs @@ -227,6 +227,28 @@ impl Index { Ok((index, tail != Tail::Damaged)) } + /// Adds the records among `objects`, which this process just wrote, so this + /// index knows them without being read again. + /// + /// # Errors + /// + /// Fails if a record doesn't decode. + pub(crate) fn add<'o>( + &mut self, + objects: impl IntoIterator, + ) -> Result<(), StoreError> { + for object in objects { + let entry = Entry::of(object).map_err(|source| StoreError::Decode { + id: object.id(), + source, + })?; + if let Some(entry) = entry { + self.insert(entry); + } + } + Ok(()) + } + /// Adds a record, unless it's already indexed. fn insert(&mut self, entry: Entry) { if !self.known.insert(entry.record()) { diff --git a/apps/origin-src/src/workspace/annotate.rs b/apps/origin-src/src/workspace/annotate.rs index c5c63af2..88b055bc 100644 --- a/apps/origin-src/src/workspace/annotate.rs +++ b/apps/origin-src/src/workspace/annotate.rs @@ -24,10 +24,11 @@ impl WorkingCopy { /// unique prefix of its id, a change for its head, or a revision. A /// prefix naming a change and another revision than its head is /// ambiguous. - fn find_revision(&self, index: &Index, rev: &str) -> Result { + fn find_revision(&self, rev: &str) -> Result { if rev == "@" { return Ok(self.id); } + let index = &self.index; match (index.find_change(rev), index.find_revision(rev)) { (Err(FindError::NotFound(_)), revision) => Ok(revision?), (Ok(change), Err(FindError::NotFound(_))) => Workspace::single_head(index, change), @@ -44,14 +45,10 @@ impl WorkingCopy { /// The record `annotated` names, as [`WorkingCopy::find_revision`] /// finds a revision, or a statement by a unique prefix of its id. - fn find_record( - &self, - index: &Index, - annotated: Annotated<'_>, - ) -> Result { + fn find_record(&self, annotated: Annotated<'_>) -> Result { Ok(match annotated { - Annotated::Revision(rev) => RecordId::Revision(self.find_revision(index, rev)?), - Annotated::Statement(prefix) => RecordId::Statement(index.find_statement(prefix)?), + Annotated::Revision(rev) => RecordId::Revision(self.find_revision(rev)?), + Annotated::Statement(prefix) => RecordId::Statement(self.index.find_statement(prefix)?), }) } } @@ -99,16 +96,21 @@ impl Workspace { Principal::Node(self.config.node().clone()) } - /// Issues `core.divergent` if `revision`, just saved, made its change - /// diverge. The statement is the same for the same heads, so checking - /// again writes nothing. - pub(super) fn flag_divergence(&self, revision: RevisionId) -> Result<(), WorkspaceError> { + /// Issues a `core.divergent` statement if saving `revision` made its change + /// diverge, as `index`, which has `revision`, shows. The statement is the same + /// for the same heads, so calling this again writes nothing. + pub(super) fn flag_divergence( + &self, + index: &Index, + revision: RevisionId, + ) -> Result<(), WorkspaceError> { let revision: Revision = self.store.get(revision)?; // A new change has a single revision. - if revision.predecessors.is_empty() { + if revision.predecessors.is_empty() || !index.is_divergent(revision.change) { return Ok(()); } - Annotations::open(&self.store)?.flag_divergence(revision.change, self.node_principal())?; + Annotations::new(&self.store, index.clone()) + .flag_divergence(revision.change, self.node_principal())?; Ok(()) } @@ -135,8 +137,8 @@ impl Workspace { return Err(WorkspaceError::ReservedKind(kind)); } let working_copy = self.refresh()?; - let mut annotations = Annotations::open(&self.store)?; - let subject = working_copy.find_record(annotations.index(), annotated)?; + let subject = working_copy.find_record(annotated)?; + let mut annotations = Annotations::new(&self.store, working_copy.index); let payload = payload.map(Payload::blob).transpose()?; let statement = Statement { subject, @@ -232,8 +234,8 @@ impl Workspace { /// Fails if the revision cannot be found or a record cannot be read. pub fn statements(&mut self, rev: &str) -> Result<(), WorkspaceError> { let working_copy = self.snapshot()?; - let annotations = Annotations::open(&self.store)?; - let revision = working_copy.find_revision(annotations.index(), rev)?; + let revision = working_copy.find_revision(rev)?; + let annotations = Annotations::new(&self.store, working_copy.index); let subject = RecordId::Revision(revision); if annotations.index().statements(subject).is_empty() { println!("No statements about revision {}", revision.short()); @@ -326,8 +328,8 @@ impl Workspace { /// written. pub fn promote(&mut self, rev: &str) -> Result<(), WorkspaceError> { let working_copy = self.refresh()?; - let mut annotations = Annotations::open(&self.store)?; - let revision = working_copy.find_revision(annotations.index(), rev)?; + let revision = working_copy.find_revision(rev)?; + let mut annotations = Annotations::new(&self.store, working_copy.index); let promoted = annotations.promote(RecordId::Revision(revision), &self.node_principal())?; if promoted.is_empty() { println!("Nothing to promote to revision {}", revision.short()); diff --git a/apps/origin-src/src/workspace/land.rs b/apps/origin-src/src/workspace/land.rs index 92b0167b..7ec75eec 100644 --- a/apps/origin-src/src/workspace/land.rs +++ b/apps/origin-src/src/workspace/land.rs @@ -14,7 +14,6 @@ use origin_shared::{ }; use super::{WorkingCopy, Workspace, WorkspaceError}; -use crate::index::Index; use crate::policy::Policy; use crate::queue::{Blocked, Landing, Pending, Proposal, Queue}; @@ -33,11 +32,10 @@ impl Workspace { onto: Option, ) -> Result<(), WorkspaceError> { let working_copy = self.refresh()?; - let index = Index::open(&self.store)?; - let change = working_copy.find_change(&index, change.unwrap_or("@"))?; + let change = working_copy.find_change(change.unwrap_or("@"))?; let onto = match onto { Some(onto) => onto, - None => self.target_of(Self::single_head(&index, change)?)?, + None => self.target_of(Self::single_head(&working_copy.index, change)?)?, }; let key = self.config.node_key()?; let landings = diff --git a/apps/origin-src/src/workspace/mod.rs b/apps/origin-src/src/workspace/mod.rs index 099a61d0..48b42c7e 100644 --- a/apps/origin-src/src/workspace/mod.rs +++ b/apps/origin-src/src/workspace/mod.rs @@ -59,14 +59,17 @@ pub struct Workspace { struct WorkingCopy { id: RevisionId, revision: Revision, - /// The files as the snapshot found them: the draft, which is the - /// revision's snapshot once recorded. - files: TreeId, /// The revision's record, if recording the draft made a new revision, /// which is not written yet. unsaved: Option, /// The revision's unresolved conflicts that the files still have. conflicts: Conflicts, + /// The record index as of the snapshot. Once the draft is saved, it has the new + /// revision. + index: Index, + /// The files as the snapshot found them: the draft, which is the + /// revision's snapshot once recorded. + files: TreeId, /// Keeps garbage collection from removing the objects the snapshot found, until /// the command that saves the snapshot is done. _held: LockedFile, @@ -75,11 +78,11 @@ struct WorkingCopy { impl WorkingCopy { /// The change `prefix` names: `@` for the working copy's, or a unique /// prefix of its id. - fn find_change(&self, index: &Index, prefix: &str) -> Result { + fn find_change(&self, prefix: &str) -> Result { if prefix == "@" { return Ok(self.revision.change); } - index.find_change(prefix) + self.index.find_change(prefix) } } @@ -127,9 +130,11 @@ impl Workspace { config, revision: encoded.id().into(), }; + // A first revision makes no divergence, which an index would tell. workspace.save( &[vec![empty_description], vec![encoded]], workspace.revision, + &mut Index::default(), )?; println!( "Initialized a repository in {}: node:{} is the authority of `policy` and `main`", @@ -204,7 +209,7 @@ impl Workspace { /// Snapshots the working copy into a draft, recording nothing, for the /// commands that only read it. - fn snapshot(&self) -> Result { + fn snapshot(&mut self) -> Result { let held = self.store.hold_objects()?; let revision: Revision = self.store.get(self.revision)?; let path = self.meta("state"); @@ -215,22 +220,24 @@ impl Workspace { .save(&path) .map_err(|source| WorkspaceError::io(&path, source))?; } + let index = Index::open(&self.store)?; let conflicts = self .conflicts(self.revision, &revision)? .remaining(&self.store, files)?; Ok(WorkingCopy { id: self.revision, revision, - files, unsaved: None, conflicts, + index, + files, _held: held, }) } /// Snapshots the working copy and records its draft: a new revision if /// the files changed, which still has to be saved. - fn record(&self) -> Result { + fn record(&mut self) -> Result { let working_copy = self.snapshot()?; if working_copy.files == working_copy.revision.snapshot { return Ok(working_copy); @@ -298,21 +305,23 @@ impl Workspace { fn refresh(&mut self) -> Result { let mut working_copy = self.record()?; if let Some(unsaved) = working_copy.unsaved.take() { - self.save(&[vec![unsaved]], working_copy.id)?; + self.save(&[vec![unsaved]], working_copy.id, &mut working_copy.index)?; } Ok(working_copy) } - /// Writes `layers` of objects, then makes `revision` the working copy, - /// flagging its change if that made it diverge. + /// Writes `layers` of objects and adds them to `index`, then makes `revision` + /// the working copy. Flags the change as divergent if that made it diverge. fn save( &mut self, layers: &[Vec], revision: RevisionId, + index: &mut Index, ) -> Result<(), WorkspaceError> { self.write(layers)?; + index.add(layers.iter().flatten())?; self.point_at(revision)?; - self.flag_divergence(revision) + self.flag_divergence(index, revision) } /// Writes `layers` of objects in order, each durable before the next, @@ -351,14 +360,14 @@ impl Workspace { pub fn status(&mut self) -> Result<(), WorkspaceError> { let working_copy = self.snapshot()?; let revision = &working_copy.revision; - let index = Index::open(&self.store)?; + let index = &working_copy.index; println!( "Working copy: {}{}", self.headline(revision)?, - self.flags(&index, revision.change)? + self.flags(index, revision.change)? ); println!("On top of: {}", self.describe_base(revision.base)?); - Self::print_divergence(&index, revision.change); + Self::print_divergence(index, revision.change); if !working_copy.conflicts.is_empty() { Self::print_conflicts(&working_copy.conflicts); } @@ -398,12 +407,12 @@ impl Workspace { /// Fails if the change cannot be found, or like [`Workspace::diff`]. pub fn show(&mut self, change: Option<&str>) -> Result<(), WorkspaceError> { let working_copy = self.snapshot()?; - let index = Index::open(&self.store)?; + let index = &working_copy.index; // Each revision, with the files to show: the working copy's draft. let revisions = match change { None => vec![(working_copy.id, Some(working_copy.files))], Some(change) => index - .heads(working_copy.find_change(&index, change)?) + .heads(working_copy.find_change(change)?) .into_iter() .map(|head| (head, None)) .collect(), @@ -417,7 +426,7 @@ impl Workspace { println!( "Change {}{}", revision.change.short(), - self.flags(&index, revision.change)? + self.flags(index, revision.change)? ); let edited = if files == revision.snapshot { "" @@ -426,7 +435,7 @@ impl Workspace { }; println!("Revision {} by {}{edited}", id.short(), revision.author); println!("On top of: {}", self.describe_base(revision.base)?); - for (issuer, reason) in self.abandonments(&index, id)? { + for (issuer, reason) in self.abandonments(index, id)? { println!("Abandoned by {issuer}: {reason}"); } println!(); @@ -460,7 +469,7 @@ impl Workspace { /// Fails if the working copy cannot be snapshotted or the new change /// cannot be written. pub fn new_change(&mut self, description: &str) -> Result<(), WorkspaceError> { - let working_copy = self.record()?; + let mut working_copy = self.record()?; let description = EncodedObject::blob(description.as_bytes()); let revision = Revision { change: ChangeId::random().map_err(WorkspaceError::Random)?, @@ -474,14 +483,14 @@ impl Workspace { let encoded = revision.encode(); let id = encoded.id().into(); let recorded = working_copy.unsaved.is_some(); - let first = working_copy.unsaved.into_iter().chain([description]); + let first = working_copy.unsaved.take().into_iter().chain([description]); // It starts with the files of the one below, and their conflicts. let layers = self.revision_layers(first.collect(), &encoded, &working_copy.conflicts); - self.save(&layers, id)?; + self.save(&layers, id, &mut working_copy.index)?; // Edits recorded on a revision that is no longer its change's head // make that change diverge. if recorded { - self.flag_divergence(working_copy.id)?; + self.flag_divergence(&working_copy.index, working_copy.id)?; } println!("Working copy: {}", self.headline(&revision)?); println!("On top of: {}", self.headline(&working_copy.revision)?); @@ -496,19 +505,19 @@ impl Workspace { /// Fails if the working copy cannot be snapshotted or the revision /// cannot be written. pub fn describe(&mut self, description: &str) -> Result<(), WorkspaceError> { - let working_copy = self.record()?; + let mut working_copy = self.record()?; let description = EncodedObject::blob(description.as_bytes()); let revision = Revision { predecessors: vec![working_copy.id], description: description.id().into(), author: self.config.user(), - ..working_copy.revision + ..working_copy.revision.clone() }; let encoded = revision.encode(); let id = encoded.id().into(); - let first = working_copy.unsaved.into_iter().chain([description]); + let first = working_copy.unsaved.take().into_iter().chain([description]); let layers = self.revision_layers(first.collect(), &encoded, &working_copy.conflicts); - self.save(&layers, id)?; + self.save(&layers, id, &mut working_copy.index)?; println!("Working copy: {}", self.headline(&revision)?); Ok(()) } @@ -536,7 +545,7 @@ impl Workspace { let encoded = revision.encode(); let id = encoded.id().into(); let layers = self.revision_layers(Vec::new(), &encoded, &working_copy.conflicts); - self.save(&layers, id)?; + self.save(&layers, id, &mut working_copy.index)?; println!("Resolved {}", Self::list_paths(&resolved)); if !working_copy.conflicts.is_empty() { Self::print_conflicts(&working_copy.conflicts); @@ -553,9 +562,9 @@ impl Workspace { /// cannot be snapshotted or checked out. pub fn edit(&mut self, change: &str) -> Result<(), WorkspaceError> { let working_copy = self.refresh()?; - let index = Index::open(&self.store)?; - let change = working_copy.find_change(&index, change)?; - let head = Self::single_head(&index, change)?; + let index = &working_copy.index; + let change = working_copy.find_change(change)?; + let head = Self::single_head(index, change)?; let revision: Revision = self.store.get(head)?; if head != working_copy.id { self.checkout(working_copy.revision.snapshot, revision.snapshot)?; @@ -578,12 +587,12 @@ impl Workspace { /// checked out. pub fn abandon(&mut self, change: Option<&str>, reason: &str) -> Result<(), WorkspaceError> { let working_copy = self.refresh()?; - let index = Index::open(&self.store)?; + let index = &working_copy.index; let change = match change { - Some(change) => working_copy.find_change(&index, change)?, + Some(change) => working_copy.find_change(change)?, None => working_copy.revision.change, }; - let head = Self::single_head(&index, change)?; + let head = Self::single_head(index, change)?; let reason = EncodedObject::blob(reason.as_bytes()); let statement = Statement { subject: RecordId::Revision(head), @@ -649,8 +658,8 @@ impl Workspace { /// cannot be written. pub fn converge(&mut self, change: &str) -> Result<(), WorkspaceError> { let working_copy = self.refresh()?; - let index = Index::open(&self.store)?; - let change = working_copy.find_change(&index, change)?; + let index = &working_copy.index; + let change = working_copy.find_change(change)?; let heads = index.heads(change); if heads.len() < 2 { return Err(WorkspaceError::NotDivergent(change)); @@ -757,9 +766,9 @@ impl Workspace { limit: Option, ) -> Result<(), WorkspaceError> { let working_copy = self.snapshot()?; - let index = Index::open(&self.store)?; + let index = &working_copy.index; let mut next = working_copy.revision.base; - let mut stack = vec![(working_copy.id, working_copy.revision)]; + let mut stack = vec![(working_copy.id, working_copy.revision.clone())]; let acceptance = loop { match next { Base::Revision(id) => { @@ -785,7 +794,7 @@ impl Workspace { println!( "{marker} {}{}", self.headline(revision)?, - self.flags(&index, revision.change)? + self.flags(index, revision.change)? ); } } @@ -803,7 +812,7 @@ impl Workspace { println!( " {}{}", self.headline(&revision)?, - self.flags(&index, revision.change)? + self.flags(index, revision.change)? ); } Source::Target(id) => { @@ -823,10 +832,10 @@ impl Workspace { /// Fails if the change cannot be found, or a record cannot be read. pub fn evolog(&mut self, change: &str) -> Result<(), WorkspaceError> { let working_copy = self.snapshot()?; - let index = Index::open(&self.store)?; - let change = working_copy.find_change(&index, change)?; + let index = &working_copy.index; + let change = working_copy.find_change(change)?; let heads = index.heads(change); - println!("Change {}{}", change.short(), self.flags(&index, change)?); + println!("Change {}{}", change.short(), self.flags(index, change)?); for &id in index.revisions(change).iter().rev() { let revision: Revision = self.store.get(id)?; let marker = if id == working_copy.id { '@' } else { '○' };