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

core: don't let context flags override eachother

This matches the behavior of GNU grep which does not ignore
before-context and after-context completely if the context flag is also
provided.

Note that this change wasn't done just to match GNU grep. In this case,
GNU grep has the more sensible behavior.

Fixes #2288, Closes #2451
This commit is contained in:
Andrew Gallant 2023-07-07 10:51:51 -04:00
parent 54e609d657
commit d675844510
4 changed files with 34 additions and 14 deletions

View File

@ -2,6 +2,12 @@ TBD
=== ===
Unreleased changes. Release notes have not yet been written. Unreleased changes. Release notes have not yet been written.
**BREAKING CHANGES**
* `rg -C1 -A2` used to be equivalent to `rg -A2`, but now it is equivalent to
`rg -B1 -A2`. That is, `-A` and `-B` no longer completely override `-C`.
Instead, they only partially override `-C`.
Feature enhancements: Feature enhancements:
* Added or improved file type filtering for Fuchsia, GraphQL * Added or improved file type filtering for Fuchsia, GraphQL
@ -12,6 +18,8 @@ Bug fixes:
Fix bug when using `-w` with a regex that can match the empty string. Fix bug when using `-w` with a regex that can match the empty string.
* [BUG #1911](https://github.com/BurntSushi/ripgrep/issues/1911): * [BUG #1911](https://github.com/BurntSushi/ripgrep/issues/1911):
Disable mmap searching in all non-64-bit environments. Disable mmap searching in all non-64-bit environments.
* [BUG #2288](https://github.com/BurntSushi/ripgrep/issues/2288):
`-A` and `-B` now only each partially override `-C`.
* [BUG #2236](https://github.com/BurntSushi/ripgrep/issues/2236): * [BUG #2236](https://github.com/BurntSushi/ripgrep/issues/2236):
Fix gitignore parsing bug where a trailing `\/` resulted in an error. Fix gitignore parsing bug where a trailing `\/` resulted in an error.
* [BUG #2480](https://github.com/BurntSushi/ripgrep/issues/2480): * [BUG #2480](https://github.com/BurntSushi/ripgrep/issues/2480):

View File

@ -698,7 +698,7 @@ fn flag_after_context(args: &mut Vec<RGArg>) {
"\ "\
Show NUM lines after each match. Show NUM lines after each match.
This overrides the --context and --passthru flags. This overrides the --passthru flag and partially overrides --context.
" "
); );
let arg = RGArg::flag("after-context", "NUM") let arg = RGArg::flag("after-context", "NUM")
@ -706,8 +706,7 @@ This overrides the --context and --passthru flags.
.help(SHORT) .help(SHORT)
.long_help(LONG) .long_help(LONG)
.number() .number()
.overrides("passthru") .overrides("passthru");
.overrides("context");
args.push(arg); args.push(arg);
} }
@ -768,7 +767,7 @@ fn flag_before_context(args: &mut Vec<RGArg>) {
"\ "\
Show NUM lines before each match. Show NUM lines before each match.
This overrides the --context and --passthru flags. This overrides the --passthru flag and partially overrides --context.
" "
); );
let arg = RGArg::flag("before-context", "NUM") let arg = RGArg::flag("before-context", "NUM")
@ -776,8 +775,7 @@ This overrides the --context and --passthru flags.
.help(SHORT) .help(SHORT)
.long_help(LONG) .long_help(LONG)
.number() .number()
.overrides("passthru") .overrides("passthru");
.overrides("context");
args.push(arg); args.push(arg);
} }
@ -1009,8 +1007,7 @@ fn flag_context(args: &mut Vec<RGArg>) {
Show NUM lines before and after each match. This is equivalent to providing Show NUM lines before and after each match. This is equivalent to providing
both the -B/--before-context and -A/--after-context flags with the same value. both the -B/--before-context and -A/--after-context flags with the same value.
This overrides both the -B/--before-context and -A/--after-context flags, This overrides the --passthru flag.
in addition to the --passthru flag.
" "
); );
let arg = RGArg::flag("context", "NUM") let arg = RGArg::flag("context", "NUM")
@ -1018,9 +1015,7 @@ in addition to the --passthru flag.
.help(SHORT) .help(SHORT)
.long_help(LONG) .long_help(LONG)
.number() .number()
.overrides("passthru") .overrides("passthru");
.overrides("before-context")
.overrides("after-context");
args.push(arg); args.push(arg);
} }

View File

@ -992,10 +992,10 @@ impl ArgMatches {
/// If there was a problem parsing the values from the user as an integer, /// If there was a problem parsing the values from the user as an integer,
/// then an error is returned. /// then an error is returned.
fn contexts(&self) -> Result<(usize, usize)> { fn contexts(&self) -> Result<(usize, usize)> {
let after = self.usize_of("after-context")?.unwrap_or(0);
let before = self.usize_of("before-context")?.unwrap_or(0);
let both = self.usize_of("context")?.unwrap_or(0); let both = self.usize_of("context")?.unwrap_or(0);
Ok(if both > 0 { (both, both) } else { (before, after) }) let after = self.usize_of("after-context")?.unwrap_or(both);
let before = self.usize_of("before-context")?.unwrap_or(both);
Ok((before, after))
} }
/// Returns the unescaped context separator in UTF-8 bytes. /// Returns the unescaped context separator in UTF-8 bytes.

View File

@ -921,6 +921,23 @@ rgtest!(f1842_field_match_separator, |dir: Dir, _: TestCommand| {
eqnice!(expected, dir.command().args(&args).stdout()); eqnice!(expected, dir.command().args(&args).stdout());
}); });
// See: https://github.com/BurntSushi/ripgrep/issues/2288
rgtest!(f2288_context_partial_override, |dir: Dir, mut cmd: TestCommand| {
dir.create("test", "1\n2\n3\n4\n5\n6\n7\n8\n9\n");
cmd.args(&["-C1", "-A2", "5", "test"]);
eqnice!("4\n5\n6\n7\n", cmd.stdout());
});
// See: https://github.com/BurntSushi/ripgrep/issues/2288
rgtest!(
f2288_context_partial_override_rev,
|dir: Dir, mut cmd: TestCommand| {
dir.create("test", "1\n2\n3\n4\n5\n6\n7\n8\n9\n");
cmd.args(&["-A2", "-C1", "5", "test"]);
eqnice!("4\n5\n6\n7\n", cmd.stdout());
}
);
rgtest!(no_context_sep, |dir: Dir, mut cmd: TestCommand| { rgtest!(no_context_sep, |dir: Dir, mut cmd: TestCommand| {
dir.create("test", "foo\nctx\nbar\nctx\nfoo\nctx"); dir.create("test", "foo\nctx\nbar\nctx\nfoo\nctx");
cmd.args(&["-A1", "--no-context-separator", "foo", "test"]); cmd.args(&["-A1", "--no-context-separator", "foo", "test"]);