comparison rust/hg-core/src/repo.rs @ 47405:88119fffecc8

rhg: look for repository in ancestors also instead of cwd only 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. Differential Revision: https://phab.mercurial-scm.org/D10767
author Pulkit Goyal <7895pulkit@gmail.com>
date Mon, 24 May 2021 16:27:54 +0530
parents ebdef6283798
children 6e49769b7f97
comparison
equal deleted inserted replaced
47404:ebdef6283798 47405:88119fffecc8
41 pub struct Vfs<'a> { 41 pub struct Vfs<'a> {
42 pub(crate) base: &'a Path, 42 pub(crate) base: &'a Path,
43 } 43 }
44 44
45 impl Repo { 45 impl Repo {
46 /// tries to find nearest repository root in current working directory or
47 /// its ancestors
48 pub fn find_repo_root() -> Result<PathBuf, RepoError> {
49 let current_directory = crate::utils::current_dir()?;
50 // ancestors() is inclusive: it first yields `current_directory`
51 // as-is.
52 for ancestor in current_directory.ancestors() {
53 if ancestor.join(".hg").is_dir() {
54 return Ok(ancestor.to_path_buf());
55 }
56 }
57 return Err(RepoError::NotFound {
58 at: current_directory,
59 });
60 }
61
46 /// Find a repository, either at the given path (which must contain a `.hg` 62 /// Find a repository, either at the given path (which must contain a `.hg`
47 /// sub-directory) or by searching the current directory and its 63 /// sub-directory) or by searching the current directory and its
48 /// ancestors. 64 /// ancestors.
49 /// 65 ///
50 /// A method with two very different "modes" like this usually a code smell 66 /// A method with two very different "modes" like this usually a code smell
64 Err(RepoError::NotFound { 80 Err(RepoError::NotFound {
65 at: root.to_owned(), 81 at: root.to_owned(),
66 }) 82 })
67 } 83 }
68 } else { 84 } else {
69 let current_directory = crate::utils::current_dir()?; 85 let root = Self::find_repo_root()?;
70 // ancestors() is inclusive: it first yields `current_directory` 86 Self::new_at_path(root, config)
71 // as-is.
72 for ancestor in current_directory.ancestors() {
73 if ancestor.join(".hg").is_dir() {
74 return Self::new_at_path(ancestor.to_owned(), config);
75 }
76 }
77 Err(RepoError::NotFound {
78 at: current_directory,
79 })
80 } 87 }
81 } 88 }
82 89
83 /// To be called after checking that `.hg` is a sub-directory 90 /// To be called after checking that `.hg` is a sub-directory
84 fn new_at_path( 91 fn new_at_path(