Skip to content

Commit

Permalink
config: move read_user/repo_config() methods to ConfigEnv
Browse files Browse the repository at this point in the history
  • Loading branch information
yuja committed Nov 27, 2024
1 parent 1d485b7 commit a890e1a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
6 changes: 3 additions & 3 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions cli/src/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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...>
Expand All @@ -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());
Expand Down
48 changes: 26 additions & 22 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down

0 comments on commit a890e1a

Please sign in to comment.