-
Notifications
You must be signed in to change notification settings - Fork 834
templates: Expose copy/move path formatting and status char #8184
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2399,7 +2399,33 @@ fn builtin_tree_diff_entry_methods<'repo>() -> CommitTemplateBuildMethodFnMap<'r | |
| Ok(out_property.into_dyn_wrapped()) | ||
| }, | ||
| ); | ||
| // TODO: add status_code() or status_char()? | ||
| map.insert( | ||
| "formatted_path", | ||
| |language, _diagnostics, _build_ctx, self_property, function| { | ||
| function.expect_no_arguments()?; | ||
| let path_converter = language.path_converter; | ||
| let out_property = self_property.map(move |entry| { | ||
| if entry.path.copy_operation().is_some() { | ||
| path_converter.format_copied_path(entry.path.source(), entry.path.target()) | ||
| } else { | ||
| path_converter.format_file_path(entry.path.target()) | ||
| } | ||
| }); | ||
| Ok(out_property.into_dyn_wrapped()) | ||
| }, | ||
| ); | ||
| map.insert( | ||
| "status_char", | ||
| |_language, _diagnostics, _build_ctx, self_property, function| { | ||
| function.expect_no_arguments()?; | ||
| let out_property = self_property.map(|entry| { | ||
| let (_label, sigil) = | ||
| diff_util::diff_status_label_and_char(&entry.path, &entry.values); | ||
| sigil.to_string() | ||
| }); | ||
| Ok(out_property.into_dyn_wrapped()) | ||
| }, | ||
| ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's probably better to rebase this on top of #8123.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The relevant code is now in |
||
| map.insert( | ||
| "source", | ||
| |_language, _diagnostics, _build_ctx, self_property, function| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1440,6 +1440,109 @@ fn test_log_diff_predefined_formats() { | |
| "); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_log_diff_formatted_path() { | ||
| let test_env = TestEnvironment::default(); | ||
| test_env.run_jj_in(".", ["git", "init", "repo"]).success(); | ||
| let work_dir = test_env.work_dir("repo"); | ||
|
|
||
| // Create a structure with nested directories. | ||
| work_dir.create_dir("src"); | ||
| work_dir.create_dir("src/old"); | ||
| work_dir.create_dir("src/new"); | ||
| work_dir.write_file("src/old/file.rs", "content"); | ||
| work_dir.write_file("src/common.rs", "content"); | ||
| work_dir.write_file("top.txt", "content"); | ||
| work_dir.run_jj(["commit", "-m", "initial"]).success(); | ||
|
|
||
| // Rename within nested directory (common prefix/suffix). | ||
| work_dir | ||
| .run_jj(["file", "track", "src/new/file.rs"]) | ||
| .success(); | ||
| work_dir.write_file("src/new/file.rs", "content"); | ||
| std::fs::remove_file(work_dir.root().join("src/old/file.rs")).unwrap(); | ||
| work_dir.run_jj(["commit", "-m", "rename nested"]).success(); | ||
|
|
||
| // Rename at top level. | ||
| work_dir.run_jj(["file", "track", "renamed.txt"]).success(); | ||
| work_dir.write_file("renamed.txt", "content"); | ||
| std::fs::remove_file(work_dir.root().join("top.txt")).unwrap(); | ||
| work_dir.run_jj(["commit", "-m", "rename top"]).success(); | ||
|
|
||
| // Test formatted_path() shows compact format for renames. | ||
| let template = indoc! {r#" | ||
| concat( | ||
| description.first_line() ++ ":\n", | ||
| diff.files().map(|e| | ||
| e.formatted_path() ++ " [" ++ e.status() ++ "]\n" | ||
| ).join(""), | ||
| ) | ||
| "#}; | ||
| let output = work_dir.run_jj(["log", "--no-graph", "-r", "::@- & ~root()", "-T", template]); | ||
| insta::assert_snapshot!(output, @r" | ||
| rename top: | ||
| {top.txt => renamed.txt} [renamed] | ||
| rename nested: | ||
| src/{old => new}/file.rs [renamed] | ||
| initial: | ||
| src/common.rs [added] | ||
| src/old/file.rs [added] | ||
| top.txt [added] | ||
| [EOF] | ||
| "); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_log_diff_status_char() { | ||
| let test_env = TestEnvironment::default(); | ||
| test_env.run_jj_in(".", ["git", "init", "repo"]).success(); | ||
| let work_dir = test_env.work_dir("repo"); | ||
|
|
||
| // Create a structure with nested directories. | ||
| work_dir.create_dir("src"); | ||
| work_dir.create_dir("src/old"); | ||
| work_dir.create_dir("src/new"); | ||
| work_dir.write_file("src/old/file.rs", "content"); | ||
| work_dir.write_file("src/common.rs", "content"); | ||
| work_dir.write_file("top.txt", "content"); | ||
| work_dir.run_jj(["commit", "-m", "initial"]).success(); | ||
|
|
||
| // Rename within nested directory (common prefix/suffix). | ||
| work_dir | ||
| .run_jj(["file", "track", "src/new/file.rs"]) | ||
| .success(); | ||
| work_dir.write_file("src/new/file.rs", "content"); | ||
| std::fs::remove_file(work_dir.root().join("src/old/file.rs")).unwrap(); | ||
| work_dir.run_jj(["commit", "-m", "rename nested"]).success(); | ||
|
|
||
| // Rename at top level. | ||
| work_dir.run_jj(["file", "track", "renamed.txt"]).success(); | ||
| work_dir.write_file("renamed.txt", "content"); | ||
| std::fs::remove_file(work_dir.root().join("top.txt")).unwrap(); | ||
| work_dir.run_jj(["commit", "-m", "rename top"]).success(); | ||
|
|
||
| let template = indoc! {r#" | ||
| concat( | ||
| description.first_line() ++ ":\n", | ||
| diff.files().map(|e| | ||
| e.status_char() ++ " " ++ e.path() ++ "\n" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: can you instead update existing tests to minimize test execution time? I think there are tests for |
||
| ).join(""), | ||
| ) | ||
| "#}; | ||
| let output = work_dir.run_jj(["log", "--no-graph", "-r", "::@- & ~root()", "-T", template]); | ||
| insta::assert_snapshot!(output, @r" | ||
| rename top: | ||
| R renamed.txt | ||
| rename nested: | ||
| R src/new/file.rs | ||
| initial: | ||
| A src/common.rs | ||
| A src/old/file.rs | ||
| A top.txt | ||
| [EOF] | ||
| "); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_file_list_entries() { | ||
| let test_env = TestEnvironment::default(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: since this only matters for copied entries, I think the function name should include "copy", e.g.
display_copied_path(). Maybe we could addcopied_path() -> CopiedTreeDiffEntryPathfunction, but I don't think we'll add functions other thancopied_path().display().Another option is to add
path.display_copy_from(source.path())function toRepoPathtemplate type.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my head this is not a copy specific function. It only makes a difference for copies, but it has the behavior you would want in general. Still open yo name changed, but I would prefer something that doesn't suggest it's copy specific.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean
path().display()vsformatted_path()matters only when the entry was copied/renamed. It may be confusing if we have two ways of formatting paths.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally
path().display()should take the move into account so that we only have one way of doing formatting. I guess that would involve makingpath()return some new type.It would be a mildly breaking change, but code relying on the old behavior can easily migrate to use (the much more explicit)
target().