Skip to content

Commit

Permalink
cli: add --no-ignore-files flag
Browse files Browse the repository at this point in the history
The purpose of this flag is to force ripgrep to ignore all --ignore-file
flags (whether they come before or after --no-ignore-files).

This flag can be overridden with --ignore-files.

Fixes #1466
  • Loading branch information
BurntSushi committed Mar 15, 2020
1 parent e7b177c commit 34a01a7
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Feature enhancements:
Add `--no-require-git` flag to allow ripgrep to respect gitignores anywhere.
* [FEATURE #1420](https://github.com/BurntSushi/ripgrep/pull/1420):
Add `--no-ignore-exclude` to disregard rules in `.git/info/exclude` files.
* [FEATURE #1466](https://github.com/BurntSushi/ripgrep/pull/1466):
Add `--no-ignore-files` flag to disable all `--ignore-file` flags.
* [FEATURE #1488](https://github.com/BurntSushi/ripgrep/pull/1488):
Add '--engine' flag for easier switching between regex engines.
* [FEATURE 75cbe88f](https://github.com/BurntSushi/ripgrep/commit/75cbe88f):
Expand Down
6 changes: 5 additions & 1 deletion complete/_rg
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,14 @@ _rg() {
"--no-require-git[don't require git repository to respect gitignore rules]"
$no'--require-git[require git repository to respect gitignore rules]'

+ '(ignore-dot)' # .ignore-file options
+ '(ignore-dot)' # .ignore options
"--no-ignore-dot[don't respect .ignore files]"
$no'--ignore-dot[respect .ignore files]'

+ '(ignore-files)' # custom global ignore file options
"--no-ignore-files[don't respect --ignore-file flags]"
$no'--ignore-files[respect --ignore-file files]'

+ '(json)' # JSON options
'--json[output results in JSON Lines format]'
$no"--no-json[don't output results in JSON Lines format]"
Expand Down
28 changes: 27 additions & 1 deletion crates/core/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ pub fn all_args_and_flags() -> Vec<RGArg> {
flag_no_ignore(&mut args);
flag_no_ignore_dot(&mut args);
flag_no_ignore_exclude(&mut args);
flag_no_ignore_files(&mut args);
flag_no_ignore_global(&mut args);
flag_no_ignore_messages(&mut args);
flag_no_ignore_parent(&mut args);
Expand Down Expand Up @@ -1955,7 +1956,11 @@ fn flag_no_ignore(args: &mut Vec<RGArg>) {
const LONG: &str = long!(
"\
Don't respect ignore files (.gitignore, .ignore, etc.). This implies
--no-ignore-parent, --no-ignore-dot and --no-ignore-vcs.
--no-ignore-dot, --no-ignore-exclude, --no-ignore-global, no-ignore-parent and
--no-ignore-vcs.
This does *not* imply --no-ignore-files, since --ignore-file is specified
explicitly as a command line argument.
This flag can be disabled with the --ignore flag.
"
Expand Down Expand Up @@ -2011,6 +2016,27 @@ This flag can be disabled with the --ignore-exclude flag.
args.push(arg);
}

fn flag_no_ignore_files(args: &mut Vec<RGArg>) {
const SHORT: &str = "Don't respect --ignore-file arguments.";
const LONG: &str = long!(
"\
When set, any --ignore-file flags, even ones that come after this flag, are
ignored.
This flag can be disabled with the --ignore-files flag.
"
);
let arg = RGArg::switch("no-ignore-files")
.help(SHORT)
.long_help(LONG)
.overrides("ignore-files");
args.push(arg);

let arg =
RGArg::switch("ignore-files").hidden().overrides("no-ignore-files");
args.push(arg);
}

fn flag_no_ignore_global(args: &mut Vec<RGArg>) {
const SHORT: &str = "Don't respect global ignore files.";
const LONG: &str = long!(
Expand Down
16 changes: 13 additions & 3 deletions crates/core/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,9 +862,11 @@ impl ArgMatches {
for path in &paths[1..] {
builder.add(path);
}
for path in self.ignore_paths() {
if let Some(err) = builder.add_ignore(path) {
ignore_message!("{}", err);
if !self.no_ignore_files() {
for path in self.ignore_paths() {
if let Some(err) = builder.add_ignore(path) {
ignore_message!("{}", err);
}
}
}
builder
Expand Down Expand Up @@ -1228,6 +1230,14 @@ impl ArgMatches {
self.is_present("no-ignore-exclude") || self.no_ignore()
}

/// Returns true if explicitly given ignore files should be ignored.
fn no_ignore_files(&self) -> bool {
// We don't look at no-ignore here because --no-ignore is explicitly
// documented to not override --ignore-file. We could change this, but
// it would be a fairly severe breaking change.
self.is_present("no-ignore-files")
}

/// Returns true if global ignore files should be ignored.
fn no_ignore_global(&self) -> bool {
self.is_present("no-ignore-global") || self.no_ignore()
Expand Down
24 changes: 23 additions & 1 deletion tests/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ rgtest!(f1414_no_require_git, |dir: Dir, mut cmd: TestCommand| {
});

// See: https://github.com/BurntSushi/ripgrep/pull/1420
rgtest!(f1420_no_ignore_dot, |dir: Dir, mut cmd: TestCommand| {
rgtest!(f1420_no_ignore_exclude, |dir: Dir, mut cmd: TestCommand| {
dir.create_dir(".git/info");
dir.create(".git/info/exclude", "foo");
dir.create("bar", "");
Expand All @@ -765,6 +765,28 @@ rgtest!(f1420_no_ignore_dot, |dir: Dir, mut cmd: TestCommand| {
eqnice!("bar\nfoo\n", cmd.arg("--no-ignore-exclude").stdout());
});

// See: https://github.com/BurntSushi/ripgrep/pull/1466
rgtest!(f1466_no_ignore_files, |dir: Dir, mut cmd: TestCommand| {
dir.create(".myignore", "bar");
dir.create("bar", "");
dir.create("foo", "");

// Test that --no-ignore-files disables --ignore-file.
// And that --ignore-files overrides --no-ignore-files.
cmd.arg("--sort").arg("path").arg("--files");
eqnice!("bar\nfoo\n", cmd.stdout());
eqnice!("foo\n", cmd.arg("--ignore-file").arg(".myignore").stdout());
eqnice!("bar\nfoo\n", cmd.arg("--no-ignore-files").stdout());
eqnice!("foo\n", cmd.arg("--ignore-files").stdout());

// Test that the -u flag does not disable --ignore-file.
let mut cmd = dir.command();
cmd.arg("--sort").arg("path").arg("--files");
cmd.arg("--ignore-file").arg(".myignore");
eqnice!("foo\n", cmd.stdout());
eqnice!("foo\n", cmd.arg("-u").stdout());
});

rgtest!(no_context_sep, |dir: Dir, mut cmd: TestCommand| {
dir.create("test", "foo\nctx\nbar\nctx\nfoo\nctx");
cmd.args(&["-A1", "--no-context-separator", "foo", "test"]);
Expand Down

0 comments on commit 34a01a7

Please sign in to comment.