Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ should not be broken.
* `jj log` now supports a `--count` flag to print the number of commits instead
of displaying them.

* `TreeDiffEntry` type now has a `formatted_path()` method that formats
renames/copies appropriately.
* `TreeDiffEntry` type now has `formatted_path()` and `status_char()` methods.
`formatted_path()` shows renames/copies with common prefix extraction (e.g.,
`src/{old => new}/file.rs`). `status_char()` returns single-character status
codes (M/A/D/C/R).

### Fixed bugs

Expand Down
13 changes: 12 additions & 1 deletion cli/src/commit_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2414,7 +2414,18 @@ fn builtin_tree_diff_entry_methods<'repo>() -> CommitTemplateBuildMethodFnMap<'r
Ok(out_property.into_dyn_wrapped())
},
);
// TODO: add status_code() or status_char()?
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())
},
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably better to rebase this on top of #8123.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The relevant code is now in main

map.insert(
"source",
|_language, _diagnostics, _build_ctx, self_property, function| {
Expand Down
51 changes: 51 additions & 0 deletions cli/tests/test_commit_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,57 @@ fn test_log_diff_formatted_path() {
");
}

#[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"
Copy link
Contributor

Choose a reason for hiding this comment

The 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 .status() in test_commit_template.rs or test_diff_command.rs.

).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();
Expand Down
2 changes: 2 additions & 0 deletions docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,8 @@ This type cannot be printed. The following methods are defined.
points to the target (or right) entry.
* `.status() -> String`: One of `"modified"`, `"added"`, `"removed"`,
`"copied"`, or `"renamed"`.
* `.status_char() -> String`: Single-character status indicator: `"M"` for modified,
`"A"` for added, `"D"` for removed, `"C"` for copied, or `"R"` for renamed.
* `.formatted_path() -> String`: Formatted path with common prefix/suffix extraction
for renames and copies. For example, `src/{old => new}/file.rs` instead of showing
full paths. For non-copy/rename operations, equivalent to `.path()`.
Expand Down
Loading