Skip to main content

workyard_git/
relationships.rs

1//! Reconcile verified linked-worktree identities, including broken location pointers.
2mod location;
3pub(crate) mod pointer;
4mod portability;
5mod repair;
6use crate::{
7	Runner, operations::operations_present, output::path_line, process::args,
8	worktree_path::same_path,
9};
10use location::locate;
11pub use portability::Placement;
12use std::path::Path;
13use workyard_core::{
14	AmendGit, Control, Participant, Problem, ProblemCode, Relationship, Repository,
15};
16
17impl AmendGit for Runner {
18	async fn relationship(
19		&self,
20		repository: &Repository,
21		participant: &Participant,
22		control: &Control,
23	) -> Result<Relationship, Problem> {
24		self.inspect_relationship(repository, participant, control)
25			.await
26	}
27
28	async fn repair(
29		&self,
30		repository: &Repository,
31		participant: &Participant,
32		control: &Control,
33	) -> Result<(), Problem> {
34		self.repair_links(repository, participant, control).await
35	}
36
37	async fn move_worktree(
38		&self,
39		repository: &Repository,
40		participant: &Participant,
41		destination: &Path,
42		control: &Control,
43	) -> Result<(), Problem> {
44		let _gate = relationship_lock(repository, control).await?;
45		let current = self.relationship(repository, participant, control).await?;
46		if current.needs_repair
47			|| !same_path(&current.participant.destination, &participant.destination)
48		{
49			return Err(changed(
50				"Participant location changed before its rename; retry amend.",
51			));
52		}
53		let mut arguments = args(&["worktree", "move", "--"]);
54		arguments.push(dunce::simplified(&participant.destination).into());
55		arguments.push(dunce::simplified(destination).into());
56		self.effect(&repository.source, &arguments, control).await?;
57		let moved = self
58			.relationship(repository, participant, &Control::new(|| false))
59			.await?;
60		if moved.needs_repair || !same_path(&moved.participant.destination, destination) {
61			return Err(changed(
62				"Git moved the worktree, but its registered destination differs from the requested path.",
63			));
64		}
65		Ok(())
66	}
67}
68
69pub(crate) async fn relationship_lock(
70	repository: &Repository,
71	control: &Control,
72) -> Result<workyard_system::Lock, Problem> {
73	let directory = repository.common_dir.join("workyard");
74	std::fs::create_dir_all(&directory)
75		.map_err(|error| Problem::io("Create repository coordination directory", error))?;
76	let lock = workyard_system::Lock::wait(&directory.join("relationships.lock"), control).await?;
77	lock.verify()?;
78	Ok(lock)
79}
80
81pub(super) fn changed(message: &str) -> Problem {
82	Problem::new(ProblemCode::RepositoryChanged, message).remedy("Inspect the supplied repository and participant paths; preserve their files and retry after correcting the location.")
83}
84
85impl Runner {
86	async fn inspect_relationship(
87		&self,
88		repository: &Repository,
89		participant: &Participant,
90		control: &Control,
91	) -> Result<Relationship, Problem> {
92		crate::Repository::from(repository)
93			.revalidate_local(self, control)
94			.await?;
95		if repository.identity != participant.backing_identity {
96			return Err(changed(
97				"The supplied repository is not this participant's backing storage.",
98			));
99		}
100		let location = locate(repository, participant)?;
101		if operations_present(&location.administration, &repository.common_dir)? {
102			return Err(Problem::new(
103				ProblemCode::ResourceBusy,
104				"The participant has an unfinished Git operation or native lock.",
105			)
106			.remedy("Finish or abort the affected Git operation, then retry the operation."));
107		}
108		let common_matches = self
109			.uses_backing(&location.destination, repository, control)
110			.await?;
111		let needs_repair = !location.registration_matches || !common_matches;
112		let mut current = participant.clone();
113		current.worktree_identity = Some(workyard_system::directory_identity(
114			&location.administration,
115		)?);
116		current.source = repository.source.clone();
117		current.common_dir = repository.common_dir.clone();
118		if !needs_repair && !same_path(&participant.destination, &location.destination) {
119			current.placement = None;
120		}
121		current.destination = location.destination;
122		Ok(Relationship {
123			participant: current,
124			needs_repair,
125		})
126	}
127
128	async fn uses_backing(
129		&self,
130		destination: &Path,
131		repository: &Repository,
132		control: &Control,
133	) -> Result<bool, Problem> {
134		let args = args(&["rev-parse", "--path-format=absolute", "--git-common-dir"]);
135		let observed = self.checked(destination, &args, control).await;
136		control.check()?;
137		let bytes = match observed {
138			Ok(bytes) => bytes,
139			Err(problem) if problem.code == ProblemCode::GitFailed => return Ok(false),
140			Err(problem) => return Err(problem),
141		};
142		let common = path_line(&bytes).map_err(|_| changed("Invalid native Git path."))?;
143		Ok(workyard_system::directory_identity(&common)? == repository.identity)
144	}
145
146	async fn repair_links(
147		&self,
148		repository: &Repository,
149		participant: &Participant,
150		control: &Control,
151	) -> Result<(), Problem> {
152		let _gate = relationship_lock(repository, control).await?;
153		let current = self
154			.inspect_relationship(repository, participant, control)
155			.await?;
156		let location = locate(repository, participant)?;
157		self.repair_registered_tree(
158			repository,
159			participant,
160			&location.administration,
161			&current.participant.destination,
162			None,
163			control,
164		)
165		.await?;
166		let repaired = self
167			.inspect_relationship(repository, participant, &Control::new(|| false))
168			.await?;
169		if repaired.needs_repair {
170			return Err(changed(
171				"Git completed repair but the participant links remain unresolved.",
172			));
173		}
174		Ok(())
175	}
176}