Skip to content

Commit

Permalink
Warn user about the working copy when configuring the author
Browse files Browse the repository at this point in the history
  • Loading branch information
InCogNiTo124 committed Aug 6, 2024
1 parent 35b04f4 commit baa308c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

* `jj describe` can now update the description of multiple commits.

* When reconfiguring the author, warn that the working copy won't be updated

### Fixed bugs

* `jj status` will show different messages in a conflicted tree, depending
Expand Down
73 changes: 69 additions & 4 deletions cli/src/commands/config/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use jj_lib::commit::Commit;
use jj_lib::repo::Repo;
use tracing::instrument;

use super::ConfigLevelArgs;
use crate::cli_util::{get_new_config_file_path, CommandHelper};
use crate::cli_util::{get_new_config_file_path, CommandHelper, WorkspaceCommandHelper};
use crate::command_error::{user_error, CommandError};
use crate::config::{write_config_value_to_file, ConfigNamePathBuf};
use crate::ui::Ui;

/// Update config file to set the given option to a given value.
#[derive(clap::Args, Clone, Debug)]
pub struct ConfigSetArgs {
Expand All @@ -32,8 +33,8 @@ pub struct ConfigSetArgs {
}

#[instrument(skip_all)]
pub fn cmd_config_set(
_ui: &mut Ui,
pub(crate) fn cmd_config_set(
ui: &mut Ui,
command: &CommandHelper,
args: &ConfigSetArgs,
) -> Result<(), CommandError> {
Expand All @@ -44,5 +45,69 @@ pub 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<Commit> {
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 the working copy author, use \"jj describe \
--reset-author --no-edit\""
)?)
}

0 comments on commit baa308c

Please sign in to comment.