cli: get settings from repo or workspace

This ensures that WorkspaceCommandHelper created for initialized/cloned repo
refers to the settings object for that repo, not the settings loaded for the
cwd repo.

I also removed WorkspaceCommandEnvironment::settings() because it doesn't own
a workspace object. It's implementation detail that the env object holds a
copy of workspace.settings(), and the caller should be accessible to the
workspace object.
This commit is contained in:
Yuya Nishihara 2024-12-29 17:18:17 +09:00
parent 393f3acb03
commit 56bd4765f5
2 changed files with 20 additions and 14 deletions

View file

@ -337,6 +337,11 @@ impl CommandHelper {
&self.data.raw_config &self.data.raw_config
} }
/// Settings for the current command and workspace.
///
/// This may be different from the settings for new workspace created by
/// e.g. `jj git init`. There may be conditional variables and repo config
/// `.jj/repo/config.toml` loaded for the cwd workspace.
pub fn settings(&self) -> &UserSettings { pub fn settings(&self) -> &UserSettings {
&self.data.settings &self.data.settings
} }
@ -724,6 +729,7 @@ impl AdvanceBookmarksSettings {
/// Metadata and configuration loaded for a specific workspace. /// Metadata and configuration loaded for a specific workspace.
pub struct WorkspaceCommandEnvironment { pub struct WorkspaceCommandEnvironment {
command: CommandHelper, command: CommandHelper,
settings: UserSettings,
revset_aliases_map: RevsetAliasesMap, revset_aliases_map: RevsetAliasesMap,
template_aliases_map: TemplateAliasesMap, template_aliases_map: TemplateAliasesMap,
path_converter: RepoPathUiConverter, path_converter: RepoPathUiConverter,
@ -736,31 +742,29 @@ pub struct WorkspaceCommandEnvironment {
impl WorkspaceCommandEnvironment { impl WorkspaceCommandEnvironment {
#[instrument(skip_all)] #[instrument(skip_all)]
fn new(ui: &Ui, command: &CommandHelper, workspace: &Workspace) -> Result<Self, CommandError> { fn new(ui: &Ui, command: &CommandHelper, workspace: &Workspace) -> Result<Self, CommandError> {
let revset_aliases_map = revset_util::load_revset_aliases(ui, command.settings().config())?; let settings = workspace.settings();
let template_aliases_map = load_template_aliases(ui, command.settings().config())?; let revset_aliases_map = revset_util::load_revset_aliases(ui, settings.config())?;
let template_aliases_map = load_template_aliases(ui, settings.config())?;
let path_converter = RepoPathUiConverter::Fs { let path_converter = RepoPathUiConverter::Fs {
cwd: command.cwd().to_owned(), cwd: command.cwd().to_owned(),
base: workspace.workspace_root().to_owned(), base: workspace.workspace_root().to_owned(),
}; };
let mut env = Self { let mut env = Self {
command: command.clone(), command: command.clone(),
settings: settings.clone(),
revset_aliases_map, revset_aliases_map,
template_aliases_map, template_aliases_map,
path_converter, path_converter,
workspace_id: workspace.workspace_id().to_owned(), workspace_id: workspace.workspace_id().to_owned(),
immutable_heads_expression: RevsetExpression::root(), immutable_heads_expression: RevsetExpression::root(),
short_prefixes_expression: None, short_prefixes_expression: None,
conflict_marker_style: command.settings().get("ui.conflict-marker-style")?, conflict_marker_style: settings.get("ui.conflict-marker-style")?,
}; };
env.immutable_heads_expression = env.load_immutable_heads_expression(ui)?; env.immutable_heads_expression = env.load_immutable_heads_expression(ui)?;
env.short_prefixes_expression = env.load_short_prefixes_expression(ui)?; env.short_prefixes_expression = env.load_short_prefixes_expression(ui)?;
Ok(env) Ok(env)
} }
pub fn settings(&self) -> &UserSettings {
self.command.settings()
}
pub(crate) fn path_converter(&self) -> &RepoPathUiConverter { pub(crate) fn path_converter(&self) -> &RepoPathUiConverter {
&self.path_converter &self.path_converter
} }
@ -774,7 +778,7 @@ impl WorkspaceCommandEnvironment {
path_converter: &self.path_converter, path_converter: &self.path_converter,
workspace_id: &self.workspace_id, workspace_id: &self.workspace_id,
}; };
let now = if let Some(timestamp) = self.settings().commit_timestamp() { let now = if let Some(timestamp) = self.settings.commit_timestamp() {
chrono::Local chrono::Local
.timestamp_millis_opt(timestamp.timestamp.0) .timestamp_millis_opt(timestamp.timestamp.0)
.unwrap() .unwrap()
@ -783,7 +787,7 @@ impl WorkspaceCommandEnvironment {
}; };
RevsetParseContext::new( RevsetParseContext::new(
&self.revset_aliases_map, &self.revset_aliases_map,
self.settings().user_email(), self.settings.user_email(),
now.into(), now.into(),
self.command.revset_extensions(), self.command.revset_extensions(),
Some(workspace_context), Some(workspace_context),
@ -836,10 +840,10 @@ impl WorkspaceCommandEnvironment {
ui: &Ui, ui: &Ui,
) -> Result<Option<Rc<UserRevsetExpression>>, CommandError> { ) -> Result<Option<Rc<UserRevsetExpression>>, CommandError> {
let revset_string = self let revset_string = self
.settings() .settings
.get_string("revsets.short-prefixes") .get_string("revsets.short-prefixes")
.optional()? .optional()?
.map_or_else(|| self.settings().get_string("revsets.log"), Ok)?; .map_or_else(|| self.settings.get_string("revsets.log"), Ok)?;
if revset_string.is_empty() { if revset_string.is_empty() {
Ok(None) Ok(None)
} else { } else {
@ -976,7 +980,7 @@ impl WorkspaceCommandHelper {
env: WorkspaceCommandEnvironment, env: WorkspaceCommandEnvironment,
loaded_at_head: bool, loaded_at_head: bool,
) -> Result<Self, CommandError> { ) -> Result<Self, CommandError> {
let settings = env.settings(); let settings = workspace.settings();
let commit_summary_template_text = settings.get_string("templates.commit_summary")?; let commit_summary_template_text = settings.get_string("templates.commit_summary")?;
let op_summary_template_text = settings.get_string("templates.op_summary")?; let op_summary_template_text = settings.get_string("templates.op_summary")?;
let may_update_working_copy = let may_update_working_copy =
@ -999,8 +1003,9 @@ impl WorkspaceCommandHelper {
Ok(helper) Ok(helper)
} }
/// Settings for this workspace.
pub fn settings(&self) -> &UserSettings { pub fn settings(&self) -> &UserSettings {
self.env.settings() self.workspace.settings()
} }
pub fn git_backend(&self) -> Option<&GitBackend> { pub fn git_backend(&self) -> Option<&GitBackend> {
@ -2287,6 +2292,7 @@ impl WorkspaceCommandTransaction<'_> {
self.helper self.helper
} }
/// Settings for this workspace.
pub fn settings(&self) -> &UserSettings { pub fn settings(&self) -> &UserSettings {
self.helper.settings() self.helper.settings()
} }

View file

@ -100,7 +100,7 @@ fn do_op_log(
current_op: &Operation, current_op: &Operation,
args: &OperationLogArgs, args: &OperationLogArgs,
) -> Result<(), CommandError> { ) -> Result<(), CommandError> {
let settings = workspace_env.settings(); let settings = repo_loader.settings();
let graph_style = GraphStyle::from_settings(settings)?; let graph_style = GraphStyle::from_settings(settings)?;
let with_content_format = LogContentFormat::new(ui, settings)?; let with_content_format = LogContentFormat::new(ui, settings)?;