rust/hg-core/src/repo.rs
changeset 46505 a25033eb43b5
parent 46503 d8730ff51d5a
child 46593 5ce2aa7c2ad5
equal deleted inserted replaced
46504:2e5dd18d6dc3 46505:a25033eb43b5
    41 pub(crate) struct Vfs<'a> {
    41 pub(crate) struct Vfs<'a> {
    42     base: &'a Path,
    42     base: &'a Path,
    43 }
    43 }
    44 
    44 
    45 impl Repo {
    45 impl Repo {
    46     /// Search the current directory and its ancestores for a repository:
    46     /// Find a repository, either at the given path (which must contain a `.hg`
    47     /// a working directory that contains a `.hg` sub-directory.
    47     /// sub-directory) or by searching the current directory and its
       
    48     /// ancestors.
    48     ///
    49     ///
    49     /// `explicit_path` is for `--repository` command-line arguments.
    50     /// A method with two very different "modes" like this usually a code smell
       
    51     /// to make two methods instead, but in this case an `Option` is what rhg
       
    52     /// sub-commands get from Clap for the `-R` / `--repository` CLI argument.
       
    53     /// Having two methods would just move that `if` to almost all callers.
    50     pub fn find(
    54     pub fn find(
    51         config: &Config,
    55         config: &Config,
    52         explicit_path: Option<&Path>,
    56         explicit_path: Option<&Path>,
    53     ) -> Result<Self, RepoError> {
    57     ) -> Result<Self, RepoError> {
    54         if let Some(root) = explicit_path {
    58         if let Some(root) = explicit_path {
    72                 }
    76                 }
    73             }
    77             }
    74             Err(RepoError::NotFound {
    78             Err(RepoError::NotFound {
    75                 at: current_directory,
    79                 at: current_directory,
    76             })
    80             })
       
    81         }
       
    82     }
       
    83 
       
    84     /// Like `Repo::find`, but not finding a repository is not an error if no
       
    85     /// explicit path is given. `Ok(None)` is returned in that case.
       
    86     ///
       
    87     /// If an explicit path *is* given, not finding a repository there is still
       
    88     /// an error.
       
    89     ///
       
    90     /// For sub-commands that don’t need a repository, configuration should
       
    91     /// still be affected by a repository’s `.hg/hgrc` file. This is the
       
    92     /// constructor to use.
       
    93     pub fn find_optional(
       
    94         config: &Config,
       
    95         explicit_path: Option<&Path>,
       
    96     ) -> Result<Option<Self>, RepoError> {
       
    97         match Self::find(config, explicit_path) {
       
    98             Ok(repo) => Ok(Some(repo)),
       
    99             Err(RepoError::NotFound { .. }) if explicit_path.is_none() => {
       
   100                 Ok(None)
       
   101             }
       
   102             Err(error) => Err(error),
    77         }
   103         }
    78     }
   104     }
    79 
   105 
    80     /// To be called after checking that `.hg` is a sub-directory
   106     /// To be called after checking that `.hg` is a sub-directory
    81     fn new_at_path(
   107     fn new_at_path(