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 14, 2024
1 parent 49577f7 commit 5066d08
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 12 deletions.
22 changes: 12 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
* A tilde (`~`) at the start of the path will now be expanded to the user's home
directory when configuring a `signing.key` for SSH commit signing.

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

### Fixed bugs

## [0.20.0] - 2024-08-07
Expand Down Expand Up @@ -92,7 +94,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

* `jj backout` can now back out multiple commits at once.

* `jj git clone some/nested/path` now creates the full directory tree for
* `jj git clone some/nested/path` now creates the full directory tree for
nested destination paths if they don't exist.

* String patterns now support case‐insensitive matching by suffixing any
Expand Down Expand Up @@ -203,9 +205,9 @@ Thanks to the people who made this release happen!

* A new `jj file` subcommand now replaces several existing uncategorized
commands, which are deprecated.
- `jj file show` replaces `jj cat`.
- `jj file chmod` replaces `jj chmod`.
- `jj file list` replaces `jj files`.
* `jj file show` replaces `jj cat`.
* `jj file chmod` replaces `jj chmod`.
* `jj file list` replaces `jj files`.

### New features

Expand All @@ -226,9 +228,9 @@ Thanks to the people who made this release happen!
* Conflicted files are individually simplified before being materialized.

* The `jj file` subcommand now contains several existing file utilities.
- `jj file show`, replacing `jj cat`.
- `jj file chmod` replacing `jj chmod`.
- `jj file list` replacing `jj files`.
* `jj file show`, replacing `jj cat`.
* `jj file chmod` replacing `jj chmod`.
* `jj file list` replacing `jj files`.

* New command `jj branch move` let you update branches by name pattern or source
revision.
Expand Down Expand Up @@ -939,7 +941,7 @@ Thanks to the people who made this release happen!

### New features

* `jj workspace add` can now take _multiple_ `--revision` arguments, which will
* `jj workspace add` can now take *multiple* `--revision` arguments, which will
create a new workspace with its working-copy commit on top of all the parents,
as if you had run `jj new r1 r2 r3 ...`.

Expand Down Expand Up @@ -1717,7 +1719,7 @@ Thanks to the people who made this release happen!
* `jj duplicate` followed by `jj rebase` of a tree containing both the original
and duplicate commit no longer crashes. The fix should also resolve any
remaining
instances of https://github.com/martinvonz/jj/issues/27.
instances of <https://github.com/martinvonz/jj/issues/27>.

* Fix the output of `jj debug completion --help` by reversing fish and zsh text.

Expand Down Expand Up @@ -2145,7 +2147,7 @@ No changes, only trying to get the automated build to work.

### Fixed bugs

- Fixed crash when `core.excludesFile` pointed to nonexistent file, and made
* Fixed crash when `core.excludesFile` pointed to nonexistent file, and made
leading `~/` in that config expand to `$HOME/`
[#131](https://github.com/martinvonz/jj/issues/131)

Expand Down
70 changes: 68 additions & 2 deletions cli/src/commands/config/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::io;

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::{
parse_toml_value_or_bare_string, write_config_value_to_file, ConfigNamePathBuf,
Expand All @@ -33,9 +37,15 @@ pub struct ConfigSetArgs {
level: ConfigLevelArgs,
}

/// Denotes a type of author change
enum AuthorChange {
Name,
Email,
}

#[instrument(skip_all)]
pub fn cmd_config_set(
_ui: &mut Ui,
ui: &mut Ui,
command: &CommandHelper,
args: &ConfigSetArgs,
) -> Result<(), CommandError> {
Expand All @@ -46,7 +56,63 @@ pub fn cmd_config_set(
path = config_path.display()
)));
}

// TODO(#531): Infer types based on schema (w/ --type arg to override).
let value = parse_toml_value_or_bare_string(&args.value);

// 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_author(ui, &command, &value, AuthorChange::Name)?;
} else if args.name == ConfigNamePathBuf::from_iter(vec!["user", "email"]) {
check_wc_author(ui, &command, &value, AuthorChange::Email)?;
};

write_config_value_to_file(&args.name, 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_author(
ui: &mut Ui,
command: &CommandHelper,
new_value: &toml_edit::Value,
author_change: AuthorChange,
) -> io::Result<()> {
let helper = match command.workspace_helper(ui) {
Ok(helper) => helper,
Err(_) => return Ok(()), // config set should work even if cwd isn't a jj repo
};
if let Some(wc_commit) = maybe_wc_commit(&helper) {
let author = wc_commit.author();
let orig_value = match author_change {
AuthorChange::Name => &author.name,
AuthorChange::Email => &author.email,
};
if new_value.as_str() != Some(orig_value) {
warn_wc_author(ui, &author.name, &author.email)?
}
}
Ok(())
}

/// Prints a warning message about the working copy to the user
fn warn_wc_author(ui: &Ui, user_name: &str, user_email: &str) -> io::Result<()> {
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\""
)?)
}
37 changes: 37 additions & 0 deletions cli/tests/test_config_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,43 @@ fn test_config_show_paths() {
"###);
}

#[test]
fn test_config_author_change_warning() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);
let repo_path = test_env.env_root().join("repo");
let (stdout, stderr) = test_env.jj_cmd_ok(
&repo_path,
&["config", "set", "--repo", "user.email", "'Foo'"],
);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @r###"
Warning: This setting will only impact future commits.
The author of the working copy will stay "Test User <[email protected]>".
To change the working copy author, use "jj describe --reset-author --no-edit"
"###);

// test_env.jj_cmd resets state for every invocation
// for this test, the state (user.email) is needed
let mut log_cmd = test_env.jj_cmd(&repo_path, &["describe", "--reset-author", "--no-edit"]);
log_cmd.env_remove("JJ_EMAIL");
log_cmd.assert().success();

let (stdout, _) = test_env.jj_cmd_ok(&repo_path, &["log"]);
assert!(stdout.contains("Foo"));
}

#[test]
fn test_config_author_change_warning_root_env() {
let mut test_env = TestEnvironment::default();
let user_config_path = test_env.config_path().join("config.toml");
test_env.set_config_path(user_config_path);
test_env.jj_cmd_success(
test_env.env_root(),
&["config", "set", "--user", "user.email", "'Foo'"],
);
}

fn find_stdout_lines(keyname_pattern: &str, stdout: &str) -> String {
let key_line_re = Regex::new(&format!(r"(?m)^{keyname_pattern} = .*$")).unwrap();
key_line_re
Expand Down

0 comments on commit 5066d08

Please sign in to comment.