Skip to content

Commit

Permalink
Add a --edit flag
Browse files Browse the repository at this point in the history
The --edit flag forces the editor to be shown, even if it would have
been hidden due to the --no-edit flag or another flag that implies it
(such as --message or --stdin).
  • Loading branch information
MrJohz committed Jan 21, 2025
1 parent be32d4e commit c7061b2
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### New features

* `jj describe` now accepts `--edit`.

* `jj evolog` and `jj op log` now accept `--reversed`.

* `jj restore` now supports `-i`/`--interactive` selection.
Expand Down
24 changes: 21 additions & 3 deletions cli/src/commands/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ pub(crate) struct DescribeArgs {
/// This is mainly useful in combination with e.g. `--reset-author`.
#[arg(long)]
no_edit: bool,
/// Open an editor
///
/// This is mainly useful to reverse the effects of --no-edit or another
/// flag/option that implies --no-edit e.g. --message
#[arg(long)]
edit: bool,
/// Reset the author to the configured user
///
/// This resets the author name, email, and timestamp.
Expand Down Expand Up @@ -133,7 +139,13 @@ pub(crate) fn cmd_describe(
None
};

let commit_descriptions: Vec<(_, _)> = if args.no_edit || shared_description.is_some() {
let skip_editor = if args.edit {
false
} else {
args.no_edit || shared_description.is_some()
};

let commit_descriptions: Vec<(_, _)> = if skip_editor {
commits
.iter()
.map(|commit| {
Expand All @@ -151,8 +163,12 @@ pub(crate) fn cmd_describe(
.map(|commit| -> Result<_, CommandError> {
let mut commit_builder = tx.repo_mut().rewrite_commit(commit).detach();
if commit_builder.description().is_empty() {
commit_builder
.set_description(tx.settings().get_string("ui.default-description")?);
let description = if let Some(description) = &shared_description {
description.into()
} else {
tx.settings().get_string("ui.default-description")?
};
commit_builder.set_description(description);
}
if args.reset_author {
let new_author = commit_builder.committer().clone();
Expand Down Expand Up @@ -215,6 +231,8 @@ pub(crate) fn cmd_describe(
}
};

let commit_descriptions = commit_descriptions;

// Filter out unchanged commits to avoid rebasing descendants in
// `transform_descendants` below unnecessarily.
let commit_descriptions: HashMap<_, _> = commit_descriptions
Expand Down
3 changes: 3 additions & 0 deletions cli/tests/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,9 @@ Starts an editor to let you edit the description of changes. The editor will be
* `--no-edit` — Don't open an editor
This is mainly useful in combination with e.g. `--reset-author`.
* `--edit` — Open an editor
This is mainly useful to reverse the effects of --no-edit or another flag/option that implies --no-edit e.g. --message
* `--reset-author` — Reset the author to the configured user
This resets the author name, email, and timestamp.
Expand Down
57 changes: 57 additions & 0 deletions cli/tests/test_describe_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,63 @@ fn test_describe_avoids_unc() {
assert_eq!(edited_path, dunce::simplified(&edited_path));
}

#[test]
fn test_describe_opens_editor_if_edit_flag_overrides_default_behaviour() {
let mut test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);
let workspace_path = test_env.env_root().join("repo");

std::fs::write(workspace_path.join("file1"), "foo\n").unwrap();
std::fs::write(workspace_path.join("file2"), "bar\n").unwrap();
let edit_script = test_env.set_up_fake_editor();
std::fs::write(edit_script, ["dump editor"].join("\0")).unwrap();
let (stdout, stderr) = test_env.jj_cmd_ok(
&workspace_path,
&["describe", "-m", "message from command line", "--edit"],
);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @r"
Working copy now at: qpvuntsm 41069237 message from command line
Parent commit : zzzzzzzz 00000000 (empty) (no description set)
");
insta::assert_snapshot!(
std::fs::read_to_string(test_env.env_root().join("editor")).unwrap(), @r#"
message from command line
JJ: This commit contains the following changes:
JJ: A file1
JJ: A file2
JJ: Lines starting with "JJ:" (like this one) will be removed.
"#);
}

#[test]
fn test_edit_flag_overrides_no_edit_flag() {
let mut test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);
let workspace_path = test_env.env_root().join("repo");

std::fs::write(workspace_path.join("file1"), "foo\n").unwrap();
std::fs::write(workspace_path.join("file2"), "bar\n").unwrap();
let edit_script = test_env.set_up_fake_editor();
std::fs::write(
&edit_script,
indoc! {"
write
Updated commit message
"},
)
.unwrap();
let (stdout, stderr) =
test_env.jj_cmd_ok(&workspace_path, &["describe", "--no-edit", "--edit"]);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @r"
Working copy now at: qpvuntsm 52f5f954 Updated commit message
Parent commit : zzzzzzzz 00000000 (empty) (no description set)
");
}

fn get_log_output(test_env: &TestEnvironment, repo_path: &Path) -> String {
let template = r#"commit_id.short() ++ " " ++ description"#;
test_env.jj_cmd_success(repo_path, &["log", "-T", template])
Expand Down

0 comments on commit c7061b2

Please sign in to comment.