Skip to content

Commit cc2fe2e

Browse files
committed
feat(cli): land and watch through the remote
The chains of a remote's targets are copies of the remote's, which only the remote extends, even when this node is their authority. So `land`, `land --from` and `target new` go to the remote's queue for every target the remote has: `land` pushes the stack first, and each fetches the target's chain and the landed records after. Targets only this repository has still land here. A change with no `--onto` lands on the target its stack is on, which `Store::stack_target` finds. `targets` and `land --status` show the remote's targets and waiting landings with this repository's own, and `watch` long-polls the remote's events, or with `--local`, this node's. 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.
1 parent 83bc498 commit cc2fe2e

9 files changed

Lines changed: 257 additions & 50 deletions

File tree

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ End-to-end tests run the `origin` binary with `ORIGIN_CONFIG_DIR` set to a
4242
temporary directory, which holds the user's `config.toml` and node key.
4343
The HTTP tests drive every SDK method against a real server. One fails
4444
while `docs/openapi.json` is stale: `just openapi` rewrites it. The sync
45-
tests clone, push and fetch through `origin serve` and through local paths;
45+
tests clone, push and land through `origin serve` and through local paths;
4646
`tests/common` holds the helpers every end-to-end test shares.
4747

4848
## Commits & Pull Requests

‎README.md‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ origin workspace list | rm <path>
4747

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

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

104109
Changes are named by a unique prefix of their id, or `@` for the working
105110
copy's. A revision is `@`, a change (for its head), or a unique prefix of
@@ -142,9 +147,10 @@ demo = "repos/demo"
142147
The HTTP API, under `/api/v1`, puts records in batches, if the changes
143148
they revise are where the client saw them, and fetches them by id, walks
144149
snapshots for the trees and blobs a client lacks (the only way files leave
145-
a server), in parts of at most 64 MiB each way, compares the records of changes,
146-
lists statements, revisions, targets and their logs, lands changes and
147-
targets, and streams events by long-poll: `GET
150+
a server), in parts of at most 64 MiB each way, compares the records of
151+
changes, lists statements, revisions, targets and their logs, lands
152+
changes and targets, and streams
153+
events by long-poll: `GET
148154
/api/v1/repos/<repo>/events?cursor=<n>&wait=30s` answers as soon as there
149155
are events after the cursor, with a JSON batch, the next cursor, and the
150156
generation of the op log it's of: pass it back as `generation`, and a

‎apps/origin-src/src/cli.rs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ enum WorkspaceCommand {
216216
changes: Vec<String>,
217217
},
218218
/// Print events as they happen, one per line. The first line is the cursor
219-
/// to resume from. Runs until interrupted.
219+
/// to resume from. Watches the remote's events if the repository has a
220+
/// remote. Runs until interrupted.
220221
Watch {
221222
/// Only show events with this name, such as `check.requested`, or events
222223
/// about statements of this kind, such as `core.conflict` or `ci.*`.
@@ -230,6 +231,9 @@ enum WorkspaceCommand {
230231
/// event kept.
231232
#[arg(long)]
232233
since: Option<u64>,
234+
/// Watch this node's events instead of the remote's.
235+
#[arg(long)]
236+
local: bool,
233237
},
234238
}
235239

@@ -570,7 +574,8 @@ impl WorkspaceCommand {
570574
kind,
571575
subject,
572576
since,
573-
} => workspace.watch(kind, subject, since)?,
577+
local,
578+
} => workspace.watch(kind, subject, since, local)?,
574579
}
575580
Ok(())
576581
}

‎apps/origin-src/src/service.rs‎

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,8 @@ impl OriginService {
632632
landed
633633
}
634634

635-
/// Returns the target the stack of `change` is on: the target of the acceptance
636-
/// at the bottom of its head's stack.
635+
/// Returns the target the stack of `change` is on: the target of its head's
636+
/// stack.
637637
fn target_of(&self, index: &Index, change: ChangeId) -> Result<TargetName, ServiceError> {
638638
let head = match index.heads(change)[..] {
639639
[head] => head,
@@ -646,18 +646,7 @@ impl OriginService {
646646
.into());
647647
}
648648
};
649-
let mut base = self.store.get(head)?.base;
650-
let acceptance = loop {
651-
match base {
652-
Base::Revision(id) => base = self.store.get(id)?.base,
653-
Base::Acceptance(id) => break id,
654-
}
655-
};
656-
let target = self.store.get(acceptance)?.body().target;
657-
Ok(self
658-
.store
659-
.target_name(target)?
660-
.ok_or(StoreError::NotFound(acceptance.into()))?)
649+
Ok(self.store.stack_target(head)?)
661650
}
662651

663652
/// Lists every landing waiting on every target, and why it waits.

‎apps/origin-src/src/targets.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,26 @@ impl<P: Platform> Store<P> {
270270
.then_some(next))
271271
}
272272

273+
/// Returns the target the stack of `revision` is on: the target of the
274+
/// acceptance at the bottom of the stack, found by following bases.
275+
///
276+
/// # Errors
277+
///
278+
/// Fails if a record of the stack cannot be read, or the store doesn't
279+
/// know the target's name.
280+
pub fn stack_target(&self, revision: RevisionId) -> Result<TargetName, StoreError> {
281+
let mut base = self.get(revision)?.base;
282+
let acceptance = loop {
283+
match base {
284+
Base::Revision(id) => base = self.get(id)?.base,
285+
Base::Acceptance(id) => break id,
286+
}
287+
};
288+
let target = self.get(acceptance)?.body().target;
289+
self.target_name(target)?
290+
.ok_or(StoreError::NotFound(acceptance.into()))
291+
}
292+
273293
/// The name of the target `id`, or `None` if it doesn't exist.
274294
///
275295
/// # Errors

‎apps/origin-src/src/workspace/land.rs‎

Lines changed: 90 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
//!
44
//! They go through the repository's
55
//! [`OriginService`](crate::service::OriginService), whose landing queue runs
6-
//! in this process, as this node. After a landing, the working copy of the
7-
//! workspace that ran it follows its change. If the change landed, or the
8-
//! target already had it, the working copy starts a new change on the target's
9-
//! tip, so later edits build on what the target has. If the queue rebased the
10-
//! change, the working copy moves to the rebased revision. If the working copy
11-
//! is on top of a change that landed, it is rebased onto the tip.
6+
//! in this process, as this node. If the repository has a remote that has the
7+
//! target, they go to the remote instead, whose node must be the target's
8+
//! authority: `land` pushes the stack first, then fetches the target's chain
9+
//! and the stack's records. After a landing, the working copy of the workspace
10+
//! that ran it follows its change. If the change landed, or the target already
11+
//! had it, the working copy starts a new change on the target's tip, so later
12+
//! edits build on what the target has. If the queue rebased the change, the
13+
//! working copy moves to the rebased revision. If the working copy is on top of
14+
//! a change that landed, it is rebased onto the tip.
1215
//!
1316
//! Other workspaces don't move: they are behind their change until `origin
1417
//! pull` moves them the same way, merging the edits made since. Where those
@@ -17,7 +20,7 @@
1720
//! those files change or `origin resolve` accepts them (see
1821
//! [`conflict`](crate::conflict)).
1922
20-
use std::mem;
23+
use std::{mem, slice};
2124

2225
use origin_shared::api::{LandRequest, Landings, NewTarget};
2326
use origin_shared::landing::{Blocked, Landing, Pending, Proposal};
@@ -29,6 +32,7 @@ use super::{Behind, WorkingCopy, Workspace, WorkspaceError};
2932
use crate::conflict::Conflicts;
3033
use crate::index::Index;
3134
use crate::oplog::Why;
35+
use crate::remote::Remote;
3236
use crate::service::Caller;
3337
use crate::tree::Merge;
3438

@@ -48,13 +52,36 @@ impl Workspace {
4852
) -> Result<(), WorkspaceError> {
4953
let mut working_copy = self.save_draft()?;
5054
let change = working_copy.find_change(change.unwrap_or("@"))?;
55+
let onto = if let Some(onto) = onto {
56+
onto
57+
} else {
58+
let head = Self::single_head(&working_copy.index, change)?;
59+
self.store.stack_target(head)?
60+
};
5161
let request = LandRequest {
5262
proposal: Proposal::Change(change),
53-
onto,
63+
onto: Some(onto.clone()),
64+
};
65+
let Landings { onto, landings } = match self.remote_queue(&onto)? {
66+
Some(remote) => {
67+
let sync = self.sync(remote);
68+
sync.push(&[change])?;
69+
let landed = remote.land(&request)?;
70+
sync.fetch_chains(slice::from_ref(&landed.onto))?;
71+
let mut changes = vec![change];
72+
changes.extend(landed.landings.iter().filter_map(
73+
|landing| match landing.proposal() {
74+
Proposal::Change(change) => Some(*change),
75+
Proposal::Target(_) => None,
76+
},
77+
));
78+
sync.fetch_changes(&changes)?;
79+
landed
80+
}
81+
None => self
82+
.service(Some(mem::take(&mut working_copy.index)))?
83+
.land(&Caller::Owner, &request)?,
5484
};
55-
let Landings { onto, landings } = self
56-
.service(Some(mem::take(&mut working_copy.index)))?
57-
.land(&Caller::Owner, &request)?;
5885
for landing in &landings {
5986
self.print_landing(landing, &onto)?;
6087
}
@@ -85,20 +112,31 @@ impl Workspace {
85112
proposal: Proposal::Target(from.clone()),
86113
onto: Some(onto.clone()),
87114
};
88-
let landed = self.service(None)?.land(&Caller::Owner, &request)?;
115+
let landed = match self.remote_queue(onto)? {
116+
Some(remote) => {
117+
let landed = remote.land(&request)?;
118+
self.sync(remote).fetch_chains(slice::from_ref(onto))?;
119+
landed
120+
}
121+
None => self.service(None)?.land(&Caller::Owner, &request)?,
122+
};
89123
for landing in &landed.landings {
90124
self.print_landing(landing, onto)?;
91125
}
92126
Ok(())
93127
}
94128

95-
/// `origin land --status`: lists the landings that are waiting, and why.
129+
/// `origin land --status`: lists the landings that are waiting, and why: first
130+
/// in this node's queues, then in the remote's.
96131
///
97132
/// # Errors
98133
///
99134
/// Fails if the queues cannot be read.
100135
pub fn land_status(&self) -> Result<(), WorkspaceError> {
101-
let pending = self.service(None)?.landings(&Caller::Owner)?;
136+
let mut pending = self.service(None)?.landings(&Caller::Owner)?;
137+
if let Some(remote) = self.remote()? {
138+
pending.extend(remote.landings()?);
139+
}
102140
if pending.is_empty() {
103141
println!("No landing waits.");
104142
}
@@ -120,9 +158,18 @@ impl Workspace {
120158
name: name.clone(),
121159
from: from.clone(),
122160
};
123-
let head = self
124-
.service(None)?
125-
.create_target(&Caller::Owner, &request)?;
161+
// A target starts where `from` lives: here, for this repository's
162+
// own targets, else on the remote.
163+
let head = match self.remote_queue(from)? {
164+
Some(remote) => {
165+
let head = remote.create_target(&request)?;
166+
self.sync(remote).fetch_chains(slice::from_ref(name))?;
167+
head
168+
}
169+
None => self
170+
.service(None)?
171+
.create_target(&Caller::Owner, &request)?,
172+
};
126173
let landed = self.store.get(head.acceptance)?.body().landed.clone();
127174
let from = match landed[..] {
128175
[Source::Target(from)] => self.describe_acceptance(from)?,
@@ -133,13 +180,22 @@ impl Workspace {
133180
}
134181

135182
/// `origin targets`: lists every target with its tip, its authority and what
136-
/// landing on it requires.
183+
/// landing on it requires. If the repository has a remote, lists the remote's
184+
/// targets first, since this repository only holds those it follows, then this
185+
/// repository's own.
137186
///
138187
/// # Errors
139188
///
140-
/// Fails if the targets or the policy cannot be read.
189+
/// Fails if the remote fails, or the targets or the policy cannot be read.
141190
pub fn targets(&self) -> Result<(), WorkspaceError> {
142-
for target in self.service(None)?.targets(&Caller::Owner)? {
191+
let mut targets = self.service(None)?.targets(&Caller::Owner)?;
192+
if let Some(remote) = self.remote()? {
193+
let remote = remote.targets()?;
194+
targets.retain(|own| !remote.iter().any(|target| target.name == own.name));
195+
targets.extend(remote);
196+
targets.sort_by(|a, b| a.name.cmp(&b.name));
197+
}
198+
for target in targets {
143199
let authority = target
144200
.authority
145201
.map_or_else(|| "no authority".to_owned(), |node| format!("node:{node}"));
@@ -156,6 +212,20 @@ impl Workspace {
156212
Ok(())
157213
}
158214

215+
/// Returns the remote to submit landings on `target` to, or `None` if they land
216+
/// here: when the repository has no remote, or `target` is this repository's
217+
/// own, which the remote doesn't have. The chains of other targets are copies
218+
/// of the remote's, which only the remote extends, even when this node is their
219+
/// authority.
220+
fn remote_queue(&self, target: &TargetName) -> Result<Option<&Remote>, WorkspaceError> {
221+
let Some(remote) = self.remote()? else {
222+
return Ok(None);
223+
};
224+
let own = self.store.head(target)?.is_some()
225+
&& !remote.targets()?.iter().any(|known| known.name == *target);
226+
Ok((!own).then_some(remote))
227+
}
228+
159229
/// `origin new [description] --on <target>`: starts a change on the tip of a
160230
/// target.
161231
///

‎apps/origin-src/src/workspace/ops.rs‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,20 +204,24 @@ impl Workspace {
204204
Err(WorkspaceError::NothingToUndo)
205205
}
206206

207-
/// `origin watch [--kind <kind>]... [--subject <subject>]... [--since
208-
/// <cursor>]`: prints the events of those kinds about those subjects as they
207+
/// `origin watch [--kind <kind>]... [--subject <subject>]... [--since <cursor>]
208+
/// [--local]`: prints the events of those kinds about those subjects as they
209209
/// happen, one per line, from `since`, or from now. Without kinds or subjects,
210-
/// prints every event. Runs until interrupted.
210+
/// prints every event. The events are the remote's, long-polled, if the
211+
/// repository has one, or this node's with `local`. Runs until interrupted.
211212
///
212213
/// # Errors
213214
///
214-
/// Fails if the cursor expired, or the op log or a record cannot be read.
215+
/// Fails if the cursor expired, the remote fails, or the op log or a record
216+
/// cannot be read.
215217
pub fn watch(
216218
&self,
217219
kinds: Vec<KindPattern>,
218220
subjects: Vec<String>,
219221
since: Option<u64>,
222+
local: bool,
220223
) -> Result<(), WorkspaceError> {
224+
let remote = if local { None } else { self.remote()? };
221225
let service = self.service(None)?;
222226
let mut query = EventsQuery {
223227
cursor: since,
@@ -228,7 +232,10 @@ impl Workspace {
228232
subject: List(subjects),
229233
};
230234
loop {
231-
let page = service.events(&Caller::Owner, &query)?;
235+
let page = match remote {
236+
Some(remote) => remote.events(&query)?,
237+
None => service.events(&Caller::Owner, &query)?,
238+
};
232239
for event in &page.events {
233240
println!("{event}");
234241
}

0 commit comments

Comments
 (0)