Skip to content

Commit

Permalink
copy-tracking: implement copy-tracking for --types
Browse files Browse the repository at this point in the history
  • Loading branch information
fowles committed Aug 15, 2024
1 parent eccc3e2 commit ac41aa5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
### New features

* The following diff formats now include information about copies and moves:
`--color-words`, `--stat`, `--summary`
`--color-words`, `--stat`, `--summary`, `--types`

* A tilde (`~`) at the start of the path will now be expanded to the user's home
directory when configuring a `signing.key` for SSH commit signing.
Expand Down
56 changes: 38 additions & 18 deletions cli/src/diff_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,14 @@ impl<'a> DiffRenderer<'a> {
show_diff_stat(formatter, store, tree_diff, path_converter, width)?;
}
DiffFormat::Types => {
let no_copy_tracking = Default::default();
let tree_diff = from_tree.diff_stream(to_tree, matcher, &no_copy_tracking);
show_types(formatter, tree_diff, path_converter)?;
show_types(
formatter,
path_converter,
from_tree,
to_tree,
matcher,
copy_records,
)?;
}
DiffFormat::NameOnly => {
let tree_diff = from_tree.diff_stream(to_tree, matcher, copy_records);
Expand Down Expand Up @@ -368,6 +373,22 @@ impl<'a> DiffRenderer<'a> {
}
}

fn collect_copied_sources<'a>(
copy_records: &'a CopyRecords,
matcher: &dyn Matcher,
) -> HashSet<&'a RepoPath> {
copy_records
.iter()
.filter_map(|record| {
if matcher.matches(&record.target) {
Some(record.source.as_ref())
} else {
None
}
})
.collect()
}

fn show_color_words_diff_hunks(
left: &[u8],
right: &[u8],
Expand Down Expand Up @@ -1168,17 +1189,7 @@ pub fn show_diff_summary(
copy_records: &CopyRecords,
) -> io::Result<()> {
let mut tree_diff = from_tree.diff_stream(to_tree, matcher, copy_records);

let copied_sources: HashSet<&RepoPath> = copy_records
.iter()
.filter_map(|record| {
if matcher.matches(&record.target) {
Some(record.source.as_ref())
} else {
None
}
})
.collect();
let copied_sources = collect_copied_sources(copy_records, matcher);

async {
while let Some(TreeDiffEntry {
Expand Down Expand Up @@ -1345,23 +1356,32 @@ pub fn show_diff_stat(

pub fn show_types(
formatter: &mut dyn Formatter,
mut tree_diff: TreeDiffStream,
path_converter: &RepoPathUiConverter,
from_tree: &MergedTree,
to_tree: &MergedTree,
matcher: &dyn Matcher,
copy_records: &CopyRecords,
) -> io::Result<()> {
let mut tree_diff = from_tree.diff_stream(to_tree, matcher, copy_records);
let copied_sources = collect_copied_sources(copy_records, matcher);

async {
while let Some(TreeDiffEntry {
source: _, // TODO handle copy tracking
target: repo_path,
source,
target,
value: diff,
}) = tree_diff.next().await
{
let (before, after) = diff.unwrap();
if after.is_absent() && copied_sources.contains(source.as_ref()) {
continue;
}
writeln!(
formatter.labeled("modified"),
"{}{} {}",
diff_summary_char(&before),
diff_summary_char(&after),
path_converter.format_file_path(&repo_path)
path_converter.format_copied_path(&source, &target)
)?;
}
Ok(())
Expand Down
7 changes: 6 additions & 1 deletion cli/tests/test_diff_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,14 @@ fn test_diff_basic() {

let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "--types"]);
insta::assert_snapshot!(stdout, @r###"
FF file2
FF {file1 => file3}
"###);

let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "--types", "glob:file[12]"]);
insta::assert_snapshot!(stdout, @r###"
F- file1
FF file2
-F file3
"###);

let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "--git"]);
Expand Down
3 changes: 1 addition & 2 deletions cli/tests/test_show_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,8 @@ fn test_show_basic() {
(no description set)
F- file1
FF file2
-F file3
FF {file1 => file3}
"###);

let stdout = test_env.jj_cmd_success(&repo_path, &["show", "--git"]);
Expand Down

0 comments on commit ac41aa5

Please sign in to comment.