Patchwork D9962: rhg: Parse system and user configuration at program start

login
register
mail settings
Submitter phabricator
Date Feb. 5, 2021, 9:27 a.m.
Message ID <differential-rev-PHID-DREV-ra3xhozanwlr474l37al-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/48279/
State Superseded
Headers show

Comments

phabricator - Feb. 5, 2021, 9:27 a.m.
SimonSapin created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  … and pass it around up to `Repo::find`

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  rust/hg-core/src/repo.rs
  rust/rhg/src/commands.rs
  rust/rhg/src/commands/cat.rs
  rust/rhg/src/commands/debugdata.rs
  rust/rhg/src/commands/debugrequirements.rs
  rust/rhg/src/commands/files.rs
  rust/rhg/src/commands/root.rs
  rust/rhg/src/error.rs
  rust/rhg/src/main.rs

CHANGE DETAILS




To: SimonSapin, #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
@@ -123,20 +123,23 @@ 
     matches: ArgMatches,
     ui: &ui::Ui,
 ) -> Result<(), CommandError> {
+    let config = hg::config::Config::load()?;
+
     match matches.subcommand() {
-        ("root", _) => commands::root::RootCommand::new().run(&ui),
+        ("root", _) => commands::root::RootCommand::new().run(&ui, &config),
         ("files", Some(matches)) => {
-            commands::files::FilesCommand::try_from(matches)?.run(&ui)
+            commands::files::FilesCommand::try_from(matches)?.run(&ui, &config)
         }
         ("cat", Some(matches)) => {
-            commands::cat::CatCommand::try_from(matches)?.run(&ui)
+            commands::cat::CatCommand::try_from(matches)?.run(&ui, &config)
         }
         ("debugdata", Some(matches)) => {
-            commands::debugdata::DebugDataCommand::try_from(matches)?.run(&ui)
+            commands::debugdata::DebugDataCommand::try_from(matches)?
+                .run(&ui, &config)
         }
         ("debugrequirements", _) => {
             commands::debugrequirements::DebugRequirementsCommand::new()
-                .run(&ui)
+                .run(&ui, &config)
         }
         _ => unreachable!(), // Because of AppSettings::SubcommandRequired,
     }
diff --git a/rust/rhg/src/error.rs b/rust/rhg/src/error.rs
--- a/rust/rhg/src/error.rs
+++ b/rust/rhg/src/error.rs
@@ -1,6 +1,7 @@ 
 use crate::ui::utf8_to_local;
 use crate::ui::UiError;
 use format_bytes::format_bytes;
+use hg::config::{ConfigError, ConfigParseError};
 use hg::errors::HgError;
 use hg::repo::RepoFindError;
 use hg::revlog::revlog::RevlogError;
@@ -66,6 +67,36 @@ 
     }
 }
 
+impl From<ConfigError> for CommandError {
+    fn from(error: ConfigError) -> Self {
+        match error {
+            ConfigError::Parse(ConfigParseError {
+                origin,
+                line,
+                bytes,
+            }) => {
+                let line_message = if let Some(line_number) = line {
+                    format_bytes!(
+                        b" at line {}",
+                        line_number.to_string().into_bytes()
+                    )
+                } else {
+                    Vec::new()
+                };
+                CommandError::Abort {
+                    message: format_bytes!(
+                        b"config parse error in {}{}: '{}'",
+                        origin.to_bytes(),
+                        line_message,
+                        bytes
+                    ),
+                }
+            }
+            ConfigError::Other(error) => error.into(),
+        }
+    }
+}
+
 impl From<(RevlogError, &str)> for CommandError {
     fn from((err, rev): (RevlogError, &str)) -> CommandError {
         match err {
diff --git a/rust/rhg/src/commands/root.rs b/rust/rhg/src/commands/root.rs
--- a/rust/rhg/src/commands/root.rs
+++ b/rust/rhg/src/commands/root.rs
@@ -2,6 +2,7 @@ 
 use crate::error::CommandError;
 use crate::ui::Ui;
 use format_bytes::format_bytes;
+use hg::config::Config;
 use hg::repo::Repo;
 use hg::utils::files::get_bytes_from_path;
 
@@ -20,8 +21,8 @@ 
 }
 
 impl Command for RootCommand {
-    fn run(&self, ui: &Ui) -> Result<(), CommandError> {
-        let repo = Repo::find()?;
+    fn run(&self, ui: &Ui, config: &Config) -> Result<(), CommandError> {
+        let repo = Repo::find(config)?;
         let bytes = get_bytes_from_path(repo.working_directory_path());
         ui.write_stdout(&format_bytes!(b"{}\n", bytes.as_slice()))?;
         Ok(())
diff --git a/rust/rhg/src/commands/files.rs b/rust/rhg/src/commands/files.rs
--- a/rust/rhg/src/commands/files.rs
+++ b/rust/rhg/src/commands/files.rs
@@ -1,6 +1,7 @@ 
 use crate::commands::Command;
 use crate::error::CommandError;
 use crate::ui::Ui;
+use hg::config::Config;
 use hg::operations::list_rev_tracked_files;
 use hg::operations::Dirstate;
 use hg::repo::Repo;
@@ -46,8 +47,8 @@ 
 }
 
 impl<'a> Command for FilesCommand<'a> {
-    fn run(&self, ui: &Ui) -> Result<(), CommandError> {
-        let repo = Repo::find()?;
+    fn run(&self, ui: &Ui, config: &Config) -> Result<(), CommandError> {
+        let repo = Repo::find(config)?;
         if let Some(rev) = self.rev {
             let files =
                 list_rev_tracked_files(&repo, rev).map_err(|e| (e, rev))?;
diff --git a/rust/rhg/src/commands/debugrequirements.rs b/rust/rhg/src/commands/debugrequirements.rs
--- a/rust/rhg/src/commands/debugrequirements.rs
+++ b/rust/rhg/src/commands/debugrequirements.rs
@@ -1,6 +1,7 @@ 
 use crate::commands::Command;
 use crate::error::CommandError;
 use crate::ui::Ui;
+use hg::config::Config;
 use hg::repo::Repo;
 
 pub const HELP_TEXT: &str = "
@@ -16,8 +17,8 @@ 
 }
 
 impl Command for DebugRequirementsCommand {
-    fn run(&self, ui: &Ui) -> Result<(), CommandError> {
-        let repo = Repo::find()?;
+    fn run(&self, ui: &Ui, config: &Config) -> Result<(), CommandError> {
+        let repo = Repo::find(config)?;
         let mut output = String::new();
         let mut requirements: Vec<_> = repo.requirements().iter().collect();
         requirements.sort();
diff --git a/rust/rhg/src/commands/debugdata.rs b/rust/rhg/src/commands/debugdata.rs
--- a/rust/rhg/src/commands/debugdata.rs
+++ b/rust/rhg/src/commands/debugdata.rs
@@ -1,6 +1,7 @@ 
 use crate::commands::Command;
 use crate::error::CommandError;
 use crate::ui::Ui;
+use hg::config::Config;
 use hg::operations::{debug_data, DebugDataKind};
 use hg::repo::Repo;
 use micro_timer::timed;
@@ -22,8 +23,8 @@ 
 
 impl<'a> Command for DebugDataCommand<'a> {
     #[timed]
-    fn run(&self, ui: &Ui) -> Result<(), CommandError> {
-        let repo = Repo::find()?;
+    fn run(&self, ui: &Ui, config: &Config) -> Result<(), CommandError> {
+        let repo = Repo::find(config)?;
         let data = debug_data(&repo, self.rev, self.kind)
             .map_err(|e| (e, self.rev))?;
 
diff --git a/rust/rhg/src/commands/cat.rs b/rust/rhg/src/commands/cat.rs
--- a/rust/rhg/src/commands/cat.rs
+++ b/rust/rhg/src/commands/cat.rs
@@ -1,6 +1,7 @@ 
 use crate::commands::Command;
 use crate::error::CommandError;
 use crate::ui::Ui;
+use hg::config::Config;
 use hg::operations::cat;
 use hg::repo::Repo;
 use hg::utils::hg_path::HgPathBuf;
@@ -29,8 +30,8 @@ 
 
 impl<'a> Command for CatCommand<'a> {
     #[timed]
-    fn run(&self, ui: &Ui) -> Result<(), CommandError> {
-        let repo = Repo::find()?;
+    fn run(&self, ui: &Ui, config: &Config) -> Result<(), CommandError> {
+        let repo = Repo::find(config)?;
         let cwd = hg::utils::current_dir()?;
 
         let mut files = vec![];
diff --git a/rust/rhg/src/commands.rs b/rust/rhg/src/commands.rs
--- a/rust/rhg/src/commands.rs
+++ b/rust/rhg/src/commands.rs
@@ -5,10 +5,11 @@ 
 pub mod root;
 use crate::error::CommandError;
 use crate::ui::Ui;
+use hg::config::Config;
 
 /// The common trait for rhg commands
 ///
 /// Normalize the interface of the commands provided by rhg
 pub trait Command {
-    fn run(&self, ui: &Ui) -> Result<(), CommandError>;
+    fn run(&self, ui: &Ui, config: &Config) -> Result<(), CommandError>;
 }
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
@@ -1,3 +1,4 @@ 
+use crate::config::Config;
 use crate::errors::{HgError, IoResultExt};
 use crate::requirements;
 use crate::utils::files::get_path_from_bytes;
@@ -31,7 +32,7 @@ 
 impl Repo {
     /// Search the current directory and its ancestores for a repository:
     /// a working directory that contains a `.hg` sub-directory.
-    pub fn find() -> Result<Self, RepoFindError> {
+    pub fn find(_config: &Config) -> Result<Self, RepoFindError> {
         let current_directory = crate::utils::current_dir()?;
         // ancestors() is inclusive: it first yields `current_directory` as-is.
         for ancestor in current_directory.ancestors() {