Skip to content

Commit

Permalink
Add --no-ignore-vcs flag.
Browse files Browse the repository at this point in the history
This flag will respect .ignore but not .gitignore.

Closes #68.
  • Loading branch information
BurntSushi committed Sep 25, 2016
1 parent 423f2a1 commit 8eeb0c0
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 6 deletions.
6 changes: 6 additions & 0 deletions doc/rg.1
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@ Don\[aq]t respect ignore files in parent directories.
.RS
.RE
.TP
.B \-\-no\-ignore\-vcs
Don\[aq]t respect version control ignore files (e.g., .gitignore).
Note that .ignore files will continue to be respected.
.RS
.RE
.TP
.B \-p, \-\-pretty
Alias for \-\-color=always \-\-heading \-n.
.RS
Expand Down
4 changes: 4 additions & 0 deletions doc/rg.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ the raw speed of grep.
--no-ignore-parent
: Don't respect ignore files in parent directories.

--no-ignore-vcs
: Don't respect version control ignore files (e.g., .gitignore).
Note that .ignore files will continue to be respected.

-p, --pretty
: Alias for --color=always --heading -n.

Expand Down
10 changes: 10 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ Less common options:
--no-ignore-parent
Don't respect ignore files in parent directories.
--no-ignore-vcs
Don't respect version control ignore files (e.g., .gitignore).
Note that .ignore files will continue to be respected.
-p, --pretty
Alias for --color=always --heading -n.
Expand Down Expand Up @@ -205,6 +209,7 @@ pub struct RawArgs {
flag_no_heading: bool,
flag_no_ignore: bool,
flag_no_ignore_parent: bool,
flag_no_ignore_vcs: bool,
flag_no_line_number: bool,
flag_no_mmap: bool,
flag_no_filename: bool,
Expand Down Expand Up @@ -250,6 +255,7 @@ pub struct Args {
mmap: bool,
no_ignore: bool,
no_ignore_parent: bool,
no_ignore_vcs: bool,
quiet: bool,
replace: Option<Vec<u8>>,
text: bool,
Expand Down Expand Up @@ -374,6 +380,9 @@ impl RawArgs {
no_ignore_parent:
// --no-ignore implies --no-ignore-parent
self.flag_no_ignore_parent || no_ignore,
no_ignore_vcs:
// --no-ignore implies --no-ignore-vcs
self.flag_no_ignore_vcs || no_ignore,
quiet: self.flag_quiet,
replace: self.flag_replace.clone().map(|s| s.into_bytes()),
text: text,
Expand Down Expand Up @@ -639,6 +648,7 @@ impl Args {
let mut ig = Ignore::new();
ig.ignore_hidden(!self.hidden);
ig.no_ignore(self.no_ignore);
ig.no_ignore_vcs(self.no_ignore_vcs);
ig.add_types(self.types.clone());
if !self.no_ignore_parent {
try!(ig.push_parents(path));
Expand Down
32 changes: 26 additions & 6 deletions src/ignore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,11 @@ pub struct Ignore {
types: Types,
/// Whether to ignore hidden files or not.
ignore_hidden: bool,
/// When true, don't look at .gitignore or .agignore files for ignore
/// When true, don't look at .gitignore or .ignore files for ignore
/// rules.
no_ignore: bool,
/// When true, don't look at .gitignore files for ignore rules.
no_ignore_vcs: bool,
}

impl Ignore {
Expand All @@ -104,6 +106,7 @@ impl Ignore {
types: Types::empty(),
ignore_hidden: true,
no_ignore: false,
no_ignore_vcs: true,
}
}

Expand All @@ -119,6 +122,12 @@ impl Ignore {
self
}

/// When set, VCS ignore files are ignored.
pub fn no_ignore_vcs(&mut self, yes: bool) -> &mut Ignore {
self.no_ignore_vcs = yes;
self
}

/// Add a set of globs that overrides all other match logic.
pub fn add_override(&mut self, gi: Gitignore) -> &mut Ignore {
self.overrides = Overrides::new(Some(gi));
Expand All @@ -143,6 +152,9 @@ impl Ignore {
let mut path = &*path;
let mut saw_git = path.join(".git").is_dir();
let mut ignore_names = IGNORE_NAMES.to_vec();
if self.no_ignore_vcs {
ignore_names.retain(|&name| name != ".gitignore");
}
let mut ignore_dir_results = vec![];
while let Some(parent) = path.parent() {
if self.no_ignore {
Expand Down Expand Up @@ -173,9 +185,12 @@ impl Ignore {
pub fn push<P: AsRef<Path>>(&mut self, path: P) -> Result<(), Error> {
if self.no_ignore {
self.stack.push(IgnoreDir::empty(path));
return Ok(());
Ok(())
} else if self.no_ignore_vcs {
self.push_ignore_dir(IgnoreDir::without_vcs(path))
} else {
self.push_ignore_dir(IgnoreDir::new(path))
}
self.push_ignore_dir(IgnoreDir::new(path))
}

/// Pushes the result of building a directory matcher on to the stack.
Expand Down Expand Up @@ -305,13 +320,18 @@ pub struct IgnoreDir {

impl IgnoreDir {
/// Create a new matcher for the given directory.
///
/// If no ignore glob patterns could be found in the directory then `None`
/// is returned.
pub fn new<P: AsRef<Path>>(path: P) -> Result<IgnoreDir, Error> {
IgnoreDir::with_ignore_names(path, IGNORE_NAMES.iter())
}

/// Create a new matcher for the given directory.
///
/// Don't respect VCS ignore files.
pub fn without_vcs<P: AsRef<Path>>(path: P) -> Result<IgnoreDir, Error> {
let names = IGNORE_NAMES.iter().filter(|name| **name != ".gitignore");
IgnoreDir::with_ignore_names(path, names)
}

/// Create a new IgnoreDir that never matches anything with the given path.
pub fn empty<P: AsRef<Path>>(path: P) -> IgnoreDir {
IgnoreDir {
Expand Down
12 changes: 12 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,18 @@ be, to a very large extent, the result of luck. Sherlock Holmes
assert_eq!(lines, expected);
});

// See: https://github.com/BurntSushi/ripgrep/issues/68
clean!(feature_68, "test", ".", |wd: WorkDir, mut cmd: Command| {
wd.create(".gitignore", "foo");
wd.create(".ignore", "bar");
wd.create("foo", "test");
wd.create("bar", "test");
cmd.arg("--no-ignore-vcs");

let lines: String = wd.stdout(&mut cmd);
assert_eq!(lines, "foo:test\n");
});

#[test]
fn binary_nosearch() {
let wd = WorkDir::new("binary_nosearch");
Expand Down

0 comments on commit 8eeb0c0

Please sign in to comment.