Skip to content

Commit

Permalink
op-diff: Use op_summary template
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Sep 15, 2024
1 parent 020c79e commit 084983d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 68 deletions.
60 changes: 29 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 All @@ -48,6 +47,7 @@ use crate::formatter::Formatter;
use crate::graphlog::get_graphlog;
use crate::graphlog::Edge;
use crate::graphlog::GraphStyle;
use crate::operation_templater::OperationTemplateLanguage;
use crate::templater::TemplateRenderer;
use crate::ui::Ui;

Expand Down Expand Up @@ -124,42 +124,40 @@ pub fn cmd_op_diff(
workspace_env.parse_template(&language, &text, CommitTemplateLanguage::wrap_commit)?
};

let from_op_summary_template = {
let workspace_env = workspace_command.env();
let language = OperationTemplateLanguage::new(
from_op.op_store().root_operation_id(),
Some(from_op.id()),
workspace_env.operation_template_extensions(),
);
let text = command
.settings()
.config()
.get_string("templates.op_summary")?;
workspace_env.parse_template(&language, &text, OperationTemplateLanguage::wrap_operation)?
};
let to_op_summary_template = {
let workspace_env = workspace_command.env();
let language = OperationTemplateLanguage::new(
to_op.op_store().root_operation_id(),
Some(to_op.id()),
workspace_env.operation_template_extensions(),
);
let text = command
.settings()
.config()
.get_string("templates.op_summary")?;
workspace_env.parse_template(&language, &text, OperationTemplateLanguage::wrap_operation)?
};
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
}
)?;
from_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
}
)?;
to_op_summary_template.format(&to_op, &mut *formatter)?;
writeln!(formatter)
})?;

Expand Down
73 changes: 36 additions & 37 deletions cli/tests/test_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,11 +865,10 @@ fn test_op_diff() {
&repo_path,
&["op", "diff", "--from", "0000000", "--to", "0000000"],
);
insta::assert_snapshot!(&stdout, @r###"
From operation 000000000000: root()
To operation 000000000000: root()
"###);
insta::assert_snapshot!(&stdout, @r#"
From operation 000000000000 root()
To operation 000000000000 root()
"#);
let stdout = test_env.jj_cmd_success(&repo_path, &["op", "diff", "--from", "@", "--to", "@"]);
insta::assert_snapshot!(&stdout, @r#"
From operation ea112f6a02be 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 check out git remote's default branch
Expand All @@ -880,9 +879,9 @@ fn test_op_diff() {
// `jj op diff --op @` should behave identically to `jj op diff --from
// @- --to @` (if `@` is not a merge commit).
let stdout = test_env.jj_cmd_success(&repo_path, &["op", "diff", "--from", "@-", "--to", "@"]);
insta::assert_snapshot!(&stdout, @r###"
From operation cba9d7096849: fetch from git remote into empty repo
To operation ea112f6a02be: check out git remote's default branch
insta::assert_snapshot!(&stdout, @r#"
From operation cba9d7096849 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 fetch from git remote into empty repo
To operation ea112f6a02be 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 check out git remote's default branch
Changed commits:
○ Change sqpuoqvxutmz
Expand All @@ -899,7 +898,7 @@ fn test_op_diff() {
bookmark-1@origin:
+ tracked ulyvmwyz 1d843d1f bookmark-1 | Commit 1
- untracked ulyvmwyz 1d843d1f bookmark-1 | Commit 1
"###);
"#);
let stdout_without_from_to = test_env.jj_cmd_success(&repo_path, &["op", "diff"]);
assert_eq!(stdout, stdout_without_from_to);

Expand Down Expand Up @@ -1277,9 +1276,9 @@ fn test_op_diff_patch() {
Parent commit : qpvuntsm 6b1027d2 (no description set)
"###);
let stdout = test_env.jj_cmd_success(&repo_path, &["op", "diff", "--op", "@-", "-p", "--git"]);
insta::assert_snapshot!(&stdout, @r###"
From operation b51416386f26: add workspace 'default'
To operation 6188e9d1f7da: snapshot working copy
insta::assert_snapshot!(&stdout, @r#"
From operation b51416386f26 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 add workspace 'default'
To operation 6188e9d1f7da 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00 snapshot working copy
Changed commits:
○ Change qpvuntsmwlqt
Expand All @@ -1292,16 +1291,16 @@ fn test_op_diff_patch() {
+++ b/file
@@ -1,0 +1,1 @@
+a
"###);
"#);
let stdout = test_env.jj_cmd_success(&repo_path, &["op", "diff", "--op", "@", "-p", "--git"]);
insta::assert_snapshot!(&stdout, @r###"
From operation 6188e9d1f7da: snapshot working copy
To operation 8f6a879bef11: new empty commit
insta::assert_snapshot!(&stdout, @r#"
From operation 6188e9d1f7da 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00 snapshot working copy
To operation 8f6a879bef11 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00 new empty commit
Changed commits:
○ Change rlvkpnrzqnoo
+ rlvkpnrz 56950632 (empty) (no description set)
"###);
"#);

// Squash the working copy commit.
std::fs::write(repo_path.join("file"), "b\n").unwrap();
Expand Down Expand Up @@ -1349,16 +1348,16 @@ fn test_op_diff_patch() {
Parent commit : qpvuntsm 2ac85fd1 (no description set)
"###);
let stdout = test_env.jj_cmd_success(&repo_path, &["op", "diff", "-p", "--git"]);
insta::assert_snapshot!(&stdout, @r###"
From operation c53f5f1afbc6: squash commits into 6b1027d2770cd0a39c468e525e52bf8c47e1464a
To operation e13dc1c7a3b3: abandon commit 9f4fb57fba25a7b47ce5980a5d9a4766778331e8
insta::assert_snapshot!(&stdout, @r#"
From operation c53f5f1afbc6 2001-02-03 04:05:11.000 +07:00 - 2001-02-03 04:05:11.000 +07:00 squash commits into 6b1027d2770cd0a39c468e525e52bf8c47e1464a
To operation e13dc1c7a3b3 2001-02-03 04:05:13.000 +07:00 - 2001-02-03 04:05:13.000 +07:00 abandon commit 9f4fb57fba25a7b47ce5980a5d9a4766778331e8
Changed commits:
○ Change yqosqzytrlsw
+ yqosqzyt 33f321c4 (empty) (no description set)
○ Change mzvwutvlkqwt
- mzvwutvl hidden 9f4fb57f (empty) (no description set)
"###);
"#);
}

#[test]
Expand Down Expand Up @@ -1442,9 +1441,9 @@ fn test_op_diff_sibling() {
"--summary",
],
);
insta::assert_snapshot!(&stdout, @r###"
From operation b8584dcdb220: new empty commit
To operation 654f3215449e: describe commit 230dd059e1b059aefc0da06a2e5a7dbf22362f22
insta::assert_snapshot!(&stdout, @r#"
From operation b8584dcdb220 2001-02-03 04:05:11.000 +07:00 - 2001-02-03 04:05:11.000 +07:00 new empty commit
To operation 654f3215449e 2001-02-03 04:05:12.000 +07:00 - 2001-02-03 04:05:12.000 +07:00 describe commit 230dd059e1b059aefc0da06a2e5a7dbf22362f22
Changed commits:
○ Change qpvuntsmwlqt
Expand All @@ -1457,7 +1456,7 @@ fn test_op_diff_sibling() {
○ Change zsuskulnrvyr
- zsuskuln hidden 8afecaef A.2
A file2
"###);
"#);
let stdout = test_env.jj_cmd_success(
&repo_path,
&[
Expand All @@ -1472,9 +1471,9 @@ fn test_op_diff_sibling() {
"--summary",
],
);
insta::assert_snapshot!(&stdout, @r###"
From operation 654f3215449e: describe commit 230dd059e1b059aefc0da06a2e5a7dbf22362f22
To operation b8584dcdb220: new empty commit
insta::assert_snapshot!(&stdout, @r#"
From operation 654f3215449e 2001-02-03 04:05:12.000 +07:00 - 2001-02-03 04:05:12.000 +07:00 describe commit 230dd059e1b059aefc0da06a2e5a7dbf22362f22
To operation b8584dcdb220 2001-02-03 04:05:11.000 +07:00 - 2001-02-03 04:05:11.000 +07:00 new empty commit
Changed commits:
○ Change mzvwutvlkqwt
Expand All @@ -1487,7 +1486,7 @@ fn test_op_diff_sibling() {
A file2
○ Change qpvuntsmwlqt
- qpvuntsm hidden 02ef2bc4 (empty) B
"###);
"#);
}

#[test]
Expand Down Expand Up @@ -1516,9 +1515,9 @@ fn test_op_diff_word_wrap() {
test_env.jj_cmd_ok(&repo_path, &["debug", "snapshot"]);

// ui.log-word-wrap option works, and diff stat respects content width
insta::assert_snapshot!(render(&["op", "diff", "--from=@---", "--stat"], 40, true), @r###"
From operation b51416386f26: add workspace 'default'
To operation d12081b11443: snapshot working copy
insta::assert_snapshot!(render(&["op", "diff", "--from=@---", "--stat"], 40, true), @r#"
From operation b51416386f26 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 add workspace 'default'
To operation d12081b11443 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00 snapshot working copy
Changed commits:
○ Change sqpuoqvxutmz
Expand Down Expand Up @@ -1565,14 +1564,14 @@ fn test_op_diff_word_wrap() {
+ untracked tqyxmszt 3e785984
bookmark-3@origin | Commit 3
- untracked (absent)
"###);
"#);

// Graph width should be subtracted from the term width
let config = r#"templates.commit_summary='"0 1 2 3 4 5 6 7 8 9"'"#;
insta::assert_snapshot!(
render(&["op", "diff", "--from=@---", "--config-toml", config], 10, true), @r###"
From operation b51416386f26: add workspace 'default'
To operation d12081b11443: snapshot working copy
render(&["op", "diff", "--from=@---", "--config-toml", config], 10, true), @r#"
From operation b51416386f26 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 add workspace 'default'
To operation d12081b11443 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00 snapshot working copy
Changed
commits:
Expand Down Expand Up @@ -1637,7 +1636,7 @@ fn test_op_diff_word_wrap() {
-
untracked
(absent)
"###);
"#);
}

#[test]
Expand Down

0 comments on commit 084983d

Please sign in to comment.