Skip to content

Commit

Permalink
config: move ConfigSource to jj-lib, make it Copy + Ord
Browse files Browse the repository at this point in the history
I decided to add `path: Option<PathBuf>` as a separate field, not a parameter
of ConfigSource::Repo|User variants.
  • Loading branch information
yuja committed Nov 25, 2024
1 parent 75b8e9b commit d75aaf3
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 23 deletions.
4 changes: 2 additions & 2 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ use jj_lib::backend::TreeValue;
use jj_lib::commit::Commit;
use jj_lib::config::ConfigError;
use jj_lib::config::ConfigNamePathBuf;
use jj_lib::config::ConfigSource;
use jj_lib::conflicts::ConflictMarkerStyle;
use jj_lib::file_util;
use jj_lib::fileset;
Expand Down Expand Up @@ -151,7 +152,6 @@ use crate::config::new_config_path;
use crate::config::AnnotatedValue;
use crate::config::CommandNameAndArgs;
use crate::config::ConfigEnvError;
use crate::config::ConfigSource;
use crate::config::LayeredConfigs;
use crate::diff_util;
use crate::diff_util::DiffFormat;
Expand Down Expand Up @@ -2765,7 +2765,7 @@ impl LogContentFormat {
}

pub fn get_new_config_file_path(
config_source: &ConfigSource,
config_source: ConfigSource,
command: &CommandHelper,
) -> Result<PathBuf, CommandError> {
let edit_path = match config_source {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/config/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ pub fn cmd_config_edit(
command: &CommandHelper,
args: &ConfigEditArgs,
) -> Result<(), CommandError> {
let config_path = get_new_config_file_path(&args.level.expect_source_kind(), command)?;
let config_path = get_new_config_file_path(args.level.expect_source_kind(), command)?;
run_ui_editor(command.settings(), &config_path)
}
2 changes: 1 addition & 1 deletion cli/src/commands/config/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use clap_complete::ArgValueCandidates;
use jj_lib::config::ConfigNamePathBuf;
use jj_lib::config::ConfigSource;
use tracing::instrument;

use super::ConfigLevelArgs;
Expand All @@ -22,7 +23,6 @@ use crate::command_error::CommandError;
use crate::complete;
use crate::config::to_toml_value;
use crate::config::AnnotatedValue;
use crate::config::ConfigSource;
use crate::generic_templater::GenericTemplateLanguage;
use crate::template_builder::TemplateLanguage as _;
use crate::templater::TemplatePropertyExt as _;
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod path;
mod set;
mod unset;

use jj_lib::config::ConfigSource;
use tracing::instrument;

use self::edit::cmd_config_edit;
Expand All @@ -35,7 +36,6 @@ use self::unset::cmd_config_unset;
use self::unset::ConfigUnsetArgs;
use crate::cli_util::CommandHelper;
use crate::command_error::CommandError;
use crate::config::ConfigSource;
use crate::ui::Ui;

#[derive(clap::Args, Clone, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/config/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn cmd_config_path(
command: &CommandHelper,
args: &ConfigPathArgs,
) -> Result<(), CommandError> {
let config_path = get_new_config_file_path(&args.level.expect_source_kind(), command)?;
let config_path = get_new_config_file_path(args.level.expect_source_kind(), command)?;
writeln!(
ui.stdout(),
"{}",
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/config/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn cmd_config_set(
command: &CommandHelper,
args: &ConfigSetArgs,
) -> Result<(), CommandError> {
let config_path = get_new_config_file_path(&args.level.expect_source_kind(), command)?;
let config_path = get_new_config_file_path(args.level.expect_source_kind(), command)?;
if config_path.is_dir() {
return Err(user_error(format!(
"Can't set config in path {path} (dirs not supported)",
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/config/unset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn cmd_config_unset(
command: &CommandHelper,
args: &ConfigUnsetArgs,
) -> Result<(), CommandError> {
let config_path = get_new_config_file_path(&args.level.expect_source_kind(), command)?;
let config_path = get_new_config_file_path(args.level.expect_source_kind(), command)?;
if config_path.is_dir() {
return Err(user_error(format!(
"Can't set config in path {path} (dirs not supported)",
Expand Down
15 changes: 3 additions & 12 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use std::process::Command;
use itertools::Itertools;
use jj_lib::config::ConfigError;
use jj_lib::config::ConfigNamePathBuf;
use jj_lib::config::ConfigSource;
use jj_lib::settings::ConfigResultExt as _;
use regex::Captures;
use regex::Regex;
Expand Down Expand Up @@ -84,17 +85,6 @@ pub enum ConfigEnvError {
ConfigCreateError(#[from] std::io::Error),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ConfigSource {
Default,
EnvBase,
// TODO: Track explicit file paths, especially for when user config is a dir.
User,
Repo,
EnvOverrides,
CommandArg,
}

#[derive(Clone, Debug, PartialEq)]
pub struct AnnotatedValue {
pub path: ConfigNamePathBuf,
Expand All @@ -117,6 +107,7 @@ pub struct AnnotatedValue {
pub struct LayeredConfigs {
default: config::Config,
env_base: config::Config,
// TODO: Track explicit file paths, especially for when user config is a dir.
user: Option<config::Config>,
repo: Option<config::Config>,
env_overrides: config::Config,
Expand Down Expand Up @@ -221,7 +212,7 @@ impl LayeredConfigs {
config_vals.push(AnnotatedValue {
path,
value: value.to_owned(),
source: source.clone(),
source,
// Note: Value updated below.
is_overridden: false,
});
Expand Down
6 changes: 3 additions & 3 deletions cli/src/revset_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::sync::Arc;
use itertools::Itertools as _;
use jj_lib::backend::CommitId;
use jj_lib::commit::Commit;
use jj_lib::config::ConfigSource;
use jj_lib::id_prefix::IdPrefixContext;
use jj_lib::repo::Repo;
use jj_lib::revset;
Expand All @@ -43,7 +44,6 @@ use thiserror::Error;

use crate::command_error::user_error;
use crate::command_error::CommandError;
use crate::config::ConfigSource;
use crate::config::LayeredConfigs;
use crate::formatter::Formatter;
use crate::templater::TemplateRenderer;
Expand Down Expand Up @@ -136,7 +136,7 @@ impl<'repo> RevsetExpressionEvaluator<'repo> {

fn warn_user_redefined_builtin(
ui: &Ui,
source: &ConfigSource,
source: ConfigSource,
name: &str,
) -> Result<(), CommandError> {
match source {
Expand Down Expand Up @@ -177,7 +177,7 @@ pub fn load_revset_aliases(
continue;
};
for (decl, value) in table.into_iter().sorted_by(|a, b| a.0.cmp(&b.0)) {
warn_user_redefined_builtin(ui, &source, &decl)?;
warn_user_redefined_builtin(ui, source, &decl)?;

let r = value
.into_string()
Expand Down
17 changes: 17 additions & 0 deletions lib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,23 @@ impl fmt::Display for ConfigNamePathBuf {
}
}

/// Source of configuration variables in order of precedence.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum ConfigSource {
/// Default values (which has the lowest precedence.)
Default,
/// Base environment variables.
EnvBase,
/// User configuration files.
User,
/// Repo configuration files.
Repo,
/// Override environment variables.
EnvOverrides,
/// Command-line arguments (which has the highest precedence.)
CommandArg,
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit d75aaf3

Please sign in to comment.