diff --git a/apps/origin-src/benches/land.rs b/apps/origin-src/benches/land.rs index 791b91f0..d74d0fff 100644 --- a/apps/origin-src/benches/land.rs +++ b/apps/origin-src/benches/land.rs @@ -106,7 +106,11 @@ impl Repo { } fn land_on(&self, change: ChangeId, onto: &TargetName) { - let landings = self.queue().land_change(change, onto).unwrap(); + let mut annotations = Annotations::open(&self.store).unwrap(); + let landings = self + .queue() + .land_change(&mut annotations, change, onto) + .unwrap(); assert!( matches!(landings[..], [Landing::Landed { .. }]), "{landings:?}" @@ -196,9 +200,10 @@ fn land_target_after_long_histories(bencher: Bencher<'_, '_>) { repo.land_on(change, &feature); }) .bench_local_values(|()| { + let mut annotations = Annotations::open(&repo.store).unwrap(); let landing = repo .queue() - .land_target(&feature, &TargetName::MAIN) + .land_target(&mut annotations, &feature, &TargetName::MAIN) .unwrap(); assert!(matches!(landing, Landing::Landed { .. }), "{landing:?}"); }); diff --git a/apps/origin-src/src/annotations/mod.rs b/apps/origin-src/src/annotations/mod.rs index 2046f932..a9988f03 100644 --- a/apps/origin-src/src/annotations/mod.rs +++ b/apps/origin-src/src/annotations/mod.rs @@ -65,6 +65,22 @@ impl<'a, P: Platform> Annotations<'a, P> { &self.index } + /// Returns the index, so the next user can reuse it. + #[must_use] + pub fn into_index(self) -> Index { + self.index + } + + /// Brings the index up to date with the records written since it was read. + /// See [`Index::refresh`]. + /// + /// # Errors + /// + /// Fails like [`Index::refresh`]. + pub fn refresh(&mut self) -> Result<(), StoreError> { + self.index.refresh(self.store) + } + /// Writes a statement and its payload. Issuing a statement the store /// has is a no-op. /// diff --git a/apps/origin-src/src/index.rs b/apps/origin-src/src/index.rs index 899b85c2..16c0bf05 100644 --- a/apps/origin-src/src/index.rs +++ b/apps/origin-src/src/index.rs @@ -143,6 +143,21 @@ impl Index { Ok(Self::rebuild_locked(store, &index)?.index) } + /// Catches up with the op log of `store`, as [`Index::open`] does, for a + /// process that keeps the index open. It reads only the op log entries + /// since the last catch-up. Reopens the index if the op log no longer has + /// those entries. + /// + /// # Errors + /// + /// Fails like [`Index::open`]. + pub fn refresh(&mut self, store: &Store

) -> Result<(), StoreError> { + if !self.catch_up(store)? { + *self = Self::open(store)?; + } + Ok(()) + } + /// Reconstructs the index of `store` from its records, and replaces the /// one on disk. Records the old index knew keep their arrival order. /// Objects that are corrupt or don't decode are skipped, and reported on @@ -1293,6 +1308,25 @@ mod tests { ); } + #[test] + fn refreshing_catches_up_an_index_kept_open() { + let (_dir, store) = store(); + let mut index = Index::open(&store).unwrap(); + let first = revision(change(1), &[], "first"); + store.write_all(slice::from_ref(&first)).unwrap(); + index.refresh(&store).unwrap(); + assert_eq!(index, Index::open(&store).unwrap()); + + // Once the op log dropped what it missed, it reads the index again. + for i in 0..100 { + let object = revision(change(2), &[], &format!("{i}")); + store.write_all(slice::from_ref(&object)).unwrap(); + } + index.refresh(&store).unwrap(); + assert_eq!(index.revisions(change(2)).len(), 100); + assert_eq!(index.heads(change(1)), [revision_id(&first)]); + } + #[test] fn rejects_records_that_do_not_decode() { let (_dir, store) = store(); diff --git a/apps/origin-src/src/queue/mod.rs b/apps/origin-src/src/queue/mod.rs index 1623f3ca..e5ec5395 100644 --- a/apps/origin-src/src/queue/mod.rs +++ b/apps/origin-src/src/queue/mod.rs @@ -128,6 +128,8 @@ impl<'a, P: Platform> Queue<'a, P> { /// from the bottom of the stack up, and returns the outcome of each. Stops /// at the first one that waits, which the queue keeps as a landing of /// `change`, so retrying it lands the rest of the stack once that one can. + /// Statements are read through `annotations`, whose index is brought up to + /// date first, so a caller that keeps one open avoids reading it again. /// /// # Errors /// @@ -136,10 +138,11 @@ impl<'a, P: Platform> Queue<'a, P> { /// cannot be read or written. pub fn land_change( &self, + annotations: &mut Annotations<'a, P>, change: ChangeId, onto: &TargetName, ) -> Result, LandError> { - let mut session = self.session(onto)?; + let mut session = self.session(annotations, onto)?; let stack = session.stack(change)?; if stack.is_empty() { session.forget(&Proposal::Change(change))?; @@ -158,7 +161,8 @@ impl<'a, P: Platform> Queue<'a, P> { Ok(landings) } - /// Lands the tip of `source` on `onto`. + /// Lands the tip of `source` on `onto`, reading statements through + /// `annotations` as [`Queue::land_change`] does. /// /// # Errors /// @@ -167,13 +171,14 @@ impl<'a, P: Platform> Queue<'a, P> { /// written. pub fn land_target( &self, + annotations: &mut Annotations<'a, P>, source: &TargetName, onto: &TargetName, ) -> Result { if source == onto { return Err(LandError::SameTarget(onto.clone())); } - let mut session = self.session(onto)?; + let mut session = self.session(annotations, onto)?; let tip = self .store .head(source)? @@ -343,10 +348,15 @@ impl<'a, P: Platform> Queue<'a, P> { Ok(()) } - /// Starts landing on `onto`: locks its queue, then reads the policy in - /// force and the target's tip. - fn session(&self, onto: &TargetName) -> Result, LandError> { + /// Starts landing on `onto`: locks its queue, reads the policy in force and + /// the target's tip, and brings `annotations` up to date. + fn session<'q>( + &'q self, + annotations: &'q mut Annotations<'a, P>, + onto: &TargetName, + ) -> Result, LandError> { let lock = self.lock(onto)?; + annotations.refresh()?; let tip = self .store .head(onto)? @@ -367,7 +377,7 @@ impl<'a, P: Platform> Queue<'a, P> { policy_head, state: self.store.get(tip.acceptance)?.body().state, tip, - annotations: Annotations::open(self.store)?, + annotations, _lock: lock, }) } diff --git a/apps/origin-src/src/queue/session.rs b/apps/origin-src/src/queue/session.rs index ef3ea13f..8e94ef19 100644 --- a/apps/origin-src/src/queue/session.rs +++ b/apps/origin-src/src/queue/session.rs @@ -35,7 +35,9 @@ pub(super) struct Session<'q, 'a, P> { /// The target's latest acceptance, and its snapshot. pub(super) tip: Head, pub(super) state: TreeId, - pub(super) annotations: Annotations<'a, P>, + /// The statements, through an index brought up to date when the lock was + /// taken. + pub(super) annotations: &'q mut Annotations<'a, P>, pub(super) _lock: LockedFile, } diff --git a/apps/origin-src/src/queue/tests/changes.rs b/apps/origin-src/src/queue/tests/changes.rs index c694400d..e411f47b 100644 --- a/apps/origin-src/src/queue/tests/changes.rs +++ b/apps/origin-src/src/queue/tests/changes.rs @@ -79,7 +79,11 @@ fn refuses_divergent_and_abandoned_changes() { let main = TargetName::MAIN; let error = repo .queue() - .land_change(ChangeId::from_bytes([1; 16]), &main) + .land_change( + &mut repo.annotations(), + ChangeId::from_bytes([1; 16]), + &main, + ) .unwrap_err(); assert!(matches!(error, LandError::Divergent { .. }), "{error}"); @@ -97,7 +101,11 @@ fn refuses_divergent_and_abandoned_changes() { repo.store.write_all(&[abandon.encode()]).unwrap(); let error = repo .queue() - .land_change(ChangeId::from_bytes([2; 16]), &main) + .land_change( + &mut repo.annotations(), + ChangeId::from_bytes([2; 16]), + &main, + ) .unwrap_err(); assert!(matches!(error, LandError::Abandoned(_)), "{error}"); } @@ -108,12 +116,16 @@ fn only_the_authority_lands() { repo.change_on_main(1, &[("a", "a")]); let change = ChangeId::from_bytes([1; 16]); let other = Queue::new(&repo.store, &repo.key, PrincipalName::new("other").unwrap()); - let error = other.land_change(change, &TargetName::MAIN).unwrap_err(); + let error = other + .land_change(&mut repo.annotations(), change, &TargetName::MAIN) + .unwrap_err(); assert!(matches!(error, LandError::NotAuthority { .. }), "{error}"); let impostor = NodeKey::from_bytes(&[8; 32]); let queue = Queue::new(&repo.store, &impostor, PrincipalName::new("queue").unwrap()); - let error = queue.land_change(change, &TargetName::MAIN).unwrap_err(); + let error = queue + .land_change(&mut repo.annotations(), change, &TargetName::MAIN) + .unwrap_err(); assert!(matches!(error, LandError::WrongKey(_)), "{error}"); assert_eq!(repo.tip(&TargetName::MAIN).seq, 0); } diff --git a/apps/origin-src/src/queue/tests/conflicts.rs b/apps/origin-src/src/queue/tests/conflicts.rs index 84005566..6a0a6f76 100644 --- a/apps/origin-src/src/queue/tests/conflicts.rs +++ b/apps/origin-src/src/queue/tests/conflicts.rs @@ -81,7 +81,11 @@ fn unresolved_conflicts_never_land() { ); let error = repo .queue() - .land_change(ChangeId::from_bytes([1; 16]), &TargetName::MAIN) + .land_change( + &mut repo.annotations(), + ChangeId::from_bytes([1; 16]), + &TargetName::MAIN, + ) .unwrap_err(); assert!( matches!(&error, LandError::Unresolved { paths, .. } if *paths == [path.clone()]), diff --git a/apps/origin-src/src/queue/tests/mod.rs b/apps/origin-src/src/queue/tests/mod.rs index b64b82ae..2b24e3a2 100644 --- a/apps/origin-src/src/queue/tests/mod.rs +++ b/apps/origin-src/src/queue/tests/mod.rs @@ -141,9 +141,17 @@ impl Repo { pub(crate) fn land(&self, change: u8) -> Vec { self.queue() - .land_change(ChangeId::from_bytes([change; 16]), &TargetName::MAIN) + .land_change( + &mut self.annotations(), + ChangeId::from_bytes([change; 16]), + &TargetName::MAIN, + ) .unwrap() } + + pub(crate) fn annotations(&self) -> Annotations<'_> { + Annotations::open(&self.store).unwrap() + } } fn change(id: u8) -> Proposal { diff --git a/apps/origin-src/src/queue/tests/policy.rs b/apps/origin-src/src/queue/tests/policy.rs index 1aa2b1cb..4d624658 100644 --- a/apps/origin-src/src/queue/tests/policy.rs +++ b/apps/origin-src/src/queue/tests/policy.rs @@ -66,7 +66,11 @@ fn collection_applies_a_policy_landed_while_it_waits() { repo.revision(2, &[], tip, &[("policy.toml", &longer)]); let landed = repo .queue() - .land_change(ChangeId::from_bytes([2; 16]), &TargetName::POLICY) + .land_change( + &mut repo.annotations(), + ChangeId::from_bytes([2; 16]), + &TargetName::POLICY, + ) .unwrap(); assert!(matches!(landed[..], [Landing::Landed { .. }]), "{landed:?}"); drop(held); @@ -82,9 +86,11 @@ fn policy_landings_keep_a_valid_policy() { let tip = Base::Acceptance(repo.tip(&policy).acceptance); for (change, toml) in [(1, "not = [valid"), (2, "[nodes]\n")] { repo.revision(change, &[], tip, &[("policy.toml", toml)]); - let result = repo - .queue() - .land_change(ChangeId::from_bytes([change; 16]), &policy); + let result = repo.queue().land_change( + &mut repo.annotations(), + ChangeId::from_bytes([change; 16]), + &policy, + ); assert!( matches!( result, @@ -113,7 +119,11 @@ fn policy_landings_keep_a_valid_policy() { .unwrap(); let landings = repo .queue() - .land_change(ChangeId::from_bytes([3; 16]), &policy) + .land_change( + &mut repo.annotations(), + ChangeId::from_bytes([3; 16]), + &policy, + ) .unwrap(); assert!( matches!(landings[..], [Landing::Landed { .. }]), @@ -146,7 +156,11 @@ fn each_policy_landing_is_governed_by_the_one_before() { repo.revision(2, &[], Base::Revision(first), &[("policy.toml", &open)]); let landings = repo .queue() - .land_change(ChangeId::from_bytes([2; 16]), &policy) + .land_change( + &mut repo.annotations(), + ChangeId::from_bytes([2; 16]), + &policy, + ) .unwrap(); assert_eq!(landed(&landings[0]), first); assert!(matches!(landings[1], Landing::Pending(_)), "{landings:?}"); diff --git a/apps/origin-src/src/queue/tests/targets.rs b/apps/origin-src/src/queue/tests/targets.rs index 36926fc4..17a0493b 100644 --- a/apps/origin-src/src/queue/tests/targets.rs +++ b/apps/origin-src/src/queue/tests/targets.rs @@ -22,7 +22,11 @@ fn targets_land_on_each_other() { let on_overhaul = Base::Acceptance(repo.tip(&overhaul).acceptance); repo.revision(2, &[], on_overhaul, &[("shared", "v2"), ("new", "n")]); queue - .land_change(ChangeId::from_bytes([2; 16]), &overhaul) + .land_change( + &mut repo.annotations(), + ChangeId::from_bytes([2; 16]), + &overhaul, + ) .unwrap(); repo.change_on_main(3, &[("other", "o")]); repo.land(3); @@ -30,10 +34,14 @@ fn targets_land_on_each_other() { // Catching up merges `main`'s change since the fork; finishing // lands the overhaul on `main`. assert!(matches!( - queue.land_target(&main, &overhaul).unwrap(), + queue + .land_target(&mut repo.annotations(), &main, &overhaul) + .unwrap(), Landing::Landed { .. } )); - let landing = queue.land_target(&overhaul, &main).unwrap(); + let landing = queue + .land_target(&mut repo.annotations(), &overhaul, &main) + .unwrap(); assert!(matches!( landing, Landing::Landed { @@ -45,11 +53,13 @@ fn targets_land_on_each_other() { assert_eq!(repo.state(&main), expected); assert_eq!(repo.state(&overhaul), expected); assert_eq!( - queue.land_target(&overhaul, &main).unwrap(), + queue + .land_target(&mut repo.annotations(), &overhaul, &main) + .unwrap(), Landing::Unchanged(Proposal::Target(overhaul.clone())) ); assert!(matches!( - queue.land_target(&main, &main), + queue.land_target(&mut repo.annotations(), &main, &main), Err(LandError::SameTarget(_)) )); } @@ -67,7 +77,7 @@ fn sibling_targets_merge_from_where_they_forked() { let on = |target| Base::Acceptance(repo.tip(target).acceptance); repo.revision(2, &[], on(&a), &[("keep", "k")]); queue - .land_change(ChangeId::from_bytes([2; 16]), &a) + .land_change(&mut repo.annotations(), ChangeId::from_bytes([2; 16]), &a) .unwrap(); repo.revision( 3, @@ -76,10 +86,10 @@ fn sibling_targets_merge_from_where_they_forked() { &[("keep", "k"), ("delete", "d"), ("other", "o")], ); queue - .land_change(ChangeId::from_bytes([3; 16]), &b) + .land_change(&mut repo.annotations(), ChangeId::from_bytes([3; 16]), &b) .unwrap(); assert!(matches!( - queue.land_target(&a, &b).unwrap(), + queue.land_target(&mut repo.annotations(), &a, &b).unwrap(), Landing::Landed { .. } )); assert_eq!(repo.state(&b), repo.files(&[("keep", "k"), ("other", "o")])); @@ -108,7 +118,11 @@ fn a_merge_that_changes_nothing_still_records_the_tip() { let on = Base::Acceptance(repo.tip(target).acceptance); pass(RecordId::Revision(repo.revision(change, &[], on, files))); queue - .land_change(ChangeId::from_bytes([change; 16]), target) + .land_change( + &mut repo.annotations(), + ChangeId::from_bytes([change; 16]), + target, + ) .unwrap(); }; queue.create_target(&a, &TargetName::MAIN).unwrap(); @@ -118,24 +132,24 @@ fn a_merge_that_changes_nothing_still_records_the_tip() { land(2, &b, &[("f", "f")]); let state = repo.state(&b); assert!(matches!( - queue.land_target(&a, &b).unwrap(), + queue.land_target(&mut repo.annotations(), &a, &b).unwrap(), Landing::Landed { .. } )); assert_eq!(repo.state(&b), state); assert_eq!( - queue.land_target(&a, &b).unwrap(), + queue.land_target(&mut repo.annotations(), &a, &b).unwrap(), Landing::Unchanged(Proposal::Target(a.clone())) ); // So once `a` deletes the file, landing it on `b` deletes it there. land(3, &a, &[]); - let landing = queue.land_target(&a, &b).unwrap(); + let landing = queue.land_target(&mut repo.annotations(), &a, &b).unwrap(); let Landing::Pending(pending) = landing else { panic!("a deletion waits for its evidence: {landing:?}") }; pass(pending.subject); assert!(matches!( - queue.land_target(&a, &b).unwrap(), + queue.land_target(&mut repo.annotations(), &a, &b).unwrap(), Landing::Landed { .. } )); assert_eq!(repo.state(&b), repo.files(&[])); @@ -147,7 +161,9 @@ fn a_merge_that_changes_nothing_still_records_the_tip() { for _ in 0..2 { for (from, onto) in [(&c, &TargetName::MAIN), (&TargetName::MAIN, &c)] { assert_eq!( - queue.land_target(from, onto).unwrap(), + queue + .land_target(&mut repo.annotations(), from, onto) + .unwrap(), Landing::Unchanged(Proposal::Target(from.clone())) ); } @@ -163,7 +179,11 @@ fn targets_merge_from_their_newest_shared_acceptance() { let on = Base::Acceptance(repo.tip(target).acceptance); repo.revision(change, &[], on, files); queue - .land_change(ChangeId::from_bytes([change; 16]), target) + .land_change( + &mut repo.annotations(), + ChangeId::from_bytes([change; 16]), + target, + ) .unwrap(); }; // `c` and `d` fork from an empty `main`, `a` and `b` from the next @@ -184,16 +204,16 @@ fn targets_merge_from_their_newest_shared_acceptance() { land(30, &c, &[("c", "c")]); land(31, &d, &[("d", "d")]); assert!(matches!( - queue.land_target(&c, &a).unwrap(), + queue.land_target(&mut repo.annotations(), &c, &a).unwrap(), Landing::Landed { .. } )); assert!(matches!( - queue.land_target(&d, &b).unwrap(), + queue.land_target(&mut repo.annotations(), &d, &b).unwrap(), Landing::Landed { .. } )); assert!(matches!( - queue.land_target(&a, &b).unwrap(), + queue.land_target(&mut repo.annotations(), &a, &b).unwrap(), Landing::Landed { .. } )); assert_eq!( @@ -212,7 +232,11 @@ fn criss_crossed_targets_merge_what_their_bases_agree_on() { let on = Base::Acceptance(repo.tip(target).acceptance); repo.revision(change, &[], on, files); queue - .land_change(ChangeId::from_bytes([change; 16]), target) + .land_change( + &mut repo.annotations(), + ChangeId::from_bytes([change; 16]), + target, + ) .unwrap(); }; queue.create_target(&a, &TargetName::MAIN).unwrap(); @@ -241,7 +265,8 @@ fn criss_crossed_targets_merge_what_their_bases_agree_on() { land(3, &a, &[("a", "1"), ("b", "0"), ("shared", "a")]); land(4, &b, &[("a", "0"), ("b", "1"), ("shared", "b")]); - let Landing::Pending(pending) = queue.land_target(&a, &b).unwrap() else { + let Landing::Pending(pending) = queue.land_target(&mut repo.annotations(), &a, &b).unwrap() + else { panic!("the bases disagree on `shared`, so it can't be merged blindly"); }; assert!( @@ -251,7 +276,7 @@ fn criss_crossed_targets_merge_what_their_bases_agree_on() { // Once `a` takes `b`'s side, the disjoint edits merge. land(5, &a, &[("a", "1"), ("b", "0"), ("shared", "b")]); assert!(matches!( - queue.land_target(&a, &b).unwrap(), + queue.land_target(&mut repo.annotations(), &a, &b).unwrap(), Landing::Landed { .. } )); assert_eq!( @@ -268,7 +293,11 @@ fn bases_that_disagree_on_a_kind_conflict_on_any_change_under_it() { let on = Base::Acceptance(repo.tip(target).acceptance); repo.revision(change, &[], on, files); queue - .land_change(ChangeId::from_bytes([change; 16]), target) + .land_change( + &mut repo.annotations(), + ChangeId::from_bytes([change; 16]), + target, + ) .unwrap(); }; let [a, b] = ["a", "b"].map(|name| TargetName::new(name).unwrap()); @@ -299,7 +328,8 @@ fn bases_that_disagree_on_a_kind_conflict_on_any_change_under_it() { // What `p` was is unknown, so the deletion can't be told from an // addition: it conflicts, rather than bring `p/delete` back. - let Landing::Pending(pending) = queue.land_target(&a, &b).unwrap() else { + let Landing::Pending(pending) = queue.land_target(&mut repo.annotations(), &a, &b).unwrap() + else { panic!("the bases disagree on `p`, so it can't be merged blindly"); }; assert!( diff --git a/apps/origin-src/src/workspace/land.rs b/apps/origin-src/src/workspace/land.rs index 02e43d0b..6446f298 100644 --- a/apps/origin-src/src/workspace/land.rs +++ b/apps/origin-src/src/workspace/land.rs @@ -16,12 +16,15 @@ //! those files change or `origin resolve` accepts them (see //! [`conflict`](crate::conflict)). +use std::mem; + use origin_shared::landing::{Blocked, Landing, Pending, Proposal}; use origin_shared::{ Base, ChangeId, EncodedObject, Record, Revision, RevisionId, Source, TargetName, TreeId, }; use super::{Behind, WorkingCopy, Workspace, WorkspaceError}; +use crate::annotations::Annotations; use crate::conflict::Conflicts; use crate::index::Index; use crate::oplog::Why; @@ -50,8 +53,12 @@ impl Workspace { None => self.target_of(Self::single_head(&working_copy.index, change)?)?, }; let key = self.config.node_key()?; - let landings = - Queue::new(&self.store, &key, self.config.node().clone()).land_change(change, &onto)?; + let mut annotations = Annotations::new(&self.store, mem::take(&mut working_copy.index)); + let landings = Queue::new(&self.store, &key, self.config.node().clone()).land_change( + &mut annotations, + change, + &onto, + )?; for landing in &landings { self.print_landing(landing, &onto)?; } @@ -63,9 +70,9 @@ impl Workspace { let unchanged = Landing::Unchanged(Proposal::Change(working_copy.revision.change)); if landings.contains(&unchanged) { let base = working_copy.revision.snapshot; - self.start_on(&working_copy, &onto, "", base)?; + self.start_on(&mut working_copy, &onto, "", base)?; } else { - self.follow(&working_copy)?; + self.follow(&mut working_copy)?; } Ok(()) } @@ -79,8 +86,11 @@ impl Workspace { /// the records cannot be read or written. pub fn land_target(&self, from: &TargetName, onto: &TargetName) -> Result<(), WorkspaceError> { let key = self.config.node_key()?; - let landing = - Queue::new(&self.store, &key, self.config.node().clone()).land_target(from, onto)?; + let landing = Queue::new(&self.store, &key, self.config.node().clone()).land_target( + &mut Annotations::open(&self.store)?, + from, + onto, + )?; self.print_landing(&landing, onto) } @@ -158,8 +168,9 @@ impl Workspace { /// Fails if the target doesn't exist, the working copy cannot be snapshotted or /// is behind its change, or the change cannot be written or checked out. pub fn new_on(&mut self, target: &TargetName, description: &str) -> Result<(), WorkspaceError> { - let working_copy = self.refresh()?; - self.start_on(&working_copy, target, description, working_copy.files) + let mut working_copy = self.refresh()?; + let files = working_copy.files; + self.start_on(&mut working_copy, target, description, files) } /// `origin pull`: moves the working copy to where its change went without it, @@ -174,8 +185,8 @@ impl Workspace { /// Fails if the working copy cannot be snapshotted, a file was edited during /// the move, or the records cannot be read or written. pub fn pull(&mut self) -> Result<(), WorkspaceError> { - let working_copy = self.save_draft()?; - if self.follow(&working_copy)? { + let mut working_copy = self.save_draft()?; + if self.follow(&mut working_copy)? { return Ok(()); } let Base::Acceptance(base) = working_copy.revision.base else { @@ -192,7 +203,7 @@ impl Workspace { return Ok(()); } let files = self.base_tree(working_copy.revision.base)?; - self.rebase_onto_tip(&working_copy, files, &target) + self.rebase_onto_tip(&mut working_copy, files, &target) } /// Starts a change with `description` on the tip of `target`, carrying over the @@ -200,7 +211,7 @@ impl Workspace { /// over them. fn start_on( &mut self, - working_copy: &WorkingCopy, + working_copy: &mut WorkingCopy, target: &TargetName, description: &str, base: TreeId, @@ -245,7 +256,7 @@ impl Workspace { /// Moves the working copy to where its change went without it, merging the /// edits made since, or rebases it onto the tip once the change below it /// landed. Returns whether it moved. - fn follow(&mut self, working_copy: &WorkingCopy) -> Result { + fn follow(&mut self, working_copy: &mut WorkingCopy) -> Result { match &working_copy.behind { Some(Behind::Landed { target, .. }) => { let base = working_copy.revision.snapshot; @@ -291,7 +302,7 @@ impl Workspace { /// with the edits made since in a new revision that evolves from it. fn move_onto( &mut self, - working_copy: &WorkingCopy, + working_copy: &mut WorkingCopy, rebased: RevisionId, ) -> Result<(), WorkspaceError> { let theirs: Revision = self.store.get(rebased)?; @@ -324,7 +335,7 @@ impl Workspace { /// Rebases the working copy, made on `base`, onto the tip of `onto`. fn rebase_onto_tip( &mut self, - working_copy: &WorkingCopy, + working_copy: &mut WorkingCopy, base: TreeId, onto: &TargetName, ) -> Result<(), WorkspaceError> { @@ -367,7 +378,7 @@ impl Workspace { /// revision is checked out. fn write_merge( &self, - working_copy: &WorkingCopy, + working_copy: &mut WorkingCopy, merge: Merge, with: Vec, revision: &EncodedObject, diff --git a/apps/origin-src/src/workspace/mod.rs b/apps/origin-src/src/workspace/mod.rs index ebe951a2..41544d28 100644 --- a/apps/origin-src/src/workspace/mod.rs +++ b/apps/origin-src/src/workspace/mod.rs @@ -56,6 +56,7 @@ pub use annotate::{Annotated, Payload}; use std::fmt; use std::fs; use std::io; +use std::mem; use std::path::{Path, PathBuf}; use origin_platform::{LockedFile, Native}; @@ -442,9 +443,9 @@ impl Workspace { } None => loaded, }; - let index = Index::open(&self.store)?; + let mut index = Index::open(&self.store)?; let behind = self.behind(&index, self.revision, &revision)?; - let mut conflicts = self.conflicts(self.revision, &revision)?; + let mut conflicts = self.conflicts(&mut index, self.revision, &revision)?; if behind.is_none() { conflicts = conflicts.remaining(&self.store, files)?; } @@ -554,18 +555,25 @@ impl Workspace { Ok(()) } - /// Returns the unresolved conflicts of `revision`, whose ID is `id`. - fn conflicts(&self, id: RevisionId, revision: &Revision) -> Result { - let annotations = Annotations::open(&self.store)?; - let claims = annotations.claims(RecordId::Revision(id))?; - Ok(Conflicts::of(&self.store, &claims, revision)?) + /// Returns the unresolved conflicts of `revision`, whose ID is `id`, found + /// through `index`. + fn conflicts( + &self, + index: &mut Index, + id: RevisionId, + revision: &Revision, + ) -> Result { + let annotations = Annotations::new(&self.store, mem::take(index)); + let claims = annotations.claims(RecordId::Revision(id)); + *index = annotations.into_index(); + Ok(Conflicts::of(&self.store, &claims?, revision)?) } /// Returns the unresolved conflicts a new change on `base` starts with: those /// of the revision `base` is, or none if `base` is an acceptance. - fn base_conflicts(&self, base: Base) -> Result { + fn base_conflicts(&self, index: &mut Index, base: Base) -> Result { match base { - Base::Revision(id) => self.conflicts(id, &self.store.get(id)?), + Base::Revision(id) => self.conflicts(index, id, &self.store.get(id)?), Base::Acceptance(_) => Ok(Conflicts::default()), } } @@ -1056,7 +1064,7 @@ impl Workspace { /// cannot be written. Then no abandonment is written and nothing is /// checked out. pub fn abandon(&mut self, change: Option<&str>, reason: &str) -> Result<(), WorkspaceError> { - let working_copy = self.refresh()?; + let mut working_copy = self.refresh()?; let index = &working_copy.index; let change = match change { Some(change) => working_copy.find_change(change)?, @@ -1100,7 +1108,7 @@ impl Workspace { author: self.config.user(), }; let encoded = revision.encode(); - let conflicts = self.base_conflicts(revision.base)?; + let conflicts = self.base_conflicts(&mut working_copy.index, revision.base)?; let mut layers = self.revision_layers(vec![reason, empty], &encoded, &conflicts); // Published before the revision that replaces the change. layers[1].push(statement); @@ -1126,10 +1134,9 @@ impl Workspace { /// Fails if the change cannot be found or hasn't diverged, or the revision /// cannot be written. pub fn converge(&mut self, change: &str) -> Result<(), WorkspaceError> { - let working_copy = self.refresh()?; - let index = &working_copy.index; + let mut working_copy = self.refresh()?; let change = working_copy.find_change(change)?; - let heads = index.heads(change); + let heads = working_copy.index.heads(change); if heads.len() < 2 { return Err(WorkspaceError::NotDivergent(change)); } @@ -1163,7 +1170,8 @@ impl Workspace { // The heads' own, where the merge kept their side. let mut conflicts = Conflicts::default(); for &head in &heads { - let unresolved = self.conflicts(head, &self.store.get(head)?)?; + let revision = self.store.get(head)?; + let unresolved = self.conflicts(&mut working_copy.index, head, &revision)?; conflicts = conflicts.with(unresolved.remaining(&self.store, snapshot)?); } let conflicts = conflicts.with(merged); diff --git a/apps/origin-src/src/workspace/ops.rs b/apps/origin-src/src/workspace/ops.rs index 53600d52..d769bbe5 100644 --- a/apps/origin-src/src/workspace/ops.rs +++ b/apps/origin-src/src/workspace/ops.rs @@ -131,7 +131,7 @@ impl Workspace { /// Fails if there is nothing to undo, the latest operation created the /// workspace, or the working copy cannot be snapshotted or moved. pub fn undo(&mut self) -> Result<(), WorkspaceError> { - let working_copy = self.refresh()?; + let mut working_copy = self.refresh()?; let (op, command, from) = self.last_undoable(working_copy.id)?; let revision: Revision = self.store.get(from)?; let heads = working_copy.index.heads(revision.change); @@ -140,7 +140,7 @@ impl Workspace { } else { // A newer revision of the change exists: restore this one's // contents, and its unresolved conflicts, in a revision after it. - let conflicts = self.conflicts(from, &revision)?; + let conflicts = self.conflicts(&mut working_copy.index, from, &revision)?; let restored = Revision { predecessors: heads, author: self.config.user(), diff --git a/apps/origin-src/src/workspace/registry.rs b/apps/origin-src/src/workspace/registry.rs index 7fbf348d..bb28dc58 100644 --- a/apps/origin-src/src/workspace/registry.rs +++ b/apps/origin-src/src/workspace/registry.rs @@ -42,7 +42,7 @@ impl Workspace { change: Option<&str>, path: Option<&Path>, ) -> Result<(), WorkspaceError> { - let working_copy = self.refresh()?; + let mut working_copy = self.refresh()?; let (id, revision, records) = if let Some(change) = change { let change = working_copy.find_change(change)?; let head = Self::single_head(&working_copy.index, change)?; @@ -61,7 +61,7 @@ impl Workspace { }; let encoded = revision.encode(); let id = encoded.id().into(); - let conflicts = self.base_conflicts(base)?; + let conflicts = self.base_conflicts(&mut working_copy.index, base)?; let layers = self.revision_layers(vec![description], &encoded, &conflicts); (id, revision, layers) };