Skip to content

Commit

Permalink
templater: integrate copy tracking in commit.diff() template
Browse files Browse the repository at this point in the history
  • Loading branch information
yuja committed Aug 24, 2024
1 parent b78c83e commit 8d166c7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
8 changes: 7 additions & 1 deletion cli/src/commit_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1311,11 +1311,17 @@ impl TreeDiff {
commit: &Commit,
matcher: Rc<dyn Matcher>,
) -> BackendResult<Self> {
let mut copy_records = CopyRecords::default();
for parent in commit.parent_ids() {
let records =
diff_util::get_copy_records(repo.store(), parent, commit.id(), &*matcher)?;
copy_records.add_records(records)?;
}
Ok(TreeDiff {
from_tree: commit.parent_tree(repo)?,
to_tree: commit.tree()?,
matcher,
copy_records: Default::default(), // TODO: real copy tracking
copy_records,
})
}

Expand Down
42 changes: 33 additions & 9 deletions cli/tests/test_commit_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -951,9 +951,15 @@ fn test_log_diff_predefined_formats() {

std::fs::write(repo_path.join("file1"), "a\nb\n").unwrap();
std::fs::write(repo_path.join("file2"), "a\n").unwrap();
std::fs::write(repo_path.join("rename-source"), "rename").unwrap();
test_env.jj_cmd_ok(&repo_path, &["new"]);
std::fs::write(repo_path.join("file1"), "a\nb\nc\n").unwrap();
std::fs::write(repo_path.join("file2"), "b\nc\n").unwrap();
std::fs::rename(
repo_path.join("rename-source"),
repo_path.join("rename-target"),
)
.unwrap();

let template = r#"
concat(
Expand Down Expand Up @@ -982,6 +988,7 @@ fn test_log_diff_predefined_formats() {
Modified regular file file2:
 1  1: ab
 2: c
Modified regular file rename-target (rename-source => rename-target):
=== git ===
diff --git a/file1 b/file1
index 422c2b7ab3..de980441c3 100644
Expand All @@ -999,13 +1006,18 @@ fn test_log_diff_predefined_formats() {
-a
+b
+c
diff --git a/rename-source b/rename-target
rename from rename-source
rename to rename-target
=== stat ===
file1 | 1 +
file2 | 3 ++-
2 files changed, 3 insertions(+), 1 deletion(-)
file1 | 1 +
file2 | 3 ++-
{rename-source => rename-target} | 0
3 files changed, 3 insertions(+), 1 deletion(-)
=== summary ===
M file1
M file2
R {rename-source => rename-target}
"###);

// color labels
Expand All @@ -1022,6 +1034,7 @@ fn test_log_diff_predefined_formats() {
<<log diff color_words header::Modified regular file file2:>>
<<log diff color_words removed line_number:: 1>><<log diff color_words:: >><<log diff color_words added line_number:: 1>><<log diff color_words::: >><<log diff color_words removed token::a>><<log diff color_words added token::b>><<log diff color_words::>>
<<log diff color_words:: >><<log diff color_words added line_number:: 2>><<log diff color_words::: >><<log diff color_words added token::c>>
<<log diff color_words header::Modified regular file rename-target (rename-source => rename-target):>>
<<log::=== git ===>>
<<log diff git file_header::diff --git a/file1 b/file1>>
<<log diff git file_header::index 422c2b7ab3..de980441c3 100644>>
Expand All @@ -1039,13 +1052,18 @@ fn test_log_diff_predefined_formats() {
<<log diff git removed::->><<log diff git removed token::a>><<log diff git removed::>>
<<log diff git added::+>><<log diff git added token::b>><<log diff git added::>>
<<log diff git added::+>><<log diff git added token::c>>
<<log diff git file_header::diff --git a/rename-source b/rename-target>>
<<log diff git file_header::rename from rename-source>>
<<log diff git file_header::rename to rename-target>>
<<log::=== stat ===>>
<<log diff stat::file1 | 1 >><<log diff stat added::+>><<log diff stat removed::>>
<<log diff stat::file2 | 3 >><<log diff stat added::++>><<log diff stat removed::->>
<<log diff stat stat-summary::2 files changed, 3 insertions(+), 1 deletion(-)>>
<<log diff stat::file1 | 1 >><<log diff stat added::+>><<log diff stat removed::>>
<<log diff stat::file2 | 3 >><<log diff stat added::++>><<log diff stat removed::->>
<<log diff stat::{rename-source => rename-target} | 0>><<log diff stat removed::>>
<<log diff stat stat-summary::3 files changed, 3 insertions(+), 1 deletion(-)>>
<<log::=== summary ===>>
<<log diff summary modified::M file1>>
<<log diff summary modified::M file2>>
<<log diff summary renamed::R {rename-source => rename-target}>>
"###);

// cwd != workspace root
Expand All @@ -1062,6 +1080,7 @@ fn test_log_diff_predefined_formats() {
Modified regular file repo/file2:
1 1: ab
2: c
Modified regular file repo/rename-target (repo/rename-source => repo/rename-target):
=== git ===
diff --git a/file1 b/file1
index 422c2b7ab3..de980441c3 100644
Expand All @@ -1079,13 +1098,18 @@ fn test_log_diff_predefined_formats() {
-a
+b
+c
diff --git a/rename-source b/rename-target
rename from rename-source
rename to rename-target
=== stat ===
repo/file1 | 1 +
repo/file2 | 3 ++-
2 files changed, 3 insertions(+), 1 deletion(-)
repo/file1 | 1 +
repo/file2 | 3 ++-
repo/{rename-source => rename-target} | 0
3 files changed, 3 insertions(+), 1 deletion(-)
=== summary ===
M repo/file1
M repo/file2
R repo/{rename-source => rename-target}
"###);

// color_words() with parameters
Expand Down

0 comments on commit 8d166c7

Please sign in to comment.