settings: pass around &UserSettings instead of &config::Config consistently

Ui::with_config() is unchanged because I'm not sure if UserSettings should be
constructed earlier. I assume UserSettings will hold an immutable copy of
LayerdConfigs object, whereas Ui has to be initialized before all config layers
get loaded.
This commit is contained in:
Yuya Nishihara 2024-11-21 22:59:16 +09:00
parent 6d26d53eab
commit f239b1e8f0
4 changed files with 20 additions and 16 deletions

View file

@ -658,10 +658,10 @@ struct AdvanceBookmarksSettings {
} }
impl AdvanceBookmarksSettings { impl AdvanceBookmarksSettings {
fn from_config(config: &config::Config) -> Result<Self, CommandError> { fn from_settings(settings: &UserSettings) -> Result<Self, CommandError> {
let get_setting = |setting_key| { let get_setting = |setting_key| {
let setting = format!("experimental-advance-branches.{setting_key}"); let setting = format!("experimental-advance-branches.{setting_key}");
match config.get::<Vec<String>>(&setting).optional()? { match settings.config().get::<Vec<String>>(&setting).optional()? {
Some(patterns) => patterns Some(patterns) => patterns
.into_iter() .into_iter()
.map(|s| { .map(|s| {
@ -2180,7 +2180,7 @@ Then run `jj squash` to move the resolution into the conflicted commit."#,
&self, &self,
from: impl IntoIterator<Item = &'a CommitId>, from: impl IntoIterator<Item = &'a CommitId>,
) -> Result<Vec<AdvanceableBookmark>, CommandError> { ) -> Result<Vec<AdvanceableBookmark>, CommandError> {
let ab_settings = AdvanceBookmarksSettings::from_config(self.settings().config())?; let ab_settings = AdvanceBookmarksSettings::from_settings(self.settings())?;
if !ab_settings.feature_enabled() { if !ab_settings.feature_enabled() {
// Return early if we know that there's no work to do. // Return early if we know that there's no work to do.
return Ok(Vec::new()); return Ok(Vec::new());

View file

@ -38,6 +38,7 @@ use jj_lib::repo_path::RepoPathBuf;
use jj_lib::repo_path::RepoPathUiConverter; use jj_lib::repo_path::RepoPathUiConverter;
use jj_lib::revset::RevsetExpression; use jj_lib::revset::RevsetExpression;
use jj_lib::revset::RevsetIteratorExt; use jj_lib::revset::RevsetIteratorExt;
use jj_lib::settings::UserSettings;
use jj_lib::store::Store; use jj_lib::store::Store;
use jj_lib::tree::Tree; use jj_lib::tree::Tree;
use pollster::FutureExt; use pollster::FutureExt;
@ -144,7 +145,7 @@ pub(crate) fn cmd_fix(
args: &FixArgs, args: &FixArgs,
) -> Result<(), CommandError> { ) -> Result<(), CommandError> {
let mut workspace_command = command.workspace_helper(ui)?; let mut workspace_command = command.workspace_helper(ui)?;
let tools_config = get_tools_config(ui, command.settings().config())?; let tools_config = get_tools_config(ui, command.settings())?;
let root_commits: Vec<CommitId> = if args.source.is_empty() { let root_commits: Vec<CommitId> = if args.source.is_empty() {
let revs = command.settings().config().get_string("revsets.fix")?; let revs = command.settings().config().get_string("revsets.fix")?;
workspace_command.parse_revset(ui, &RevisionArg::from(revs))? workspace_command.parse_revset(ui, &RevisionArg::from(revs))?
@ -445,7 +446,8 @@ struct RawToolConfig {
/// Fails if any of the commands or patterns are obviously unusable, but does /// Fails if any of the commands or patterns are obviously unusable, but does
/// not check for issues that might still occur later like missing executables. /// not check for issues that might still occur later like missing executables.
/// This is a place where we could fail earlier in some cases, though. /// This is a place where we could fail earlier in some cases, though.
fn get_tools_config(ui: &mut Ui, config: &config::Config) -> Result<ToolsConfig, CommandError> { fn get_tools_config(ui: &mut Ui, settings: &UserSettings) -> Result<ToolsConfig, CommandError> {
let config = settings.config();
let mut tools_config = ToolsConfig { tools: Vec::new() }; let mut tools_config = ToolsConfig { tools: Vec::new() };
// TODO: Remove this block of code and associated documentation after at least // TODO: Remove this block of code and associated documentation after at least
// one release where the feature is marked deprecated. // one release where the feature is marked deprecated.

View file

@ -24,10 +24,9 @@
use std::path::PathBuf; use std::path::PathBuf;
use config::Config;
use crate::config::ConfigError; use crate::config::ConfigError;
use crate::settings::ConfigResultExt; use crate::settings::ConfigResultExt;
use crate::settings::UserSettings;
/// Config for Watchman filesystem monitor (<https://facebook.github.io/watchman/>). /// Config for Watchman filesystem monitor (<https://facebook.github.io/watchman/>).
#[derive(Default, Eq, PartialEq, Clone, Debug)] #[derive(Default, Eq, PartialEq, Clone, Debug)]
@ -59,11 +58,12 @@ pub enum FsmonitorSettings {
impl FsmonitorSettings { impl FsmonitorSettings {
/// Creates an `FsmonitorSettings` from a `config`. /// Creates an `FsmonitorSettings` from a `config`.
pub fn from_config(config: &Config) -> Result<FsmonitorSettings, ConfigError> { pub fn from_settings(settings: &UserSettings) -> Result<FsmonitorSettings, ConfigError> {
match config.get_string("core.fsmonitor") { match settings.config().get_string("core.fsmonitor") {
Ok(s) => match s.as_str() { Ok(s) => match s.as_str() {
"watchman" => Ok(Self::Watchman(WatchmanConfig { "watchman" => Ok(Self::Watchman(WatchmanConfig {
register_trigger: config register_trigger: settings
.config()
.get_bool("core.watchman.register_snapshot_trigger") .get_bool("core.watchman.register_snapshot_trigger")
.optional()? .optional()?
.unwrap_or_default(), .unwrap_or_default(),

View file

@ -50,12 +50,14 @@ pub struct GitSettings {
} }
impl GitSettings { impl GitSettings {
pub fn from_config(config: &config::Config) -> Self { pub fn from_settings(settings: &UserSettings) -> Self {
let auto_local_bookmark = config let auto_local_bookmark = settings
.config()
.get_bool("git.auto-local-bookmark") .get_bool("git.auto-local-bookmark")
.or_else(|_| config.get_bool("git.auto-local-branch")) .or_else(|_| settings.config().get_bool("git.auto-local-branch"))
.unwrap_or(false); .unwrap_or(false);
let abandon_unreachable_commits = config let abandon_unreachable_commits = settings
.config()
.get_bool("git.abandon-unreachable-commits") .get_bool("git.abandon-unreachable-commits")
.unwrap_or(true); .unwrap_or(true);
GitSettings { GitSettings {
@ -169,7 +171,7 @@ impl UserSettings {
} }
pub fn fsmonitor_settings(&self) -> Result<FsmonitorSettings, ConfigError> { pub fn fsmonitor_settings(&self) -> Result<FsmonitorSettings, ConfigError> {
FsmonitorSettings::from_config(&self.config) FsmonitorSettings::from_settings(self)
} }
// Must not be changed to avoid git pushing older commits with no set email // Must not be changed to avoid git pushing older commits with no set email
@ -236,7 +238,7 @@ impl UserSettings {
} }
pub fn git_settings(&self) -> GitSettings { pub fn git_settings(&self) -> GitSettings {
GitSettings::from_config(&self.config) GitSettings::from_settings(self)
} }
pub fn max_new_file_size(&self) -> Result<u64, ConfigError> { pub fn max_new_file_size(&self) -> Result<u64, ConfigError> {