ignore_files/lib.rs
1//! Find, parse, and interpret ignore files.
2//!
3//! Ignore files are files that contain ignore patterns, often following the `.gitignore` format.
4//! There may be one or more global ignore files, which apply everywhere, and one or more per-folder
5//! ignore files, which apply to a specific folder and its subfolders. Furthermore, there may be
6//! more ignore files in _these_ subfolders, and so on. Discovering and interpreting all of these in
7//! a single context is not a simple task: this is what this crate provides.
8//!
9//! Discovery and loading are explicit snapshots. This crate does not watch ignore files for edits or
10//! automatically rediscover files created later; call the discovery/loading APIs again, or update an
11//! unfinished [`IgnoreFilter`] with its mutation methods.
12
13#![cfg_attr(not(test), warn(unused_crate_dependencies))]
14
15use std::path::{Path, PathBuf};
16
17use normalize_path::NormalizePath;
18use project_origins::ProjectType;
19
20#[doc(inline)]
21pub use discover::*;
22mod discover;
23
24#[doc(inline)]
25pub use error::*;
26mod error;
27
28#[doc(inline)]
29pub use filter::*;
30mod filter;
31
32/// Directory names treated as version-control metadata.
33///
34/// Used internally for ignore-file discovery. Reuse this list when ignoring VCS metadata to
35/// automatically follow additions to the crate's discovery support.
36pub const VCS_DIR_NAMES: &[&str] = &[
37 ".bzr",
38 "_darcs",
39 ".fossil-settings",
40 ".git",
41 ".hg",
42 ".pijul",
43 ".svn",
44];
45
46/// An ignore file.
47///
48/// This records both the path to the ignore file and some basic metadata about it: which project
49/// type it applies to if any, and which subtree it applies in if any (`None` = global ignore file).
50#[derive(Debug, Clone, PartialEq, Eq, Hash)]
51pub struct IgnoreFile {
52 /// The path to the ignore file.
53 pub path: PathBuf,
54
55 /// The path to the subtree the ignore file applies to, or `None` for global ignores.
56 pub applies_in: Option<PathBuf>,
57
58 /// Which project type the ignore file applies to, or was found through.
59 pub applies_to: Option<ProjectType>,
60}
61
62pub(crate) fn simplify_path(path: &Path) -> PathBuf {
63 dunce::simplified(path).normalize()
64}