(start: &Path)
| 73 | } |
| 74 | |
| 75 | pub(super) fn discover_layout(start: &Path) -> Result<RepositoryLayout, RepositoryError> { |
| 76 | let start = search_start(start)?; |
| 77 | let home_dir = dirs::home_dir().and_then(|path| std::fs::canonicalize(path).ok()); |
| 78 | let mut current = Some(start.clone()); |
| 79 | |
| 80 | while let Some(dir) = current { |
| 81 | let local_dot_dir = dir.join(DOT_DIR); |
| 82 | let repository_pointer = local_dot_dir.join(REPOSITORY_POINTER); |
| 83 | if repository_pointer.is_file() { |
| 84 | let common_dot_dir = read_repository_pointer(&repository_pointer)?; |
| 85 | return layout_for_paths(dir, common_dot_dir, local_dot_dir, false); |
| 86 | } |
| 87 | |
| 88 | if super::has_database(&local_dot_dir) { |
| 89 | return layout_for_paths(dir, local_dot_dir.clone(), local_dot_dir, false); |
| 90 | } |
| 91 | |
| 92 | if home_dir.as_deref() == Some(dir.as_path()) { |
| 93 | break; |
| 94 | } |
| 95 | current = dir.parent().map(Path::to_path_buf); |
| 96 | } |
| 97 | |
| 98 | if let Some(git) = resolve_git_admin(&start)? { |
| 99 | let common_parent = |
| 100 | git.common_dir |
| 101 | .parent() |
| 102 | .ok_or_else(|| RepositoryError::InvalidRepository { |
| 103 | reason: format!( |
| 104 | "resolved Git common directory '{}' has no parent", |
| 105 | git.common_dir.display() |
| 106 | ), |
| 107 | })?; |
| 108 | let common_dot_dir = common_parent.join(DOT_DIR); |
| 109 | // CB-13D ::24 R1: linked-layout detection keys on the resolved Git |
| 110 | // common directory's .atomic presence, not on `pristine.redb` file |
| 111 | // existence — a linked worktree whose common store has not yet |
| 112 | // created the database file (or is mid-migration) must still |
| 113 | // resolve to its common layout instead of being misdetected. |
| 114 | if common_dot_dir.is_dir() { |
| 115 | let working_copy_dot_dir = git.worktree_root.join(DOT_DIR); |
| 116 | return Ok(RepositoryLayout { |
| 117 | working_root: git.worktree_root.clone(), |
| 118 | common_dot_dir: canonical_existing(&common_dot_dir, "Atomic common directory")?, |
| 119 | working_copy_dot_dir, |
| 120 | location_fingerprint: fingerprint(&git.worktree_root, Some(&git)), |
| 121 | pointer_needs_write: true, |
| 122 | }); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | Err(RepositoryError::NotFound { |
| 127 | path: start.display().to_string(), |
| 128 | }) |
| 129 | } |
| 130 | |
| 131 | pub(super) fn ensure_repository_pointer(layout: &RepositoryLayout) -> Result<(), RepositoryError> { |
| 132 | if !layout.pointer_needs_write { |
no test coverage detected