diff --git a/apps/origin-src/src/workspace/mod.rs b/apps/origin-src/src/workspace/mod.rs index 1fe5a5f..ec6da43 100644 --- a/apps/origin-src/src/workspace/mod.rs +++ b/apps/origin-src/src/workspace/mod.rs @@ -86,9 +86,9 @@ use std::sync::OnceLock; use origin_platform::{LockMode, LockedFile, Native}; use origin_shared::api::{LogQuery, RevisionEntry, Revisions}; use origin_shared::{ - AcceptanceId, AttachmentId, Base, ChangeId, EncodedObject, Kind, Principal, Record, RecordId, - RepoPath, Revision, RevisionId, Source, Statement, StatementId, TargetId, TargetName, Tree, - TreeId, + AcceptanceId, AttachmentId, Base, ChangeId, EncodedObject, Kind, ObjectKind, Principal, Record, + RecordId, RepoPath, Revision, RevisionId, Source, Statement, StatementId, TargetId, TargetName, + Tree, TreeId, }; use crate::ancestry::Ancestry; @@ -727,14 +727,56 @@ impl Workspace { } /// Writes `layers` of objects in order, each durable before the next, - /// so objects may refer to those of earlier layers. + /// so objects may refer to those of earlier layers. Revisions are + /// published holding the lock of every publication of revisions (see + /// [`Workspace::publishing`]). fn write(&self, layers: &[Vec]) -> Result<(), WorkspaceError> { + let mut publishing = None; for layer in layers.iter().filter(|layer| !layer.is_empty()) { + if publishing.is_none() { + publishing = self.publishing(layer)?; + } self.store.write_all(layer)?; } Ok(()) } + /// Takes the lock every publication of revisions takes (see + /// [`Store::lock_pushes`]) if `objects` hold a revision, once no + /// revision they hold evolves from one the landing queue rebased since + /// the command looked: the queue checks a change's heads under it + /// before it publishes a rebase, so the change never diverges. + fn publishing(&self, objects: &[EncodedObject]) -> Result, WorkspaceError> { + let mut revisions = Vec::new(); + for object in objects { + if object.kind().ok() == Some(ObjectKind::Revision) { + let revision: Revision = + object.decode_as().map_err(|source| StoreError::Decode { + id: object.id(), + source, + })?; + revisions.push(revision); + } + } + if revisions.is_empty() { + return Ok(None); + } + let lock = self.store.lock_pushes()?; + let index = Index::open(&self.store)?; + for revision in &revisions { + for &predecessor in &revision.predecessors { + let before = self.store.get(predecessor)?; + if let Some(behind @ Behind::Rebased(_)) = + self.behind(&index, predecessor, &before)? + { + let behind = self.describe_behind(revision.change, &behind)?; + return Err(WorkspaceError::Behind(behind)); + } + } + } + Ok(Some(lock)) + } + /// Durably records that the working copy, which has the files of /// `revision`, is `revision`, and logs the move. fn point_at(&mut self, revision: RevisionId, why: Why) -> Result<(), WorkspaceError> { diff --git a/apps/origin-src/tests/cli.rs b/apps/origin-src/tests/cli.rs index e34762a..7fc4125 100644 --- a/apps/origin-src/tests/cli.rs +++ b/apps/origin-src/tests/cli.rs @@ -2005,3 +2005,37 @@ fn watch_prints_events_from_a_cursor_or_as_they_happen() { "{issued}" ); } + +#[test] +fn revisions_never_follow_one_the_queue_rebased_meanwhile() { + let repo = Repo::new(); + repo.write("file", "work\n"); + repo.ok(&["describe", "Work"]); + let head: RevisionId = repo.revision().parse().unwrap(); + let store = Store::new(repo.path(".origin"), Native::default()); + // The queue checked the head is the change's only one, and is about to + // publish its rebase, while the user describes the change. + let publishing = store.lock_pushes().unwrap(); + let describe = repo + .command(repo.root(), &["describe", "Edited"]) + .stdout(Stdio::null()) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); + thread::sleep(Duration::from_millis(500)); + let rebased = Revision { + predecessors: vec![head], + author: "node:queue".parse().unwrap(), + ..store.get(head).unwrap() + }; + store.write_all(&[rebased.encode()]).unwrap(); + drop(publishing); + + // The description waited, then found the head rebased. + let described = describe.wait_with_output().unwrap(); + let error = String::from_utf8_lossy(&described.stderr); + assert!(!described.status.success(), "{error}"); + assert!(error.contains("rebased"), "{error}"); + let status = repo.ok(&["status"]); + assert!(!status.contains("divergent"), "{status}"); +}