diff --git a/CHANGELOG.md b/CHANGELOG.md index e1444c566b..7b5d09d734 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -99,6 +99,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). This simplifies the use case of configuring code formatters for specific file types. See `jj help fix` for details. +* When reconfiguring the author, warn that the working copy won't be updated + ### Fixed bugs * `jj diff --git` no longer shows the contents of binary files. diff --git a/cli/src/commands/config.rs b/cli/src/commands/config.rs index 4c33a0021d..6888d68308 100644 --- a/cli/src/commands/config.rs +++ b/cli/src/commands/config.rs @@ -14,9 +14,13 @@ use std::io::Write; +use jj_lib::commit::Commit; +use jj_lib::repo::Repo; use tracing::instrument; -use crate::cli_util::{get_new_config_file_path, run_ui_editor, CommandHelper}; +use crate::cli_util::{ + get_new_config_file_path, run_ui_editor, CommandHelper, WorkspaceCommandHelper, +}; use crate::command_error::{config_error, user_error, CommandError}; use crate::config::{ to_toml_value, write_config_value_to_file, AnnotatedValue, ConfigNamePathBuf, ConfigSource, @@ -285,7 +289,7 @@ pub(crate) fn cmd_config_get( #[instrument(skip_all)] pub(crate) fn cmd_config_set( - _ui: &mut Ui, + ui: &mut Ui, command: &CommandHelper, args: &ConfigSetArgs, ) -> Result<(), CommandError> { @@ -296,9 +300,73 @@ pub(crate) fn cmd_config_set( path = config_path.display() ))); } + + // If the user is trying to change the author config, we should warn them that + // it won't affect the working copy author + if args.name == ConfigNamePathBuf::from_iter(vec!["user", "name"]) { + check_wc_user_name(command, ui, &args.value)?; + } else if args.name == ConfigNamePathBuf::from_iter(vec!["user", "email"]) { + check_wc_user_email(command, ui, &args.value)?; + }; + write_config_value_to_file(&args.name, &args.value, &config_path) } +/// Returns the commit of the working copy if it exists. +fn maybe_wc_commit(helper: &WorkspaceCommandHelper) -> Option { + let repo = helper.repo(); + let maybe_wc_commit = helper + .get_wc_commit_id() + .map(|id| repo.store().get_commit(id)) + .transpose() + .unwrap(); + maybe_wc_commit +} + +/// Check if the working copy author name matches the user's config value +/// If it doesn't, print a warning message +fn check_wc_user_name( + command: &CommandHelper, + ui: &mut Ui, + user_name: &str, +) -> Result<(), CommandError> { + let helper = command.workspace_helper(ui)?; + if let Some(wc_commit) = maybe_wc_commit(&helper) { + let author = wc_commit.author(); + if author.name != user_name { + warn_wc_author(&author.name, &author.email, ui)? + } + }; + Ok(()) +} + +/// Check if the working copy author email matches the user's config value +/// If it doesn't, print a warning message +fn check_wc_user_email( + command: &CommandHelper, + ui: &mut Ui, + user_email: &str, +) -> Result<(), CommandError> { + let helper = command.workspace_helper(ui)?; + if let Some(wc_commit) = maybe_wc_commit(&helper) { + let author = wc_commit.author(); + if author.email != user_email { + warn_wc_author(&author.name, &author.email, ui)? + } + }; + Ok(()) +} + +/// Prints a warning message about the working copy to the user +fn warn_wc_author(user_name: &str, user_email: &str, ui: &mut Ui) -> Result<(), CommandError> { + Ok(writeln!( + ui.warning_default(), + "This setting will only impact future commits.\nThe author of the working copy will stay \ + \"{user_name} <{user_email}>\". \nTo change it, use \"jj describe --reset-author \ + --no-edit\"" + )?) +} + #[instrument(skip_all)] pub(crate) fn cmd_config_edit( _ui: &mut Ui,