workyard_git/relationships/
portability.rs1use super::{changed, location, relationship_lock};
3use crate::{Runner, operations::locks_present, worktree_path::same_path};
4use std::{
5 collections::HashSet,
6 path::{Path, PathBuf},
7};
8use workyard_core::{Control, Participant, PlacementHint, Problem, ProblemCode, Repository};
9
10#[derive(Clone, Debug, PartialEq, Eq)]
12pub struct Placement {
13 pub registered: PathBuf,
15 pub destination: PathBuf,
17 pub competing: bool,
19 pub needs_repair: bool,
21 pub common_dir: PathBuf,
23 pub source: PathBuf,
25 backing: String,
26 admin: String,
27 local_identity: (u64, u64),
28 registered_identity: Option<(u64, u64)>,
29}
30
31impl Placement {
32 pub fn diagnostic(&self) -> Problem {
34 placement_problem(&self.registered, &self.destination, self.competing)
35 }
36
37 pub fn update_hint(&self, participant: &mut Participant, workspace: &Path) {
39 participant.common_dir = self.common_dir.clone();
40 participant.source = self.source.clone();
41 participant.destination = self.destination.clone();
42 participant.placement = Some(PlacementHint {
43 source: self.source.clone(),
44 common_dir: self.common_dir.clone(),
45 destination: self.destination.clone(),
46 workspace: workspace.to_owned(),
47 worktree_entry: Some(self.local_identity),
48 });
49 }
50}
51
52impl Runner {
53 pub async fn inspect_placement(
55 &self,
56 participant: &Participant,
57 project: Option<&Path>,
58 control: &Control,
59 ) -> Result<Placement, Problem> {
60 let repository = self
61 .placement_repository(participant, project, control)
62 .await?;
63 self.preview_in(&repository, participant, control).await
64 }
65
66 pub(crate) async fn placement_repository(
67 &self,
68 participant: &Participant,
69 project: Option<&Path>,
70 control: &Control,
71 ) -> Result<Repository, Problem> {
72 let former = participant
73 .placement
74 .as_ref()
75 .map(|h| h.common_dir.as_path());
76 let paths = [
77 Some(participant.source.as_path()),
78 Some(participant.common_dir.as_path()),
79 project,
80 former,
81 Some(participant.destination.as_path()),
82 ];
83 for path in paths.into_iter().flatten() {
84 control.check()?;
85 if !path
86 .try_exists()
87 .map_err(|e| Problem::io("Inspect backing location", e))?
88 {
89 continue;
90 }
91 let Ok(mut repo) = crate::Repository::local(self, path, control).await else {
92 control.check()?;
93 continue;
94 };
95 if repo.identity != participant.backing_identity {
96 continue;
97 }
98 if same_path(path, &participant.destination) {
99 repo.source = repo.common_dir.clone();
100 }
101 return Ok(repo.into());
102 }
103 Err(changed(
104 "The recorded backing repository is unavailable. Track its new location and retry workyard recover.",
105 ))
106 }
107
108 async fn preview_in(
109 &self,
110 repo: &Repository,
111 participant: &Participant,
112 control: &Control,
113 ) -> Result<Placement, Problem> {
114 let link = location::inspect(repo, participant)?;
115 let admin = workyard_system::directory_identity(&link.administration)?;
116 if participant.worktree_identity.as_ref() != Some(&admin) {
117 return Err(changed("Linked worktree identity cannot be verified."));
118 }
119 let common_matches = self.uses_backing(&link.destination, repo, control).await?;
120 let local_identity = workyard_system::entry_identity(&link.destination)?;
121 let registered_identity = if link.competing {
122 Some(workyard_system::entry_identity(&link.registered)?)
123 } else {
124 None
125 };
126 Ok(Placement {
127 registered: link.registered,
128 destination: link.destination,
129 competing: link.competing,
130 needs_repair: !link.registration_matches || !common_matches,
131 common_dir: repo.common_dir.clone(),
132 source: repo.source.clone(),
133 backing: repo.identity.clone(),
134 admin,
135 local_identity,
136 registered_identity,
137 })
138 }
139
140 pub async fn apply_placement(
142 &self,
143 participant: &Participant,
144 preview: &Placement,
145 activate: bool,
146 unavailable_worktrees: &HashSet<String>,
147 control: &Control,
148 ) -> Result<(), Problem> {
149 let repo: Repository = crate::Repository::local(self, &preview.source, control)
150 .await?
151 .into();
152 if repo.identity != preview.backing || participant.backing_identity != preview.backing {
153 return Err(changed("Backing storage changed since inspection."));
154 }
155 let _gate = relationship_lock(&repo, control).await?;
156 crate::Repository::from(&repo)
157 .revalidate_local(self, control)
158 .await?;
159 let current = self.preview_in(&repo, participant, control).await?;
160 if !current.needs_repair
163 && current.destination == preview.destination
164 && current.admin == preview.admin
165 && current.local_identity == preview.local_identity
166 {
167 return Ok(());
168 }
169 if ¤t != preview {
170 return Err(changed(
171 "Worktree locations changed since inspection. Run workyard recover again.",
172 ));
173 }
174 if !current.needs_repair {
175 return Ok(());
176 }
177 if current.competing && !activate {
178 return Err(current.diagnostic());
179 }
180 let link = location::inspect(&repo, participant)?;
181 if locks_present(&link.administration, &repo.common_dir)? {
182 return Err(Problem::new(
183 ProblemCode::ResourceBusy,
184 "A Git writer is active. Retry recovery when it finishes.",
185 ));
186 }
187 control.check()?;
188 self.repair_registered_tree(
189 &repo,
190 participant,
191 &link.administration,
192 ¤t.destination,
193 Some(unavailable_worktrees),
194 control,
195 )
196 .await?;
197 let repaired = self
198 .preview_in(&repo, participant, &Control::new(|| false))
199 .await?;
200 if repaired.needs_repair
201 || repaired.destination != current.destination
202 || repaired.local_identity != current.local_identity
203 {
204 return Err(changed(
205 "Git completed repair but the verified worktree is not registered here.",
206 ));
207 }
208 Ok(())
209 }
210}
211
212pub(super) fn placement_problem(registered: &Path, candidate: &Path, competing: bool) -> Problem {
213 let message = format!("Git registers {registered:?}; workspace files are at {candidate:?}.");
214 let (code, remedy) = if competing {
215 (
216 ProblemCode::DecisionRequired,
217 "Run workyard recover in the workspace you want to use.",
218 )
219 } else {
220 (
221 ProblemCode::RepositoryChanged,
222 "Run workyard recover to repair the worktree link.",
223 )
224 };
225 let mut problem = Problem::new(code, message).remedy(remedy);
226 problem.worktree_locations = Some(Box::new(workyard_core::WorktreeLocations {
227 registered: registered.into(),
228 candidate: candidate.into(),
229 }));
230 problem
231}