MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / find_repository_root_from

Function find_repository_root_from

atomic-cli/src/commands/mod.rs:303–358  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Calls 6

has_databaseFunction · 0.85
detect_repository_rootFunction · 0.85
canonicalizeMethod · 0.80
is_dirMethod · 0.80
as_refMethod · 0.80
is_fileMethod · 0.45