pub struct IgnoreMatcher {
pub warnings: Vec<String>,
/* private fields */
}Expand description
Engine used to evaluate ignore patterns against repository-relative paths.
Fields§
§warnings: Vec<String>Warnings emitted while loading in-tree .gitignore (e.g. symlink paths).
Implementations§
Source§impl IgnoreMatcher
impl IgnoreMatcher
Sourcepub fn from_repository(repo: &Repository) -> Result<Self>
pub fn from_repository(repo: &Repository) -> Result<Self>
Build a matcher from repository exclude sources.
§Parameters
repo- open repository.
§Errors
Returns Error::Io if configured pattern files cannot be read.
Examples found in repository?
9fn main() -> grit_lib::error::Result<()> {
10 let root = tempfile::tempdir()?;
11 let repo = init_repository(root.path(), false, "main", None, "files")?;
12
13 let wt = repo.work_tree.as_ref().expect("non-bare");
14 fs::write(wt.join(".gitignore"), "*.log\n")?;
15
16 let mut matcher = IgnoreMatcher::from_repository(&repo)?;
17 let (ignored_log, m_log) = matcher.check_path(&repo, None, "build.log", false)?;
18 let (ignored_txt, m_txt) = matcher.check_path(&repo, None, "readme.txt", false)?;
19
20 println!("build.log ignored={ignored_log} ({m_log:?})");
21 println!("readme.txt ignored={ignored_txt} ({m_txt:?})");
22
23 Ok(())
24}Sourcepub fn add_exclude_from_files(
&mut self,
paths: &[PathBuf],
cwd: &Path,
) -> Result<()>
pub fn add_exclude_from_files( &mut self, paths: &[PathBuf], cwd: &Path, ) -> Result<()>
Append patterns from ls-files --exclude-from / -X files (relative paths resolve from cwd).
Matches Git’s EXC_FILE lists loaded after core.excludesfile and .git/info/exclude.
Sourcepub fn add_cli_excludes(&mut self, patterns: &[String])
pub fn add_cli_excludes(&mut self, patterns: &[String])
Append patterns from ls-files --exclude / -x (Git command-line exclude list).
Sourcepub fn take_warnings(&mut self) -> Vec<String>
pub fn take_warnings(&mut self) -> Vec<String>
Take any warnings accumulated while loading ignore files (caller prints to stderr).
Sourcepub fn check_path(
&mut self,
repo: &Repository,
index: Option<&Index>,
repo_rel_path: &str,
is_dir: bool,
) -> Result<(bool, Option<IgnoreMatch>)>
pub fn check_path( &mut self, repo: &Repository, index: Option<&Index>, repo_rel_path: &str, is_dir: bool, ) -> Result<(bool, Option<IgnoreMatch>)>
Check whether a repository-relative path is ignored.
§Parameters
repo- repository handle.index- optional index; when present, tracked entries are not ignored.repo_rel_path- normalized repository-relative path with/separators.is_dir- whether the queried path is a directory.
§Returns
Tuple (ignored, match_info) where match_info is the last matching
pattern (including negated matches).
§Errors
Returns Error::Io when a relevant .gitignore cannot be read.
Examples found in repository?
9fn main() -> grit_lib::error::Result<()> {
10 let root = tempfile::tempdir()?;
11 let repo = init_repository(root.path(), false, "main", None, "files")?;
12
13 let wt = repo.work_tree.as_ref().expect("non-bare");
14 fs::write(wt.join(".gitignore"), "*.log\n")?;
15
16 let mut matcher = IgnoreMatcher::from_repository(&repo)?;
17 let (ignored_log, m_log) = matcher.check_path(&repo, None, "build.log", false)?;
18 let (ignored_txt, m_txt) = matcher.check_path(&repo, None, "readme.txt", false)?;
19
20 println!("build.log ignored={ignored_log} ({m_log:?})");
21 println!("readme.txt ignored={ignored_txt} ({m_txt:?})");
22
23 Ok(())
24}