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 Jul 20, 2024
1 parent 8df7857 commit 9f9cd2c
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions cli/src/commands/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ use std::io::Write;

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, short_change_hash, CommandHelper};
use crate::command_error::{config_error, user_error, CommandError};
use crate::config::{
to_toml_value, write_config_value_to_file, AnnotatedValue, ConfigNamePathBuf, ConfigSource,
};
use crate::generic_templater::GenericTemplateLanguage;

use crate::template_builder::TemplateLanguage as _;
use crate::templater::TemplatePropertyExt as _;
use crate::ui::Ui;
use jj_lib::repo::Repo;

#[derive(clap::Args, Clone, Debug)]
#[command(group = clap::ArgGroup::new("config_level").multiple(false).required(true))]
Expand Down Expand Up @@ -285,7 +287,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> {
Expand All @@ -296,9 +298,53 @@ 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"]) {
let config = command.settings().config();
let user_name: String = config.get("user.name")?;

// only warn if the author is actually changing
if user_name != args.value {
let user_email: String = config.get("user.email")?;
warn_wc_author(&user_name, &user_email, command, ui)?
}
} else if args.name == ConfigNamePathBuf::from_iter(vec!["user", "email"]) {
let config = command.settings().config();
let user_email: String = config.get("user.email")?;

// only warn if the author is actually changing
if user_email != args.value {
let user_name: String = config.get("user.name")?;
warn_wc_author(&user_name, &user_email, command, ui)?
}
}
write_config_value_to_file(&args.name, &args.value, &config_path)
}

fn warn_wc_author(
user_name: &str,
user_email: &str,
command: &CommandHelper,
ui: &mut Ui,
) -> Result<(), CommandError> {
let helper = command.workspace_helper(ui)?;
let repo = helper.repo();
let maybe_wc_commit = helper
.get_wc_commit_id()
.map(|id| repo.store().get_commit(id))
.transpose()?;

let change_hash = match maybe_wc_commit {
Some(commit) => format!("{} ", &short_change_hash(&commit.change_id())[..8]), // extra space for formatting
None => String::from(""),
};
Ok(writeln!(
ui.warning_default(),
"This setting will only impact future commits.\nThe author of the working copy {change_hash}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,
Expand Down

0 comments on commit 9f9cd2c

Please sign in to comment.