开始使用
Diff Review Lite — Rank findings before merge

Diff Review Lite — Rank findings before merge

Point it at a branch diff and it reads the lines you are adding and writes a severity-ranked list of review findings — hard-coded secrets, `shell=True`, string-built SQL, mutable default arguments, bare `except`, `SELECT *`, and more — each with its file, line, and a one-line fix. It runs 19 deterministic checks and ranks them worst-first. Read-only and offline: it never runs your code or makes a network call, so it fits a pre-commit hook or CI with `--fail-on high`. #工程

评分
需要更多评价
订单数
0
如何使用
下载

The problem

The diff is up and the reviewer starts reading from the top, left to right, hoping to notice the one line that matters. The hard-coded key, the shell=True, the SQL glued together with +, the mutable default argument — these are mechanical mistakes that a human eye tires of catching on the hundredth file. They hide in a wall of green. A first pass that only looks at the lines you are adding, ranks what it finds worst-first, and tells you where each one is, lets the human spend their attention on the judgment calls instead of the checklist.

How it works

  1. Reads a unified diff (from a file, standard input, or --file for a whole source file) and keeps only the added lines — the code being introduced — with each line's real file-and-line number tracked from the hunk headers.
  2. Runs 19 deterministic checks over each added line. No code is run, no file is imported, no network is touched — the checks are text and regular-expression matching, so the same input always gives the same findings.
  3. Ranks every finding by severity (critical > high > medium > low), then by file and line, and renders a markdown report: a count, a table, and a details block with a suggested fix per finding.

Because it is deterministic and read-only, it fits a pre-commit hook or a CI step as well as the keyboard: --fail-on high exits 1 when a finding at or above that severity is present.

Usage

# review a diff file
python3 scripts/diff_review_lite.py --diff examples/sample.diff

# review a branch diff from a pipe (nothing is run but the parse)
git diff main...HEAD | python3 scripts/diff_review_lite.py --stdin

# review a whole source file, as if every line were added
python3 scripts/diff_review_lite.py --file scripts/diff_review_lite.py

# the findings as JSON, for your own tooling
python3 scripts/diff_review_lite.py --diff examples/sample.diff --json

# a CI gate: exit 1 if any finding is high or worse
git diff main...HEAD | python3 scripts/diff_review_lite.py --stdin --fail-on high

Example

The package ships a sample diff in examples/sample.diff (a hard-coded key, a shell=True, a string-built SQL query, and a new file with a mutable default argument and a TODO). Running the tool on it prints exactly this:

$ python3 scripts/diff_review_lite.py --diff examples/sample.diff
# Code review findings

**6 findings** (critical 1, high 3, medium 1, low 1)

| # | Severity | Category | Location | Finding |
|---|---|---|---|---|
| 1 | critical | security | `src/auth.py:3` | a credential appears to be hard-coded in the source |
| 2 | high | security | `src/auth.py:6` | subprocess with shell=True can be injected by its input |
| 3 | high | security | `src/auth.py:9` | a SQL statement is built by string formatting |
| 4 | high | bug | `src/cache.py:1` | a mutable default argument is shared across all calls |
| 5 | medium | performance | `src/auth.py:9` | SELECT * fetches every column and breaks on schema changes |
| 6 | low | style | `src/cache.py:4` | a TODO marker was added |

## Details

### 1. [critical] a credential appears to be hard-coded in the source

- **Location:** `src/auth.py:3`
- **Category:** security
- **Fix:** read it from the environment or a secrets store, not a literal

### 2. [high] subprocess with shell=True can be injected by its input

- **Location:** `src/auth.py:6`
- **Category:** security
- **Fix:** pass a list of arguments and keep shell=False

### 3. [high] a SQL statement is built by string formatting

- **Location:** `src/auth.py:9`
- **Category:** security
- **Fix:** use a parameterised query with placeholders

### 4. [high] a mutable default argument is shared across all calls

- **Location:** `src/cache.py:1`
- **Category:** bug
- **Fix:** default to None and build the list/dict inside the body

### 5. [medium] SELECT * fetches every column and breaks on schema changes

- **Location:** `src/auth.py:9`
- **Category:** performance
- **Fix:** name the columns you use

### 6. [low] a TODO marker was added

- **Location:** `src/cache.py:4`
- **Category:** style
- **Fix:** track it in an issue or resolve it before merging

Severities

severity meaning examples
critical can cause a breach or data loss hard-coded secret, eval/exec on input, rm -rf on a root/variable path
high a likely bug or injection shell=True, string-built SQL, bare except, mutable default argument, curl | sh
medium a probable defect or cost swallowed exception, assert for validation, JS loose ==, SELECT *
low style and hygiene == None, leftover debug print, TODO, long line, trailing whitespace

Exit codes

code meaning
0 the review ran (no gate tripped)
1 --fail-on was given and a finding met the threshold
2 usage error (bad arguments, missing file, or empty input)

Limits (read these)

  • It is a first pass, not a review. The checks are mechanical pattern matches on single lines. They catch the common, obvious mistakes; they do not understand your program, cannot judge logic, and will not tell you whether the change is correct. A clean report means the easy things passed, not that the code is good.
  • False positives and misses are expected. A string that reads like a secret but is a test fixture will be flagged; a real injection spread across several lines will be missed. Treat each finding as a prompt to look, not a verdict. Line-based matching has no type information.
  • It reads only the added lines. Removing a bad line is not reviewed; code you did not touch is not reviewed. Point --file at a whole file to review every line of it.
  • Read-only and offline. The tool never runs your code, imports it, commits, pushes, checks out, edits your tree, or makes a network call. It only reads the diff (or file) text you give it.