changeset 53153:84f537bd7fbc

hg-core: add a find_repo_root_from method This method allow to find a repository from an arbitrary directory. This will be useful to find the repository associated with a store.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Wed, 19 Mar 2025 10:27:35 +0100
parents fbe3880cdf67
children 10a7d4a66bcb
files rust/hg-core/src/repo.rs
diffstat 1 files changed, 7 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-core/src/repo.rs	Thu Mar 20 12:38:49 2025 +0100
+++ b/rust/hg-core/src/repo.rs	Wed Mar 19 10:27:35 2025 +0100
@@ -94,15 +94,18 @@
     /// its ancestors
     pub fn find_repo_root() -> Result<PathBuf, RepoError> {
         let current_directory = crate::utils::current_dir()?;
-        // ancestors() is inclusive: it first yields `current_directory`
-        // as-is.
-        for ancestor in current_directory.ancestors() {
+        Repo::find_repo_root_from(&current_directory)
+    }
+
+    /// tries to find nearest repository root from a given path
+    pub fn find_repo_root_from(path: &Path) -> Result<PathBuf, RepoError> {
+        for ancestor in path.ancestors() {
             if is_dir(ancestor.join(".hg"))? {
                 return Ok(ancestor.to_path_buf());
             }
         }
         Err(RepoError::NotFound {
-            at: current_directory,
+            at: path.to_path_buf(),
         })
     }