use std::path::Path; use std::process::Stdio; use std::time::Duration; use tokio::io::{AsyncBufRead, AsyncBufReadExt, BufReader}; use crate::command::GitEnv; use crate::error::{Error, Result}; use crate::history::{git, read_pipe_head, validate_oid}; pub const MAX_QUERY_BYTES: usize = 200; const MAX_GLOB_BYTES: usize = 512; const MAX_LINE_CHARS: usize = 500; const MAX_RECORD_BYTES: usize = 4 * MAX_LINE_CHARS + 4096; #[derive(Debug, Clone, PartialEq, Eq)] pub struct GrepOptions { pub fixed: bool, pub ignore_case: bool, pub path_glob: Option, pub max_matches: usize, pub timeout: Duration, } impl Default for GrepOptions { fn default() -> Self { Self { fixed: true, ignore_case: false, path_glob: None, max_matches: 200, timeout: Duration::from_secs(10), } } } #[derive(Debug, Clone, PartialEq, Eq)] pub struct GrepHit { pub path: String, pub line_no: u32, pub line: String, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct GrepResult { pub hits: Vec, pub truncated: bool, } fn validate_query(query: &str) -> Result<()> { if query.is_empty() || query.len() > MAX_QUERY_BYTES { return Err(Error::InvalidInput(format!( "query must be 1 to {MAX_QUERY_BYTES} bytes" ))); } if query.contains(['\0', '\n', '\r']) { return Err(Error::InvalidInput( "query contains NUL or a newline".into(), )); } Ok(()) } fn validate_glob(glob: &str) -> Result<()> { if glob.is_empty() || glob.len() > MAX_GLOB_BYTES { return Err(Error::InvalidInput(format!( "path glob must be 1 to {MAX_GLOB_BYTES} bytes" ))); } if glob.starts_with('-') || glob.contains("..") { return Err(Error::InvalidInput(format!("unsafe path glob: {glob:?}"))); } if glob.contains(['\0', '\n', '\r']) { return Err(Error::InvalidInput( "path glob contains NUL or a newline".into(), )); } Ok(()) } fn parse_record(record: &[u8], commit: &str) -> Option { let mut parts = record.splitn(3, |&b| b == 0); let header = parts.next()?; let line_bytes = parts.next()?; let text = parts.next().unwrap_or_default(); let prefix = format!("{commit}:"); let path = header.strip_prefix(prefix.as_bytes())?; let line_no = std::str::from_utf8(line_bytes).ok()?.parse::().ok()?; let text = String::from_utf8_lossy(text); let text = text.strip_suffix('\r').unwrap_or(&text); Some(GrepHit { path: String::from_utf8_lossy(path).into_owned(), line_no, line: text.chars().take(MAX_LINE_CHARS).collect(), }) } async fn read_line_capped( reader: &mut R, buf: &mut Vec, keep: usize, ) -> std::io::Result { buf.clear(); let mut started = false; loop { let available = reader.fill_buf().await?; if available.is_empty() { return Ok(started); } started = true; let newline = available.iter().position(|&b| b == b'\n'); let content = newline.unwrap_or(available.len()); let take = content.min(keep - buf.len()); buf.extend_from_slice(&available[..take]); let consumed = newline.map_or(available.len(), |i| i + 1); reader.consume(consumed); if newline.is_some() { return Ok(true); } } } pub async fn grep( env: &GitEnv, repo: &Path, commit: &str, query: &str, opts: GrepOptions, ) -> Result { validate_oid(commit)?; validate_query(query)?; if let Some(glob) = &opts.path_glob { validate_glob(glob)?; } let globs: Vec<&str> = opts.path_glob.as_deref().into_iter().collect(); let mut cmd = git(env, "grep") .git_dir(repo) .arg("-n") .arg("-I") .arg("--no-color") .arg("--null") .arg("--max-count=20") .arg(if opts.fixed { "-F" } else { "-E" }); if opts.ignore_case { cmd = cmd.arg("-i"); } let cmd = cmd.user_pattern(query)?.user_arg(commit)?.paths(&globs); let mut command = cmd.into_tokio(); command.stdout(Stdio::piped()).stderr(Stdio::piped()); let mut child = command.spawn()?; let stdout = child .stdout .take() .ok_or_else(|| Error::Failed("git stdout missing".into()))?; let stderr_task = child.stderr.take().map(|e| tokio::spawn(read_pipe_head(e))); let max = opts.max_matches; let mut hits = Vec::new(); let mut truncated = false; let reading = async { let mut lines = BufReader::new(stdout); let mut buf = Vec::new(); while read_line_capped(&mut lines, &mut buf, MAX_RECORD_BYTES).await? { let Some(hit) = parse_record(&buf, commit) else { continue; }; if hits.len() == max { truncated = true; break; } hits.push(hit); } Ok::<(), Error>(()) }; match tokio::time::timeout(opts.timeout, reading).await { Ok(read) => read?, Err(_) => { let _ = child.start_kill(); let _ = child.wait().await; return Err(Error::Failed(format!( "code search timed out after {} seconds", opts.timeout.as_secs() ))); } } if truncated { let _ = child.start_kill(); } let status = child.wait().await?; let stderr = match stderr_task { Some(task) => task.await.unwrap_or_default(), None => Vec::new(), }; if !truncated && !status.success() && status.code() != Some(1) { return Err(Error::Failed(format!( "git grep exited with {status}: {}", String::from_utf8_lossy(&stderr).trim() ))); } Ok(GrepResult { hits, truncated }) } #[cfg(test)] mod tests { use super::*; use std::process::Command; fn env(dir: &Path) -> GitEnv { GitEnv { git_bin: "git".into(), data_dir: dir.to_path_buf(), } } fn git_in(dir: &Path, args: &[&str]) { let status = Command::new("git") .current_dir(dir) .env("GIT_CONFIG_NOSYSTEM", "1") .args([ "-c", "user.name=Test", "-c", "user.email=test@example.com", "-c", "commit.gpgsign=false", ]) .args(args) .status() .expect("git runs"); assert!(status.success(), "git {args:?} failed"); } fn fixture(dir: &Path) -> (std::path::PathBuf, String) { let work = dir.join("work"); std::fs::create_dir_all(work.join("src")).unwrap(); std::fs::create_dir_all(work.join("docs")).unwrap(); std::fs::write( work.join("src/main.rs"), "fn main() {\n println!(\"Hello\");\n}\nlet a.b = 1;\n", ) .unwrap(); std::fs::write(work.join("src/lib.rs"), "pub fn hello() {}\n").unwrap(); std::fs::write(work.join("docs/Guide.md"), "say hello there\n").unwrap(); git_in(&work, &["init", "-q", "-b", "main"]); git_in(&work, &["add", "-A"]); git_in(&work, &["commit", "-q", "-m", "init"]); let out = Command::new("git") .current_dir(&work) .args(["rev-parse", "HEAD"]) .output() .expect("rev-parse"); let oid = String::from_utf8(out.stdout).unwrap().trim().to_owned(); (work.join(".git"), oid) } #[tokio::test] async fn grep_modes_and_limits_on_one_fixture() { let dir = tempfile::tempdir().unwrap(); let (repo, oid) = fixture(dir.path()); let env = env(dir.path()); let res = grep(&env, &repo, &oid, "a.b", GrepOptions::default()) .await .unwrap(); assert_eq!(res.hits.len(), 1, "fixed"); assert_eq!(res.hits[0].path, "src/main.rs", "fixed"); assert_eq!(res.hits[0].line_no, 4, "fixed"); assert_eq!(res.hits[0].line, "let a.b = 1;", "fixed"); assert!(!res.truncated, "fixed"); let sensitive = grep(&env, &repo, &oid, "hello", GrepOptions::default()) .await .unwrap(); assert_eq!( sensitive.hits.len(), 2, "lib.rs and Guide.md, case-sensitive" ); let opts = GrepOptions { ignore_case: true, ..GrepOptions::default() }; let insensitive = grep(&env, &repo, &oid, "HELLO", opts).await.unwrap(); assert_eq!(insensitive.hits.len(), 3, "ignore case"); let regex = GrepOptions { fixed: false, ..GrepOptions::default() }; let res = grep(&env, &repo, &oid, "^pub fn \\w+", regex) .await .unwrap(); assert_eq!(res.hits.len(), 1, "regex"); assert_eq!(res.hits[0].path, "src/lib.rs", "regex"); let opts = GrepOptions { path_glob: Some("*.md".into()), ..GrepOptions::default() }; let res = grep(&env, &repo, &oid, "hello", opts).await.unwrap(); assert_eq!(res.hits.len(), 1, "path glob"); assert_eq!(res.hits[0].path, "docs/Guide.md", "path glob"); let opts = GrepOptions { ignore_case: true, max_matches: 2, ..GrepOptions::default() }; let res = grep(&env, &repo, &oid, "hello", opts).await.unwrap(); assert_eq!(res.hits.len(), 2, "max matches"); assert!(res.truncated, "max matches"); let res = grep(&env, &repo, &oid, "zzz-not-here", GrepOptions::default()) .await .unwrap(); assert!(res.hits.is_empty(), "no match"); assert!(!res.truncated, "no match"); } #[tokio::test] async fn query_starting_with_dash_is_a_pattern() { let dir = tempfile::tempdir().unwrap(); let (repo, oid) = fixture(dir.path()); std::fs::write(dir.path().join("work/flags.txt"), "--verbose\n").unwrap(); git_in(&dir.path().join("work"), &["add", "-A"]); git_in(&dir.path().join("work"), &["commit", "-q", "-m", "flags"]); let out = Command::new("git") .current_dir(dir.path().join("work")) .args(["rev-parse", "HEAD"]) .output() .unwrap(); let oid2 = String::from_utf8(out.stdout).unwrap().trim().to_owned(); assert_ne!(oid, oid2); let res = grep( &env(dir.path()), &repo, &oid2, "--verbose", GrepOptions::default(), ) .await .unwrap(); assert_eq!(res.hits.len(), 1); assert_eq!(res.hits[0].path, "flags.txt"); } #[tokio::test] async fn bad_inputs_are_rejected() { let dir = tempfile::tempdir().unwrap(); let (repo, oid) = fixture(dir.path()); let env = env(dir.path()); let opts = || GrepOptions::default(); let long = "x".repeat(MAX_QUERY_BYTES + 1); for query in ["", long.as_str(), "a\nb", "a\0b", "a\rb"] { assert!( grep(&env, &repo, &oid, query, opts()).await.is_err(), "query {query:?} should be rejected" ); } for glob in ["-x", "../etc", "src/../x", ""] { let o = GrepOptions { path_glob: Some(glob.into()), ..GrepOptions::default() }; assert!( grep(&env, &repo, &oid, "hello", o).await.is_err(), "glob {glob:?} should be rejected" ); } assert!( grep(&env, &repo, "not-an-oid", "hello", opts()) .await .is_err() ); let bad_regex = GrepOptions { fixed: false, ..GrepOptions::default() }; assert!(grep(&env, &repo, &oid, "(", bad_regex).await.is_err()); } #[test] fn parse_record_truncates_and_strips_cr() { let long = "y".repeat(600); let record = format!("{}:a/b.rs\x007\x00{long}\r", "c".repeat(40)); let hit = parse_record(record.as_bytes(), &"c".repeat(40)).unwrap(); assert_eq!(hit.path, "a/b.rs"); assert_eq!(hit.line_no, 7); assert_eq!(hit.line.chars().count(), MAX_LINE_CHARS); assert!(parse_record(b"other:x\x001\x00t", &"c".repeat(40)).is_none()); } }