diff --git a/CHANGELOG.md b/CHANGELOG.md index b28c2fffe1..f12d377feb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -79,6 +79,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 commits with no description) if authored by the current user. [#2000](https://github.com/martinvonz/jj/issues/2000) +* `jj commit` now accepts `--reset-author` option to match `jj describe`. + ### Fixed bugs * `jj git push` now ignores immutable commits when checking whether a diff --git a/cli/src/commands/commit.rs b/cli/src/commands/commit.rs index 4ad91c6d7c..e173032153 100644 --- a/cli/src/commands/commit.rs +++ b/cli/src/commands/commit.rs @@ -39,6 +39,16 @@ pub(crate) struct CommitArgs { /// Put these paths in the first commit #[arg(value_hint = clap::ValueHint::AnyPath)] paths: Vec, + /// Reset the author to the configured user + /// + /// This resets the author name, email, and timestamp. + /// + /// You can use it in combination with the JJ_USER and JJ_EMAIL + /// environment variables to set a different author: + /// + /// $ JJ_USER='Foo Bar' JJ_EMAIL=foo@bar.com jj commit --reset-author + #[arg(long)] + reset_author: bool, } #[instrument(skip_all)] @@ -102,12 +112,16 @@ new working-copy commit. edit_description(tx.base_repo(), &template, command.settings())? }; - let new_commit = tx + let mut commit_builder = tx .mut_repo() .rewrite_commit(command.settings(), &commit) .set_tree_id(tree_id) - .set_description(description) - .write()?; + .set_description(description); + if args.reset_author { + let new_author = commit_builder.committer().clone(); + commit_builder = commit_builder.set_author(new_author); + } + let new_commit = commit_builder.write()?; let workspace_ids = tx .mut_repo() .view() diff --git a/cli/tests/cli-reference@.md.snap b/cli/tests/cli-reference@.md.snap index 516da42ffa..a473a5c5f6 100644 --- a/cli/tests/cli-reference@.md.snap +++ b/cli/tests/cli-reference@.md.snap @@ -437,6 +437,13 @@ Update the description and create a new change on top * `-i`, `--interactive` — Interactively choose which changes to include in the first commit * `--tool ` — Specify diff editor to be used (implies --interactive) * `-m`, `--message ` — The change description to use (don't open editor) +* `--reset-author` — Reset the author to the configured user + + This resets the author name, email, and timestamp. + + You can use it in combination with the JJ_USER and JJ_EMAIL environment variables to set a different author: + + $ JJ_USER='Foo Bar' JJ_EMAIL=foo@bar.com jj commit --reset-author diff --git a/cli/tests/test_commit_command.rs b/cli/tests/test_commit_command.rs index 5d706de574..d1d00aa028 100644 --- a/cli/tests/test_commit_command.rs +++ b/cli/tests/test_commit_command.rs @@ -217,6 +217,52 @@ fn test_commit_paths_warning() { "###); } +#[test] +fn test_commit_reset_author() { + 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"); + + test_env.add_config( + r#"[template-aliases] +'format_signature(signature)' = 'signature.name() ++ " " ++ signature.email() ++ " " ++ signature.timestamp()'"#, + ); + let get_signatures = || { + test_env.jj_cmd_success( + &repo_path, + &[ + "log", + "-r@", + "-T", + r#"format_signature(author) ++ "\n" ++ format_signature(committer)"#, + ], + ) + }; + insta::assert_snapshot!(get_signatures(), @r###" + @ Test User test.user@example.com 2001-02-03 04:05:07.000 +07:00 + │ Test User test.user@example.com 2001-02-03 04:05:07.000 +07:00 + ~ + "###); + + // Reset the author (the committer is always reset) + test_env.jj_cmd_ok( + &repo_path, + &[ + "commit", + "--config-toml", + r#"user.name = "Ove Ridder" + user.email = "ove.ridder@example.com""#, + "--reset-author", + "-m1", + ], + ); + insta::assert_snapshot!(get_signatures(), @r###" + @ Ove Ridder ove.ridder@example.com 2001-02-03 04:05:09.000 +07:00 + │ Ove Ridder ove.ridder@example.com 2001-02-03 04:05:09.000 +07:00 + ~ + "###); +} + fn get_log_output(test_env: &TestEnvironment, cwd: &Path) -> String { let template = r#"commit_id.short() ++ " " ++ description"#; test_env.jj_cmd_success(cwd, &["log", "-T", template])