Skip to content
Merged
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ origin land --status # the landings waiting for evidence or a con
origin targets # every target, its tip, authority and requirements
origin target new <name> --from <target>
origin new [description] --on <target> # start a change on a target's tip

origin workspace new [--change <change>] [<path>] # another working copy, sharing the repository
origin workspace list | rm <path>
```

Landing rebases a change onto the target's tip, file by file, checks the
Expand All @@ -49,6 +52,13 @@ target's authority. A landing that conflicts or lacks evidence waits: issue
the statements, or a new revision, and run `origin land` again. Policy is a
target too: edit it with `origin new --on policy`, then `origin land`.

Workspaces work like git worktrees: each has its own current change and
shares the repository's records, so agents can work side by side. A new
workspace clones its files from the other working copies where the file
system can (Btrfs, XFS, APFS), and copies them otherwise. Landing moves
only the working copy of the workspace it runs in, and no other command
moves your files on its own.

Changes are named by a unique prefix of their id, or `@` for the working
copy's. A revision is `@`, a change (for its head), or a unique prefix of
the revision's id. Your name and node key live in `$ORIGIN_CONFIG_DIR`, or
Expand Down
29 changes: 29 additions & 0 deletions apps/origin-src/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ enum WorkspaceCommand {
Target(TargetCommand),
/// List the targets with their tips, authorities and requirements.
Targets,
/// Manage workspaces: working copies that share this repository.
#[command(subcommand)]
Workspace(WorkspacesCommand),
}

#[derive(Debug, Args)]
Expand Down Expand Up @@ -334,6 +337,27 @@ enum IndexCommand {
Rebuild,
}

#[derive(Debug, Subcommand)]
enum WorkspacesCommand {
/// Create a workspace, with a new change on the same base as the working
/// copy's change.
New {
/// Check out this change's head instead of starting a new change.
#[arg(long)]
change: Option<String>,
/// Where to create the workspace: by default, beside the main working
/// copy, named after the change.
path: Option<PathBuf>,
},
/// List the workspaces and their changes.
List,
/// Remove a workspace, keeping its work as a revision of its change.
Rm {
/// The workspace's path.
path: PathBuf,
},
}

impl Cli {
/// Parses the command line, then checks what parsing can't: an
/// `annotate`'s arguments, whose meaning depends on `--on`. Exits as a
Expand Down Expand Up @@ -416,6 +440,11 @@ impl WorkspaceCommand {
workspace.target_new(&name, &from)?;
}
Self::Targets => workspace.targets()?,
Self::Workspace(WorkspacesCommand::New { change, path }) => {
workspace.add_workspace(change.as_deref(), path.as_deref())?;
}
Self::Workspace(WorkspacesCommand::List) => workspace.list_workspaces()?,
Self::Workspace(WorkspacesCommand::Rm { path }) => workspace.remove_workspace(&path)?,
}
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion apps/origin-src/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use origin_shared::{InvalidName, NodeKey, Principal, PrincipalName};
use serde::Deserialize;

/// The user's configuration.
#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct UserConfig {
dir: PathBuf,
user: PrincipalName,
Expand Down
6 changes: 6 additions & 0 deletions apps/origin-src/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ impl<P: Platform> Store<P> {
self.write_index(&[], &[])
}

/// The directory holding the store: a repository's `.origin`.
#[must_use]
pub fn root(&self) -> &Path {
&self.root
}

pub(crate) fn sync_dir(&self, dir: &Path) -> Result<(), StoreError> {
self.platform
.sync_dir(dir)
Expand Down
123 changes: 102 additions & 21 deletions apps/origin-src/src/workspace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
//! ```text
//! .origin/
//! objects/ targets/ index/ the store, see `store`, `targets` and `index`
//! workspaces/ the other workspaces, see `registry`
//! workspace the revision the working copy is
//! state the stat cache of the last snapshot
//! ```
//!
//! A repository can have several working copies, called workspaces, that share
//! its store. The main working copy's `.origin` holds the store. The others'
//! `.origin` holds only `workspace`, `state`, and a `repository` file that
//! names the main one's `.origin`.
//!
//! The working copy is always on a revision of the current change. Every
//! command that reads the working copy snapshots its files first. The files it
//! finds are the draft. Commands that only read the working copy show the draft
Expand All @@ -26,6 +32,7 @@

mod annotate;
mod land;
mod registry;

pub use annotate::{Annotated, Payload};

Expand All @@ -49,7 +56,7 @@ use crate::output::FileDiff;
use crate::policy::PolicyError;
use crate::queue::{LandError, Queue};
use crate::store::{Store, StoreError};
use crate::worktree::{SnapshotError, StatCache, Worktree};
use crate::worktree::{CloneSource, SnapshotError, StatCache, Worktree};

/// A working copy and its repository.
#[derive(Debug)]
Expand Down Expand Up @@ -98,6 +105,16 @@ impl Workspace {
/// The metadata directory at the root of every working copy.
pub const META_DIR: &str = ".origin";

/// The file in [`Workspace::META_DIR`] that names the working copy's revision.
const POINTER: &str = "workspace";

/// The file in [`Workspace::META_DIR`] that holds the stat cache.
const STATE: &str = "state";

/// The file in a workspace's [`Workspace::META_DIR`] that names the main
/// working copy's, which holds the store.
const REPOSITORY: &str = "repository";

/// `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 @@ -179,32 +196,56 @@ impl Workspace {
)
}

/// Opens the working copy containing `start`.
/// Opens the working copy that contains `start`.
///
/// # Errors
///
/// Fails if neither `start` nor any parent has a repository, it is on a
/// network file system, or its working copy file is unreadable.
/// Fails if neither `start` nor any of its parents has a repository, or like
/// [`Workspace::open`].
pub fn discover(start: &Path, config: UserConfig) -> Result<Self, WorkspaceError> {
let root = start
.ancestors()
.find(|dir| dir.join(Self::META_DIR).is_dir())
.ok_or_else(|| WorkspaceError::NotFound(start.to_owned()))?;
Self::open(root, config)
}

/// Opens the working copy at `root`: either the main one, whose `.origin` holds
/// the store, or another workspace, whose `.origin/repository` names the main
/// one's.
fn open(root: &Path, config: UserConfig) -> Result<Self, WorkspaceError> {
let meta = root.join(Self::META_DIR);
let path = meta.join("workspace");
let revision = fs::read_to_string(&path)
.map_err(|source| WorkspaceError::io(&path, source))?
.trim_end()
.parse()
.map_err(|_| WorkspaceError::Malformed(path))?;
let repository = meta.join(Self::REPOSITORY);
let store = match Self::read_path(&repository) {
Ok(store) if store.is_dir() => store,
Ok(store) => return Err(WorkspaceError::MissingRepository(store)),
Err(WorkspaceError::Io { source, .. }) if source.kind() == io::ErrorKind::NotFound => {
meta
}
Err(error) => return Err(error),
};
let revision =
Self::read_pointer(root)?.ok_or_else(|| WorkspaceError::Incomplete(root.to_owned()))?;
Ok(Self {
root: root.to_owned(),
store: Store::open_writable(meta, Native::default())?,
store: Store::open_writable(store, Native::default())?,
config,
revision,
})
}

/// Returns the revision of the working copy at `root`, if it has one.
fn read_pointer(root: &Path) -> Result<Option<RevisionId>, WorkspaceError> {
let path = root.join(Self::META_DIR).join(Self::POINTER);
let pointer = match fs::read_to_string(&path) {
Ok(pointer) => pointer,
Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(None),
Err(source) => return Err(WorkspaceError::io(&path, source)),
};
let revision = pointer.trim_end().parse();
Ok(Some(revision.map_err(|_| WorkspaceError::Malformed(path))?))
}

fn meta(&self, name: &str) -> PathBuf {
self.root.join(Self::META_DIR).join(name)
}
Expand All @@ -220,7 +261,7 @@ impl Workspace {
fn snapshot(&mut self) -> Result<WorkingCopy, WorkspaceError> {
let held = self.store.hold_objects()?;
let revision: Revision = self.store.get(self.revision)?;
let path = self.meta("state");
let path = self.meta(Self::STATE);
let loaded = StatCache::load(&path);
let (files, cache) = Worktree::new(&self.root, &self.store).snapshot(&loaded)?;
let cache = match cache {
Expand Down Expand Up @@ -348,12 +389,18 @@ impl Workspace {

/// Durably records that the working copy is `revision`.
fn point_at(&mut self, revision: RevisionId) -> Result<(), WorkspaceError> {
self.write_durably(self.meta(Self::POINTER), format!("{revision}\n").as_bytes())?;
self.revision = revision;
Ok(())
}

/// Durably replaces a metadata file.
fn write_durably(&self, path: PathBuf, contents: &[u8]) -> Result<(), WorkspaceError> {
let write = self.store.durable_write();
write
.stage(self.meta("workspace"), format!("{revision}\n").as_bytes())
.stage(path, contents)
.and_then(|()| write.commit())
.map_err(StoreError::from)?;
self.revision = revision;
Ok(())
}

Expand Down Expand Up @@ -391,17 +438,30 @@ impl Workspace {
Ok(Worktree::new(&self.root, &self.store).restore(&changes)?)
}

/// Makes the files match `to`, given that they match `from`, durably. With
/// `unchanged`, the stat cache of the snapshot that found `from`, it changes
/// nothing if a file it would change was edited since.
/// Makes the files match `to`, given that they match `from`, durably, cloning
/// the files other workspaces already have. With `unchanged`, the stat cache of
/// the snapshot that found `from`, it changes nothing if a file it would change
/// was edited since.
fn checkout(
&self,
from: TreeId,
to: TreeId,
unchanged: Option<&StatCache>,
) -> Result<(), WorkspaceError> {
let changes = self.store.diff(from, to)?;
Ok(Worktree::new(&self.root, &self.store).checkout(&changes, &[], unchanged)?)
if changes.is_empty() {
return Ok(());
}
let sources: Vec<_> = self
.workspaces()?
.into_iter()
.filter(|root| *root != self.root)
.map(|root| {
let cache = root.join(Self::META_DIR).join(Self::STATE);
CloneSource::saved(root, cache)
})
.collect();
Ok(Worktree::new(&self.root, &self.store).checkout(&changes, &sources, unchanged)?)
}

/// `origin status`: prints the working copy's change, what it is on
Expand Down Expand Up @@ -506,10 +566,10 @@ impl Workspace {
Ok(())
}

/// Prints the changes `files` make to the snapshot of `base`.
fn print_diff(&self, base: Base, files: TreeId) -> Result<(), WorkspaceError> {
/// Prints the changes `snapshot` makes to `base`, line by line.
fn print_diff(&self, base: Base, snapshot: TreeId) -> Result<(), WorkspaceError> {
let base = self.base_tree(base)?;
for change in self.store.diff(base, files)? {
for change in self.store.diff(base, snapshot)? {
print!("{}", FileDiff::new(&self.store, &change)?);
}
Ok(())
Expand Down Expand Up @@ -1079,6 +1139,27 @@ pub enum WorkspaceError {
/// The working copy file isn't a revision id.
#[error("{} is not a revision id", .0.display())]
Malformed(PathBuf),
/// A workspace's creation never finished.
#[error(
"the workspace at {} was never finished: remove it with `origin workspace rm`",
.0.display()
)]
Incomplete(PathBuf),
/// A workspace's repository is gone.
#[error("this workspace's repository, {}, is gone", .0.display())]
MissingRepository(PathBuf),
/// `workspace new` was given a path inside a working copy.
#[error("{} is inside a working copy", .0.display())]
Nested(PathBuf),
/// `workspace new` was given a directory with files in it.
#[error("{} is not empty", .0.display())]
NotEmpty(PathBuf),
/// `workspace rm` was given a path that isn't another workspace.
#[error("{} is not a workspace of this repository", .0.display())]
UnknownWorkspace(PathBuf),
/// `workspace rm` was given the main working copy, which holds the store.
#[error("{} is the main working copy, which holds the repository", .0.display())]
RemoveMain(PathBuf),
/// A metadata file cannot be accessed.
#[error("cannot access {}", path.display())]
Io {
Expand Down
Loading
Loading