Skip to content

Commit

Permalink
core: don't let context flags override eachother
Browse files Browse the repository at this point in the history
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
  • Loading branch information
BurntSushi committed Jul 8, 2023
1 parent 54e609d commit d675844
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ TBD
===
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:

* Added or improved file type filtering for Fuchsia, GraphQL
Expand All @@ -12,6 +18,8 @@ Bug fixes:
Fix bug when using `-w` with a regex that can match the empty string.
* [BUG #1911](https://github.com/BurntSushi/ripgrep/issues/1911):
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):
Fix gitignore parsing bug where a trailing `\/` resulted in an error.
* [BUG #2480](https://github.com/BurntSushi/ripgrep/issues/2480):
Expand Down
17 changes: 6 additions & 11 deletions crates/core/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,16 +698,15 @@ fn flag_after_context(args: &mut Vec<RGArg>) {
"\
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")
.short("A")
.help(SHORT)
.long_help(LONG)
.number()
.overrides("passthru")
.overrides("context");
.overrides("passthru");
args.push(arg);
}

Expand Down Expand Up @@ -768,16 +767,15 @@ fn flag_before_context(args: &mut Vec<RGArg>) {
"\
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")
.short("B")
.help(SHORT)
.long_help(LONG)
.number()
.overrides("passthru")
.overrides("context");
.overrides("passthru");
args.push(arg);
}

Expand Down Expand Up @@ -1009,18 +1007,15 @@ fn flag_context(args: &mut Vec<RGArg>) {
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.
This overrides both the -B/--before-context and -A/--after-context flags,
in addition to the --passthru flag.
This overrides the --passthru flag.
"
);
let arg = RGArg::flag("context", "NUM")
.short("C")
.help(SHORT)
.long_help(LONG)
.number()
.overrides("passthru")
.overrides("before-context")
.overrides("after-context");
.overrides("passthru");
args.push(arg);
}

Expand Down
6 changes: 3 additions & 3 deletions crates/core/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -992,10 +992,10 @@ impl ArgMatches {
/// If there was a problem parsing the values from the user as an integer,
/// then an error is returned.
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);
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.
Expand Down
17 changes: 17 additions & 0 deletions tests/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,23 @@ rgtest!(f1842_field_match_separator, |dir: Dir, _: TestCommand| {
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| {
dir.create("test", "foo\nctx\nbar\nctx\nfoo\nctx");
cmd.args(&["-A1", "--no-context-separator", "foo", "test"]);
Expand Down

0 comments on commit d675844

Please sign in to comment.