Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion crates/bashkit/src/builtins/rg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4915,6 +4915,8 @@ impl Builtin for Rg {
&& !opts.invert_match
{
Some(opts.max_count.unwrap_or(usize::MAX).min(1))
} else if opts.invert_match {
None
} else {
opts.max_count
};
Expand All @@ -4925,13 +4927,18 @@ impl Builtin for Rg {
if opts.invert_match {
let matched_line_set: HashSet<usize> =
context_match_lines.iter().copied().collect();
let inverted_match_lines: Vec<usize> = lines
let mut inverted_match_lines: Vec<usize> = lines
.iter()
.enumerate()
.filter_map(|(line_idx, _)| {
(!matched_line_set.contains(&line_idx)).then_some(line_idx)
})
.collect();
if let Some(limit) = opts.max_count
&& inverted_match_lines.len() > limit
{
inverted_match_lines.truncate(limit);
}
match_count = inverted_match_lines.len();
count_value = inverted_match_lines.len();
let inverted_matches = if opts.count_only || opts.count_matches {
Expand Down Expand Up @@ -12413,6 +12420,18 @@ mod tests {
assert_eq!(lines.len(), 1);
}

#[tokio::test]
async fn test_rg_multiline_invert_max_count() {
let result = run_rg(
&["-m", "1", "-v", "-U", "foo\nbar", "/test.txt"],
None,
&[("/test.txt", b"foo\nbar\nkeep1\nfoo\nbar\nkeep2\n")],
)
.await;
assert_eq!(result.exit_code, 0);
assert_eq!(result.stdout, "keep1\n");
}

#[tokio::test]
async fn test_rg_max_count_requires_value() {
let args: Vec<String> = vec!["hello".to_string(), "-m".to_string()];
Expand Down
Loading