Skip to main content

workyard_git/repository/
inspection.rs

1use super::Repository;
2use super::remote::choose_remote;
3use super::{decode_path, utf8};
4use crate::{Runner, process::args};
5use std::path::Path;
6use workyard_core::{Control, Problem};
7
8impl Repository {
9	/// Inspect the selected repository or task without applying changes.
10	pub async fn inspect(
11		runner: &Runner,
12		path: &Path,
13		requested_remote: Option<&str>,
14		control: &Control,
15	) -> Result<Self, Problem> {
16		let mut repository = Self::local(runner, path, control).await?;
17		let output = runner
18			.checked(&repository.source, &args(&["remote"]), control)
19			.await?;
20		let remotes: Vec<_> = utf8(&output)?.lines().map(str::to_owned).collect();
21		repository.remote = choose_remote(&remotes, requested_remote)
22			.map_err(|problem| problem.in_repository(&repository.source))?;
23		Ok(repository)
24	}
25
26	/// Placement and destructive admission depend only on local Git identity.
27	pub(crate) async fn local(
28		runner: &Runner,
29		path: &Path,
30		control: &Control,
31	) -> Result<Self, Problem> {
32		let source = path
33			.canonicalize()
34			.map_err(|e| Problem::io(format!("Resolve repository {path:?}"), e))?;
35		let common = runner
36			.checked(
37				&source,
38				&args(&["rev-parse", "--path-format=absolute", "--git-common-dir"]),
39				control,
40			)
41			.await?;
42		let common_dir = decode_path(&common)?
43			.canonicalize()
44			.map_err(|e| Problem::io("Resolve Git common directory", e))?;
45		let identity = workyard_system::directory_identity(&common_dir)?;
46		Ok(Self {
47			source,
48			common_dir,
49			remote: None,
50			identity,
51		})
52	}
53
54	/// Check the repository against its earlier identity before proceeding.
55	pub async fn revalidate(&self, runner: &Runner, control: &Control) -> Result<(), Problem> {
56		let current = Self::inspect(runner, &self.source, self.remote.as_deref(), control).await?;
57		self.verify_backing(&current)
58	}
59
60	pub(crate) async fn revalidate_local(
61		&self,
62		runner: &Runner,
63		control: &Control,
64	) -> Result<(), Problem> {
65		let current = Self::local(runner, &self.source, control).await?;
66		self.verify_backing(&current)
67	}
68
69	fn verify_backing(&self, current: &Self) -> Result<(), Problem> {
70		if current.common_dir != self.common_dir || current.identity != self.identity {
71			return Err(Problem::new(
72				workyard_core::ProblemCode::RepositoryChanged,
73				format!("Git backing identity changed at {:?}.", self.source),
74			));
75		}
76
77		Ok(())
78	}
79}