use std::path::{Path, PathBuf};
use super::base_analysis_root;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BaseAnalysisRoot {
Present(PathBuf),
NewInHead(PathBuf),
}
#[must_use]
pub fn resolve_base_analysis_root(
current_root: &Path,
base_worktree_root: &Path,
) -> BaseAnalysisRoot {
let root = base_analysis_root(current_root, base_worktree_root);
if !root.is_dir() {
return BaseAnalysisRoot::NewInHead(root);
}
if !stays_inside(&root, base_worktree_root) {
tracing::warn!(
root = %root.display(),
base_worktree = %base_worktree_root.display(),
"The analysis root of the base commit resolves outside the base worktree through a link; the base snapshot is empty"
);
return BaseAnalysisRoot::NewInHead(root);
}
BaseAnalysisRoot::Present(root)
}
fn stays_inside(path: &Path, dir: &Path) -> bool {
match (dunce::canonicalize(path), dunce::canonicalize(dir)) {
(Ok(path), Ok(dir)) => path.starts_with(dir),
_ => false,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn resolve_base_analysis_root_reports_a_root_absent_from_the_base() {
let temp = tempfile::tempdir().expect("temp dir");
let repo = temp.path().join("repo");
std::fs::create_dir_all(&repo).expect("create repo");
crate::git_fixture::fixture_git(&repo, &["init", "-q", "-b", "main"]);
let existing_root = repo.join("apps").join("mobile");
std::fs::create_dir_all(&existing_root).expect("create existing root");
let new_root = repo.join("apps").join("new");
std::fs::create_dir_all(&new_root).expect("create new root");
let base_worktree = temp.path().join("base-worktree");
std::fs::create_dir_all(base_worktree.join("apps").join("mobile"))
.expect("create base subdirectory");
assert_eq!(
resolve_base_analysis_root(&existing_root, &base_worktree),
BaseAnalysisRoot::Present(base_worktree.join("apps").join("mobile"))
);
assert_eq!(
resolve_base_analysis_root(&new_root, &base_worktree),
BaseAnalysisRoot::NewInHead(base_worktree.join("apps").join("new"))
);
}
#[cfg(unix)]
#[test]
fn resolve_base_analysis_root_does_not_follow_a_link_out_of_the_base_worktree() {
let temp = tempfile::tempdir().expect("temp dir");
let repo = temp.path().join("repo");
std::fs::create_dir_all(&repo).expect("create repo");
crate::git_fixture::fixture_git(&repo, &["init", "-q", "-b", "main"]);
let app_root = repo.join("apps").join("mobile");
std::fs::create_dir_all(&app_root).expect("create app root");
let outside = temp.path().join("outside");
std::fs::create_dir_all(outside.join("mobile")).expect("create outside dir");
let base_worktree = temp.path().join("base-worktree");
std::fs::create_dir_all(&base_worktree).expect("create base worktree");
std::os::unix::fs::symlink(&outside, base_worktree.join("apps")).expect("link");
assert_eq!(
resolve_base_analysis_root(&app_root, &base_worktree),
BaseAnalysisRoot::NewInHead(base_worktree.join("apps").join("mobile"))
);
}
#[cfg(unix)]
#[test]
fn resolve_base_analysis_root_follows_a_link_inside_the_base_worktree() {
let temp = tempfile::tempdir().expect("temp dir");
let repo = temp.path().join("repo");
std::fs::create_dir_all(&repo).expect("create repo");
crate::git_fixture::fixture_git(&repo, &["init", "-q", "-b", "main"]);
let app_root = repo.join("apps").join("mobile");
std::fs::create_dir_all(&app_root).expect("create app root");
let base_worktree = temp.path().join("base-worktree");
std::fs::create_dir_all(base_worktree.join("packages").join("mobile"))
.expect("create base dir");
std::os::unix::fs::symlink("packages", base_worktree.join("apps")).expect("link");
assert_eq!(
resolve_base_analysis_root(&app_root, &base_worktree),
BaseAnalysisRoot::Present(base_worktree.join("apps").join("mobile"))
);
}
#[cfg(unix)]
#[test]
fn a_base_worktree_with_a_committed_link_out_of_it_has_no_base_root() {
use crate::git_fixture::fixture_git;
let temp = tempfile::tempdir().expect("temp dir");
let repo = temp.path().join("repo");
std::fs::create_dir_all(&repo).expect("create repo");
let outside = temp.path().join("outside");
std::fs::create_dir_all(outside.join("app")).expect("outside dir");
let outside = dunce::canonicalize(&outside).expect("outside path");
fixture_git(&repo, &["init", "-q", "-b", "main"]);
std::os::unix::fs::symlink(&outside, repo.join("packages")).expect("link");
fixture_git(&repo, &["add", "."]);
fixture_git(&repo, &["commit", "-q", "-m", "base with a link"]);
std::fs::remove_file(repo.join("packages")).expect("remove link");
std::fs::create_dir_all(repo.join("packages").join("app")).expect("app dir");
std::fs::write(repo.join("packages/app/index.ts"), "export {};\n").expect("source");
fixture_git(&repo, &["add", "-A"]);
fixture_git(&repo, &["commit", "-q", "-m", "head with a directory"]);
let worktree = super::super::TemporaryBaseWorktree::create(&repo, "HEAD~1", &[])
.expect("base worktree");
let linked = worktree.path().join("packages");
assert!(
std::fs::symlink_metadata(&linked).is_ok_and(|meta| meta.file_type().is_symlink()),
"the base worktree recreates the link"
);
assert_eq!(
resolve_base_analysis_root(&repo.join("packages").join("app"), worktree.path()),
BaseAnalysisRoot::NewInHead(linked.join("app"))
);
}
}