Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions apps/origin-src/src/workspace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
//! workspace the revision the working copy is
//! state the stat cache of the last snapshot
//! moving the move in progress, if a checkout is
//! lock held by the command working in the working copy
//! ```
//!
//! A repository can have several working copies, its workspaces, which
//! share its store and op log. The others have only `workspace`, `state`
//! and `moving` in their `.origin`, and a `repository` file naming the main
//! one's.
//! share its store and op log. The others have only `workspace`, `state`,
//! `moving` and `lock` in their `.origin`, and a `repository` file naming
//! the main one's. Commands in different workspaces run at once; in one,
//! each waits for the one before, except `watch`, which never touches the
//! working copy, and `workspace rm`, which opens the workspace it removes.
//!
//! The working copy is always on a revision of the current change. Every
//! command that reads it snapshots it first. The files the snapshot finds
Expand Down Expand Up @@ -78,7 +81,7 @@ use std::mem;
use std::path::{Path, PathBuf};
use std::sync::{Arc, OnceLock};

use origin_platform::{LockedFile, Native};
use origin_platform::{LockMode, LockedFile, Native};
use origin_shared::api::{LogQuery, RevisionEntry, Revisions};
use origin_shared::{
AcceptanceId, Base, BlobId, ChangeId, EncodedObject, Kind, Principal, Record, RecordId,
Expand Down Expand Up @@ -118,6 +121,9 @@ pub struct Workspace {
unreadable_move: Option<PathBuf>,
/// The repository's remote, once opened: see [`Workspace::remote`].
remote: OnceLock<Option<Remote>>,
/// Held while the command that opened the working copy works in it,
/// so only one at a time does: see [`Workspace::open`].
lock: Option<LockedFile>,
}

/// The working copy as of a snapshot.
Expand Down Expand Up @@ -235,6 +241,10 @@ impl Workspace {
/// in progress: see [`Moving`].
const MOVING: &str = "moving";

/// The file in [`Workspace::META_DIR`] held locked by the command
/// working in the working copy.
const LOCK: &str = "lock";

/// `origin init`: creates a repository in `root`, with the `policy`
/// target naming this node the authority of `policy` and `main`, an
/// empty `main`, and a first change on top of it.
Expand Down Expand Up @@ -303,10 +313,19 @@ impl Workspace {

/// Opens the working copy at `root`: the main one, whose `.origin` holds
/// the store, or another workspace, whose `.origin/repository` names
/// the main one's. Finishes a move a crash cut short first.
/// the main one's. Waits for the command working in it, if any, then
/// finishes a move a crash cut short.
fn open(root: &Path, config: UserConfig, actor: &Actor) -> Result<Self, WorkspaceError> {
let root = &Self::canonical(root)?;
let meta = root.join(Self::META_DIR);
// Held until the command is done, so `moving` is only ever found
// once the move it records was cut short.
let lock = LockedFile::open(
&Native::default(),
&meta.join(Self::LOCK),
LockMode::Exclusive,
)
.map_err(StoreError::from)?;
let repository = meta.join(Self::REPOSITORY);
let store = match Self::read_path(&repository) {
Ok(store) if store.is_dir() => store,
Expand All @@ -320,7 +339,10 @@ impl Workspace {
Self::read_pointer(root)?.ok_or_else(|| WorkspaceError::Incomplete(root.to_owned()))?;
let store = Store::open_writable(store, Native::default())?;
let store = store.with_actor(actor.in_workspace(root));
let mut workspace = Self::at(root.to_owned(), store, config, revision);
let mut workspace = Self {
lock: Some(lock),
..Self::at(root.to_owned(), store, config, revision)
};
workspace.finish_move()?;
Ok(workspace)
}
Expand All @@ -336,6 +358,7 @@ impl Workspace {
unlogged: None,
unreadable_move: None,
remote: OnceLock::new(),
lock: None,
}
}

Expand Down
4 changes: 3 additions & 1 deletion apps/origin-src/src/workspace/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,14 @@ impl Workspace {
/// Fails if the cursor expired, the remote fails, or the op log or a
/// record cannot be read.
pub fn watch(
&self,
&mut self,
kinds: Vec<KindPattern>,
subjects: Vec<String>,
since: Option<u64>,
local: bool,
) -> Result<(), WorkspaceError> {
// It never touches the working copy, so others may meanwhile.
self.lock = None;
let remote = if local { None } else { self.remote()? };
let service = self.service(None)?;
let mut query = EventsQuery {
Expand Down
9 changes: 6 additions & 3 deletions apps/origin-src/src/workspace/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
//!
//! The main working copy's `.origin/workspaces/` has a file for each other
//! workspace, named by the hash of its root and holding that root. Records
//! are immutable, and only target chains and the index are locked, so
//! commands run concurrently in different workspaces.
//! are immutable, and only target chains, the index and each working copy
//! are locked, so commands run concurrently in different workspaces.
//!
//! Creating a workspace registers it, checks out its files, and only then
//! publishes its `.origin/workspace`, so a crash never leaves partial files
Expand Down Expand Up @@ -115,7 +115,10 @@ impl Workspace {
/// Fails if `path` is the main working copy or no workspace, if the
/// workspace is behind its change with edits, which no revision holds
/// then, or if it cannot be snapshotted or deleted.
pub fn remove_workspace(&self, path: &Path) -> Result<(), WorkspaceError> {
pub fn remove_workspace(&mut self, path: &Path) -> Result<(), WorkspaceError> {
// It works in the workspace it removes, which may be this one, and
// opening it takes its lock.
self.lock = None;
let root = Self::canonical(path)?;
if root == self.main_root() {
return Err(WorkspaceError::RemoveMain(root));
Expand Down
20 changes: 20 additions & 0 deletions apps/origin-src/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,13 @@ fn removing_a_workspace_keeps_its_work() {
.contains("+not snapshotted yet\n")
);

// A workspace removes itself.
let own = others.path().join("own");
let change = repo.workspace(&own, &[]);
let removed = repo.ok_in(&own, &["workspace", "rm", "."]);
assert!(removed.starts_with(&format!("Kept its working copy: {change} ")));
assert!(!own.exists());

assert!(
repo.fails(&["workspace", "rm", "."])
.contains("is the main working copy")
Expand Down Expand Up @@ -1419,6 +1426,19 @@ fn commands_in_different_workspaces_run_concurrently() {
}
}

#[test]
fn commands_in_one_workspace_wait_for_each_other() {
let repo = Repo::new();
// As a command working in the working copy holds it.
let lock = fs::File::create(repo.path(".origin/lock")).unwrap();
lock.lock().unwrap();
let (mut status, _) = repo.spawn(&["status"]);
thread::sleep(Duration::from_millis(500));
assert!(status.try_wait().unwrap().is_none(), "it waits");
drop(lock);
assert!(status.wait().unwrap().success());
}

#[test]
fn workspaces_land_their_own_changes() {
let repo = Repo::new();
Expand Down
Loading