Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ legacy-format support for migration.

## Keep `index`, `serve` and search flags aligned

`--max-results <N>` ends a search after the file in which the N-th matching
line is found, and a server stops reading files there. Prefer it to piping
into `head` when a pattern may be broad: the output is whole files, and the
search itself is cut short.

Some flags describe the index, so `index`, `serve` and search have to agree on
them. If they differ, the client either cannot find the server or silently
searches a different set of files.
Expand Down
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ These tables describe search flags. Use `tgrep index --help` and
| `--files-without-match` | Print files that do NOT match |
| `-q, --quiet` | Suppress output; exit code only |
| `-m, --max-count <N>` | Limit matching lines per file (see [Match limits](#match-limits)) |
| `--max-results <N>` | Stop after the file in which the N-th matching line is found (see [Match limits](#match-limits)) |
| `-g, --glob <GLOB>` | Filter files by glob pattern, case-sensitive (repeatable); overrides `-t`/`-T` |
| `--iglob <GLOB>` | Case-insensitive glob filter (repeatable) |
| `--glob-case-insensitive` | Treat all `-g` globs as case-insensitive |
Expand Down Expand Up @@ -619,6 +620,18 @@ of lines covered by matches, so a multiline match is not cut short.
tgrep reads a whole-file buffer even with `-m`; its `bytes_searched` statistic
can therefore exceed ripgrep's.

`--max-results` limits matching lines across the whole invocation, which
ripgrep has no flag for. Files are reported whole: the search ends after the
file that brings the total to the limit, so a few more lines than the limit
can be printed. The lines counted are those of the source: a multiline match
counts every line it covers, however it is printed, and so do the matching
lines of a binary file that is reported with a note. A server stops reading
candidate files at that point too,
which keeps a broad query on a large tree fast; a shared daemon and a search
with `--sort` search every candidate and apply the limit to the result. It
cannot be combined with `-l`, `--files-without-match`, `-c`,
`--count-matches`, `-q` or `--files`.

### JSON statistics

`--json` emits ripgrep-shaped `begin`, `match`, `context`, `end`, and `summary`
Expand Down Expand Up @@ -830,7 +843,9 @@ exit code determined by the search alone. Suppress the message with
with the rows of the candidates searched since the last one and the
`candidates` and `searched` counts so far, then the final result with the
remaining rows. The CLI asks for this when it prints one file's rows after
another, and prints each part as it arrives.
another, and prints each part as it arrives. `"max_results": N` ends the
search after the file in which the N-th matching line is found, and the
final result then carries `"truncated": true`.

The server's content cache is limited to 50,000 entries and 1 GiB of decoded
content, with a 64 MiB per-entry limit. `--files` combines content-index paths
Expand Down
14 changes: 14 additions & 0 deletions tgrep-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ struct Cli {
#[arg(short = 'm', long = "max-count", global = true)]
max_count: Option<usize>,

/// Stop after the file in which the NUM-th matching line is found.
#[arg(
long = "max-results",
global = true,
value_name = "NUM",
value_parser = clap::value_parser!(u64).range(1..),
conflicts_with_all = ["files_only", "files_without_match", "count", "count_matches", "quiet", "list_files"]
)]
max_results: Option<u64>,

/// List files that would be searched (no search performed).
#[arg(long = "files", global = true)]
list_files: bool,
Expand Down Expand Up @@ -1142,6 +1152,7 @@ impl Cli {
count: self.count,
word_boundary: self.word_regexp,
max_count: self.max_count,
max_results: self.max_results,
json,
vimgrep: self.vimgrep,
stats: self.stats || self.debug || self.trace,
Expand Down Expand Up @@ -1687,6 +1698,9 @@ fn run_search(
process::exit(2);
}
}
if opts.max_results_reached(&writer) {
break;
}
}

writer.finish()?;
Expand Down
14 changes: 14 additions & 0 deletions tgrep-cli/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ pub struct OutputWriter {
all_searches: u64,
total_matches: u64,
total_matched_lines: u64,
/// Matching lines of the files reported so far, for `--max-results`.
reported_lines: u64,
}

impl OutputWriter {
Expand Down Expand Up @@ -340,9 +342,21 @@ impl OutputWriter {
all_searches: 0,
total_matches: 0,
total_matched_lines: 0,
reported_lines: 0,
}
}

/// The matching lines of the files reported across the invocation.
pub fn reported_lines(&self) -> u64 {
self.reported_lines
}

/// Count a reported file's matching lines towards `--max-results`. These
/// are lines of the source, however its matches are printed.
pub fn note_reported_lines(&mut self, lines: u64) {
self.reported_lines += lines;
}

pub fn match_totals(&self) -> (u64, u64) {
(self.total_matches, self.total_matched_lines)
}
Expand Down
157 changes: 149 additions & 8 deletions tgrep-cli/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ pub struct SearchOptions {
pub count: bool,
pub word_boundary: bool,
pub max_count: Option<usize>,
/// `--max-results`: stop after the file that brings the matching lines
/// of the files reported to this many.
pub max_results: Option<u64>,
pub json: bool,
pub vimgrep: bool,
pub stats: bool,
Expand Down Expand Up @@ -247,6 +250,22 @@ impl SearchOptions {
|| self.effective_passthru())
}

/// The `max_results` to send a plain server when `reported` lines are
/// already counted: what is left of the limit, so that a later search
/// path ends as early as the first. None for a sorted search: its limit
/// counts files in sorted order, which the server does not search in.
fn server_max_results(&self, reported: u64) -> Option<u64> {
self.max_results
.filter(|_| self.sort.is_none())
.map(|limit| limit.saturating_sub(reported))
}

/// Whether `--max-results` has been reached, so no further file is searched.
pub fn max_results_reached(&self, writer: &OutputWriter) -> bool {
self.max_results
.is_some_and(|limit| writer.reported_lines() >= limit)
}

/// Resolve smart-case from literals in every supplied pattern.
pub fn effective_case_insensitive(&self) -> Result<bool> {
// `-s` wins over `-S`, matching ripgrep, but `-i` still wins over `-s`.
Expand Down Expand Up @@ -1135,12 +1154,17 @@ fn search_via_server(
) -> Result<bool> {
let mut stream = info.connect(SERVER_READ_TIMEOUT)?;
let mut request = server_search_request(scope, opts, ci)?;
if let Some(limit) = opts.server_max_results(writer.reported_lines()) {
// Lets the server stop reading candidate files at the limit. One that
// predates the option ignores it and replies in full, and rendering
// applies the limit either way. A shared daemon is not sent it: that
// one rejects options it does not know and pages its replies.
request["params"]["max_results"] = serde_json::json!(limit);
}
if opts.streams_from_server() {
// The server then sends each chunk of files as soon as it is searched,
// as `partial` replies ahead of the final one, and the rows are
// printed as they arrive. One that predates the option replies once.
// A shared daemon is not asked: that one rejects options it does not
// know and pages its replies.
request["params"]["stream"] = serde_json::json!(true);
}
writeln!(stream, "{request}")?;
Expand Down Expand Up @@ -1408,7 +1432,10 @@ fn server_search_request(
"vimgrep": opts.vimgrep,
"detail": detail,
"positions": positions,
"stats": opts.stats,
// The per-file totals are also what `--max-results` counts: the
// limit runs across every search path, and rows do not show how
// many lines of the source a match covers.
"stats": opts.stats || opts.max_results.is_some(),
},
"id": 1,
});
Expand Down Expand Up @@ -1617,7 +1644,7 @@ fn render_server_result(
} else {
None
};
let file_stats = if opts.stats || opts.count_matches {
let file_stats = if opts.stats || opts.count_matches || opts.max_results.is_some() {
result
.get("file_stats")
.map(|stats| -> Result<Vec<FileMatchStats>> {
Expand Down Expand Up @@ -1719,6 +1746,8 @@ fn render_server_result(
}
}

// The rows ahead of the file at which `--max-results` ended the output.
let mut reported_rows = matches.len();
if opts.quiet {
// No output, but still report stats for the first matching file.
} else if opts.files_only {
Expand Down Expand Up @@ -1783,8 +1812,34 @@ fn render_server_result(
writer.write_count(file, n)?;
}
} else {
for m in matches {
// Each file's matching lines, for `--max-results`, when the reply
// has them.
let source_lines: Option<std::collections::HashMap<&str, u64>> = file_stats
.as_ref()
.filter(|_| opts.max_results.is_some())
.map(|stats| {
stats
.iter()
.map(|stats| (stats.file.as_str(), stats.matched_lines))
.collect()
});
let mut current_file = None;
for (at, m) in matches.iter().enumerate() {
let file = m.get("file").and_then(|f| f.as_str()).unwrap_or("");
if current_file != Some(file) {
if opts.max_results_reached(writer) {
reported_rows = at;
break;
}
current_file = Some(file);
if opts.max_results.is_some() {
let lines = source_lines
.as_ref()
.and_then(|lines| lines.get(file).copied())
.unwrap_or_else(|| row_source_lines(&matches[at..]));
writer.note_reported_lines(lines);
}
}
if let Some(stats) = &search_stats {
writer.set_file_bytes_searched(stats[file]);
}
Expand Down Expand Up @@ -1867,18 +1922,32 @@ fn render_server_result(
// Apply the same scope to per-file totals as to output rows. The
// server's `num_matches` includes context rows, and
// rendered spans can split or merge the original matches.
// Nor do the totals cover files that `--max-results` left out.
let reported = &matches[..reported_rows];
let (num, lines) = if let Some(stats) = &file_stats {
let first_file = matches
.first()
.and_then(|m| m.get("file"))
.and_then(|f| f.as_str());
let reported_files: Option<std::collections::HashSet<&str>> =
(reported_rows < matches.len()).then(|| {
reported
.iter()
.filter_map(|m| m.get("file").and_then(|f| f.as_str()))
.collect()
});
stats
.iter()
.filter(|s| !opts.quiet || first_file == Some(s.file.as_str()))
.filter(|s| {
reported_files
.as_ref()
.is_none_or(|files| files.contains(s.file.as_str()))
})
.fold((0, 0), |(m, l), s| (m + s.matches, l + s.matched_lines))
} else {
// Older servers only provide rendered rows.
count_reported_matches(matches)
count_reported_matches(reported)
};
let mut narrowing_note = "";
if let Some(stats) = result.get("index_stats")
Expand All @@ -1902,6 +1971,77 @@ fn render_server_result(
Ok(had_matches)
}

/// The matching source lines among the rows of the file that `rows` starts
/// with, for a server that predates per-file totals. A row of a rewritten
/// block counts the source lines of the block, and any other row its own
/// line, which is all that such a reply tells.
fn row_source_lines(rows: &[serde_json::Value]) -> u64 {
let file = rows.first().and_then(|row| row.get("file"));
let mut count = 0;
let mut counted_to = 0;
for row in rows.iter().take_while(|row| row.get("file") == file) {
if row
.get("type")
.and_then(|kind| kind.as_str())
.unwrap_or("match")
!= "match"
{
continue;
}
let line = row.get("line").and_then(|line| line.as_u64()).unwrap_or(0);
let block = row.get("block").and_then(|block| block.as_array());
let bound = |at: usize| block.and_then(|block| block.get(at)?.as_u64());
let first = bound(0).unwrap_or(line).max(counted_to + 1);
let last = bound(1).unwrap_or(line);
if last >= first {
count += last - first + 1;
counted_to = last;
}
}
count
}

#[cfg(test)]
mod max_results_tests {
use super::*;

#[test]
fn rows_count_source_lines() {
let rows = serde_json::json!([
{"file": "a", "line": 3, "type": "context"},
{"file": "a", "line": 4},
{"file": "a", "line": 4, "type": "match"},
{"file": "a", "line": 6, "type": "match", "block": [6, 8]},
{"file": "a", "line": 8, "type": "match", "block": [8, 9]},
{"file": "a", "type": "binary", "lines": 7},
{"file": "b", "line": 1},
]);
let rows = rows.as_array().unwrap();
assert_eq!(row_source_lines(rows), 5);
assert_eq!(row_source_lines(&rows[6..]), 1);
assert_eq!(row_source_lines(&[]), 0);
}

#[test]
fn limited_request() {
let mut opts = SearchOptions {
pattern: "needle".into(),
..Default::default()
};
let stats = |opts: &SearchOptions| {
server_search_request(&IndexScope::Whole, opts, false).unwrap()["params"]["stats"]
.clone()
};
assert_eq!(stats(&opts), false);
assert_eq!(opts.server_max_results(0), None);

opts.max_results = Some(10);
assert_eq!(stats(&opts), true, "the limit counts per-file totals");
assert_eq!(opts.server_max_results(0), Some(10));
assert_eq!(opts.server_max_results(9), Some(1), "a later search path");
}
}

fn search_local_index(
root: &Path,
index_dir: &Path,
Expand Down Expand Up @@ -2051,7 +2191,7 @@ fn search_local_index(

if search_file(&read, &matcher, rel_path, opts, writer, explicit)? {
had_matches = true;
if opts.quiet {
if opts.quiet || opts.max_results_reached(writer) {
break;
}
}
Expand Down Expand Up @@ -2265,7 +2405,7 @@ fn brute_force_search(

if search_file(&read, &matcher, &rel_path, opts, writer, false)? {
had_matches = true;
if opts.quiet {
if opts.quiet || opts.max_results_reached(writer) {
break;
}
}
Expand Down Expand Up @@ -2682,6 +2822,7 @@ fn search_decoded_file(
let (matches, matched_lines) = found.match_totals();
writer.note_matches(matches, matched_lines);
}
writer.note_reported_lines(found.match_totals().1);

let has_matches = !found.is_empty();
if !has_matches {
Expand Down
Loading
Loading