Find the repository root starting from a specific path. Like [`find_repository_root`], but starts from the given path instead of the current working directory. # Arguments `start_path` - The path to start searching from # Returns The path to the repository root. # Errors Returns [`CliError::RepositoryNotFound`] if no repository is found. # Example ```rust,ignore let repo_root = find_repos
(start_path: &Path)
| 299 | /// let repo_root = find_repository_root_from("/home/user/project/src")?; |
| 300 | /// ``` |
| 301 | pub fn find_repository_root_from(start_path: &Path) -> CliResult<PathBuf> { |
| 302 | let mut current = start_path.to_path_buf(); |
| 303 | |
| 304 | // Canonicalize if possible to handle symlinks correctly |
| 305 | if let Ok(canonical) = current.canonicalize() { |
| 306 | current = canonical; |
| 307 | } |
| 308 | |
| 309 | // Never walk above $HOME: ~/.atomic holds global configuration (identity, |
| 310 | // config.toml, logs) but no repository database, so treating it as a |
| 311 | // search boundary prevents `atomic status`/`record`/etc. from silently |
| 312 | // failing when run in the home directory, and keeps home from being |
| 313 | // mistaken for a repo root. |
| 314 | let home = home_dir(); |
| 315 | |
| 316 | loop { |
| 317 | let dot_dir = current.join(DOT_DIR); |
| 318 | // A bare `.atomic/` may hold global configuration (e.g. ~/.atomic), |
| 319 | // not a repository. Require the repository's graph database too. |
| 320 | if dot_dir.is_dir() && atomic_repository::has_database(&dot_dir) { |
| 321 | return Ok(current); |
| 322 | } |
| 323 | |
| 324 | // A sandbox working tree has no `.atomic/` of its own — it carries a |
| 325 | // pointer to the canonical graph. Treat the sandbox root as a valid |
| 326 | // repository root; `Repository::open*` resolves the pointer. |
| 327 | if current.join(SANDBOX_POINTER).is_file() { |
| 328 | return Ok(current); |
| 329 | } |
| 330 | |
| 331 | // CB-13D ::24 R1/R5: a LINKED worktree also has no local `.atomic/` |
| 332 | // — its `.git` pointer resolves to the common repository's `.atomic`. |
| 333 | // The repository layer's layout detection is authoritative here; a |
| 334 | // plain `git worktree` used to fall through to "not a repository". |
| 335 | if current.join(".git").is_file() { |
| 336 | if let Ok(Some(root)) = atomic_repository::detect_repository_root(¤t) { |
| 337 | return Ok(root); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | // Stop at $HOME rather than walking into / — home is not a repo. |
| 342 | if home.as_ref().is_some_and(|h| current == *h) { |
| 343 | return Err(CliError::RepositoryNotFound { |
| 344 | searched_path: start_path.to_path_buf(), |
| 345 | }); |
| 346 | } |
| 347 | |
| 348 | // Move to parent directory |
| 349 | if !current.pop() { |
| 350 | // Reached the root without finding a repository |
| 351 | return Err(CliError::RepositoryNotFound { |
| 352 | searched_path: start_path.to_path_buf(), |
| 353 | }); |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /// Open a repository, optionally at a specific path. |