Skip to content

Commit

Permalink
cli: get settings from repo or workspace
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yuja committed Jan 5, 2025
1 parent b6be33f commit ae5b920
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
31 changes: 18 additions & 13 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,10 @@ impl CommandHelper {
&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`.
pub fn settings(&self) -> &UserSettings {
&self.data.settings
}
Expand Down Expand Up @@ -714,6 +718,7 @@ impl AdvanceBookmarksSettings {
/// Metadata and configuration loaded for a specific workspace.
pub struct WorkspaceCommandEnvironment {
command: CommandHelper,
settings: UserSettings,
revset_aliases_map: RevsetAliasesMap,
template_aliases_map: TemplateAliasesMap,
path_converter: RepoPathUiConverter,
Expand All @@ -726,31 +731,29 @@ pub struct WorkspaceCommandEnvironment {
impl WorkspaceCommandEnvironment {
#[instrument(skip_all)]
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 template_aliases_map = load_template_aliases(ui, command.settings().config())?;
let settings = workspace.settings();
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 {
cwd: command.cwd().to_owned(),
base: workspace.workspace_root().to_owned(),
};
let mut env = Self {
command: command.clone(),
settings: settings.clone(),
revset_aliases_map,
template_aliases_map,
path_converter,
workspace_id: workspace.workspace_id().to_owned(),
immutable_heads_expression: RevsetExpression::root(),
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.short_prefixes_expression = env.load_short_prefixes_expression(ui)?;
Ok(env)
}

pub fn settings(&self) -> &UserSettings {
self.command.settings()
}

pub(crate) fn path_converter(&self) -> &RepoPathUiConverter {
&self.path_converter
}
Expand All @@ -764,7 +767,7 @@ impl WorkspaceCommandEnvironment {
path_converter: &self.path_converter,
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
.timestamp_millis_opt(timestamp.timestamp.0)
.unwrap()
Expand All @@ -773,7 +776,7 @@ impl WorkspaceCommandEnvironment {
};
RevsetParseContext::new(
&self.revset_aliases_map,
self.settings().user_email(),
self.settings.user_email(),
now.into(),
self.command.revset_extensions(),
Some(workspace_context),
Expand Down Expand Up @@ -826,10 +829,10 @@ impl WorkspaceCommandEnvironment {
ui: &Ui,
) -> Result<Option<Rc<UserRevsetExpression>>, CommandError> {
let revset_string = self
.settings()
.settings
.get_string("revsets.short-prefixes")
.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() {
Ok(None)
} else {
Expand Down Expand Up @@ -966,7 +969,7 @@ impl WorkspaceCommandHelper {
env: WorkspaceCommandEnvironment,
loaded_at_head: bool,
) -> Result<Self, CommandError> {
let settings = env.settings();
let settings = workspace.settings();
let commit_summary_template_text = settings.get_string("templates.commit_summary")?;
let op_summary_template_text = settings.get_string("templates.op_summary")?;
let may_update_working_copy =
Expand All @@ -989,8 +992,9 @@ impl WorkspaceCommandHelper {
Ok(helper)
}

/// Settings for this workspace.
pub fn settings(&self) -> &UserSettings {
self.env.settings()
self.workspace.settings()
}

pub fn git_backend(&self) -> Option<&GitBackend> {
Expand Down Expand Up @@ -2269,6 +2273,7 @@ impl WorkspaceCommandTransaction<'_> {
self.helper
}

/// Settings for this workspace.
pub fn settings(&self) -> &UserSettings {
self.helper.settings()
}
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/operation/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fn do_op_log(
current_op: &Operation,
args: &OperationLogArgs,
) -> Result<(), CommandError> {
let settings = workspace_env.settings();
let settings = repo_loader.settings();
let graph_style = GraphStyle::from_settings(settings)?;
let with_content_format = LogContentFormat::new(ui, settings)?;

Expand Down

0 comments on commit ae5b920

Please sign in to comment.