[Updated] [+- ] D10767: rhg: look for repository in ancestors also instead of cwd only

pulkit (Pulkit Goyal) phabricator at mercurial-scm.org
Wed Jun 9 07:51:36 UTC 2021


pulkit updated this revision to Diff 28514.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D10767?vs=28505&id=28514

BRANCH
  default

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D10767/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D10767

AFFECTED FILES
  rust/hg-core/src/repo.rs
  rust/rhg/src/main.rs

CHANGE DETAILS

diff --git a/rust/rhg/src/main.rs b/rust/rhg/src/main.rs
--- a/rust/rhg/src/main.rs
+++ b/rust/rhg/src/main.rs
@@ -174,9 +174,8 @@
         } else {
             let local_config = {
                 if std::env::var_os("HGRCSKIPREPO").is_none() {
-                    let current_dir = hg::utils::current_dir();
-                    // TODO: handle errors from current_dir
-                    if let Ok(current_dir_path) = current_dir {
+                    // TODO: handle errors from find_repo_root
+                    if let Ok(current_dir_path) = Repo::find_repo_root() {
                         let config_files = vec![
                             ConfigSource::AbsPath(
                                 current_dir_path.join(".hg/hgrc"),
diff --git a/rust/hg-core/src/repo.rs b/rust/hg-core/src/repo.rs
--- a/rust/hg-core/src/repo.rs
+++ b/rust/hg-core/src/repo.rs
@@ -43,6 +43,22 @@
 }
 
 impl Repo {
+    /// tries to find nearest repository root in current working directory or
+    /// 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() {
+            if ancestor.join(".hg").is_dir() {
+                return Ok(ancestor.to_path_buf());
+            }
+        }
+        return Err(RepoError::NotFound {
+            at: current_directory,
+        });
+    }
+
     /// Find a repository, either at the given path (which must contain a `.hg`
     /// sub-directory) or by searching the current directory and its
     /// ancestors.
@@ -66,17 +82,8 @@
                 })
             }
         } else {
-            let current_directory = crate::utils::current_dir()?;
-            // ancestors() is inclusive: it first yields `current_directory`
-            // as-is.
-            for ancestor in current_directory.ancestors() {
-                if ancestor.join(".hg").is_dir() {
-                    return Self::new_at_path(ancestor.to_owned(), config);
-                }
-            }
-            Err(RepoError::NotFound {
-                at: current_directory,
-            })
+            let root = Self::find_repo_root()?;
+            Self::new_at_path(root, config)
         }
     }
 



To: pulkit, #hg-reviewers
Cc: SimonSapin, mercurial-patches
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mercurial-scm.org/pipermail/mercurial-patches/attachments/20210609/a10fec0b/attachment-0001.html>


More information about the Mercurial-patches mailing list