Skip to main content

hashtree_cli/
ignore_rules.rs

1use ignore::{Walk, WalkBuilder};
2use std::path::{Component, Path, PathBuf};
3
4const DEFAULT_JUNK_NAMES: &[&str] = &[
5    ".DS_Store",
6    "Thumbs.db",
7    "Desktop.ini",
8    ".Spotlight-V100",
9    ".Trashes",
10    ".TemporaryItems",
11    ".fseventsd",
12    ".apdisk",
13    "$RECYCLE.BIN",
14    "System Volume Information",
15];
16
17pub fn build_content_walker(root: &Path, respect_ignore_rules: bool) -> Walk {
18    let mut builder = WalkBuilder::new(root);
19    builder
20        .git_ignore(respect_ignore_rules)
21        .git_global(respect_ignore_rules)
22        .git_exclude(respect_ignore_rules)
23        .hidden(false);
24
25    if respect_ignore_rules {
26        let root = root.to_path_buf();
27        builder.filter_entry(move |entry| should_include_entry(entry.path(), &root));
28    }
29
30    builder.build()
31}
32
33fn should_include_entry(path: &Path, root: &PathBuf) -> bool {
34    path == root || !is_default_junk_path(path)
35}
36
37fn is_default_junk_path(path: &Path) -> bool {
38    path.components().any(|component| match component {
39        Component::Normal(name) => is_default_junk_name(&name.to_string_lossy()),
40        _ => false,
41    })
42}
43
44fn is_default_junk_name(name: &str) -> bool {
45    name.starts_with("._") || DEFAULT_JUNK_NAMES.contains(&name)
46}