| Crates.io | opengrep |
| lib.rs | opengrep |
| version | 1.1.0 |
| created_at | 2025-06-09 01:59:13.663065+00 |
| updated_at | 2025-06-09 02:09:34.91552+00 |
| description | Advanced AST-aware code search tool with tree-sitter parsing and AI integration capabilities |
| homepage | https://github.com/opengrep-org/opengrep |
| repository | https://github.com/opengrep-org/opengrep |
| max_upload_size | |
| id | 1705414 |
| size | 315,225 |
Advanced AST-aware code search with AI-powered insights. OpenGrep understands your code structure and provides intelligent search capabilities beyond simple pattern matching.
cargo install opengrep
# Clone the repository
git clone https://github.com/opengrep-org/opengrep.git
cd opengrep
# Build and install
cargo install --path .
# Pull the image
docker pull opengrep/opengrep:latest
# Run a search
docker run --rm -v $(pwd):/workspace opengrep/opengrep "TODO" src/
# Search for a pattern in current directory
opengrep "TODO" .
# Case-insensitive regex search
opengrep -i -e "fn\s+\w+" src/
# Show AST context around matches
opengrep -a "struct.*User" src/
# Interactive mode
opengrep -i
# JSON output
opengrep -o json "error" src/ > results.json
# Find all function definitions
opengrep -a "function_item" src/
# Search with AST context display
opengrep --ast-context --max-ast-depth 2 "impl" src/
# Enable AI insights (requires OPENAI_API_KEY)
export OPENAI_API_KEY="your-api-key"
opengrep --ai-insights "memory leak" src/
# Get AI explanations for matches
opengrep --ai-explain "unsafe" src/
# HTML report
opengrep -o html "TODO" src/ > report.html
# CSV for spreadsheet analysis
opengrep -o csv "function" src/ > functions.csv
# Structured JSON
opengrep -o json --stats "error" src/
# Filter by programming language
opengrep -l rust -l python "struct" .
# Exclude directories
opengrep -x "target/**" -x "node_modules/**" "TODO" .
# Limit file size and use more threads
opengrep --max-file-size 1048576 -j 8 "pattern" .
OpenGrep can be configured via CLI arguments or configuration files:
Create ~/.config/opengrep/config.toml:
[search]
ignore_case = false
regex = false
threads = 8
max_file_size = 10485760
[output]
color = true
line_numbers = true
before_context = 2
after_context = 2
show_ast_context = false
[ai]
model = "gpt-4o-mini"
enable_insights = false
enable_explanation = false
max_tokens = 1000
export OPENAI_API_KEY="your-openai-api-key"
export OPENGREP_CONFIG="/path/to/config.toml"
export RUST_LOG="info" # Logging level
./build.sh build
./build.sh build 1.0.0
./build.sh clean # Clean previous builds
./build.sh format # Format code
./build.sh lint # Run linter
./build.sh test # Run tests
./build.sh docker # Build Docker image
./build.sh install # Install locally
docker build -t opengrep:latest .
# Basic search
docker run --rm -v $(pwd):/workspace opengrep:latest "pattern" /workspace
# Interactive mode
docker run --rm -it -v $(pwd):/workspace opengrep:latest -i
# With AI features
docker run --rm -v $(pwd):/workspace -e OPENAI_API_KEY opengrep:latest --ai-insights "pattern" /workspace
# Run all tests
cargo test
# Run specific test module
cargo test ast::tests
# Run with output
cargo test -- --nocapture
# Integration tests
cargo test --test integration_tests
OpenGrep can be used as a library:
use opengrep::{Config, SearchEngine};
use std::path::PathBuf;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let config = Config::default();
let engine = SearchEngine::new(config);
let results = engine.search("TODO", &[PathBuf::from("src")]).await?;
for result in results {
println!("Found {} matches in {}",
result.matches.len(),
result.path.display());
}
Ok(())
}
For web service integration, see the examples directory for FastAPI endpoint implementations.
OpenGrep is optimized for performance:
# Run benchmarks
cargo bench
# Compare with other tools
hyperfine 'opengrep "pattern" .' 'rg "pattern" .' 'grep -r "pattern" .'
AI features not working
export OPENAI_API_KEY="your-api-key"
Out of memory errors
opengrep --max-file-size 1048576 "pattern" .
Slow performance
opengrep -j $(nproc) "pattern" .
Language not detected
opengrep --list-languages # See supported languages
RUST_LOG=debug opengrep "pattern" .
git checkout -b feature/amazing-feature)git commit -m 'Add amazing feature')git push origin feature/amazing-feature)git clone https://github.com/opengrep-org/opengrep.git
cd opengrep
./build.sh format lint test
This project is dual-licensed under either:
at your option.