2022-11-26 23:57:50 +00:00
|
|
|
// Copyright 2020 The Jujutsu Authors
|
2020-12-12 08:00:42 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2023-06-28 14:12:40 +00:00
|
|
|
use jj_lib::matchers::{EverythingMatcher, FilesMatcher};
|
2023-08-28 14:55:48 +00:00
|
|
|
use jj_lib::merged_tree::DiffSummary;
|
2023-11-26 07:12:36 +00:00
|
|
|
use jj_lib::repo_path::{RepoPath, RepoPathBuf};
|
2023-09-19 12:49:42 +00:00
|
|
|
use testutils::{create_tree, TestRepo};
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2023-11-26 07:12:36 +00:00
|
|
|
fn to_owned_path_vec(paths: &[&RepoPath]) -> Vec<RepoPathBuf> {
|
|
|
|
paths.iter().map(|&path| path.to_owned()).collect()
|
|
|
|
}
|
|
|
|
|
2023-09-19 12:49:42 +00:00
|
|
|
#[test]
|
|
|
|
fn test_types() {
|
|
|
|
let test_repo = TestRepo::init();
|
2022-02-05 23:29:05 +00:00
|
|
|
let repo = &test_repo.repo;
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2023-11-26 10:21:46 +00:00
|
|
|
let clean_path = RepoPath::from_internal_string("clean");
|
|
|
|
let modified_path = RepoPath::from_internal_string("modified");
|
|
|
|
let added_path = RepoPath::from_internal_string("added");
|
|
|
|
let removed_path = RepoPath::from_internal_string("removed");
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2023-08-28 14:55:48 +00:00
|
|
|
let tree1 = create_tree(
|
2021-11-21 07:46:54 +00:00
|
|
|
repo,
|
2020-12-12 08:00:42 +00:00
|
|
|
&[
|
2023-11-26 07:12:36 +00:00
|
|
|
(clean_path, "clean"),
|
|
|
|
(modified_path, "contents before"),
|
|
|
|
(removed_path, "removed contents"),
|
2020-12-12 08:00:42 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
|
2023-08-28 14:55:48 +00:00
|
|
|
let tree2 = create_tree(
|
2021-11-21 07:46:54 +00:00
|
|
|
repo,
|
2020-12-12 08:00:42 +00:00
|
|
|
&[
|
2023-11-26 07:12:36 +00:00
|
|
|
(clean_path, "clean"),
|
|
|
|
(modified_path, "contents after"),
|
|
|
|
(added_path, "added contents"),
|
2020-12-12 08:00:42 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2023-09-07 18:35:59 +00:00
|
|
|
tree1.diff_summary(&tree2, &EverythingMatcher).unwrap(),
|
2020-12-12 08:00:42 +00:00
|
|
|
DiffSummary {
|
2023-11-26 07:12:36 +00:00
|
|
|
modified: to_owned_path_vec(&[modified_path]),
|
|
|
|
added: to_owned_path_vec(&[added_path]),
|
|
|
|
removed: to_owned_path_vec(&[removed_path]),
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-09-19 12:49:42 +00:00
|
|
|
#[test]
|
|
|
|
fn test_tree_file_transition() {
|
|
|
|
let test_repo = TestRepo::init();
|
2022-02-05 23:29:05 +00:00
|
|
|
let repo = &test_repo.repo;
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2023-11-26 10:21:46 +00:00
|
|
|
let dir_file_path = RepoPath::from_internal_string("dir/file");
|
|
|
|
let dir_path = RepoPath::from_internal_string("dir");
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2023-11-26 07:12:36 +00:00
|
|
|
let tree1 = create_tree(repo, &[(dir_file_path, "contents")]);
|
|
|
|
let tree2 = create_tree(repo, &[(dir_path, "contents")]);
|
2020-12-12 08:00:42 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2023-09-07 18:35:59 +00:00
|
|
|
tree1.diff_summary(&tree2, &EverythingMatcher).unwrap(),
|
2020-12-12 08:00:42 +00:00
|
|
|
DiffSummary {
|
|
|
|
modified: vec![],
|
2023-11-26 07:12:36 +00:00
|
|
|
added: to_owned_path_vec(&[dir_path]),
|
|
|
|
removed: to_owned_path_vec(&[dir_file_path]),
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-09-07 18:35:59 +00:00
|
|
|
tree2.diff_summary(&tree1, &EverythingMatcher).unwrap(),
|
2020-12-12 08:00:42 +00:00
|
|
|
DiffSummary {
|
|
|
|
modified: vec![],
|
2023-11-26 07:12:36 +00:00
|
|
|
added: to_owned_path_vec(&[dir_file_path]),
|
|
|
|
removed: to_owned_path_vec(&[dir_path]),
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-09-19 12:49:42 +00:00
|
|
|
#[test]
|
|
|
|
fn test_sorting() {
|
|
|
|
let test_repo = TestRepo::init();
|
2022-02-05 23:29:05 +00:00
|
|
|
let repo = &test_repo.repo;
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2023-11-26 10:21:46 +00:00
|
|
|
let a_path = RepoPath::from_internal_string("a");
|
|
|
|
let b_path = RepoPath::from_internal_string("b");
|
|
|
|
let f_a_path = RepoPath::from_internal_string("f/a");
|
|
|
|
let f_b_path = RepoPath::from_internal_string("f/b");
|
|
|
|
let f_f_a_path = RepoPath::from_internal_string("f/f/a");
|
|
|
|
let f_f_b_path = RepoPath::from_internal_string("f/f/b");
|
|
|
|
let n_path = RepoPath::from_internal_string("n");
|
|
|
|
let s_b_path = RepoPath::from_internal_string("s/b");
|
|
|
|
let z_path = RepoPath::from_internal_string("z");
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2023-08-28 14:55:48 +00:00
|
|
|
let tree1 = create_tree(
|
2021-11-21 07:46:54 +00:00
|
|
|
repo,
|
2020-12-12 08:00:42 +00:00
|
|
|
&[
|
2023-11-26 07:12:36 +00:00
|
|
|
(a_path, "before"),
|
|
|
|
(f_a_path, "before"),
|
|
|
|
(f_f_a_path, "before"),
|
2020-12-12 08:00:42 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
|
2023-08-28 14:55:48 +00:00
|
|
|
let tree2 = create_tree(
|
2021-11-21 07:46:54 +00:00
|
|
|
repo,
|
2020-12-12 08:00:42 +00:00
|
|
|
&[
|
2023-11-26 07:12:36 +00:00
|
|
|
(a_path, "after"),
|
|
|
|
(b_path, "after"),
|
|
|
|
(f_a_path, "after"),
|
|
|
|
(f_b_path, "after"),
|
|
|
|
(f_f_a_path, "after"),
|
|
|
|
(f_f_b_path, "after"),
|
|
|
|
(n_path, "after"),
|
|
|
|
(s_b_path, "after"),
|
|
|
|
(z_path, "after"),
|
2020-12-12 08:00:42 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2023-09-07 18:35:59 +00:00
|
|
|
tree1.diff_summary(&tree2, &EverythingMatcher).unwrap(),
|
2020-12-12 08:00:42 +00:00
|
|
|
DiffSummary {
|
2023-11-26 07:12:36 +00:00
|
|
|
modified: to_owned_path_vec(&[a_path, f_a_path, f_f_a_path]),
|
|
|
|
added: to_owned_path_vec(&[b_path, f_b_path, f_f_b_path, n_path, s_b_path, z_path]),
|
|
|
|
removed: vec![],
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-09-07 18:35:59 +00:00
|
|
|
tree2.diff_summary(&tree1, &EverythingMatcher).unwrap(),
|
2020-12-12 08:00:42 +00:00
|
|
|
DiffSummary {
|
2023-11-26 07:12:36 +00:00
|
|
|
modified: to_owned_path_vec(&[a_path, f_a_path, f_f_a_path]),
|
2020-12-12 08:00:42 +00:00
|
|
|
added: vec![],
|
2023-11-26 07:12:36 +00:00
|
|
|
removed: to_owned_path_vec(&[b_path, f_b_path, f_f_b_path, n_path, s_b_path, z_path]),
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2021-06-05 21:29:40 +00:00
|
|
|
|
2023-09-19 12:49:42 +00:00
|
|
|
#[test]
|
|
|
|
fn test_matcher_dir_file_transition() {
|
|
|
|
let test_repo = TestRepo::init();
|
2022-02-05 23:29:05 +00:00
|
|
|
let repo = &test_repo.repo;
|
2021-06-05 21:29:40 +00:00
|
|
|
|
2023-11-26 10:21:46 +00:00
|
|
|
let a_path = RepoPath::from_internal_string("a");
|
|
|
|
let a_a_path = RepoPath::from_internal_string("a/a");
|
2021-06-05 21:29:40 +00:00
|
|
|
|
2023-11-26 07:12:36 +00:00
|
|
|
let tree1 = create_tree(repo, &[(a_path, "before")]);
|
|
|
|
let tree2 = create_tree(repo, &[(a_a_path, "after")]);
|
2021-06-05 21:29:40 +00:00
|
|
|
|
2023-11-26 08:42:12 +00:00
|
|
|
let matcher = FilesMatcher::new([&a_path]);
|
2021-06-05 21:29:40 +00:00
|
|
|
assert_eq!(
|
2023-09-07 18:35:59 +00:00
|
|
|
tree1.diff_summary(&tree2, &matcher).unwrap(),
|
2021-06-05 21:29:40 +00:00
|
|
|
DiffSummary {
|
|
|
|
modified: vec![],
|
|
|
|
added: vec![],
|
2023-11-26 07:12:36 +00:00
|
|
|
removed: to_owned_path_vec(&[a_path]),
|
2021-06-05 21:29:40 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-09-07 18:35:59 +00:00
|
|
|
tree2.diff_summary(&tree1, &matcher).unwrap(),
|
2021-06-05 21:29:40 +00:00
|
|
|
DiffSummary {
|
|
|
|
modified: vec![],
|
2023-11-26 07:12:36 +00:00
|
|
|
added: to_owned_path_vec(&[a_path]),
|
|
|
|
removed: vec![],
|
2021-06-05 21:29:40 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2023-11-26 07:12:36 +00:00
|
|
|
let matcher = FilesMatcher::new([a_a_path]);
|
2021-06-05 21:29:40 +00:00
|
|
|
assert_eq!(
|
2023-09-07 18:35:59 +00:00
|
|
|
tree1.diff_summary(&tree2, &matcher).unwrap(),
|
2021-06-05 21:29:40 +00:00
|
|
|
DiffSummary {
|
|
|
|
modified: vec![],
|
2023-11-26 07:12:36 +00:00
|
|
|
added: to_owned_path_vec(&[a_a_path]),
|
|
|
|
removed: vec![],
|
2021-06-05 21:29:40 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-09-07 18:35:59 +00:00
|
|
|
tree2.diff_summary(&tree1, &matcher).unwrap(),
|
2021-06-05 21:29:40 +00:00
|
|
|
DiffSummary {
|
|
|
|
modified: vec![],
|
|
|
|
added: vec![],
|
2023-11-26 07:12:36 +00:00
|
|
|
removed: to_owned_path_vec(&[a_a_path]),
|
2021-06-05 21:29:40 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2023-11-26 07:12:36 +00:00
|
|
|
let matcher = FilesMatcher::new([a_path, a_a_path]);
|
2021-06-05 21:29:40 +00:00
|
|
|
assert_eq!(
|
2023-09-07 18:35:59 +00:00
|
|
|
tree1.diff_summary(&tree2, &matcher).unwrap(),
|
2021-06-05 21:29:40 +00:00
|
|
|
DiffSummary {
|
|
|
|
modified: vec![],
|
2023-11-26 07:12:36 +00:00
|
|
|
added: to_owned_path_vec(&[a_a_path]),
|
|
|
|
removed: to_owned_path_vec(&[a_path]),
|
2021-06-05 21:29:40 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-09-07 18:35:59 +00:00
|
|
|
tree2.diff_summary(&tree1, &matcher).unwrap(),
|
2021-06-05 21:29:40 +00:00
|
|
|
DiffSummary {
|
|
|
|
modified: vec![],
|
2023-11-26 07:12:36 +00:00
|
|
|
added: to_owned_path_vec(&[a_path]),
|
|
|
|
removed: to_owned_path_vec(&[a_a_path]),
|
2021-06-05 21:29:40 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-09-19 12:49:42 +00:00
|
|
|
#[test]
|
|
|
|
fn test_matcher_normal_cases() {
|
|
|
|
let test_repo = TestRepo::init();
|
2022-02-05 23:29:05 +00:00
|
|
|
let repo = &test_repo.repo;
|
2021-06-05 21:29:40 +00:00
|
|
|
|
2023-11-26 10:21:46 +00:00
|
|
|
let a_path = RepoPath::from_internal_string("a");
|
|
|
|
let dir1_a_path = RepoPath::from_internal_string("dir1/a");
|
|
|
|
let dir2_b_path = RepoPath::from_internal_string("dir2/b");
|
|
|
|
let z_path = RepoPath::from_internal_string("z");
|
2021-06-05 21:29:40 +00:00
|
|
|
|
2023-11-26 07:12:36 +00:00
|
|
|
let tree1 = create_tree(repo, &[(a_path, "before"), (dir1_a_path, "before")]);
|
2021-06-05 21:29:40 +00:00
|
|
|
// File "a" gets modified
|
|
|
|
// File "dir1/a" gets modified
|
|
|
|
// File "dir2/b" gets created
|
|
|
|
// File "z" gets created
|
2023-08-28 14:55:48 +00:00
|
|
|
let tree2 = create_tree(
|
2021-11-21 07:46:54 +00:00
|
|
|
repo,
|
2021-06-05 21:29:40 +00:00
|
|
|
&[
|
2023-11-26 07:12:36 +00:00
|
|
|
(a_path, "after"),
|
|
|
|
(dir1_a_path, "after"),
|
|
|
|
(dir2_b_path, "after"),
|
|
|
|
(z_path, "after"),
|
2021-06-05 21:29:40 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
|
2023-11-26 07:12:36 +00:00
|
|
|
let matcher = FilesMatcher::new([a_path, z_path]);
|
2021-06-05 21:29:40 +00:00
|
|
|
assert_eq!(
|
2023-09-07 18:35:59 +00:00
|
|
|
tree1.diff_summary(&tree2, &matcher).unwrap(),
|
2021-06-05 21:29:40 +00:00
|
|
|
DiffSummary {
|
2023-11-26 07:12:36 +00:00
|
|
|
modified: to_owned_path_vec(&[a_path]),
|
|
|
|
added: to_owned_path_vec(&[z_path]),
|
|
|
|
removed: vec![],
|
2021-06-05 21:29:40 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-09-07 18:35:59 +00:00
|
|
|
tree2.diff_summary(&tree1, &matcher).unwrap(),
|
2021-06-05 21:29:40 +00:00
|
|
|
DiffSummary {
|
2023-11-26 07:12:36 +00:00
|
|
|
modified: to_owned_path_vec(&[a_path]),
|
2021-06-05 21:29:40 +00:00
|
|
|
added: vec![],
|
2023-11-26 07:12:36 +00:00
|
|
|
removed: to_owned_path_vec(&[z_path]),
|
2021-06-05 21:29:40 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2023-11-26 07:12:36 +00:00
|
|
|
let matcher = FilesMatcher::new([dir1_a_path, dir2_b_path]);
|
2021-06-05 21:29:40 +00:00
|
|
|
assert_eq!(
|
2023-09-07 18:35:59 +00:00
|
|
|
tree1.diff_summary(&tree2, &matcher).unwrap(),
|
2021-06-05 21:29:40 +00:00
|
|
|
DiffSummary {
|
2023-11-26 07:12:36 +00:00
|
|
|
modified: to_owned_path_vec(&[dir1_a_path]),
|
|
|
|
added: to_owned_path_vec(&[dir2_b_path]),
|
|
|
|
removed: vec![],
|
2021-06-05 21:29:40 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-09-07 18:35:59 +00:00
|
|
|
tree2.diff_summary(&tree1, &matcher).unwrap(),
|
2021-06-05 21:29:40 +00:00
|
|
|
DiffSummary {
|
2023-11-26 07:12:36 +00:00
|
|
|
modified: to_owned_path_vec(&[dir1_a_path]),
|
2021-06-05 21:29:40 +00:00
|
|
|
added: vec![],
|
2023-11-26 07:12:36 +00:00
|
|
|
removed: to_owned_path_vec(&[dir2_b_path]),
|
2021-06-05 21:29:40 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|