##// END OF EJS Templates
rhg: Move `Repo` object creation into `main()`...
Simon Sapin -
r47335:5ce2aa7c default
parent child Browse files
Show More
@@ -81,28 +81,6 b' impl Repo {'
81 }
81 }
82 }
82 }
83
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),
103 }
104 }
105
106 /// To be called after checking that `.hg` is a sub-directory
84 /// To be called after checking that `.hg` is a sub-directory
107 fn new_at_path(
85 fn new_at_path(
108 working_directory: PathBuf,
86 working_directory: PathBuf,
@@ -1,7 +1,6 b''
1 use crate::error::CommandError;
1 use crate::error::CommandError;
2 use clap::Arg;
2 use clap::Arg;
3 use hg::operations::cat;
3 use hg::operations::cat;
4 use hg::repo::Repo;
5 use hg::utils::hg_path::HgPathBuf;
4 use hg::utils::hg_path::HgPathBuf;
6 use micro_timer::timed;
5 use micro_timer::timed;
7 use std::convert::TryFrom;
6 use std::convert::TryFrom;
@@ -39,7 +38,7 b' pub fn run(invocation: &crate::CliInvoca'
39 None => vec![],
38 None => vec![],
40 };
39 };
41
40
42 let repo = Repo::find(invocation.non_repo_config, invocation.repo_path)?;
41 let repo = invocation.repo?;
43 let cwd = hg::utils::current_dir()?;
42 let cwd = hg::utils::current_dir()?;
44
43
45 let mut files = vec![];
44 let mut files = vec![];
@@ -2,7 +2,6 b' use crate::error::CommandError;'
2 use clap::Arg;
2 use clap::Arg;
3 use format_bytes::format_bytes;
3 use format_bytes::format_bytes;
4 use hg::errors::HgError;
4 use hg::errors::HgError;
5 use hg::repo::Repo;
6 use hg::utils::SliceExt;
5 use hg::utils::SliceExt;
7
6
8 pub const HELP_TEXT: &str = "
7 pub const HELP_TEXT: &str = "
@@ -22,13 +21,6 b" pub fn args() -> clap::App<'static, 'sta"
22 }
21 }
23
22
24 pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
23 pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
25 let opt_repo =
26 Repo::find_optional(invocation.non_repo_config, invocation.repo_path)?;
27 let config = if let Some(repo) = &opt_repo {
28 repo.config()
29 } else {
30 invocation.non_repo_config
31 };
32 let (section, name) = invocation
24 let (section, name) = invocation
33 .subcommand_args
25 .subcommand_args
34 .value_of("name")
26 .value_of("name")
@@ -37,7 +29,7 b' pub fn run(invocation: &crate::CliInvoca'
37 .split_2(b'.')
29 .split_2(b'.')
38 .ok_or_else(|| HgError::abort(""))?;
30 .ok_or_else(|| HgError::abort(""))?;
39
31
40 let value = config.get(section, name).unwrap_or(b"");
32 let value = invocation.config().get(section, name).unwrap_or(b"");
41
33
42 invocation.ui.write_stdout(&format_bytes!(b"{}\n", value))?;
34 invocation.ui.write_stdout(&format_bytes!(b"{}\n", value))?;
43 Ok(())
35 Ok(())
@@ -2,7 +2,6 b' use crate::error::CommandError;'
2 use clap::Arg;
2 use clap::Arg;
3 use clap::ArgGroup;
3 use clap::ArgGroup;
4 use hg::operations::{debug_data, DebugDataKind};
4 use hg::operations::{debug_data, DebugDataKind};
5 use hg::repo::Repo;
6 use micro_timer::timed;
5 use micro_timer::timed;
7
6
8 pub const HELP_TEXT: &str = "
7 pub const HELP_TEXT: &str = "
@@ -55,8 +54,8 b' pub fn run(invocation: &crate::CliInvoca'
55 }
54 }
56 };
55 };
57
56
58 let repo = Repo::find(invocation.non_repo_config, invocation.repo_path)?;
57 let repo = invocation.repo?;
59 let data = debug_data(&repo, rev, kind).map_err(|e| (e, rev))?;
58 let data = debug_data(repo, rev, kind).map_err(|e| (e, rev))?;
60
59
61 let mut stdout = invocation.ui.stdout_buffer();
60 let mut stdout = invocation.ui.stdout_buffer();
62 stdout.write_all(&data)?;
61 stdout.write_all(&data)?;
@@ -1,5 +1,4 b''
1 use crate::error::CommandError;
1 use crate::error::CommandError;
2 use hg::repo::Repo;
3
2
4 pub const HELP_TEXT: &str = "
3 pub const HELP_TEXT: &str = "
5 Print the current repo requirements.
4 Print the current repo requirements.
@@ -10,7 +9,7 b" pub fn args() -> clap::App<'static, 'sta"
10 }
9 }
11
10
12 pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
11 pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
13 let repo = Repo::find(invocation.non_repo_config, invocation.repo_path)?;
12 let repo = invocation.repo?;
14 let mut output = String::new();
13 let mut output = String::new();
15 let mut requirements: Vec<_> = repo.requirements().iter().collect();
14 let mut requirements: Vec<_> = repo.requirements().iter().collect();
16 requirements.sort();
15 requirements.sort();
@@ -29,15 +29,14 b" pub fn args() -> clap::App<'static, 'sta"
29 pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
29 pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
30 let rev = invocation.subcommand_args.value_of("rev");
30 let rev = invocation.subcommand_args.value_of("rev");
31
31
32 let repo = Repo::find(invocation.non_repo_config, invocation.repo_path)?;
32 let repo = invocation.repo?;
33 if let Some(rev) = rev {
33 if let Some(rev) = rev {
34 let files =
34 let files = list_rev_tracked_files(repo, rev).map_err(|e| (e, rev))?;
35 list_rev_tracked_files(&repo, rev).map_err(|e| (e, rev))?;
35 display_files(invocation.ui, repo, files.iter())
36 display_files(invocation.ui, &repo, files.iter())
37 } else {
36 } else {
38 let distate = Dirstate::new(&repo)?;
37 let distate = Dirstate::new(repo)?;
39 let files = distate.tracked_files()?;
38 let files = distate.tracked_files()?;
40 display_files(invocation.ui, &repo, files)
39 display_files(invocation.ui, repo, files)
41 }
40 }
42 }
41 }
43
42
@@ -1,6 +1,5 b''
1 use crate::error::CommandError;
1 use crate::error::CommandError;
2 use format_bytes::format_bytes;
2 use format_bytes::format_bytes;
3 use hg::repo::Repo;
4 use hg::utils::files::get_bytes_from_path;
3 use hg::utils::files::get_bytes_from_path;
5
4
6 pub const HELP_TEXT: &str = "
5 pub const HELP_TEXT: &str = "
@@ -14,7 +13,7 b" pub fn args() -> clap::App<'static, 'sta"
14 }
13 }
15
14
16 pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
15 pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
17 let repo = Repo::find(invocation.non_repo_config, invocation.repo_path)?;
16 let repo = invocation.repo?;
18 let bytes = get_bytes_from_path(repo.working_directory_path());
17 let bytes = get_bytes_from_path(repo.working_directory_path());
19 invocation
18 invocation
20 .ui
19 .ui
@@ -1,5 +1,6 b''
1 use crate::ui::utf8_to_local;
1 use crate::ui::utf8_to_local;
2 use crate::ui::UiError;
2 use crate::ui::UiError;
3 use crate::NoRepoInCwdError;
3 use format_bytes::format_bytes;
4 use format_bytes::format_bytes;
4 use hg::config::{ConfigError, ConfigParseError};
5 use hg::config::{ConfigError, ConfigParseError};
5 use hg::errors::HgError;
6 use hg::errors::HgError;
@@ -64,7 +65,7 b' impl From<RepoError> for CommandError {'
64 match error {
65 match error {
65 RepoError::NotFound { at } => CommandError::Abort {
66 RepoError::NotFound { at } => CommandError::Abort {
66 message: format_bytes!(
67 message: format_bytes!(
67 b"no repository found in '{}' (.hg not found)!",
68 b"repository {} not found",
68 get_bytes_from_path(at)
69 get_bytes_from_path(at)
69 ),
70 ),
70 },
71 },
@@ -74,6 +75,18 b' impl From<RepoError> for CommandError {'
74 }
75 }
75 }
76 }
76
77
78 impl<'a> From<&'a NoRepoInCwdError> for CommandError {
79 fn from(error: &'a NoRepoInCwdError) -> Self {
80 let NoRepoInCwdError { cwd } = error;
81 CommandError::Abort {
82 message: format_bytes!(
83 b"no repository found in '{}' (.hg not found)!",
84 get_bytes_from_path(cwd)
85 ),
86 }
87 }
88 }
89
77 impl From<ConfigError> for CommandError {
90 impl From<ConfigError> for CommandError {
78 fn from(error: ConfigError) -> Self {
91 fn from(error: ConfigError) -> Self {
79 match error {
92 match error {
@@ -6,7 +6,8 b' use clap::Arg;'
6 use clap::ArgMatches;
6 use clap::ArgMatches;
7 use format_bytes::format_bytes;
7 use format_bytes::format_bytes;
8 use hg::config::Config;
8 use hg::config::Config;
9 use std::path::Path;
9 use hg::repo::{Repo, RepoError};
10 use std::path::{Path, PathBuf};
10
11
11 mod error;
12 mod error;
12 mod exitcode;
13 mod exitcode;
@@ -74,17 +75,25 b' fn main_with_result(ui: &ui::Ui) -> Resu'
74 let non_repo_config = &hg::config::Config::load(config_args)?;
75 let non_repo_config = &hg::config::Config::load(config_args)?;
75
76
76 let repo_path = value_of_global_arg("repository").map(Path::new);
77 let repo_path = value_of_global_arg("repository").map(Path::new);
78 let repo = match Repo::find(non_repo_config, repo_path) {
79 Ok(repo) => Ok(repo),
80 Err(RepoError::NotFound { at }) if repo_path.is_none() => {
81 // Not finding a repo is not fatal yet, if `-R` was not given
82 Err(NoRepoInCwdError { cwd: at })
83 }
84 Err(error) => return Err(error.into()),
85 };
77
86
78 run(&CliInvocation {
87 run(&CliInvocation {
79 ui,
88 ui,
80 subcommand_args,
89 subcommand_args,
81 non_repo_config,
90 non_repo_config,
82 repo_path,
91 repo: repo.as_ref(),
83 })
92 })
84 }
93 }
85
94
86 fn main() {
95 fn main() {
87 let ui = Ui::new();
96 let ui = ui::Ui::new();
88
97
89 let exit_code = match main_with_result(&ui) {
98 let exit_code = match main_with_result(&ui) {
90 Ok(()) => exitcode::OK,
99 Ok(()) => exitcode::OK,
@@ -146,5 +155,22 b" pub struct CliInvocation<'a> {"
146 ui: &'a Ui,
155 ui: &'a Ui,
147 subcommand_args: &'a ArgMatches<'a>,
156 subcommand_args: &'a ArgMatches<'a>,
148 non_repo_config: &'a Config,
157 non_repo_config: &'a Config,
149 repo_path: Option<&'a Path>,
158 /// References inside `Result` is a bit peculiar but allow
159 /// `invocation.repo?` to work out with `&CliInvocation` since this
160 /// `Result` type is `Copy`.
161 repo: Result<&'a Repo, &'a NoRepoInCwdError>,
162 }
163
164 struct NoRepoInCwdError {
165 cwd: PathBuf,
150 }
166 }
167
168 impl CliInvocation<'_> {
169 fn config(&self) -> &Config {
170 if let Ok(repo) = self.repo {
171 repo.config()
172 } else {
173 self.non_repo_config
174 }
175 }
176 }
General Comments 0
You need to be logged in to leave comments. Login now