diffscope-0.4.3 is not a library.
DiffScope
A composable code review engine for automated diff analysis.
Features
- Model Agnostic: Works with OpenAI, Anthropic Claude 4, Ollama, and any OpenAI-compatible API
- Git Integration: Review uncommitted, staged, or branch changes directly
- PR Reviews: Analyze and comment on GitHub pull requests
- Smart Prompting: Advanced prompt engineering with examples, XML structure, and chain-of-thought
- Commit Messages: AI-powered commit message suggestions following conventional commits
- Composable Architecture: Modular components that work together
- Plugin System: Extensible pre-analyzers and post-processors
- Multiple Outputs: JSON, patch, or markdown formats
- CI/CD Ready: GitHub Action, GitLab CI, and Docker support
- Smart Review: Enhanced analysis with confidence scoring, fix effort estimation, and executive summaries
Quick Start
Install via Homebrew (macOS/Linux)
Install from crates.io
Docker
Usage
Basic Usage
# Review your current changes
|
# Review a specific file diff
# Get enhanced analysis with smart review
|
Git Integration
# Review what you're about to commit
# Review all uncommitted changes
# Compare your branch to main
# Get AI-powered commit message suggestions
Pull Request Review
# Review the current PR
# Review a specific PR number
# Post review comments directly to GitHub
Smart Review (Enhanced Analysis)
# Get professional-grade analysis with confidence scoring
|
# Generate executive summary report
# Review with specific AI model
|
AI Model Configuration
# OpenAI (default)
|
# Anthropic Claude
|
# Local Ollama
|
Supported Models
OpenAI: gpt-4o, gpt-4-turbo, gpt-3.5-turbo
Anthropic:
- Claude 4: claude-opus-4-20250514, claude-sonnet-4-20250514
- Claude 3.5: claude-3-5-sonnet-20241022, claude-3-5-haiku-20240307
- Claude 3: claude-3-opus-20240229, claude-3-haiku-20240307
Ollama: Any locally installed model (codellama, llama3.2, mistral, etc.)
Output Formats
# JSON output (default)
|
# Markdown report
|
# Inline patch comments
|
GitHub Action
name: AI Code Review
on:
jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: haasonsaas/diffscope@v1
with:
model: gpt-4o
openai-api-key: ${{ secrets.OPENAI_API_KEY }}
post-comments: true
Configuration
Create a .diffscope.yml file in your repository:
model: gpt-4o
temperature: 0.2
max_tokens: 4000
system_prompt: "Focus on security vulnerabilities, performance issues, and best practices"
Plugin Development
Create custom analyzers:
export interface PreAnalyzer {
id: string
run(diff: UnifiedDiff, repoPath: string): Promise<LLMContextChunk[]>
}
export interface PostProcessor {
id: string
run(comments: Comment[], repoPath: string): Promise<Comment[]>
}
Architecture
graph LR
A[git diff] --> B(core-engine)
subgraph core-engine
B1[Diff Parser]
B2[Context Fetcher]
B3[Prompt Builder]
B4[LLM Adapter]
B5[Comment Synthesizer]
end
B -->|JSON| C(output)
License
Apache-2.0 License. See LICENSE for details.
Example Output
Standard Review
Smart Review Output
š” **Code Quality Score:** 8.2/10
š **Total Issues Found:** 4
šØ **Critical Issues:** 1
š **Files Analyzed:** 3
1. 2.
**š” Recommended Fix:**
Use parameterized queries to prevent SQL injection attacks.
**š§ Code Example:**
```diff
- ++
š” High Priority Issues
ā” src/models.py:28 - š” Moderate Effort Performance
Confidence: 87% | Tags: performance, n+1-query
N+1 query problem detected in user retrieval loop.
š” Recommended Fix: Use eager loading or bulk queries to reduce database calls.
### Commit Message Suggestion
feat(auth): add JWT-based authentication system
## Author
Jonathan Haas <jonathan@haas.holdings>
## Advanced CI/CD Integration
### Enterprise GitHub Actions Workflow
Here's an example of how large organizations use diffscope in production CI/CD pipelines:
```yaml
name: AI Code Review
on:
pull_request:
types: [opened, synchronize]
branches: [main]
jobs:
ai-code-review:
name: AI Code Review with DiffScope
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout PR
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.sha }}
- name: Install DiffScope with Cache
uses: actions/cache@v4
with:
path: ~/.cargo/bin
key: ${{ runner.os }}-diffscope-${{ hashFiles('**/Cargo.lock') }}
- run: |
if ! command -v diffscope &> /dev/null; then
cargo install diffscope
fi
- name: Generate PR Diff
run: |
git diff origin/${{ github.event.pull_request.base.ref }}...HEAD > pr.diff
- name: Run AI Review
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
diffscope review --model claude-3-5-sonnet-20241022 \
--diff pr.diff --output-format json > review.json
- name: Post Review Comments
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const review = JSON.parse(fs.readFileSync('review.json', 'utf8'));
let body = '## š¤ AI Code Review\n\n';
if (review.length === 0) {
body += 'ā
**No issues found!** Code looks good!';
} else {
body += review.map((item, i) =>
`**${i+1}.** \`${item.file_path}:${item.line_number}\`\n` +
`${item.content}\n` +
(item.suggestion ? `\nš” **Suggestion:** ${item.suggestion}\n` : '')
).join('\n---\n');
}
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: body
});
Enterprise Configuration Example
For a large Python/FastAPI application at a company like Acme Inc:
.diffscope.yml
# Acme Inc DiffScope Configuration
model: "claude-3-5-sonnet-20241022"
temperature: 0.1 # Low for consistent reviews
max_tokens: 4000
system_prompt: |
You are reviewing Python code for a production FastAPI application.
Critical focus areas:
- SQL injection and security vulnerabilities
- Async/await correctness
- Resource leaks and memory issues
- API contract consistency
- Production deployment concerns
Prioritize by severity: Security > Performance > Maintainability
# File filters for monorepo
include_patterns:
- "src/**/*.py"
- "tests/**/*.py"
- "alembic/versions/*.py"
- "*.yml"
- "Dockerfile*"
exclude_patterns:
- "**/__pycache__/**"
- "**/venv/**"
- "**/.pytest_cache/**"
- "**/node_modules/**"
# Review configuration
max_diff_size: 10000
context_lines: 3
Integration with Other CI Tools
GitLab CI Example:
code-review:
stage: review
image: rust:alpine
only:
- merge_requests
script:
- apk add --no-cache git
- cargo install diffscope
- git diff origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME...HEAD > mr.diff
- diffscope smart-review --diff mr.diff --output review.md
artifacts:
reports:
codequality: review.md
Jenkins Pipeline:
Best Practices for CI/CD Integration
- Cache Installation: Cache cargo/diffscope binaries to speed up CI runs
- API Key Management: Use secure secret storage for API keys
- Diff Size Limits: Set max diff size to avoid timeouts on large PRs
- Custom Prompts: Tailor system prompts to your tech stack and standards
- Output Parsing: Handle both empty reviews and JSON parsing errors gracefully
- Conditional Runs: Skip reviews on draft PRs or specific file types
Contributing
Contributions are welcome! Please open an issue first to discuss what you would like to change.
Support
- GitHub Issues: github.com/Haasonsaas/diffscope/issues