Skip to main content

fallow_engine/
repo_refs_base_root.rs

1//! The analysis root inside a detached base worktree, and whether the base
2//! commit contains it.
3
4use std::path::{Path, PathBuf};
5
6use super::base_analysis_root;
7
8/// Analysis root for a detached base worktree, and whether the base commit
9/// contains it at all.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub enum BaseAnalysisRoot {
12    /// The head analysis root maps onto a directory that the base commit
13    /// contains, so the base snapshot is analyzed there.
14    Present(PathBuf),
15    /// The head analysis root maps onto a directory the base commit does not
16    /// contain, such as a package added on the branch. Everything under it is
17    /// new, so the base snapshot for that root is empty.
18    NewInHead(PathBuf),
19}
20
21/// Resolve the analysis root inside a detached base worktree and report
22/// whether the base commit contains it.
23///
24/// A root that the base commit does not contain is the ordinary shape of
25/// auditing a package added on the branch. Analyzing the whole base worktree
26/// instead would compare a subdirectory head snapshot against a
27/// whole-repository base snapshot, whose key spaces do not intersect, and
28/// refusing the call would blame a `root` the caller spelled correctly.
29///
30/// The base commit can hold a link at or above the root. When the real path
31/// of the root is outside the base worktree, or cannot be read, the root
32/// counts as absent, so the base pass never reads or writes through that
33/// link.
34#[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
54/// Whether the real path of `path` is inside the real path of `dir`. A path
55/// that cannot be resolved is not inside.
56fn 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    /// Auditing a package added on the branch is the ordinary case where the
68    /// remapped root is absent from the base worktree. Consumers need that
69    /// reported rather than validating the joined path and refusing the call
70    /// (issue #2699).
71    #[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    /// The base commit can hold a link at or above the analysis root. A link
97    /// whose target is outside the base worktree must not move the base pass
98    /// out of the worktree, so the root counts as absent from the base.
99    #[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    /// A link that stays inside the base worktree is an ordinary layout.
121    #[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    /// A base worktree from a real commit recreates its links. A link of
142    /// the base commit with an absolute target outside the worktree must
143    /// not become the base analysis root.
144    #[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}