Patchwork D10767: rhg: look for repository in ancestors also instead of cwd only

login
register
mail settings
Submitter phabricator
Date May 24, 2021, 11 a.m.
Message ID <differential-rev-PHID-DREV-jbvxywofgguegosseckc-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/49087/
State Superseded
Headers show

Comments

phabricator - May 24, 2021, 11 a.m.
pulkit created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  Last patch introduced config reading at startup to parse value of `--repository`
  flag. However, that patch only tried to check for current repository at current
  working directory and not it's ancestors. This patch fixes that.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

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

CHANGE DETAILS




To: pulkit, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

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,8 +174,7 @@ 
         } else {
             let local_config = {
                 if std::env::var_os("HGRCSKIPREPO").is_none() {
-                    let current_dir = hg::utils::current_dir();
-                    if let Ok(current_dir_path) = current_dir {
+                    if let Ok(current_dir_path) = Repo::try_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 a repository in current working directory and returns its
+    /// root path
+    pub fn try_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,12 @@ 
                 })
             }
         } 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);
-                }
+            let repo_root = Self::try_find_repo_root();
+            if repo_root.is_ok() {
+                Self::new_at_path(repo_root.unwrap(), config)
+            } else {
+                Err(repo_root.unwrap_err())
             }
-            Err(RepoError::NotFound {
-                at: current_directory,
-            })
         }
     }