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
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ End-to-end tests run the `origin` binary with `ORIGIN_CONFIG_DIR` set to a
temporary directory, which holds the user's `config.toml` and node key.
The HTTP tests drive every SDK method against a real server. One fails
while `docs/openapi.json` is stale: `just openapi` rewrites it. The sync
tests clone, push and fetch through `origin serve` and through local paths;
tests clone, push and land through `origin serve` and through local paths;
`tests/common` holds the helpers every end-to-end test shares.

## Validation
Expand Down
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ origin workspace list | rm <path>

origin op log [-n <count>] # what this node did, operation by operation, in every workspace
origin undo # move this workspace's working copy back one operation
origin watch [--kind <kind>] [--subject <id>] [--since <cursor>] # events, as they happen
origin watch [--kind <kind>] [--subject <id>] [--since <cursor>] [--local] # events, as they happen

origin clone <url> [<path>] [--target <target>] # follow a remote's target, starting a change on its tip
origin remote [<url>] # show or set the remote: http://host:port/<repo>, or a path
Expand Down Expand Up @@ -97,9 +97,14 @@ snapshots only when a command reads them, starting with its target's tip.
`push` shares revisions, their files and statements, and refuses a change
the remote has a newer revision of, even one pushed while it sends:
`fetch`, then `converge` it. A statement that needs one the remote lacks
stays, and the push says so. Fetching never changes your files; `pull`
then moves the working copy onto its target's new tip, like a landing
elsewhere does.
stays, and the push says so. A landing is decided on the evidence the
remote holds when it's tried: send a statement together with the one it
supersedes, as `push` does, or a landing waiting on the first may take it.
`land`, `land --from` and `target
new` go to the remote's queue for every target the remote has, after pushing
what lands, and `watch` long-polls the remote's events (`--local` for this
node's). Fetching never changes your files; `pull` then moves the working
copy onto its target's new tip, like a landing elsewhere does.

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
Expand Down Expand Up @@ -142,9 +147,10 @@ demo = "repos/demo"
The HTTP API, under `/api/v1`, puts records in batches, if the changes
they revise are where the client saw them, and fetches them by id, walks
snapshots for the trees and blobs a client lacks (the only way files leave
a server), in parts of at most 64 MiB each way, compares the records of changes,
lists statements, revisions, targets and their logs, lands changes and
targets, and streams events by long-poll: `GET
a server), in parts of at most 64 MiB each way, compares the records of
changes, lists statements, revisions, targets and their logs, lands
changes and targets, and streams
events by long-poll: `GET
/api/v1/repos/<repo>/events?cursor=<n>&wait=30s` answers as soon as there
are events after the cursor, with a JSON batch, the next cursor, and the
generation of the op log it's of: pass it back as `generation`, and a
Expand Down
9 changes: 7 additions & 2 deletions apps/origin-src/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ enum WorkspaceCommand {
changes: Vec<String>,
},
/// Print events as they happen, one per line. The first line is the cursor
/// to resume from. Runs until interrupted.
/// to resume from. Watches the remote's events if the repository has a
/// remote. Runs until interrupted.
Watch {
/// Only show events with this name, such as `check.requested`, or events
/// about statements of this kind, such as `core.conflict` or `ci.*`.
Expand All @@ -230,6 +231,9 @@ enum WorkspaceCommand {
/// event kept.
#[arg(long)]
since: Option<u64>,
/// Watch this node's events instead of the remote's.
#[arg(long)]
local: bool,
},
}

Expand Down Expand Up @@ -571,7 +575,8 @@ impl WorkspaceCommand {
kind,
subject,
since,
} => workspace.watch(kind, subject, since)?,
local,
} => workspace.watch(kind, subject, since, local)?,
}
Ok(())
}
Expand Down
17 changes: 3 additions & 14 deletions apps/origin-src/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,8 +631,8 @@ impl OriginService {
landed
}

/// Returns the target the stack of `change` is on: the target of the acceptance
/// at the bottom of its head's stack.
/// Returns the target the stack of `change` is on: the target of its head's
/// stack.
fn target_of(&self, index: &Index, change: ChangeId) -> Result<TargetName, ServiceError> {
let head = match index.heads(change)[..] {
[head] => head,
Expand All @@ -645,18 +645,7 @@ impl OriginService {
.into());
}
};
let mut base = self.store.get(head)?.base;
let acceptance = loop {
match base {
Base::Revision(id) => base = self.store.get(id)?.base,
Base::Acceptance(id) => break id,
}
};
let target = self.store.get(acceptance)?.body().target;
Ok(self
.store
.target_name(target)?
.ok_or(StoreError::NotFound(acceptance.into()))?)
Ok(self.store.stack_target(head)?)
}

/// Lists every landing waiting on every target, and why it waits.
Expand Down
20 changes: 20 additions & 0 deletions apps/origin-src/src/targets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,26 @@ impl<P: Platform> Store<P> {
.then_some(next))
}

/// Returns the target the stack of `revision` is on: the target of the
/// acceptance at the bottom of the stack, found by following bases.
///
/// # Errors
///
/// Fails if a record of the stack cannot be read, or the store doesn't
/// know the target's name.
pub fn stack_target(&self, revision: RevisionId) -> Result<TargetName, StoreError> {
let mut base = self.get(revision)?.base;
let acceptance = loop {
match base {
Base::Revision(id) => base = self.get(id)?.base,
Base::Acceptance(id) => break id,
}
};
let target = self.get(acceptance)?.body().target;
self.target_name(target)?
.ok_or(StoreError::NotFound(acceptance.into()))
}

/// The name of the target `id`, or `None` if it doesn't exist.
///
/// # Errors
Expand Down
111 changes: 84 additions & 27 deletions apps/origin-src/src/workspace/land.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,13 @@
//!
//! They go through the repository's
//! [`OriginService`](crate::service::OriginService), whose landing queue runs
//! in this process, as this node. After a landing, the working copy of the
//! workspace that ran it follows its change. If the change landed, or the
//! target already had it, the working copy starts a new change on the target's
//! tip, so later edits build on what the target has. If the queue rebased the
//! change, the working copy moves to the rebased revision. If the working copy
//! is on top of a change that landed, it is rebased onto the tip.
//!
//! Other workspaces don't move: they are behind their change until `origin
//! pull` moves them the same way, merging the edits made since. Where those
//! edits conflict, the files hold both versions between conflict markers, and a
//! `core.conflict` statement lists them. The working copy can't land until
//! those files change or `origin resolve` accepts them (see
//! [`conflict`](crate::conflict)).
//! in this process, as this node. If the repository has a remote that has the
//! target, they go to the remote instead, whose node must be the target's
//! authority: `land` pushes the stack first, then fetches the target's chain
//! and the stack's records. After a landing, the working copy follows its
//! change (see "Behind" in `docs/design.md`).

use std::mem;
use std::{mem, slice};

use origin_shared::api::{LandRequest, Landings, NewTarget};
use origin_shared::landing::{Blocked, Landing, Pending, Proposal};
Expand All @@ -29,6 +21,7 @@ use super::{Behind, WorkingCopy, Workspace, WorkspaceError};
use crate::conflict::Conflicts;
use crate::index::Index;
use crate::oplog::Why;
use crate::remote::Remote;
use crate::service::Caller;
use crate::tree::Merge;

Expand All @@ -48,13 +41,36 @@ impl Workspace {
) -> Result<(), WorkspaceError> {
let mut working_copy = self.save_draft()?;
let change = working_copy.find_change(change.unwrap_or("@"))?;
let onto = if let Some(onto) = onto {
onto
} else {
let head = Self::single_head(&working_copy.index, change)?;
self.store.stack_target(head)?
};
let request = LandRequest {
proposal: Proposal::Change(change),
onto,
onto: Some(onto.clone()),
};
let Landings { onto, landings } = match self.remote_queue(&onto)? {
Some(remote) => {
let sync = self.sync(remote);
sync.push(&[change])?;
let landed = remote.land(&request)?;
sync.fetch_chains(slice::from_ref(&landed.onto))?;
let mut changes = vec![change];
changes.extend(landed.landings.iter().filter_map(
|landing| match landing.proposal() {
Proposal::Change(change) => Some(*change),
Proposal::Target(_) => None,
},
));
sync.fetch_changes(&changes)?;
landed
}
None => self
.service(Some(mem::take(&mut working_copy.index)))?
.land(&Caller::Owner, &request)?,
};
let Landings { onto, landings } = self
.service(Some(mem::take(&mut working_copy.index)))?
.land(&Caller::Owner, &request)?;
for landing in &landings {
self.print_landing(landing, &onto)?;
}
Expand Down Expand Up @@ -85,20 +101,31 @@ impl Workspace {
proposal: Proposal::Target(from.clone()),
onto: Some(onto.clone()),
};
let landed = self.service(None)?.land(&Caller::Owner, &request)?;
let landed = match self.remote_queue(onto)? {
Some(remote) => {
let landed = remote.land(&request)?;
self.sync(remote).fetch_chains(slice::from_ref(onto))?;
landed
}
None => self.service(None)?.land(&Caller::Owner, &request)?,
};
for landing in &landed.landings {
self.print_landing(landing, onto)?;
}
Ok(())
}

/// `origin land --status`: lists the landings that are waiting, and why.
/// `origin land --status`: lists the landings that are waiting, and why: first
/// in this node's queues, then in the remote's.
///
/// # Errors
///
/// Fails if the queues cannot be read.
pub fn land_status(&self) -> Result<(), WorkspaceError> {
let pending = self.service(None)?.landings(&Caller::Owner)?;
let mut pending = self.service(None)?.landings(&Caller::Owner)?;
if let Some(remote) = self.remote()? {
pending.extend(remote.landings()?);
}
if pending.is_empty() {
println!("No landing waits.");
}
Expand All @@ -120,9 +147,16 @@ impl Workspace {
name: name.clone(),
from: from.clone(),
};
let head = self
.service(None)?
.create_target(&Caller::Owner, &request)?;
let head = match self.remote_queue(from)? {
Some(remote) => {
let head = remote.create_target(&request)?;
self.sync(remote).fetch_chains(slice::from_ref(name))?;
head
}
None => self
.service(None)?
.create_target(&Caller::Owner, &request)?,
};
let landed = self.store.get(head.acceptance)?.body().landed.clone();
let from = match landed[..] {
[Source::Target(from)] => self.describe_acceptance(from)?,
Expand All @@ -133,13 +167,22 @@ impl Workspace {
}

/// `origin targets`: lists every target with its tip, its authority and what
/// landing on it requires.
/// landing on it requires. If the repository has a remote, lists the remote's
/// targets first, since this repository only holds those it follows, then this
/// repository's own.
///
/// # Errors
///
/// Fails if the targets or the policy cannot be read.
/// Fails if the remote fails, or the targets or the policy cannot be read.
pub fn targets(&self) -> Result<(), WorkspaceError> {
for target in self.service(None)?.targets(&Caller::Owner)? {
let mut targets = self.service(None)?.targets(&Caller::Owner)?;
if let Some(remote) = self.remote()? {
let remote = remote.targets()?;
targets.retain(|own| !remote.iter().any(|target| target.name == own.name));
targets.extend(remote);
targets.sort_by(|a, b| a.name.cmp(&b.name));
}
for target in targets {
let authority = target
.authority
.map_or_else(|| "no authority".to_owned(), |node| format!("node:{node}"));
Expand All @@ -156,6 +199,20 @@ impl Workspace {
Ok(())
}

/// Returns the remote to submit landings on `target` to, or `None` if they land
/// here: when the repository has no remote, or `target` is this repository's
/// own, which the remote doesn't have. The chains of other targets are copies
/// of the remote's, which only the remote extends, even when this node is their
/// authority.
fn remote_queue(&self, target: &TargetName) -> Result<Option<&Remote>, WorkspaceError> {
let Some(remote) = self.remote()? else {
return Ok(None);
};
let own = self.store.head(target)?.is_some()
&& !remote.targets()?.iter().any(|known| known.name == *target);
Ok((!own).then_some(remote))
}

/// `origin new [description] --on <target>`: starts a change on the tip of a
/// target.
///
Expand Down
17 changes: 12 additions & 5 deletions apps/origin-src/src/workspace/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,20 +204,24 @@ impl Workspace {
Err(WorkspaceError::NothingToUndo)
}

/// `origin watch [--kind <kind>]... [--subject <subject>]... [--since
/// <cursor>]`: prints the events of those kinds about those subjects as they
/// `origin watch [--kind <kind>]... [--subject <subject>]... [--since <cursor>]
/// [--local]`: prints the events of those kinds about those subjects as they
/// happen, one per line, from `since`, or from now. Without kinds or subjects,
/// prints every event. Runs until interrupted.
/// prints every event. The events are the remote's, long-polled, if the
/// repository has one, or this node's with `local`. Runs until interrupted.
///
/// # Errors
///
/// Fails if the cursor expired, or the op log or a record cannot be read.
/// Fails if the cursor expired, the remote fails, or the op log or a record
/// cannot be read.
pub fn watch(
&self,
kinds: Vec<KindPattern>,
subjects: Vec<String>,
since: Option<u64>,
local: bool,
) -> Result<(), WorkspaceError> {
let remote = if local { None } else { self.remote()? };
let service = self.service(None)?;
let mut query = EventsQuery {
cursor: since,
Expand All @@ -228,7 +232,10 @@ impl Workspace {
subject: List(subjects),
};
loop {
let page = service.events(&Caller::Owner, &query)?;
let page = match remote {
Some(remote) => remote.events(&query)?,
None => service.events(&Caller::Owner, &query)?,
};
for event in &page.events {
println!("{event}");
}
Expand Down
Loading