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

Report what operation has been undone in jj op undo #4445

Merged
merged 2 commits into from
Sep 15, 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 @@ -56,6 +56,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
allows to set a name for the remote instead of using the default
`origin`.

* `jj op undo` now reports information on the operation that has been undone.

### Fixed bugs

* Fixed panic when parsing invalid conflict markers of a particular form.
Expand Down
23 changes: 23 additions & 0 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@ pub struct WorkspaceCommandHelper {
env: WorkspaceCommandEnvironment,
// TODO: Parsed template can be cached if it doesn't capture 'repo lifetime
commit_summary_template_text: String,
op_summary_template_text: String,
may_update_working_copy: bool,
working_copy_shared_with_git: bool,
}
Expand All @@ -740,6 +741,7 @@ impl WorkspaceCommandHelper {
let settings = env.settings();
let commit_summary_template_text =
settings.config().get_string("templates.commit_summary")?;
let op_summary_template_text = settings.config().get_string("templates.op_summary")?;
let may_update_working_copy =
loaded_at_head && !env.command.global_args().ignore_working_copy;
let working_copy_shared_with_git = is_colocated_git_workspace(&workspace, &repo);
Expand All @@ -748,11 +750,13 @@ impl WorkspaceCommandHelper {
user_repo: ReadonlyUserRepo::new(repo),
env,
commit_summary_template_text,
op_summary_template_text,
may_update_working_copy,
working_copy_shared_with_git,
};
// Parse commit_summary template early to report error before starting
// mutable operation.
helper.parse_operation_template(&helper.op_summary_template_text)?;
helper.parse_commit_template(&helper.commit_summary_template_text)?;
helper.parse_commit_template(SHORT_CHANGE_ID_TEMPLATE_TEXT)?;
Ok(helper)
Expand Down Expand Up @@ -1288,6 +1292,19 @@ impl WorkspaceCommandHelper {
)
}

/// Parses commit template into evaluation tree.
pub fn parse_operation_template(
&self,
template_text: &str,
) -> TemplateParseResult<TemplateRenderer<'_, Operation>> {
let language = self.operation_template_language();
self.parse_template(
&language,
template_text,
OperationTemplateLanguage::wrap_operation,
)
}

/// Creates commit template language environment for this workspace.
pub fn commit_template_language(&self) -> CommitTemplateLanguage<'_> {
self.env
Expand All @@ -1309,6 +1326,12 @@ impl WorkspaceCommandHelper {
.expect("parse error should be confined by WorkspaceCommandHelper::new()")
}

/// Template for one-line summary of an operation.
pub fn operation_summary_template(&self) -> TemplateRenderer<'_, Operation> {
self.parse_operation_template(&self.op_summary_template_text)
.expect("parse error should be confined by WorkspaceCommandHelper::new()")
}

pub fn short_change_id_template(&self) -> TemplateRenderer<'_, Commit> {
self.parse_commit_template(SHORT_CHANGE_ID_TEMPLATE_TEXT)
.expect("parse error should be confined by WorkspaceCommandHelper::new()")
Expand Down
34 changes: 3 additions & 31 deletions cli/src/commands/operation/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ use jj_lib::revset;
use jj_lib::revset::RevsetIteratorExt as _;

use crate::cli_util::short_change_hash;
use crate::cli_util::short_operation_hash;
use crate::cli_util::CommandHelper;
use crate::cli_util::LogContentFormat;
use crate::command_error::CommandError;
Expand Down Expand Up @@ -123,42 +122,15 @@ pub fn cmd_op_diff(
workspace_env.parse_template(&language, &text, CommitTemplateLanguage::wrap_commit)?
};

let op_summary_template = workspace_command.operation_summary_template();
ui.request_pager();
let mut formatter = ui.stdout_formatter();
formatter.with_label("op_log", |formatter| {
write!(formatter, "From operation ")?;
write!(
formatter.labeled("id"),
"{}",
short_operation_hash(from_op.id()),
)?;
write!(formatter, ": ")?;
write!(
formatter.labeled("description"),
"{}",
if from_op.id() == from_op.op_store().root_operation_id() {
"root()"
} else {
&from_op.metadata().description
}
)?;
op_summary_template.format(&from_op, &mut *formatter)?;
writeln!(formatter)?;
write!(formatter, " To operation ")?;
write!(
formatter.labeled("id"),
"{}",
short_operation_hash(to_op.id()),
)?;
write!(formatter, ": ")?;
write!(
formatter.labeled("description"),
"{}",
if to_op.id() == to_op.op_store().root_operation_id() {
"root()"
} else {
&to_op.metadata().description
}
)?;
op_summary_template.format(&to_op, &mut *formatter)?;
writeln!(formatter)
})?;

Expand Down
7 changes: 7 additions & 0 deletions cli/src/commands/operation/undo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,12 @@ pub fn cmd_op_undo(
tx.repo_mut().set_view(new_view);
tx.finish(ui, format!("undo operation {}", bad_op.id().hex()))?;

if let Some(mut formatter) = ui.status_formatter() {
write!(formatter, "Undid operation ")?;
let template = workspace_command.operation_summary_template();
template.format(&bad_op, &mut *formatter)?;
writeln!(formatter)?;
}

Ok(())
}
7 changes: 7 additions & 0 deletions cli/src/config/templates.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ tag_list = '''
label("tag", name) ++ format_ref_targets(self) ++ "\n"
'''

op_summary = '''
if(root,
separate(" ", self.id().short(), label("root", "root()")),
separate(" ", self.id().short(), format_time_range(self.time()), self.description().first_line())
)
'''

[template-aliases]
builtin_log_oneline = '''
if(root,
Expand Down
8 changes: 6 additions & 2 deletions cli/tests/test_duplicate_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ fn test_duplicate() {

let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["undo"]);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @"");
insta::assert_snapshot!(stderr, @r#"
Undid operation c4b0b2a977fe 2001-02-03 04:05:17.000 +07:00 - 2001-02-03 04:05:17.000 +07:00 duplicate 1 commit(s)
"#);
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["duplicate" /* duplicates `c` */]);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @r###"
Expand Down Expand Up @@ -277,7 +279,9 @@ fn test_undo_after_duplicate() {

let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["undo"]);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @"");
insta::assert_snapshot!(stderr, @r#"
Undid operation 5a2bcfcdd78b 2001-02-03 04:05:11.000 +07:00 - 2001-02-03 04:05:11.000 +07:00 duplicate 1 commit(s)
"#);
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
@ 2443ea76b0b1 a
◆ 000000000000
Expand Down
4 changes: 2 additions & 2 deletions cli/tests/test_evolog_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ fn test_evolog_with_no_template() {
let repo_path = test_env.env_root().join("repo");

let stderr = test_env.jj_cmd_cli_error(&repo_path, &["evolog", "-T"]);
insta::assert_snapshot!(stderr, @r###"
insta::assert_snapshot!(stderr, @r#"
error: a value is required for '--template <TEMPLATE>' but none was supplied

For more information, try '--help'.
Expand All @@ -346,5 +346,5 @@ fn test_evolog_with_no_template() {
- description_placeholder
- email_placeholder
- name_placeholder
"###);
"#);
}
5 changes: 3 additions & 2 deletions cli/tests/test_git_colocated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,10 +756,11 @@ fn test_git_colocated_undo_head_move() {
// HEAD should be moved back
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["undo"]);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @r###"
insta::assert_snapshot!(stderr, @r#"
Working copy now at: royxmykx eb08b363 (empty) (no description set)
Parent commit : qpvuntsm 230dd059 (empty) (no description set)
"###);
Undid operation 951a1c249028 2001-02-03 04:05:13.000 +07:00 - 2001-02-03 04:05:13.000 +07:00 new empty commit
"#);
insta::assert_snapshot!(
git_repo.head().unwrap().target().unwrap().to_string(),
@"230dd059e1b059aefc0da06a2e5a7dbf22362f22");
Expand Down
4 changes: 3 additions & 1 deletion cli/tests/test_git_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,9 @@ fn test_git_fetch_undo() {
"###);
let (stdout, stderr) = test_env.jj_cmd_ok(&target_jj_repo_path, &["undo"]);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @"");
insta::assert_snapshot!(stderr, @r#"
Undid operation bbebbf6a6ebe 2001-02-03 04:05:18.000 +07:00 - 2001-02-03 04:05:18.000 +07:00 fetch from git remote(s) origin
"#);
// The undo works as expected
insta::assert_snapshot!(get_log_output(&test_env, &target_jj_repo_path), @r###"
@ 230dd059e1b0
Expand Down
4 changes: 3 additions & 1 deletion cli/tests/test_git_import_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ fn test_git_export_undo() {
// bookmark is. This is the same as remote-tracking bookmarks.
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["op", "undo"]);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @"");
insta::assert_snapshot!(stderr, @r#"
Undid operation 1bc51ad79d63 2001-02-03 04:05:10.000 +07:00 - 2001-02-03 04:05:10.000 +07:00 export git refs
"#);
insta::assert_debug_snapshot!(get_git_repo_refs(&git_repo), @r###"
[
(
Expand Down
4 changes: 2 additions & 2 deletions cli/tests/test_log_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn test_log_with_no_template() {
let repo_path = test_env.env_root().join("repo");

let stderr = test_env.jj_cmd_cli_error(&repo_path, &["log", "-T"]);
insta::assert_snapshot!(stderr, @r###"
insta::assert_snapshot!(stderr, @r#"
error: a value is required for '--template <TEMPLATE>' but none was supplied

For more information, try '--help'.
Expand All @@ -55,7 +55,7 @@ fn test_log_with_no_template() {
- description_placeholder
- email_placeholder
- name_placeholder
"###);
"#);
}

#[test]
Expand Down
Loading