Skip to content

feat: support delta-only (changed lines) matching in builtin pattern hooks (deny-pattern, require-pattern) #2610

Description

@udaya2899

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:

  1. If a developer makes a small edit in a legacy file containing pre-existing violations on untouched lines, deny-pattern fails the commit.
  2. 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

Activity

  1. Repository owner locked and limited conversation to collaborators on Aug 25, 2026
  2. converted this issue into a discussion #2611 on Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions