fallow_engine/
repo_refs_base_root.rs1use std::path::{Path, PathBuf};
5
6use super::base_analysis_root;
7
8#[derive(Debug, Clone, PartialEq, Eq)]
11pub enum BaseAnalysisRoot {
12 Present(PathBuf),
15 NewInHead(PathBuf),
19}
20
21#[must_use]
35pub fn resolve_base_analysis_root(
36 current_root: &Path,
37 base_worktree_root: &Path,
38) -> BaseAnalysisRoot {
39 let root = base_analysis_root(current_root, base_worktree_root);
40 if !root.is_dir() {
41 return BaseAnalysisRoot::NewInHead(root);
42 }
43 if !stays_inside(&root, base_worktree_root) {
44 tracing::warn!(
45 root = %root.display(),
46 base_worktree = %base_worktree_root.display(),
47 "The analysis root of the base commit resolves outside the base worktree through a link; the base snapshot is empty"
48 );
49 return BaseAnalysisRoot::NewInHead(root);
50 }
51 BaseAnalysisRoot::Present(root)
52}
53
54fn stays_inside(path: &Path, dir: &Path) -> bool {
57 match (dunce::canonicalize(path), dunce::canonicalize(dir)) {
58 (Ok(path), Ok(dir)) => path.starts_with(dir),
59 _ => false,
60 }
61}
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66
67 #[test]
72 fn resolve_base_analysis_root_reports_a_root_absent_from_the_base() {
73 let temp = tempfile::tempdir().expect("temp dir");
74 let repo = temp.path().join("repo");
75 std::fs::create_dir_all(&repo).expect("create repo");
76 crate::git_fixture::fixture_git(&repo, &["init", "-q", "-b", "main"]);
77 let existing_root = repo.join("apps").join("mobile");
78 std::fs::create_dir_all(&existing_root).expect("create existing root");
79 let new_root = repo.join("apps").join("new");
80 std::fs::create_dir_all(&new_root).expect("create new root");
81
82 let base_worktree = temp.path().join("base-worktree");
83 std::fs::create_dir_all(base_worktree.join("apps").join("mobile"))
84 .expect("create base subdirectory");
85
86 assert_eq!(
87 resolve_base_analysis_root(&existing_root, &base_worktree),
88 BaseAnalysisRoot::Present(base_worktree.join("apps").join("mobile"))
89 );
90 assert_eq!(
91 resolve_base_analysis_root(&new_root, &base_worktree),
92 BaseAnalysisRoot::NewInHead(base_worktree.join("apps").join("new"))
93 );
94 }
95
96 #[cfg(unix)]
100 #[test]
101 fn resolve_base_analysis_root_does_not_follow_a_link_out_of_the_base_worktree() {
102 let temp = tempfile::tempdir().expect("temp dir");
103 let repo = temp.path().join("repo");
104 std::fs::create_dir_all(&repo).expect("create repo");
105 crate::git_fixture::fixture_git(&repo, &["init", "-q", "-b", "main"]);
106 let app_root = repo.join("apps").join("mobile");
107 std::fs::create_dir_all(&app_root).expect("create app root");
108 let outside = temp.path().join("outside");
109 std::fs::create_dir_all(outside.join("mobile")).expect("create outside dir");
110 let base_worktree = temp.path().join("base-worktree");
111 std::fs::create_dir_all(&base_worktree).expect("create base worktree");
112 std::os::unix::fs::symlink(&outside, base_worktree.join("apps")).expect("link");
113
114 assert_eq!(
115 resolve_base_analysis_root(&app_root, &base_worktree),
116 BaseAnalysisRoot::NewInHead(base_worktree.join("apps").join("mobile"))
117 );
118 }
119
120 #[cfg(unix)]
122 #[test]
123 fn resolve_base_analysis_root_follows_a_link_inside_the_base_worktree() {
124 let temp = tempfile::tempdir().expect("temp dir");
125 let repo = temp.path().join("repo");
126 std::fs::create_dir_all(&repo).expect("create repo");
127 crate::git_fixture::fixture_git(&repo, &["init", "-q", "-b", "main"]);
128 let app_root = repo.join("apps").join("mobile");
129 std::fs::create_dir_all(&app_root).expect("create app root");
130 let base_worktree = temp.path().join("base-worktree");
131 std::fs::create_dir_all(base_worktree.join("packages").join("mobile"))
132 .expect("create base dir");
133 std::os::unix::fs::symlink("packages", base_worktree.join("apps")).expect("link");
134
135 assert_eq!(
136 resolve_base_analysis_root(&app_root, &base_worktree),
137 BaseAnalysisRoot::Present(base_worktree.join("apps").join("mobile"))
138 );
139 }
140
141 #[cfg(unix)]
145 #[test]
146 fn a_base_worktree_with_a_committed_link_out_of_it_has_no_base_root() {
147 use crate::git_fixture::fixture_git;
148 let temp = tempfile::tempdir().expect("temp dir");
149 let repo = temp.path().join("repo");
150 std::fs::create_dir_all(&repo).expect("create repo");
151 let outside = temp.path().join("outside");
152 std::fs::create_dir_all(outside.join("app")).expect("outside dir");
153 let outside = dunce::canonicalize(&outside).expect("outside path");
154 fixture_git(&repo, &["init", "-q", "-b", "main"]);
155 std::os::unix::fs::symlink(&outside, repo.join("packages")).expect("link");
156 fixture_git(&repo, &["add", "."]);
157 fixture_git(&repo, &["commit", "-q", "-m", "base with a link"]);
158 std::fs::remove_file(repo.join("packages")).expect("remove link");
159 std::fs::create_dir_all(repo.join("packages").join("app")).expect("app dir");
160 std::fs::write(repo.join("packages/app/index.ts"), "export {};\n").expect("source");
161 fixture_git(&repo, &["add", "-A"]);
162 fixture_git(&repo, &["commit", "-q", "-m", "head with a directory"]);
163
164 let worktree = super::super::TemporaryBaseWorktree::create(&repo, "HEAD~1", &[])
165 .expect("base worktree");
166 let linked = worktree.path().join("packages");
167 assert!(
168 std::fs::symlink_metadata(&linked).is_ok_and(|meta| meta.file_type().is_symlink()),
169 "the base worktree recreates the link"
170 );
171 assert_eq!(
172 resolve_base_analysis_root(&repo.join("packages").join("app"), worktree.path()),
173 BaseAnalysisRoot::NewInHead(linked.join("app"))
174 );
175 }
176}