diff rust/hg-core/src/config/config.rs @ 46486:d7685105e504

rhg: Parse per-repository configuration Differential Revision: https://phab.mercurial-scm.org/D9964
author Simon Sapin <simon.sapin@octobus.net>
date Thu, 04 Feb 2021 15:04:53 +0100
parents 2845892dd489
children eace48b4a786
line wrap: on
line diff
--- a/rust/hg-core/src/config/config.rs	Thu Feb 04 14:29:47 2021 +0100
+++ b/rust/hg-core/src/config/config.rs	Thu Feb 04 15:04:53 2021 +0100
@@ -16,7 +16,6 @@
 use std::path::{Path, PathBuf};
 
 use crate::errors::{HgResultExt, IoResultExt};
-use crate::repo::Repo;
 
 /// Holds the config values for the current repository
 /// TODO update this docstring once we support more sources
@@ -196,12 +195,28 @@
         Ok(Config { layers })
     }
 
-    /// Loads the local config. In a future version, this will also load the
-    /// `$HOME/.hgrc` and more to mirror the Python implementation.
-    pub fn load_for_repo(repo: &Repo) -> Result<Self, ConfigError> {
-        Ok(Self::load_from_explicit_sources(vec![
-            ConfigSource::AbsPath(repo.hg_vfs().join("hgrc")),
-        ])?)
+    /// Loads the per-repository config into a new `Config` which is combined
+    /// with `self`.
+    pub(crate) fn combine_with_repo(
+        &self,
+        repo_config_files: &[PathBuf],
+    ) -> Result<Self, ConfigError> {
+        let (cli_layers, other_layers) = self
+            .layers
+            .iter()
+            .cloned()
+            .partition(ConfigLayer::is_from_command_line);
+
+        let mut repo_config = Self {
+            layers: other_layers,
+        };
+        for path in repo_config_files {
+            // TODO: check if this file should be trusted:
+            // `mercurial/ui.py:427`
+            repo_config.add_trusted_file(path)?;
+        }
+        repo_config.layers.extend(cli_layers);
+        Ok(repo_config)
     }
 
     /// Returns an `Err` if the first value found is not a valid boolean.
@@ -297,8 +312,6 @@
         let config = Config::load_from_explicit_sources(sources)
             .expect("expected valid config");
 
-        dbg!(&config);
-
         let (_, value) = config.get_inner(b"section", b"item").unwrap();
         assert_eq!(
             value,