Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commit: add --reset-author option #3998

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 17 additions & 3 deletions cli/src/commands/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ pub(crate) struct CommitArgs {
/// Put these paths in the first commit
#[arg(value_hint = clap::ValueHint::AnyPath)]
paths: Vec<String>,
/// 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)]
Expand Down Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions cli/tests/cli-reference@.md.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 <NAME>` — Specify diff editor to be used (implies --interactive)
* `-m`, `--message <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



Expand Down
46 changes: 46 additions & 0 deletions cli/tests/test_commit_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down