1
0
mirror of https://github.com/BurntSushi/ripgrep.git synced 2025-03-17 20:28:03 +02:00

ripgrep: fix bug when CWD has directory named -

Specifically, when searching stdin, if the current directory has a
directory named `-`, then the `--with-filename` flag would automatically
be turned on. This is because `--with-filename` is automatically enabled
when ripgrep is given a single path that is a directory. When ripgrep is
given empty arguments, and if it is searching stdin, then its default
path list is just simple `["-"]`. The `is_dir` check passes, and
`--with-filename` gets enabled.

This commit fixes the problem by checking whether the path is `-` first.
If so, then we assume it isn't a directory. This is fine, since if it is
a directory and one asks to search it explicitly, then ripgrep will
interpret `-` as stdin anyway (which is arguably a bug on its own, but
probably not one worth fixing).

Fixes #1223, Closes #1292
This commit is contained in:
Ninan John 2019-06-05 21:03:52 +05:30 committed by Andrew Gallant
parent c2cb0a4de4
commit 9268ff8e8d
No known key found for this signature in database
GPG Key ID: B2E3A4923F8B0D44
2 changed files with 14 additions and 1 deletions

View File

@ -1594,10 +1594,11 @@ impl ArgMatches {
if self.is_present("no-filename") {
false
} else {
let path_stdin = Path::new("-");
self.is_present("with-filename")
|| self.is_present("vimgrep")
|| paths.len() > 1
|| paths.get(0).map_or(false, |p| p.is_dir())
|| paths.get(0).map_or(false, |p| p != path_stdin && p.is_dir())
}
}
}

View File

@ -706,6 +706,18 @@ rgtest!(r1203_reverse_suffix_literal, |dir: Dir, _: TestCommand| {
eqnice!("153.230000\n", cmd.arg(r"\d\d\d000").arg("test").stdout());
});
// See: https://github.com/BurntSushi/ripgrep/issues/1223
rgtest!(r1223_no_dir_check_for_default_path, |dir: Dir, mut cmd: TestCommand| {
dir.create_dir("-");
dir.create("a.json", "{}");
dir.create("a.txt", "some text");
eqnice!(
"a.json\na.txt\n",
sort_lines(&cmd.arg("a").pipe(b"a.json\na.txt"))
);
});
// See: https://github.com/BurntSushi/ripgrep/issues/1259
rgtest!(r1259_drop_last_byte_nonl, |dir: Dir, mut cmd: TestCommand| {
dir.create("patterns-nonl", "[foo]");