1use crate::{Repository, Runner};
2use std::path::Path;
3use workyard_core::{BranchPlan, Control, Git, Problem, ProblemCode};
4
5impl Git for Runner {
6 async fn worktree_identity(&self, path: &Path) -> Result<Option<String>, Problem> {
7 let output = self
8 .checked(
9 path,
10 &crate::process::args(&["rev-parse", "--path-format=absolute", "--git-dir"]),
11 &Control::new(|| false),
12 )
13 .await?;
14 let directory = crate::output::path_line(&output).map_err(|_| {
15 Problem::new(ProblemCode::RepositoryChanged, "Invalid Git path output.").remedy("Inspect git worktree list --porcelain and git status at the recorded paths. Repair Git relationships manually; preserve worktrees and native Git locks.")
16 })?;
17 Ok(Some(workyard_system::directory_identity(&directory)?))
18 }
19
20 async fn validate_branch(
21 &self,
22 root: &Path,
23 task: &str,
24 control: &Control,
25 ) -> Result<(), Problem> {
26 let output = self
27 .git(root, &["check-ref-format", "--branch", task], control)
28 .await?;
29 if !output.status.success() {
30 return Err(Problem::new(
31 ProblemCode::InvalidTask,
32 "Task is not a valid Git branch name.",
33 ));
34 }
35
36 Ok(())
37 }
38
39 async fn inspect(
40 &self,
41 path: &Path,
42 remote: Option<&str>,
43 control: &Control,
44 ) -> Result<workyard_core::Repository, Problem> {
45 let repository = Repository::inspect(self, path, remote, control).await?;
46 Ok(repository.into())
47 }
48
49 async fn revalidate(
50 &self,
51 repository: &workyard_core::Repository,
52 control: &Control,
53 ) -> Result<(), Problem> {
54 Repository::from(repository).revalidate(self, control).await
55 }
56
57 async fn plan(
58 &self,
59 repository: &workyard_core::Repository,
60 task: &str,
61 checkout: bool,
62 from: Option<&str>,
63 resolution: workyard_core::SourceResolution,
64 control: &Control,
65 ) -> Result<BranchPlan, Problem> {
66 Repository::from(repository)
67 .plan(self, task, checkout, from, resolution, control)
68 .await
69 }
70
71 async fn fetch(
72 &self,
73 repository: &workyard_core::Repository,
74 control: &Control,
75 ) -> Result<(), Problem> {
76 let _lock = fetch_lock(repository, control).await?;
77 self.revalidate(repository, control).await?;
78 Repository::from(repository).fetch(self, control).await
79 }
80
81 async fn add(
82 &self,
83 repository: &workyard_core::Repository,
84 task: &str,
85 destination: &Path,
86 plan: &BranchPlan,
87 control: &Control,
88 ) -> Result<(), Problem> {
89 Repository::from(repository)
90 .add(self, task, destination, plan, control)
91 .await
92 }
93}
94
95async fn fetch_lock(
96 repository: &workyard_core::Repository,
97 control: &Control,
98) -> Result<workyard_system::Lock, Problem> {
99 let directory = repository.common_dir.join("workyard");
100 std::fs::create_dir_all(&directory)
101 .map_err(|e| Problem::io("Create Git coordination directory", e))?;
102 let lock = workyard_system::Lock::wait(&directory.join("fetch.lock"), control).await?;
103 lock.verify()?;
104 Ok(lock)
105}