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 f0e1fc5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

* New `diff_contains()` revset function can be used to search diffs.

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

### Fixed bugs

* `jj diff --git` no longer shows the contents of binary files.
Expand Down
52 changes: 50 additions & 2 deletions cli/src/commands/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

use std::io::Write;

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, 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,
Expand Down Expand Up @@ -285,7 +286,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 +297,56 @@ 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 f0e1fc5

Please sign in to comment.