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-07-10 15:17:00 +00:00
|
|
|
#![allow(dead_code, missing_docs)]
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2022-12-28 06:20:17 +00:00
|
|
|
use std::collections::{HashMap, HashSet};
|
|
|
|
use std::iter;
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2023-07-25 18:31:05 +00:00
|
|
|
use tracing::instrument;
|
|
|
|
|
2023-11-27 03:56:08 +00:00
|
|
|
use crate::repo_path::{RepoPath, RepoPathComponentBuf};
|
2020-12-12 08:00:42 +00:00
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Debug)]
|
2022-02-14 04:05:17 +00:00
|
|
|
pub enum Visit {
|
|
|
|
/// Everything in the directory is *guaranteed* to match, no need to check
|
|
|
|
/// descendants
|
|
|
|
AllRecursively,
|
|
|
|
Specific {
|
|
|
|
dirs: VisitDirs,
|
|
|
|
files: VisitFiles,
|
|
|
|
},
|
2022-05-22 05:31:37 +00:00
|
|
|
/// Nothing in the directory or its subdirectories will match.
|
|
|
|
///
|
|
|
|
/// This is the same as `Specific` with no directories or files. Use
|
|
|
|
/// `Visit::set()` to get create an instance that's `Specific` or
|
|
|
|
/// `Nothing` depending on the values at runtime.
|
|
|
|
Nothing,
|
2021-05-17 04:21:45 +00:00
|
|
|
}
|
|
|
|
|
2022-02-12 17:39:03 +00:00
|
|
|
impl Visit {
|
2023-11-25 08:46:17 +00:00
|
|
|
fn sets(dirs: HashSet<RepoPathComponentBuf>, files: HashSet<RepoPathComponentBuf>) -> Self {
|
2022-05-22 05:31:37 +00:00
|
|
|
if dirs.is_empty() && files.is_empty() {
|
|
|
|
Self::Nothing
|
|
|
|
} else {
|
|
|
|
Self::Specific {
|
|
|
|
dirs: VisitDirs::Set(dirs),
|
|
|
|
files: VisitFiles::Set(files),
|
|
|
|
}
|
2022-02-12 17:39:03 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-17 04:43:35 +00:00
|
|
|
|
|
|
|
pub fn is_nothing(&self) -> bool {
|
2022-05-22 05:31:37 +00:00
|
|
|
*self == Visit::Nothing
|
2022-04-17 04:43:35 +00:00
|
|
|
}
|
2022-02-12 17:39:03 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
#[derive(PartialEq, Eq, Debug)]
|
2021-05-20 04:27:53 +00:00
|
|
|
pub enum VisitDirs {
|
2020-12-12 08:00:42 +00:00
|
|
|
All,
|
2023-11-25 08:46:17 +00:00
|
|
|
Set(HashSet<RepoPathComponentBuf>),
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Debug)]
|
2021-05-20 04:27:53 +00:00
|
|
|
pub enum VisitFiles {
|
2020-12-12 08:00:42 +00:00
|
|
|
All,
|
2023-11-25 08:46:17 +00:00
|
|
|
Set(HashSet<RepoPathComponentBuf>),
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
working_copy: traverse filesystem in parallel
This improves `jj status` time by a factor of ~2x on my machine (M1 Macbook Pro 2021 16-inch, uses an SSD):
```sh
$ hyperfine --parameter-list hash before,after --parameter-list repo nixpkgs,gecko-dev --setup 'git checkout {hash} && cargo build --profile release-with-debug' --warmup 3 './target/release-with-debug/jj -R ../{repo} st'
Benchmark 1: ./target/release-with-debug/jj -R ../nixpkgs st (hash = before)
Time (mean ± σ): 1.640 s ± 0.019 s [User: 0.580 s, System: 1.044 s]
Range (min … max): 1.621 s … 1.673 s 10 runs
Benchmark 2: ./target/release-with-debug/jj -R ../nixpkgs st (hash = after)
Time (mean ± σ): 760.0 ms ± 5.4 ms [User: 812.9 ms, System: 2214.6 ms]
Range (min … max): 751.4 ms … 768.7 ms 10 runs
Benchmark 3: ./target/release-with-debug/jj -R ../gecko-dev st (hash = before)
Time (mean ± σ): 11.403 s ± 0.648 s [User: 4.546 s, System: 5.932 s]
Range (min … max): 10.553 s … 12.718 s 10 runs
Benchmark 4: ./target/release-with-debug/jj -R ../gecko-dev st (hash = after)
Time (mean ± σ): 5.974 s ± 0.028 s [User: 5.387 s, System: 11.959 s]
Range (min … max): 5.937 s … 6.024 s 10 runs
$ hyperfine --parameter-list repo nixpkgs,gecko-dev --warmup 3 'git -C ../{repo} status'
Benchmark 1: git -C ../nixpkgs status
Time (mean ± σ): 865.4 ms ± 8.4 ms [User: 119.4 ms, System: 1401.2 ms]
Range (min … max): 852.8 ms … 879.1 ms 10 runs
Benchmark 2: git -C ../gecko-dev status
Time (mean ± σ): 2.892 s ± 0.029 s [User: 0.458 s, System: 14.244 s]
Range (min … max): 2.837 s … 2.934 s 10 runs
```
Conclusions:
- ~2x improvement from previous `jj status` time.
- Slightly faster than Git on nixpkgs.
- Still 2x slower than Git on gecko-dev, not sure why.
For reference, Git's default number of threads is defined in the `online_cpus` function: https://github.com/git/git/blob/ee48e70a829d1fa2da82f14787051ad8e7c45b71/thread-utils.c#L21-L66. We are using whatever the Rayon default is.
2023-08-03 15:27:56 +00:00
|
|
|
pub trait Matcher: Sync {
|
2021-05-17 04:55:51 +00:00
|
|
|
fn matches(&self, file: &RepoPath) -> bool;
|
2021-05-19 16:41:25 +00:00
|
|
|
fn visit(&self, dir: &RepoPath) -> Visit;
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-02-12 17:39:03 +00:00
|
|
|
#[derive(PartialEq, Eq, Debug)]
|
|
|
|
pub struct NothingMatcher;
|
|
|
|
|
|
|
|
impl Matcher for NothingMatcher {
|
|
|
|
fn matches(&self, _file: &RepoPath) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit(&self, _dir: &RepoPath) -> Visit {
|
2022-05-22 05:31:37 +00:00
|
|
|
Visit::Nothing
|
2022-02-12 17:39:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
#[derive(PartialEq, Eq, Debug)]
|
2021-05-17 04:17:01 +00:00
|
|
|
pub struct EverythingMatcher;
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2021-05-17 04:17:01 +00:00
|
|
|
impl Matcher for EverythingMatcher {
|
2021-05-17 04:55:51 +00:00
|
|
|
fn matches(&self, _file: &RepoPath) -> bool {
|
2020-12-12 08:00:42 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2021-05-19 16:41:25 +00:00
|
|
|
fn visit(&self, _dir: &RepoPath) -> Visit {
|
2022-02-14 04:05:17 +00:00
|
|
|
Visit::AllRecursively
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Debug)]
|
|
|
|
pub struct FilesMatcher {
|
2022-12-29 09:31:51 +00:00
|
|
|
tree: RepoPathTree,
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FilesMatcher {
|
2023-11-26 08:42:12 +00:00
|
|
|
pub fn new(files: impl IntoIterator<Item = impl AsRef<RepoPath>>) -> Self {
|
2022-12-29 09:31:51 +00:00
|
|
|
let mut tree = RepoPathTree::new();
|
2022-12-28 07:34:24 +00:00
|
|
|
for f in files {
|
2023-11-26 08:42:12 +00:00
|
|
|
tree.add_file(f.as_ref());
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
2022-12-29 09:31:51 +00:00
|
|
|
FilesMatcher { tree }
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Matcher for FilesMatcher {
|
2021-05-17 04:55:51 +00:00
|
|
|
fn matches(&self, file: &RepoPath) -> bool {
|
2022-12-29 09:31:51 +00:00
|
|
|
self.tree.get(file).map(|sub| sub.is_file).unwrap_or(false)
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 16:41:25 +00:00
|
|
|
fn visit(&self, dir: &RepoPath) -> Visit {
|
2022-12-29 09:31:51 +00:00
|
|
|
self.tree.get_visit_sets(dir)
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-17 04:21:45 +00:00
|
|
|
pub struct PrefixMatcher {
|
2022-12-29 09:31:51 +00:00
|
|
|
tree: RepoPathTree,
|
2021-05-17 04:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PrefixMatcher {
|
2023-11-26 08:42:12 +00:00
|
|
|
#[instrument(skip(prefixes))]
|
|
|
|
pub fn new(prefixes: impl IntoIterator<Item = impl AsRef<RepoPath>>) -> Self {
|
2022-12-29 09:31:51 +00:00
|
|
|
let mut tree = RepoPathTree::new();
|
2022-12-28 06:20:17 +00:00
|
|
|
for prefix in prefixes {
|
2023-11-26 08:42:12 +00:00
|
|
|
let sub = tree.add(prefix.as_ref());
|
2022-12-28 06:20:17 +00:00
|
|
|
sub.is_dir = true;
|
|
|
|
sub.is_file = true;
|
2021-05-17 04:21:45 +00:00
|
|
|
}
|
2022-12-29 09:31:51 +00:00
|
|
|
PrefixMatcher { tree }
|
2021-05-17 04:21:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Matcher for PrefixMatcher {
|
|
|
|
fn matches(&self, file: &RepoPath) -> bool {
|
2022-12-29 09:31:51 +00:00
|
|
|
self.tree.walk_to(file).any(|(sub, _)| sub.is_file)
|
2021-05-17 04:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit(&self, dir: &RepoPath) -> Visit {
|
2023-11-27 03:56:08 +00:00
|
|
|
for (sub, tail_path) in self.tree.walk_to(dir) {
|
2022-12-28 06:20:17 +00:00
|
|
|
// 'is_file' means the current path matches prefix paths
|
|
|
|
if sub.is_file {
|
|
|
|
return Visit::AllRecursively;
|
|
|
|
}
|
|
|
|
// 'dir' found, and is an ancestor of prefix paths
|
2023-11-27 03:56:08 +00:00
|
|
|
if tail_path.is_root() {
|
2022-12-28 06:20:17 +00:00
|
|
|
return sub.to_visit_sets();
|
|
|
|
}
|
2021-05-17 04:21:45 +00:00
|
|
|
}
|
2022-12-28 06:20:17 +00:00
|
|
|
Visit::Nothing
|
2021-05-17 04:21:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-22 05:10:35 +00:00
|
|
|
/// Matches paths that are matched by the first input matcher but not by the
|
|
|
|
/// second.
|
2022-04-17 04:43:35 +00:00
|
|
|
pub struct DifferenceMatcher<'input> {
|
|
|
|
/// The minuend
|
|
|
|
wanted: &'input dyn Matcher,
|
|
|
|
/// The subtrahend
|
|
|
|
unwanted: &'input dyn Matcher,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'input> DifferenceMatcher<'input> {
|
|
|
|
pub fn new(wanted: &'input dyn Matcher, unwanted: &'input dyn Matcher) -> Self {
|
|
|
|
Self { wanted, unwanted }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Matcher for DifferenceMatcher<'_> {
|
|
|
|
fn matches(&self, file: &RepoPath) -> bool {
|
|
|
|
self.wanted.matches(file) && !self.unwanted.matches(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit(&self, dir: &RepoPath) -> Visit {
|
|
|
|
match self.unwanted.visit(dir) {
|
2022-05-22 05:31:37 +00:00
|
|
|
Visit::AllRecursively => Visit::Nothing,
|
2022-05-22 05:59:43 +00:00
|
|
|
Visit::Nothing => self.wanted.visit(dir),
|
|
|
|
Visit::Specific { .. } => match self.wanted.visit(dir) {
|
|
|
|
Visit::AllRecursively => Visit::Specific {
|
|
|
|
dirs: VisitDirs::All,
|
|
|
|
files: VisitFiles::All,
|
|
|
|
},
|
2022-04-17 04:43:35 +00:00
|
|
|
wanted_visit => wanted_visit,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-22 05:10:35 +00:00
|
|
|
/// Matches paths that are matched by both input matchers.
|
|
|
|
pub struct IntersectionMatcher<'input> {
|
|
|
|
input1: &'input dyn Matcher,
|
|
|
|
input2: &'input dyn Matcher,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'input> IntersectionMatcher<'input> {
|
|
|
|
pub fn new(input1: &'input dyn Matcher, input2: &'input dyn Matcher) -> Self {
|
|
|
|
Self { input1, input2 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Matcher for IntersectionMatcher<'_> {
|
|
|
|
fn matches(&self, file: &RepoPath) -> bool {
|
|
|
|
self.input1.matches(file) && self.input2.matches(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit(&self, dir: &RepoPath) -> Visit {
|
|
|
|
match self.input1.visit(dir) {
|
|
|
|
Visit::AllRecursively => self.input2.visit(dir),
|
|
|
|
Visit::Nothing => Visit::Nothing,
|
|
|
|
Visit::Specific {
|
|
|
|
dirs: dirs1,
|
|
|
|
files: files1,
|
|
|
|
} => match self.input2.visit(dir) {
|
|
|
|
Visit::AllRecursively => Visit::Specific {
|
|
|
|
dirs: dirs1,
|
|
|
|
files: files1,
|
|
|
|
},
|
|
|
|
Visit::Nothing => Visit::Nothing,
|
|
|
|
Visit::Specific {
|
|
|
|
dirs: dirs2,
|
|
|
|
files: files2,
|
|
|
|
} => {
|
|
|
|
let dirs = match (dirs1, dirs2) {
|
|
|
|
(VisitDirs::All, VisitDirs::All) => VisitDirs::All,
|
|
|
|
(dirs1, VisitDirs::All) => dirs1,
|
|
|
|
(VisitDirs::All, dirs2) => dirs2,
|
|
|
|
(VisitDirs::Set(dirs1), VisitDirs::Set(dirs2)) => {
|
|
|
|
VisitDirs::Set(dirs1.intersection(&dirs2).cloned().collect())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let files = match (files1, files2) {
|
|
|
|
(VisitFiles::All, VisitFiles::All) => VisitFiles::All,
|
|
|
|
(files1, VisitFiles::All) => files1,
|
|
|
|
(VisitFiles::All, files2) => files2,
|
|
|
|
(VisitFiles::Set(files1), VisitFiles::Set(files2)) => {
|
|
|
|
VisitFiles::Set(files1.intersection(&files2).cloned().collect())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
match (&dirs, &files) {
|
|
|
|
(VisitDirs::Set(dirs), VisitFiles::Set(files))
|
|
|
|
if dirs.is_empty() && files.is_empty() =>
|
|
|
|
{
|
|
|
|
Visit::Nothing
|
|
|
|
}
|
|
|
|
_ => Visit::Specific { dirs, files },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
/// Keeps track of which subdirectories and files of each directory need to be
|
|
|
|
/// visited.
|
|
|
|
#[derive(PartialEq, Eq, Debug)]
|
2022-12-29 09:31:51 +00:00
|
|
|
struct RepoPathTree {
|
2023-11-25 08:46:17 +00:00
|
|
|
entries: HashMap<RepoPathComponentBuf, RepoPathTree>,
|
2022-12-28 02:59:12 +00:00
|
|
|
// is_dir/is_file aren't exclusive, both can be set to true. If entries is not empty,
|
|
|
|
// is_dir should be set.
|
|
|
|
is_dir: bool,
|
|
|
|
is_file: bool,
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 09:31:51 +00:00
|
|
|
impl RepoPathTree {
|
2020-12-12 08:00:42 +00:00
|
|
|
fn new() -> Self {
|
2022-12-29 09:31:51 +00:00
|
|
|
RepoPathTree {
|
2022-12-28 02:59:12 +00:00
|
|
|
entries: HashMap::new(),
|
|
|
|
is_dir: false,
|
|
|
|
is_file: false,
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-29 09:31:51 +00:00
|
|
|
fn add(&mut self, dir: &RepoPath) -> &mut RepoPathTree {
|
2023-11-25 10:09:58 +00:00
|
|
|
dir.components().fold(self, |sub, name| {
|
2022-12-28 02:59:12 +00:00
|
|
|
// Avoid name.clone() if entry already exists.
|
|
|
|
if !sub.entries.contains_key(name) {
|
|
|
|
sub.is_dir = true;
|
2023-11-25 10:09:58 +00:00
|
|
|
sub.entries.insert(name.to_owned(), RepoPathTree::new());
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
2022-12-28 02:59:12 +00:00
|
|
|
sub.entries.get_mut(name).unwrap()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_dir(&mut self, dir: &RepoPath) {
|
|
|
|
self.add(dir).is_dir = true;
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-05-17 04:55:51 +00:00
|
|
|
fn add_file(&mut self, file: &RepoPath) {
|
2022-12-28 02:59:12 +00:00
|
|
|
self.add(file).is_file = true;
|
|
|
|
}
|
|
|
|
|
2022-12-29 09:31:51 +00:00
|
|
|
fn get(&self, dir: &RepoPath) -> Option<&RepoPathTree> {
|
2022-12-28 02:59:12 +00:00
|
|
|
dir.components()
|
|
|
|
.try_fold(self, |sub, name| sub.entries.get(name))
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-12-28 08:44:01 +00:00
|
|
|
fn get_visit_sets(&self, dir: &RepoPath) -> Visit {
|
2022-12-28 02:59:12 +00:00
|
|
|
self.get(dir)
|
2022-12-29 09:31:51 +00:00
|
|
|
.map(RepoPathTree::to_visit_sets)
|
2022-12-28 08:44:01 +00:00
|
|
|
.unwrap_or(Visit::Nothing)
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2023-11-27 03:56:08 +00:00
|
|
|
/// Walks the tree from the root to the given `dir`, yielding each sub tree
|
|
|
|
/// and remaining path.
|
2023-11-25 10:09:58 +00:00
|
|
|
fn walk_to<'a, 'b: 'a>(
|
2022-12-28 06:20:17 +00:00
|
|
|
&'a self,
|
2023-11-25 10:09:58 +00:00
|
|
|
dir: &'b RepoPath,
|
2023-11-27 03:56:08 +00:00
|
|
|
) -> impl Iterator<Item = (&'a RepoPathTree, &'b RepoPath)> + 'a {
|
|
|
|
iter::successors(Some((self, dir)), |(sub, dir)| {
|
|
|
|
let mut components = dir.components();
|
2023-11-25 10:09:58 +00:00
|
|
|
let name = components.next()?;
|
2023-11-27 03:56:08 +00:00
|
|
|
Some((sub.entries.get(name)?, components.as_path()))
|
2023-11-25 10:09:58 +00:00
|
|
|
})
|
2022-12-28 06:20:17 +00:00
|
|
|
}
|
|
|
|
|
2022-12-28 08:44:01 +00:00
|
|
|
fn to_visit_sets(&self) -> Visit {
|
|
|
|
let mut dirs = HashSet::new();
|
|
|
|
let mut files = HashSet::new();
|
|
|
|
for (name, sub) in &self.entries {
|
|
|
|
if sub.is_dir {
|
|
|
|
dirs.insert(name.clone());
|
|
|
|
}
|
|
|
|
if sub.is_file {
|
|
|
|
files.insert(name.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Visit::sets(dirs, files)
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-09-22 04:52:04 +00:00
|
|
|
use maplit::hashset;
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
use super::*;
|
|
|
|
|
2023-11-26 10:21:46 +00:00
|
|
|
fn repo_path(value: &str) -> &RepoPath {
|
2023-11-26 08:38:34 +00:00
|
|
|
RepoPath::from_internal_string(value)
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
#[test]
|
2022-12-29 09:31:51 +00:00
|
|
|
fn test_repo_path_tree_empty() {
|
|
|
|
let tree = RepoPathTree::new();
|
2023-11-26 10:21:46 +00:00
|
|
|
assert_eq!(tree.get_visit_sets(RepoPath::root()), Visit::Nothing);
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-12-29 09:31:51 +00:00
|
|
|
fn test_repo_path_tree_root() {
|
|
|
|
let mut tree = RepoPathTree::new();
|
2023-11-26 10:21:46 +00:00
|
|
|
tree.add_dir(RepoPath::root());
|
|
|
|
assert_eq!(tree.get_visit_sets(RepoPath::root()), Visit::Nothing);
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-12-29 09:31:51 +00:00
|
|
|
fn test_repo_path_tree_dir() {
|
|
|
|
let mut tree = RepoPathTree::new();
|
2023-11-26 10:21:46 +00:00
|
|
|
tree.add_dir(repo_path("dir"));
|
2021-05-16 17:20:06 +00:00
|
|
|
assert_eq!(
|
2023-11-26 10:21:46 +00:00
|
|
|
tree.get_visit_sets(RepoPath::root()),
|
2023-11-25 08:46:17 +00:00
|
|
|
Visit::sets(hashset! {RepoPathComponentBuf::from("dir")}, hashset! {}),
|
2021-05-16 17:20:06 +00:00
|
|
|
);
|
2023-11-26 10:21:46 +00:00
|
|
|
tree.add_dir(repo_path("dir/sub"));
|
2022-12-28 02:59:12 +00:00
|
|
|
assert_eq!(
|
2023-11-26 10:21:46 +00:00
|
|
|
tree.get_visit_sets(repo_path("dir")),
|
2023-11-25 08:46:17 +00:00
|
|
|
Visit::sets(hashset! {RepoPathComponentBuf::from("sub")}, hashset! {}),
|
2022-12-28 02:59:12 +00:00
|
|
|
);
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-12-29 09:31:51 +00:00
|
|
|
fn test_repo_path_tree_file() {
|
|
|
|
let mut tree = RepoPathTree::new();
|
2023-11-26 10:21:46 +00:00
|
|
|
tree.add_file(repo_path("dir/file"));
|
2021-05-16 17:20:06 +00:00
|
|
|
assert_eq!(
|
2023-11-26 10:21:46 +00:00
|
|
|
tree.get_visit_sets(RepoPath::root()),
|
2023-11-25 08:46:17 +00:00
|
|
|
Visit::sets(hashset! {RepoPathComponentBuf::from("dir")}, hashset! {}),
|
2022-12-28 02:59:12 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-11-26 10:21:46 +00:00
|
|
|
tree.get_visit_sets(repo_path("dir")),
|
2023-11-25 08:46:17 +00:00
|
|
|
Visit::sets(hashset! {}, hashset! {RepoPathComponentBuf::from("file")}),
|
2022-12-28 02:59:12 +00:00
|
|
|
);
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-02-12 17:39:03 +00:00
|
|
|
#[test]
|
|
|
|
fn test_nothingmatcher() {
|
|
|
|
let m = NothingMatcher;
|
2023-11-26 10:21:46 +00:00
|
|
|
assert!(!m.matches(repo_path("file")));
|
|
|
|
assert!(!m.matches(repo_path("dir/file")));
|
|
|
|
assert_eq!(m.visit(RepoPath::root()), Visit::Nothing);
|
2022-02-12 17:39:03 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
#[test]
|
2021-05-17 04:21:45 +00:00
|
|
|
fn test_filesmatcher_empty() {
|
2023-11-26 08:42:12 +00:00
|
|
|
let m = FilesMatcher::new([] as [&RepoPath; 0]);
|
2023-11-26 10:21:46 +00:00
|
|
|
assert!(!m.matches(repo_path("file")));
|
|
|
|
assert!(!m.matches(repo_path("dir/file")));
|
|
|
|
assert_eq!(m.visit(RepoPath::root()), Visit::Nothing);
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-05-17 04:21:45 +00:00
|
|
|
fn test_filesmatcher_nonempty() {
|
2023-11-26 08:42:12 +00:00
|
|
|
let m = FilesMatcher::new([
|
2023-11-26 08:38:34 +00:00
|
|
|
repo_path("dir1/subdir1/file1"),
|
|
|
|
repo_path("dir1/subdir1/file2"),
|
|
|
|
repo_path("dir1/subdir2/file3"),
|
|
|
|
repo_path("file4"),
|
2022-12-28 07:34:24 +00:00
|
|
|
]);
|
2021-05-16 17:20:06 +00:00
|
|
|
|
2023-11-26 10:21:46 +00:00
|
|
|
assert!(!m.matches(repo_path("dir1")));
|
|
|
|
assert!(!m.matches(repo_path("dir1/subdir1")));
|
|
|
|
assert!(m.matches(repo_path("dir1/subdir1/file1")));
|
|
|
|
assert!(m.matches(repo_path("dir1/subdir1/file2")));
|
|
|
|
assert!(!m.matches(repo_path("dir1/subdir1/file3")));
|
2022-12-28 06:02:10 +00:00
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
assert_eq!(
|
2023-11-26 10:21:46 +00:00
|
|
|
m.visit(RepoPath::root()),
|
2022-05-22 05:31:37 +00:00
|
|
|
Visit::sets(
|
2023-11-25 08:46:17 +00:00
|
|
|
hashset! {RepoPathComponentBuf::from("dir1")},
|
|
|
|
hashset! {RepoPathComponentBuf::from("file4")}
|
2022-05-22 05:31:37 +00:00
|
|
|
)
|
2021-05-16 17:20:06 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-11-26 10:21:46 +00:00
|
|
|
m.visit(repo_path("dir1")),
|
2022-05-22 05:31:37 +00:00
|
|
|
Visit::sets(
|
2023-11-25 08:46:17 +00:00
|
|
|
hashset! {
|
|
|
|
RepoPathComponentBuf::from("subdir1"),
|
|
|
|
RepoPathComponentBuf::from("subdir2"),
|
|
|
|
},
|
2022-05-22 05:31:37 +00:00
|
|
|
hashset! {}
|
|
|
|
)
|
2021-05-16 17:20:06 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-11-26 10:21:46 +00:00
|
|
|
m.visit(repo_path("dir1/subdir1")),
|
2022-05-22 05:31:37 +00:00
|
|
|
Visit::sets(
|
|
|
|
hashset! {},
|
2023-11-25 08:46:17 +00:00
|
|
|
hashset! {
|
|
|
|
RepoPathComponentBuf::from("file1"),
|
|
|
|
RepoPathComponentBuf::from("file2"),
|
|
|
|
},
|
2022-05-22 05:31:37 +00:00
|
|
|
)
|
2021-05-16 17:20:06 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-11-26 10:21:46 +00:00
|
|
|
m.visit(repo_path("dir1/subdir2")),
|
2023-11-25 08:46:17 +00:00
|
|
|
Visit::sets(hashset! {}, hashset! {RepoPathComponentBuf::from("file3")})
|
2020-12-12 08:00:42 +00:00
|
|
|
);
|
|
|
|
}
|
2021-05-17 04:21:45 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_prefixmatcher_empty() {
|
2023-11-26 08:42:12 +00:00
|
|
|
let m = PrefixMatcher::new([] as [&RepoPath; 0]);
|
2023-11-26 10:21:46 +00:00
|
|
|
assert!(!m.matches(repo_path("file")));
|
|
|
|
assert!(!m.matches(repo_path("dir/file")));
|
|
|
|
assert_eq!(m.visit(RepoPath::root()), Visit::Nothing);
|
2021-05-17 04:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_prefixmatcher_root() {
|
2023-11-26 08:42:12 +00:00
|
|
|
let m = PrefixMatcher::new([RepoPath::root()]);
|
2021-05-17 04:21:45 +00:00
|
|
|
// Matches all files
|
2023-11-26 10:21:46 +00:00
|
|
|
assert!(m.matches(repo_path("file")));
|
|
|
|
assert!(m.matches(repo_path("dir/file")));
|
2021-05-17 04:21:45 +00:00
|
|
|
// Visits all directories
|
2023-11-26 10:21:46 +00:00
|
|
|
assert_eq!(m.visit(RepoPath::root()), Visit::AllRecursively);
|
|
|
|
assert_eq!(m.visit(repo_path("foo/bar")), Visit::AllRecursively);
|
2021-05-17 04:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_prefixmatcher_single_prefix() {
|
2023-11-26 08:42:12 +00:00
|
|
|
let m = PrefixMatcher::new([repo_path("foo/bar")]);
|
2021-05-17 04:21:45 +00:00
|
|
|
|
|
|
|
// Parts of the prefix should not match
|
2023-11-26 10:21:46 +00:00
|
|
|
assert!(!m.matches(repo_path("foo")));
|
|
|
|
assert!(!m.matches(repo_path("bar")));
|
2021-05-17 04:21:45 +00:00
|
|
|
// A file matching the prefix exactly should match
|
2023-11-26 10:21:46 +00:00
|
|
|
assert!(m.matches(repo_path("foo/bar")));
|
2021-05-17 04:21:45 +00:00
|
|
|
// Files in subdirectories should match
|
2023-11-26 10:21:46 +00:00
|
|
|
assert!(m.matches(repo_path("foo/bar/baz")));
|
|
|
|
assert!(m.matches(repo_path("foo/bar/baz/qux")));
|
2021-05-17 04:21:45 +00:00
|
|
|
// Sibling files should not match
|
2023-11-26 10:21:46 +00:00
|
|
|
assert!(!m.matches(repo_path("foo/foo")));
|
2021-05-17 04:21:45 +00:00
|
|
|
// An unrooted "foo/bar" should not match
|
2023-11-26 10:21:46 +00:00
|
|
|
assert!(!m.matches(repo_path("bar/foo/bar")));
|
2021-05-17 04:21:45 +00:00
|
|
|
|
|
|
|
// The matcher should only visit directory foo/ in the root (file "foo"
|
|
|
|
// shouldn't be visited)
|
|
|
|
assert_eq!(
|
2023-11-26 10:21:46 +00:00
|
|
|
m.visit(RepoPath::root()),
|
2023-11-25 08:46:17 +00:00
|
|
|
Visit::sets(hashset! {RepoPathComponentBuf::from("foo")}, hashset! {})
|
2021-05-17 04:21:45 +00:00
|
|
|
);
|
|
|
|
// Inside parent directory "foo/", both subdirectory "bar" and file "bar" may
|
|
|
|
// match
|
|
|
|
assert_eq!(
|
2023-11-26 10:21:46 +00:00
|
|
|
m.visit(repo_path("foo")),
|
2022-05-22 05:31:37 +00:00
|
|
|
Visit::sets(
|
2023-11-25 08:46:17 +00:00
|
|
|
hashset! {RepoPathComponentBuf::from("bar")},
|
|
|
|
hashset! {RepoPathComponentBuf::from("bar")}
|
2022-05-22 05:31:37 +00:00
|
|
|
)
|
2021-05-17 04:21:45 +00:00
|
|
|
);
|
2022-02-14 04:05:17 +00:00
|
|
|
// Inside a directory that matches the prefix, everything matches recursively
|
2023-11-26 10:21:46 +00:00
|
|
|
assert_eq!(m.visit(repo_path("foo/bar")), Visit::AllRecursively);
|
2021-05-17 04:21:45 +00:00
|
|
|
// Same thing in subdirectories of the prefix
|
2023-11-26 10:21:46 +00:00
|
|
|
assert_eq!(m.visit(repo_path("foo/bar/baz")), Visit::AllRecursively);
|
2021-05-17 04:21:45 +00:00
|
|
|
// Nothing in directories that are siblings of the prefix can match, so don't
|
|
|
|
// visit
|
2023-11-26 10:21:46 +00:00
|
|
|
assert_eq!(m.visit(repo_path("bar")), Visit::Nothing);
|
2021-05-17 04:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_prefixmatcher_nested_prefixes() {
|
2023-11-26 08:42:12 +00:00
|
|
|
let m = PrefixMatcher::new([repo_path("foo"), repo_path("foo/bar/baz")]);
|
2021-05-17 04:21:45 +00:00
|
|
|
|
2023-11-26 10:21:46 +00:00
|
|
|
assert!(m.matches(repo_path("foo")));
|
|
|
|
assert!(!m.matches(repo_path("bar")));
|
|
|
|
assert!(m.matches(repo_path("foo/bar")));
|
2022-09-09 17:32:38 +00:00
|
|
|
// Matches because the "foo" pattern matches
|
2023-11-26 10:21:46 +00:00
|
|
|
assert!(m.matches(repo_path("foo/baz/foo")));
|
2021-05-17 04:21:45 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2023-11-26 10:21:46 +00:00
|
|
|
m.visit(RepoPath::root()),
|
2022-05-22 05:31:37 +00:00
|
|
|
Visit::sets(
|
2023-11-25 08:46:17 +00:00
|
|
|
hashset! {RepoPathComponentBuf::from("foo")},
|
|
|
|
hashset! {RepoPathComponentBuf::from("foo")}
|
2022-05-22 05:31:37 +00:00
|
|
|
)
|
2021-05-17 04:21:45 +00:00
|
|
|
);
|
2022-02-14 04:05:17 +00:00
|
|
|
// Inside a directory that matches the prefix, everything matches recursively
|
2023-11-26 10:21:46 +00:00
|
|
|
assert_eq!(m.visit(repo_path("foo")), Visit::AllRecursively);
|
2021-05-17 04:21:45 +00:00
|
|
|
// Same thing in subdirectories of the prefix
|
2023-11-26 10:21:46 +00:00
|
|
|
assert_eq!(m.visit(repo_path("foo/bar/baz")), Visit::AllRecursively);
|
2021-05-17 04:21:45 +00:00
|
|
|
}
|
2022-04-17 04:43:35 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_differencematcher_remove_subdir() {
|
2023-11-26 08:42:12 +00:00
|
|
|
let m1 = PrefixMatcher::new([repo_path("foo"), repo_path("bar")]);
|
|
|
|
let m2 = PrefixMatcher::new([repo_path("foo/bar")]);
|
2022-04-17 04:43:35 +00:00
|
|
|
let m = DifferenceMatcher::new(&m1, &m2);
|
|
|
|
|
2023-11-26 10:21:46 +00:00
|
|
|
assert!(m.matches(repo_path("foo")));
|
|
|
|
assert!(!m.matches(repo_path("foo/bar")));
|
|
|
|
assert!(!m.matches(repo_path("foo/bar/baz")));
|
|
|
|
assert!(m.matches(repo_path("foo/baz")));
|
|
|
|
assert!(m.matches(repo_path("bar")));
|
2022-04-17 04:43:35 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2023-11-26 10:21:46 +00:00
|
|
|
m.visit(RepoPath::root()),
|
2022-05-22 05:31:37 +00:00
|
|
|
Visit::sets(
|
2023-11-25 08:46:17 +00:00
|
|
|
hashset! {
|
|
|
|
RepoPathComponentBuf::from("foo"),
|
|
|
|
RepoPathComponentBuf::from("bar"),
|
|
|
|
},
|
|
|
|
hashset! {
|
|
|
|
RepoPathComponentBuf::from("foo"),
|
|
|
|
RepoPathComponentBuf::from("bar"),
|
|
|
|
},
|
2022-05-22 05:31:37 +00:00
|
|
|
)
|
2022-04-17 04:43:35 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-11-26 10:21:46 +00:00
|
|
|
m.visit(repo_path("foo")),
|
2022-04-17 04:43:35 +00:00
|
|
|
Visit::Specific {
|
|
|
|
dirs: VisitDirs::All,
|
|
|
|
files: VisitFiles::All,
|
|
|
|
}
|
|
|
|
);
|
2023-11-26 10:21:46 +00:00
|
|
|
assert_eq!(m.visit(repo_path("foo/bar")), Visit::Nothing);
|
|
|
|
assert_eq!(m.visit(repo_path("foo/baz")), Visit::AllRecursively);
|
|
|
|
assert_eq!(m.visit(repo_path("bar")), Visit::AllRecursively);
|
2022-04-17 04:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_differencematcher_shared_patterns() {
|
2023-11-26 08:42:12 +00:00
|
|
|
let m1 = PrefixMatcher::new([repo_path("foo"), repo_path("bar")]);
|
|
|
|
let m2 = PrefixMatcher::new([repo_path("foo")]);
|
2022-04-17 04:43:35 +00:00
|
|
|
let m = DifferenceMatcher::new(&m1, &m2);
|
|
|
|
|
2023-11-26 10:21:46 +00:00
|
|
|
assert!(!m.matches(repo_path("foo")));
|
|
|
|
assert!(!m.matches(repo_path("foo/bar")));
|
|
|
|
assert!(m.matches(repo_path("bar")));
|
|
|
|
assert!(m.matches(repo_path("bar/foo")));
|
2022-04-17 04:43:35 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2023-11-26 10:21:46 +00:00
|
|
|
m.visit(RepoPath::root()),
|
2022-05-22 05:31:37 +00:00
|
|
|
Visit::sets(
|
2023-11-25 08:46:17 +00:00
|
|
|
hashset! {
|
|
|
|
RepoPathComponentBuf::from("foo"),
|
|
|
|
RepoPathComponentBuf::from("bar"),
|
|
|
|
},
|
|
|
|
hashset! {
|
|
|
|
RepoPathComponentBuf::from("foo"),
|
|
|
|
RepoPathComponentBuf::from("bar"),
|
|
|
|
},
|
2022-05-22 05:31:37 +00:00
|
|
|
)
|
2022-04-17 04:43:35 +00:00
|
|
|
);
|
2023-11-26 10:21:46 +00:00
|
|
|
assert_eq!(m.visit(repo_path("foo")), Visit::Nothing);
|
|
|
|
assert_eq!(m.visit(repo_path("foo/bar")), Visit::Nothing);
|
|
|
|
assert_eq!(m.visit(repo_path("bar")), Visit::AllRecursively);
|
|
|
|
assert_eq!(m.visit(repo_path("bar/foo")), Visit::AllRecursively);
|
2022-04-17 04:43:35 +00:00
|
|
|
}
|
2022-05-22 05:10:35 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_intersectionmatcher_intersecting_roots() {
|
2023-11-26 08:42:12 +00:00
|
|
|
let m1 = PrefixMatcher::new([repo_path("foo"), repo_path("bar")]);
|
|
|
|
let m2 = PrefixMatcher::new([repo_path("bar"), repo_path("baz")]);
|
2022-05-22 05:10:35 +00:00
|
|
|
let m = IntersectionMatcher::new(&m1, &m2);
|
|
|
|
|
2023-11-26 10:21:46 +00:00
|
|
|
assert!(!m.matches(repo_path("foo")));
|
|
|
|
assert!(!m.matches(repo_path("foo/bar")));
|
|
|
|
assert!(m.matches(repo_path("bar")));
|
|
|
|
assert!(m.matches(repo_path("bar/foo")));
|
|
|
|
assert!(!m.matches(repo_path("baz")));
|
|
|
|
assert!(!m.matches(repo_path("baz/foo")));
|
2022-05-22 05:10:35 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2023-11-26 10:21:46 +00:00
|
|
|
m.visit(RepoPath::root()),
|
2022-05-22 05:10:35 +00:00
|
|
|
Visit::sets(
|
2023-11-25 08:46:17 +00:00
|
|
|
hashset! {RepoPathComponentBuf::from("bar")},
|
|
|
|
hashset! {RepoPathComponentBuf::from("bar")}
|
2022-05-22 05:10:35 +00:00
|
|
|
)
|
|
|
|
);
|
2023-11-26 10:21:46 +00:00
|
|
|
assert_eq!(m.visit(repo_path("foo")), Visit::Nothing);
|
|
|
|
assert_eq!(m.visit(repo_path("foo/bar")), Visit::Nothing);
|
|
|
|
assert_eq!(m.visit(repo_path("bar")), Visit::AllRecursively);
|
|
|
|
assert_eq!(m.visit(repo_path("bar/foo")), Visit::AllRecursively);
|
|
|
|
assert_eq!(m.visit(repo_path("baz")), Visit::Nothing);
|
|
|
|
assert_eq!(m.visit(repo_path("baz/foo")), Visit::Nothing);
|
2022-05-22 05:10:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_intersectionmatcher_subdir() {
|
2023-11-26 08:42:12 +00:00
|
|
|
let m1 = PrefixMatcher::new([repo_path("foo")]);
|
|
|
|
let m2 = PrefixMatcher::new([repo_path("foo/bar")]);
|
2022-05-22 05:10:35 +00:00
|
|
|
let m = IntersectionMatcher::new(&m1, &m2);
|
|
|
|
|
2023-11-26 10:21:46 +00:00
|
|
|
assert!(!m.matches(repo_path("foo")));
|
|
|
|
assert!(!m.matches(repo_path("bar")));
|
|
|
|
assert!(m.matches(repo_path("foo/bar")));
|
|
|
|
assert!(m.matches(repo_path("foo/bar/baz")));
|
|
|
|
assert!(!m.matches(repo_path("foo/baz")));
|
2022-05-22 05:10:35 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
2023-11-26 10:21:46 +00:00
|
|
|
m.visit(RepoPath::root()),
|
2023-11-25 08:46:17 +00:00
|
|
|
Visit::sets(hashset! {RepoPathComponentBuf::from("foo")}, hashset! {})
|
2022-05-22 05:10:35 +00:00
|
|
|
);
|
2023-11-26 10:21:46 +00:00
|
|
|
assert_eq!(m.visit(repo_path("bar")), Visit::Nothing);
|
2022-05-22 05:10:35 +00:00
|
|
|
assert_eq!(
|
2023-11-26 10:21:46 +00:00
|
|
|
m.visit(repo_path("foo")),
|
2022-05-22 05:10:35 +00:00
|
|
|
Visit::sets(
|
2023-11-25 08:46:17 +00:00
|
|
|
hashset! {RepoPathComponentBuf::from("bar")},
|
|
|
|
hashset! {RepoPathComponentBuf::from("bar")}
|
2022-05-22 05:10:35 +00:00
|
|
|
)
|
|
|
|
);
|
2023-11-26 10:21:46 +00:00
|
|
|
assert_eq!(m.visit(repo_path("foo/bar")), Visit::AllRecursively);
|
2022-05-22 05:10:35 +00:00
|
|
|
}
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|