Skip to content

Commit

Permalink
[ignore] Rename to matched_path_or_any_parents()
Browse files Browse the repository at this point in the history
  • Loading branch information
behnam committed Jul 13, 2017
1 parent b109f1e commit 2b95766
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 22 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: rust
env:
global:
- PROJECT_NAME=ripgrep
- RUST_BACKTRACE: full
matrix:
include:
# Nightly channel.
Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
environment:
global:
PROJECT_NAME: ripgrep
RUST_BACKTRACE: full
matrix:
- TARGET: i686-pc-windows-gnu
CHANNEL: stable
Expand Down
38 changes: 22 additions & 16 deletions ignore/src/gitignore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,13 @@ impl Gitignore {
self.matched_stripped(self.strip(path.as_ref()), is_dir)
}

/// Returns whether the given path (file or directory) or any of its parent
/// directories matched a pattern in this gitignore matcher.
/// Returns whether the given path (file or directory, and expected to be
/// under the root) or any of its parent directories (up to the root)
/// matched a pattern in this gitignore matcher.
///
/// NOTE: This method is more expensive than walking the directory hierarchy
/// top-to-bottom and matching the entries. But, it is useful in cases when
/// a list of paths are available without a hierarchy.
/// top-to-bottom and matching the entries. But, is easier to use in cases
/// when a list of paths are available without a hierarchy.
///
/// `is_dir` should be true if the path refers to a directory and false
/// otherwise.
Expand All @@ -206,23 +207,28 @@ impl Gitignore {
/// determined by a common suffix of the directory containing this
/// gitignore) is stripped. If there is no common suffix/prefix overlap,
/// then `path` is assumed to be relative to this matcher.
pub fn matched_recursive<P: AsRef<Path>>(&self, path: P, is_dir: bool) -> Match<&Glob> {
pub fn matched_path_or_any_parents<P: AsRef<Path>>(
&self,
path: P,
is_dir: bool,
) -> Match<&Glob> {
if self.is_empty() {
return Match::None;
}
let mut current_path = self.strip(path.as_ref());
match self.matched_stripped(current_path, is_dir) {
Match::None => {
while let Some(parent) = current_path.parent() {
match self.matched_stripped(parent, is_dir) {
Match::None => current_path = parent,
a_match => return a_match,
}
}
let mut path = self.strip(path.as_ref());
debug_assert!(
!path.has_root(),
"path is expect to be under the root"
);
loop {
match self.matched_stripped(path, is_dir) {
Match::None => match path.parent() {
Some(parent) => path = parent,
None => return Match::None,
},
a_match => return a_match,
}
a_match => return a_match,
}
Match::None
}

/// Like matched, but takes a path that has already been stripped.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,31 @@ use std::path::Path;
use ignore::gitignore::{Gitignore, GitignoreBuilder};


const IGNORE_FILE: &'static str = "tests/gitignore_recursive_tests.gitignore";
const IGNORE_FILE: &'static str = "tests/gitignore_matched_path_or_any_parents_tests.gitignore";


fn get_gitignore() -> Gitignore {
let mut builder = GitignoreBuilder::new("ROOT");
builder.add(IGNORE_FILE);
let error = builder.add(IGNORE_FILE);
assert!(error.is_none(), "failed to open gitignore file");
builder.build().unwrap()
}


#[test]
#[should_panic(expected = "path is expect to be under the root")]
fn test_path_should_be_under_root() {
let gitignore = get_gitignore();
let path = "/tmp/some_file";
gitignore.matched_path_or_any_parents(Path::new(path), false);
assert!(false);
}


#[test]
fn test_files_in_root() {
let gitignore = get_gitignore();
let m = |path: &str| gitignore.matched_recursive(Path::new(path), false);
let m = |path: &str| gitignore.matched_path_or_any_parents(Path::new(path), false);

// 0x
assert!(m("ROOT/file_root_00").is_ignore());
Expand Down Expand Up @@ -50,7 +61,7 @@ fn test_files_in_root() {
#[test]
fn test_files_in_deep() {
let gitignore = get_gitignore();
let m = |path: &str| gitignore.matched_recursive(Path::new(path), false);
let m = |path: &str| gitignore.matched_path_or_any_parents(Path::new(path), false);

// 0x
assert!(m("ROOT/parent_dir/file_deep_00").is_ignore());
Expand Down Expand Up @@ -81,7 +92,7 @@ fn test_files_in_deep() {
#[test]
fn test_dirs_in_root() {
let gitignore = get_gitignore();
let m = |path: &str| gitignore.matched_recursive(Path::new(path), true);
let m = |path: &str| gitignore.matched_path_or_any_parents(Path::new(path), true);

// 00
assert!(m("ROOT/dir_root_00").is_ignore());
Expand Down Expand Up @@ -168,7 +179,7 @@ fn test_dirs_in_root() {
#[test]
fn test_dirs_in_deep() {
let gitignore = get_gitignore();
let m = |path: &str| gitignore.matched_recursive(Path::new(path), true);
let m = |path: &str| gitignore.matched_path_or_any_parents(Path::new(path), true);

// 00
assert!(m("ROOT/parent_dir/dir_deep_00").is_ignore());
Expand Down

0 comments on commit 2b95766

Please sign in to comment.