tree: move diff_summary() to MergedTree

This commit is contained in:
Martin von Zweigbergk 2023-08-27 09:57:48 -07:00 committed by Martin von Zweigbergk
parent a7a2150328
commit 1b24b522f6
4 changed files with 72 additions and 51 deletions

View file

@ -60,6 +60,17 @@ impl MergedTreeValue<'_> {
}
}
/// Summary of the changes between two trees.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct DiffSummary {
/// Modified files
pub modified: Vec<RepoPath>,
/// Added files
pub added: Vec<RepoPath>,
/// Removed files
pub removed: Vec<RepoPath>,
}
impl MergedTree {
/// Creates a new `MergedTree` representing a single tree without conflicts.
pub fn resolved(tree: Tree) -> Self {
@ -322,6 +333,31 @@ impl MergedTree {
TreeDiffIterator::new(self.clone(), other.clone(), matcher)
}
/// Collects lists of modified, added, and removed files between this tree
/// and another tree.
pub fn diff_summary(&self, other: &MergedTree, matcher: &dyn Matcher) -> DiffSummary {
let mut modified = vec![];
let mut added = vec![];
let mut removed = vec![];
for (file, before, after) in self.diff(other, matcher) {
if before.is_absent() {
added.push(file);
} else if after.is_absent() {
removed.push(file);
} else {
modified.push(file);
}
}
modified.sort();
added.sort();
removed.sort();
DiffSummary {
modified,
added,
removed,
}
}
/// Merges this tree with `other`, using `base` as base.
pub fn merge(
&self,

View file

@ -77,13 +77,6 @@ impl Hash for Tree {
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct DiffSummary {
pub modified: Vec<RepoPath>,
pub added: Vec<RepoPath>,
pub removed: Vec<RepoPath>,
}
impl Tree {
pub fn new(store: Arc<Store>, dir: RepoPath, id: TreeId, data: Arc<backend::Tree>) -> Self {
Tree {
@ -191,27 +184,6 @@ impl Tree {
TreeDiffIterator::new(self.clone(), other.clone(), matcher)
}
pub fn diff_summary(&self, other: &Tree, matcher: &dyn Matcher) -> DiffSummary {
let mut modified = vec![];
let mut added = vec![];
let mut removed = vec![];
for (file, diff) in self.diff(other, matcher) {
match diff {
Diff::Modified(_, _) => modified.push(file.clone()),
Diff::Added(_) => added.push(file.clone()),
Diff::Removed(_) => removed.push(file.clone()),
}
}
modified.sort();
added.sort();
removed.sort();
DiffSummary {
modified,
added,
removed,
}
}
pub fn conflicts_matching(&self, matcher: &dyn Matcher) -> Vec<(RepoPath, ConflictId)> {
let mut conflicts = vec![];
for (name, value) in self.entries_matching(matcher) {

View file

@ -14,10 +14,10 @@
use jj_lib::backend::{ChangeId, MergedTreeId, MillisSinceEpoch, ObjectId, Signature, Timestamp};
use jj_lib::matchers::EverythingMatcher;
use jj_lib::merged_tree::DiffSummary;
use jj_lib::repo::Repo;
use jj_lib::repo_path::RepoPath;
use jj_lib::settings::UserSettings;
use jj_lib::tree::DiffSummary;
use test_case::test_case;
use testutils::{assert_rebased, CommitGraphBuilder, TestRepo};
@ -85,8 +85,9 @@ fn test_initial(use_git: bool) {
assert_eq!(
store
.root_commit()
.tree()
.diff_summary(&commit.tree(), &EverythingMatcher),
.merged_tree()
.unwrap()
.diff_summary(&commit.merged_tree().unwrap(), &EverythingMatcher),
DiffSummary {
modified: vec![],
added: vec![dir_file_path, root_file_path],
@ -168,8 +169,9 @@ fn test_rewrite(use_git: bool) {
assert_eq!(
store
.root_commit()
.tree()
.diff_summary(&rewritten_commit.tree(), &EverythingMatcher),
.merged_tree()
.unwrap()
.diff_summary(&rewritten_commit.merged_tree().unwrap(), &EverythingMatcher),
DiffSummary {
modified: vec![],
added: vec![dir_file_path.clone(), root_file_path],
@ -178,8 +180,9 @@ fn test_rewrite(use_git: bool) {
);
assert_eq!(
initial_commit
.tree()
.diff_summary(&rewritten_commit.tree(), &EverythingMatcher),
.merged_tree()
.unwrap()
.diff_summary(&rewritten_commit.merged_tree().unwrap(), &EverythingMatcher),
DiffSummary {
modified: vec![dir_file_path],
added: vec![],

View file

@ -13,8 +13,8 @@
// limitations under the License.
use jj_lib::matchers::{EverythingMatcher, FilesMatcher};
use jj_lib::merged_tree::{DiffSummary, MergedTree};
use jj_lib::repo_path::RepoPath;
use jj_lib::tree::DiffSummary;
use test_case::test_case;
use testutils::TestRepo;
@ -48,7 +48,7 @@ fn test_types(use_git: bool) {
);
assert_eq!(
tree1.diff_summary(&tree2, &EverythingMatcher),
MergedTree::Legacy(tree1).diff_summary(&MergedTree::Legacy(tree2), &EverythingMatcher),
DiffSummary {
modified: vec![modified_path],
added: vec![added_path],
@ -70,7 +70,8 @@ fn test_tree_file_transition(use_git: bool) {
let tree2 = testutils::create_tree(repo, &[(&dir_path, "contents")]);
assert_eq!(
tree1.diff_summary(&tree2, &EverythingMatcher),
MergedTree::Legacy(tree1.clone())
.diff_summary(&MergedTree::Legacy(tree2.clone()), &EverythingMatcher),
DiffSummary {
modified: vec![],
added: vec![dir_path.clone()],
@ -78,7 +79,7 @@ fn test_tree_file_transition(use_git: bool) {
}
);
assert_eq!(
tree2.diff_summary(&tree1, &EverythingMatcher),
MergedTree::Legacy(tree2).diff_summary(&MergedTree::Legacy(tree1), &EverythingMatcher),
DiffSummary {
modified: vec![],
added: vec![dir_file_path],
@ -128,7 +129,8 @@ fn test_sorting(use_git: bool) {
);
assert_eq!(
tree1.diff_summary(&tree2, &EverythingMatcher),
MergedTree::Legacy(tree1.clone())
.diff_summary(&MergedTree::Legacy(tree2.clone()), &EverythingMatcher),
DiffSummary {
modified: vec![a_path.clone(), f_a_path.clone(), f_f_a_path.clone()],
added: vec![
@ -143,7 +145,7 @@ fn test_sorting(use_git: bool) {
}
);
assert_eq!(
tree2.diff_summary(&tree1, &EverythingMatcher),
MergedTree::Legacy(tree2).diff_summary(&MergedTree::Legacy(tree1), &EverythingMatcher),
DiffSummary {
modified: vec![a_path, f_a_path, f_f_a_path],
added: vec![],
@ -166,7 +168,8 @@ fn test_matcher_dir_file_transition(use_git: bool) {
let matcher = FilesMatcher::new(&[a_path.clone()]);
assert_eq!(
tree1.diff_summary(&tree2, &matcher),
MergedTree::Legacy(tree1.clone())
.diff_summary(&MergedTree::Legacy(tree2.clone()), &matcher),
DiffSummary {
modified: vec![],
added: vec![],
@ -174,7 +177,8 @@ fn test_matcher_dir_file_transition(use_git: bool) {
}
);
assert_eq!(
tree2.diff_summary(&tree1, &matcher),
MergedTree::Legacy(tree2.clone())
.diff_summary(&MergedTree::Legacy(tree1.clone()), &matcher),
DiffSummary {
modified: vec![],
added: vec![a_path.clone()],
@ -184,7 +188,8 @@ fn test_matcher_dir_file_transition(use_git: bool) {
let matcher = FilesMatcher::new(&[a_a_path.clone()]);
assert_eq!(
tree1.diff_summary(&tree2, &matcher),
MergedTree::Legacy(tree1.clone())
.diff_summary(&MergedTree::Legacy(tree2.clone()), &matcher),
DiffSummary {
modified: vec![],
added: vec![a_a_path.clone()],
@ -192,7 +197,8 @@ fn test_matcher_dir_file_transition(use_git: bool) {
}
);
assert_eq!(
tree2.diff_summary(&tree1, &matcher),
MergedTree::Legacy(tree2.clone())
.diff_summary(&MergedTree::Legacy(tree1.clone()), &matcher),
DiffSummary {
modified: vec![],
added: vec![],
@ -202,7 +208,8 @@ fn test_matcher_dir_file_transition(use_git: bool) {
let matcher = FilesMatcher::new(&[a_path.clone(), a_a_path.clone()]);
assert_eq!(
tree1.diff_summary(&tree2, &matcher),
MergedTree::Legacy(tree1.clone())
.diff_summary(&MergedTree::Legacy(tree2.clone()), &matcher),
DiffSummary {
modified: vec![],
added: vec![a_a_path.clone()],
@ -210,7 +217,7 @@ fn test_matcher_dir_file_transition(use_git: bool) {
}
);
assert_eq!(
tree2.diff_summary(&tree1, &matcher),
MergedTree::Legacy(tree2).diff_summary(&MergedTree::Legacy(tree1), &matcher),
DiffSummary {
modified: vec![],
added: vec![a_path],
@ -247,7 +254,8 @@ fn test_matcher_normal_cases(use_git: bool) {
let matcher = FilesMatcher::new(&[a_path.clone(), z_path.clone()]);
assert_eq!(
tree1.diff_summary(&tree2, &matcher),
MergedTree::Legacy(tree1.clone())
.diff_summary(&MergedTree::Legacy(tree2.clone()), &matcher),
DiffSummary {
modified: vec![a_path.clone()],
added: vec![z_path.clone()],
@ -255,7 +263,8 @@ fn test_matcher_normal_cases(use_git: bool) {
}
);
assert_eq!(
tree2.diff_summary(&tree1, &matcher),
MergedTree::Legacy(tree2.clone())
.diff_summary(&MergedTree::Legacy(tree1.clone()), &matcher),
DiffSummary {
modified: vec![a_path],
added: vec![],
@ -265,7 +274,8 @@ fn test_matcher_normal_cases(use_git: bool) {
let matcher = FilesMatcher::new(&[dir1_a_path.clone(), dir2_b_path.clone()]);
assert_eq!(
tree1.diff_summary(&tree2, &matcher),
MergedTree::Legacy(tree1.clone())
.diff_summary(&MergedTree::Legacy(tree2.clone()), &matcher),
DiffSummary {
modified: vec![dir1_a_path.clone()],
added: vec![dir2_b_path.clone()],
@ -273,7 +283,7 @@ fn test_matcher_normal_cases(use_git: bool) {
}
);
assert_eq!(
tree2.diff_summary(&tree1, &matcher),
MergedTree::Legacy(tree2).diff_summary(&MergedTree::Legacy(tree1), &matcher),
DiffSummary {
modified: vec![dir1_a_path],
added: vec![],