Skip to main content

workyard_core/ports/
git.rs

1use crate::{Control, Problem};
2use std::{
3	future::Future,
4	path::{Path, PathBuf},
5};
6
7/// Identity, location and selected remote of a backing Git repository.
8#[derive(Clone, Debug)]
9pub struct Repository {
10	/// Path to the backing repository.
11	pub source: PathBuf,
12	/// Git administration directory shared by the repository's worktrees.
13	pub common_dir: PathBuf,
14	/// Remote selected for branch resolution or fetch.
15	pub remote: Option<String>,
16	/// Stable identifier used to match repository observations.
17	pub identity: String,
18}
19
20/// Git branch action to use when adding the linked worktree.
21#[derive(Clone, Debug)]
22pub enum BranchPlan {
23	/// Check out the existing local branch.
24	Reuse,
25	/// Add the supplied roots to the project catalog configuration.
26	Track(String),
27	/// Create the local branch at the supplied starting revision.
28	Create(String),
29}
30
31/// Which refs may be used during branch planning.
32#[derive(Clone, Copy, Debug, PartialEq, Eq)]
33pub enum SourceResolution {
34	/// Resolve using currently available refs.
35	Cached,
36	/// Plan before the requested fetch has completed.
37	FetchPending,
38}
39
40/// Operations used to validate repository identity, resolve branches and establish worktrees.
41pub trait Git: Sync {
42	/// Identity used to verify the linked worktree before applying changes.
43	fn worktree_identity(
44		&self,
45		_path: &Path,
46	) -> impl Future<Output = Result<Option<String>, Problem>> + Send {
47		async { Ok(None) }
48	}
49
50	/// Ask Git to validate the task's proposed branch name.
51	fn validate_branch(
52		&self,
53		root: &Path,
54		task: &str,
55		control: &Control,
56	) -> impl Future<Output = Result<(), Problem>> + Send;
57
58	/// Resolve the backing repository, its identity and the selected remote.
59	fn inspect(
60		&self,
61		path: &Path,
62		remote: Option<&str>,
63		control: &Control,
64	) -> impl Future<Output = Result<Repository, Problem>> + Send;
65
66	/// Check the repository against its earlier identity before proceeding.
67	fn revalidate(
68		&self,
69		repository: &Repository,
70		control: &Control,
71	) -> impl Future<Output = Result<(), Problem>> + Send;
72
73	/// Resolve reuse, tracking or creation of the task branch.
74	fn plan(
75		&self,
76		repository: &Repository,
77		task: &str,
78		checkout: bool,
79		from: Option<&str>,
80		resolution: SourceResolution,
81		control: &Control,
82	) -> impl Future<Output = Result<BranchPlan, Problem>> + Send;
83
84	/// Refresh refs from the selected remote under Workyard's fetch coordination.
85	fn fetch(
86		&self,
87		repository: &Repository,
88		control: &Control,
89	) -> impl Future<Output = Result<(), Problem>> + Send;
90
91	/// Create the linked worktree using the resolved branch plan.
92	fn add(
93		&self,
94		repository: &Repository,
95		task: &str,
96		destination: &Path,
97		plan: &BranchPlan,
98		control: &Control,
99	) -> impl Future<Output = Result<(), Problem>> + Send;
100}
101
102/// Git observations and repair operations available during task recovery.
103pub trait RecoveryGit: Git {
104	/// Discover an identity-verified location needing repair without changing Git.
105	fn recovery_relationship(
106		&self,
107		_participant: &crate::Participant,
108		_control: &Control,
109	) -> impl Future<Output = Result<Option<crate::Relationship>, Problem>> + Send {
110		async { Ok(None) }
111	}
112
113	/// Ask Git to reconcile verified backing and linked worktree paths.
114	fn repair_relationship(
115		&self,
116		_repository: &Repository,
117		_participant: &crate::Participant,
118		_control: &Control,
119	) -> impl Future<Output = Result<(), Problem>> + Send {
120		async {
121			Err(Problem::new(
122				crate::ProblemCode::RepositoryChanged,
123				"Git adapter does not support relationship repair.",
124			))
125		}
126	}
127
128	/// Inspect actual participant placement before resuming recorded work.
129	fn reconcile(
130		&self,
131		participant: &crate::Participant,
132		task: &str,
133		control: &Control,
134	) -> impl Future<Output = Result<crate::RecoveryObservation, Problem>> + Send;
135}