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
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
2225use origin_shared:: api:: { LandRequest , Landings , NewTarget } ;
2326use origin_shared:: landing:: { Blocked , Landing , Pending , Proposal } ;
@@ -29,6 +32,7 @@ use super::{Behind, WorkingCopy, Workspace, WorkspaceError};
2932use crate :: conflict:: Conflicts ;
3033use crate :: index:: Index ;
3134use crate :: oplog:: Why ;
35+ use crate :: remote:: Remote ;
3236use crate :: service:: Caller ;
3337use 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 ///
0 commit comments