Summary
Add an option (e.g. --delta-only / --check-delta) to builtin pattern hooks (deny-pattern and require-pattern) so that patterns are evaluated only against newly added or modified lines in the Git diff, rather than scanning the full file content from disk.
Motivation
In large codebases and monorepos, teams frequently introduce rules to ban deprecated APIs, imports, or patterns in new code (ratcheted migrations).
Currently, deny-pattern scans the entire file from disk whenever a file is staged or selected:
- If a developer makes a small edit in a legacy file containing pre-existing violations on untouched lines,
deny-pattern fails the commit.
- This forces developers to either fix out-of-scope legacy code, add broad exclusions, skip hooks, or maintain custom
repo: local diff-parsing scripts.
Having native --delta-only support in repo: builtin would allow zero-setup, blazingly fast ratcheting checks directly within prek.
Proposed Configuration
repos:
- repo: builtin
hooks:
- id: deny-pattern
name: Ban deprecated helper in new/modified lines
args: [--delta-only, 'deprecated_call\(']
types: [python]
Proposed Behavior
- When
--delta-only is enabled:
- Local commit / Staged run (
prek run): Inspects staged diff hunks (e.g. git diff --cached -U0) and evaluates patterns only on added lines (+ lines in diff hunks).
- Ref-range run (
prek run --from-ref <REF> [--to-ref <REF>]): Inspects diff hunks across the specified revision range (e.g. git diff -U0 <FROM_REF>...<TO_REF>).
- Output: Reports
<file>:<line>: <matched_text> for matches found within added lines and exits with code 1. Pre-existing matches on untouched lines are ignored.
- When
--delta-only is omitted: Default behavior is preserved (scans full file from disk).
- Flag interaction:
- Incompatible with whole-file multiline matching (
-m / --multiline), or should operate on contiguous added hunk blocks.
References
Summary
Add an option (e.g.
--delta-only/--check-delta) to builtin pattern hooks (deny-patternandrequire-pattern) so that patterns are evaluated only against newly added or modified lines in the Git diff, rather than scanning the full file content from disk.Motivation
In large codebases and monorepos, teams frequently introduce rules to ban deprecated APIs, imports, or patterns in new code (ratcheted migrations).
Currently,
deny-patternscans the entire file from disk whenever a file is staged or selected:deny-patternfails the commit.repo: localdiff-parsing scripts.Having native
--delta-onlysupport inrepo: builtinwould allow zero-setup, blazingly fast ratcheting checks directly withinprek.Proposed Configuration
Proposed Behavior
--delta-onlyis enabled:prek run): Inspects staged diff hunks (e.g.git diff --cached -U0) and evaluates patterns only on added lines (+lines in diff hunks).prek run --from-ref <REF> [--to-ref <REF>]): Inspects diff hunks across the specified revision range (e.g.git diff -U0 <FROM_REF>...<TO_REF>).<file>:<line>: <matched_text>for matches found within added lines and exits with code 1. Pre-existing matches on untouched lines are ignored.--delta-onlyis omitted: Default behavior is preserved (scans full file from disk).-m/--multiline), or should operate on contiguous added hunk blocks.References
crates/prek/src/hooks/builtin_hooks/pattern.rsrustgrepbuiltin for one-off hooks #2355 (Initialrustgrep/deny-patternbuiltin request)