Skip to content

Commit

Permalink
commit: add --reset-author option
Browse files Browse the repository at this point in the history
  • Loading branch information
scott2000 committed Jun 30, 2024
1 parent 3b2197c commit 8d203e3
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,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' [email protected] 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/[email protected]
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' [email protected] 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 [email protected] 2001-02-03 04:05:07.000 +07:00
│ Test User [email protected] 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 = "[email protected]""#,
"--reset-author",
"-m1",
],
);
insta::assert_snapshot!(get_signatures(), @r###"
@ Ove Ridder [email protected] 2001-02-03 04:05:09.000 +07:00
│ Ove Ridder [email protected] 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

0 comments on commit 8d203e3

Please sign in to comment.