config: move read_user/repo_config() methods to ConfigEnv

This commit is contained in:
Yuya Nishihara 2024-11-23 19:28:23 +09:00
parent 1d485b75b6
commit a890e1a7f8
3 changed files with 32 additions and 28 deletions

View file

@ -3562,10 +3562,10 @@ impl CliRunner {
.workspace_loader_factory
.create(find_workspace_dir(&cwd))
.map_err(|err| map_workspace_load_error(err, None));
layered_configs.read_user_config(&config_env)?;
config_env.reload_user_config(&mut layered_configs)?;
if let Ok(loader) = &maybe_cwd_workspace_loader {
config_env.reset_repo_path(loader.repo_path());
layered_configs.read_repo_config(&config_env)?;
config_env.reload_repo_config(&mut layered_configs)?;
}
let config = layered_configs.merge();
ui.reset(&config).map_err(|e| {
@ -3603,7 +3603,7 @@ impl CliRunner {
.create(&cwd.join(path))
.map_err(|err| map_workspace_load_error(err, Some(path)))?;
config_env.reset_repo_path(loader.repo_path());
layered_configs.read_repo_config(&config_env)?;
config_env.reload_repo_config(&mut layered_configs)?;
Ok(loader)
} else {
maybe_cwd_workspace_loader

View file

@ -495,10 +495,10 @@ fn get_jj_command() -> Result<(JjBuilder, Config), CommandError> {
.map_err(user_error)?;
let mut config_env = ConfigEnv::from_environment()?;
let maybe_cwd_workspace_loader = DefaultWorkspaceLoaderFactory.create(find_workspace_dir(&cwd));
let _ = layered_configs.read_user_config(&config_env);
let _ = config_env.reload_user_config(&mut layered_configs);
if let Ok(loader) = &maybe_cwd_workspace_loader {
config_env.reset_repo_path(loader.repo_path());
let _ = layered_configs.read_repo_config(&config_env);
let _ = config_env.reload_repo_config(&mut layered_configs);
}
let mut config = layered_configs.merge();
// skip 2 because of the clap_complete prelude: jj -- jj <actual args...>
@ -516,7 +516,7 @@ fn get_jj_command() -> Result<(JjBuilder, Config), CommandError> {
// Try to update repo-specific config on a best-effort basis.
if let Ok(loader) = DefaultWorkspaceLoaderFactory.create(&cwd.join(&repository)) {
config_env.reset_repo_path(loader.repo_path());
let _ = layered_configs.read_repo_config(&config_env);
let _ = config_env.reload_repo_config(&mut layered_configs);
config = layered_configs.merge();
}
cmd_args.push("--repository".into());

View file

@ -129,28 +129,6 @@ impl LayeredConfigs {
LayeredConfigs { inner }
}
#[instrument]
pub fn read_user_config(&mut self, env: &ConfigEnv) -> Result<(), ConfigError> {
self.inner.remove_layers(ConfigSource::User);
if let Some(path) = env.existing_user_config_path() {
if path.is_dir() {
self.inner.load_dir(ConfigSource::User, path)?;
} else {
self.inner.load_file(ConfigSource::User, path)?;
}
}
Ok(())
}
#[instrument]
pub fn read_repo_config(&mut self, env: &ConfigEnv) -> Result<(), ConfigError> {
self.inner.remove_layers(ConfigSource::Repo);
if let Some(path) = env.existing_repo_config_path() {
self.inner.load_file(ConfigSource::Repo, path)?;
}
Ok(())
}
pub fn parse_config_args(&mut self, toml_strs: &[String]) -> Result<(), ConfigEnvError> {
self.inner.remove_layers(ConfigSource::CommandArg);
let config = toml_strs
@ -351,6 +329,21 @@ impl ConfigEnv {
}
}
/// Loads user-specific config files into the given `config`. The old
/// user-config layers will be replaced if any.
#[instrument]
pub fn reload_user_config(&self, config: &mut LayeredConfigs) -> Result<(), ConfigError> {
config.inner.remove_layers(ConfigSource::User);
if let Some(path) = self.existing_user_config_path() {
if path.is_dir() {
config.inner.load_dir(ConfigSource::User, path)?;
} else {
config.inner.load_file(ConfigSource::User, path)?;
}
}
Ok(())
}
/// Sets the directory where repo-specific config file is stored. The path
/// is usually `.jj/repo`.
pub fn reset_repo_path(&mut self, path: &Path) {
@ -376,6 +369,17 @@ impl ConfigEnv {
ConfigPath::Unavailable => None,
}
}
/// Loads repo-specific config file into the given `config`. The old
/// repo-config layer will be replaced if any.
#[instrument]
pub fn reload_repo_config(&self, config: &mut LayeredConfigs) -> Result<(), ConfigError> {
config.inner.remove_layers(ConfigSource::Repo);
if let Some(path) = self.existing_repo_config_path() {
config.inner.load_file(ConfigSource::Repo, path)?;
}
Ok(())
}
}
/// Environment variables that should be overridden by config values