Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

61.3 KB
Raw Blame
#!/usr/bin/env python3
"""Email-domain consistency check.

Email addresses committed to source *content* should use a domain reserved for
examples and tests: an RFC-2606 documentation domain (``example.com`` / ``.org``
/ ``.net`` and subdomains), an RFC-6761 reserved suffix (``.test`` /
``.invalid`` / ``.localhost`` / ``.example``), a GitHub
``@users.noreply.github.com`` alias, a GitHub service address, or an address a
project author declared in ``pyproject.toml``. Keeping fixtures and docs on the
domains reserved for that purpose is good hygiene (and keeps real addresses out
of the tree). This is the content-side companion to ``check-author-identity``,
which applies the same principle to commit metadata.

Internationalized addresses are detected in both encodings — Unicode
(U-label) domains such as ``bücher.example`` and their ASCII punycode
(A-label) form (``xn--`` TLDs), including labels with combining marks,
ASCII-letter-led Latin-extended labels, and mixed-script labels such as
``a中`` / ``中a`` in any label position, the final label included — and
so are explicitly delimited mixed-script local parts, Hiragana local parts,
and RFC 5321 quoted local parts (``"john doe"@…``), Unicode
local parts (``müller@…``) and pure-CJK local parts (``用户@…`` /
``ユーザー@…``). A quoted local part that itself contains an address
yields only the whole address, never a phantom inner match.

Unspaced CJK prose (Han, Hangul, Katakana — like Hiragana) never widens
the FINAL domain label, though a mixed-script label is still recognized
where more domain follows, and as the final label when the label itself
mixes scripts — while the Hiragana block never widens a label anywhere,
explicit delimiters included; a ``,``, ``;`` or ``&`` separator — bare or opening a
``KEY=`` assignment of any label shape — and every other bare
separator (``!``, ``$``, ``(``, ``*``, ``~``, ``'``, ``=``) ends URI
userinfo suppression, so an address separately written on the same line
is still seen (a URI password containing such a separator needs the
allow marker); Python
matrix multiplication between dotted operands (``self.left`` @
``matrix.transpose``) in ``.py``/``.pyi`` files — with string-literal
state tracked across the lines of a multi-line triple-quoted literal,
across backslash-continued ordinary strings, and rebuilt at every hunk
entry from the post-image content rather than assumed to start in code
(when that content cannot be loaded, suppression is dropped for the
file so it fails toward extraction), PEP 701 f-string and Python 3.14 template-string literals —
single- or triple-quoted, including one left open by a raw newline
inside a replacement field or by a backslash inside a field's nested
string — scanned brace-aware so same-quote replacement fields never
flip the parity and hide an address inside the literal — is an
operator rather than an address; and a HiDPI asset basename
(png/svg/webp/gif/jpeg/avif/ico/bmp/tif/apng) may contain ``+``/``%``
and end a sentence (a trailing ``.`` after the extension does not
un-suppress it). Git C-quoted diff paths (``"b/caf\303\251.py"``) are
decoded before suffix checks, keeping the original bytes (surrogate
escapes) so index lookups resolve names that are not valid UTF-8.

It checks only NEWLY-ADDED lines (the ``+`` side of the diff), so existing
content is never re-flagged and no allow-list churn is needed — a new fixture
should simply use a reserved domain (``[email protected]``, ``[email protected]``). For a
line that legitimately needs a non-reserved address (a real support address in
a doc, a security fixture that must use a real host), add the case-insensitive
``email-domains: allow`` substring anywhere on that same line.

- In CI on a pull request, it checks the PR's added lines (``merge-base..head``)
  and reads the allow-list from the *base* ref so a PR cannot authorize an
  address by editing ``pyproject.toml`` in the same change.
- Locally at the pre-commit stage, it checks the staged diff's added lines.

Those two are the only sources of added lines. Outside a ``pull_request``
(or ``pull_request_target``) event — a ``push``, ``merge_group`` or
``workflow_call`` run — and locally with a clean index, the diff is empty,
so the hook has nothing to scan and exits 0 without checking anything. Only
a *failure to resolve* a range fails closed; an absent range is vacuous.

A flagged address and Git command output are not printed, but the affected
repository path is, with the 1-based line number of the offending added line
(``path:line``) so a false positive can be located without the address
appearing in the log. Range-resolution failures fail *closed*.
"""

from __future__ import annotations

import json
import os
import re
import subprocess
import sys
import tomllib
import unicodedata
from bisect import bisect_right
from collections.abc import Callable
from pathlib import Path

NOREPLY_SUFFIX = "@users.noreply.github.com"
GITHUB_SERVICE_ADDRESSES = {"[email protected]", "[email protected]"}
# ``--src-prefix``/``--dst-prefix`` pin the unified-header prefixes: a
# local ``diff.noprefix`` config would emit a ``b/…`` file's real path
# verbatim, the hardcoded ``b/`` strip would then resolve the WRONG
# post-image (a root decoy) and rebuild hunk-entry state from it.
DIFF_FLAGS = (
    "--no-color",
    "--no-ext-diff",
    "--no-textconv",
    "--text",
    "--src-prefix=a/",
    "--dst-prefix=b/",
)

# RFC-2606 reserved second-level domains (documentation/testing).
RESERVED_DOMAINS = {"example.com", "example.org", "example.net"}
# RFC-2606 / RFC-6761 reserved suffixes. ``.example.com`` etc. cover subdomains
# such as ``smtp.example.com``; ``.example`` covers the reserved TLD.
RESERVED_SUFFIXES = (
    ".example.com",
    ".example.org",
    ".example.net",
    ".test",
    ".invalid",
    ".localhost",
    ".example",
)
# Only these GitHub service addresses legitimately appear in clone URLs / CI
# output. Other addresses at github.com are not a safe allow-list category.

# Dependency lockfiles record third-party author emails we neither own nor
# control; they are machine-generated, so checking their added lines only
# surfaces upstream identities. Matched by basename (a lockfile is a lockfile
# wherever it lives).
SKIP_BASENAMES = {
    "package-lock.json",
    "pdm.lock",
    "yarn.lock",
    "pnpm-lock.yaml",
    "poetry.lock",
    "uv.lock",
    "Pipfile.lock",
    "Cargo.lock",
    "Gemfile.lock",
    "composer.lock",
}

# Per-line opt-out marker: a case-insensitive substring anywhere on the added
# line allows a non-reserved address on that line.
ALLOW_MARKER = "email-domains: allow"


def _combining_mark_fragment() -> str:
    """Regex fragment matching one combining mark (categories Mn/Mc).

    ``re`` has no ``\\p{M}``; derive the ranges from the running
    interpreter's Unicode tables so U-labels containing marks (Devanagari
    matras, Arabic vowel signs, NFD Latin diacritics) match without a
    hand-maintained table. The scan covers the ENTIRE codepoint space:
    combining marks exist far above the BMP (Coptic U+2CEF, Tifinagh
    U+2D7F, Devanagari Extended, adaption marks, variation selectors up
    to U+E01EF), and any lower ceiling would truncate a domain at its
    last below-ceiling character: ``[email protected]`` followed by U+2CEF
    must never be accepted as the bare, reserved-looking
    ``[email protected]``. Marks below U+0300 are spacing symbols, so the
    scan starts there.

    The runs are split at the BMP boundary: a flat class of BMP marks
    compiles to a bitmap, but a single supplementary member flattens the
    whole class into linear range scans. Supplementary marks therefore sit
    behind a coarse plane test that fails in one comparison for any BMP
    character, keeping plain-ASCII scanning as fast as before.

    The class is derived from the RUNNING interpreter's Unicode table, so
    it is exactly as new as that interpreter: a codepoint assigned as a
    mark only in a later UCD is a mark under a newer Python and ordinary
    prose under an older one (U+11F5A is Mn in UCD 16 / Python 3.14 and
    unassigned in UCD 15 / Python 3.12, which this repository also runs).
    That is deliberate — a hand-pinned table would go stale and silently
    truncate domains — but it means the *tests* must pin behaviour to
    codepoints assigned in the OLDEST Unicode table in use, so every
    interpreter in CI agrees.
    """
    runs: list[tuple[int, int]] = []
    run_start = previous = -1
    category = unicodedata.category
    for codepoint in range(0x0300, sys.maxunicode + 1):
        if category(chr(codepoint))[0] != "M":
            continue
        if run_start < 0:
            run_start = previous = codepoint
        elif codepoint == previous + 1:
            previous = codepoint
        else:
            runs.append((run_start, previous))
            run_start = previous = codepoint
    if run_start >= 0:
        runs.append((run_start, previous))

    def _escape(codepoint: int) -> str:
        return (
            f"\\u{codepoint:04X}"
            if codepoint <= 0xFFFF
            else f"\\U{codepoint:08X}"
        )

    # No mark run straddles the BMP boundary today; split anyway so the
    # partition stays exhaustive for any future Unicode table.
    bmp_runs: list[tuple[int, int]] = []
    sup_runs: list[tuple[int, int]] = []
    for low, high in runs:
        if high <= 0xFFFF:
            bmp_runs.append((low, high))
        elif low > 0xFFFF:
            sup_runs.append((low, high))
        else:
            bmp_runs.append((low, 0xFFFF))
            sup_runs.append((0x10000, high))

    def _class_body(runs_list: list[tuple[int, int]]) -> str:
        return "".join(
            _escape(low) if low == high else f"{_escape(low)}-{_escape(high)}"
            for low, high in runs_list
        )

    bmp_class = f"[{_class_body(bmp_runs)}]"
    if not sup_runs:
        return bmp_class
    return (
        rf"(?:{bmp_class}|(?=[\U00010000-\U0010FFFF])"
        rf"[{_class_body(sup_runs)}])"
    )


# Final domain label, all-ASCII forms: an all-letter TLD ("com", "de") or an
# IDNA A-label ("xn--p1ai"), so the ASCII punycode form of an
# internationalized domain counts as a domain too.
_FINAL_LABEL = (
    r"(?:[A-Za-z]{2,63}"
    r"|[Xx][Nn]--[A-Za-z0-9](?:[A-Za-z0-9-]{0,55}[A-Za-z0-9])?)"
)
_ASCII_DOMAIN = (
    r"(?=[A-Za-z0-9.-]{1,253}(?![A-Za-z0-9-]|\.[A-Za-z0-9-]))"
    r"(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+"
    + _FINAL_LABEL
    + r"(?![A-Za-z0-9_%+-]|\.[A-Za-z0-9-])"
)

# U-label characters are deliberately partitioned into disjoint classes.
# The previous overlapping ASCII / Unicode alternatives let a failing final
# label lookahead re-split every ASCII character, causing exponential time.
_MARK = _combining_mark_fragment()
_HIRAGANA = r"[\u3041-\u3096]"
# Han, Hangul and Katakana bases (plus CJK signs such as iteration marks
# and Hangzhou numerals): word characters, so without an explicit rule they
# would widen a domain label. Unspaced prose in these scripts glues onto a
# completed address exactly like Hiragana, so a base from this set only
# continues a label that is already CJK-led; after any other base it is
# prose and terminates the domain instead. Ranges are the stable CJK
# letter blocks, including halfwidth kana and supplementary Han.
_CJK_PROSE = (
    r"[\u1100-\u11FF\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303A"
    r"\u3131-\u318F\u31F0-\u31FF\u3400-\u4DBF\u4E00-\u9FFF\uA960-\uA97C"
    r"\uAC00-\uD7A3\uF900-\uFAFF\u30A1-\u30FF\uFF66-\uFF9F"
    r"\U00020000-\U0002FA1F\U00030000-\U000323AF]"
)
# Everything the label grammars exclude besides ``_``: the Hiragana block
# (Hiragana prose boundary) and the CJK prose ranges above.
_NOT_PROSE = (
    r"\u3040-\u309F\u1100-\u11FF\u3005-\u3007\u3021-\u3029\u3031-\u3035"
    r"\u3038-\u303A\u3131-\u318F\u31F0-\u31FF\u3400-\u4DBF\u4E00-\u9FFF"
    r"\uA960-\uA97C\uAC00-\uD7A3\uF900-\uFAFF\u30A1-\u30FF\uFF66-\uFF9F"
    r"\U00020000-\U0002FA1F\U00030000-\U000323AF"
)
_NON_H_LABEL_CHAR = rf"(?:[^\W_{_NOT_PROSE}]|-|{_MARK})"
_NON_H_LABEL_EDGE = rf"[^\W_{_NOT_PROSE}]"
# Starts with a letter/digit; may end with a letter/digit or combining mark,
# but never a hyphen. This admits valid labels such as ``q̇``. CJK bases are
# absent from a non-CJK run: they end it (as prose), never widen it.
_NON_CJK_LABEL = (
    rf"(?>{_NON_H_LABEL_EDGE}"
    rf"(?:{_NON_H_LABEL_CHAR}{{0,61}}"
    rf"(?:{_NON_H_LABEL_EDGE}|{_MARK}))?)"
)
_CJK_LABEL = (
    rf"(?>{_CJK_PROSE}"
    rf"(?:(?:{_CJK_PROSE}|{_MARK}){{0,61}}"
    rf"(?:{_CJK_PROSE}|{_MARK}))?)"
)
# A label in a position where more domain must follow may mix scripts
# (``a中`` / ``中a`` round-trip through IDNA to ``xn--`` A-labels), so it
# accepts any word character. Atomic labels keep rejected candidates
# linear; the outer loop remains backtrackable for a trailing domain dot.
# The FINAL label keeps the stricter grammars below first: a CJK base
# directly after an all-ASCII final label is unspaced prose (the
# unspaced-prose rule), never a wider domain.
_MIXED_LABEL = (
    rf"(?>[^\W_]"
    rf"(?:(?:[^\W_]|-|{_MARK}){{0,61}}"
    rf"(?:[^\W_]|{_MARK}))?)"
)
# Mixed-script FINAL label: same shape as ``_MIXED_LABEL`` but required
# to contain a CJK base, so a lone-ASCII-letter TLD cannot slip through
# this door (``foo.a`` stays unmatchable) and pure-Hiragana prose after
# an ASCII base (``foo.aです``) keeps the Hiragana boundary. The Hiragana
# block is subtracted from the class as an explicit code-point range:
# relying on alternative ordering alone kept that boundary only while
# nothing forced backtracking past it, so inside explicit delimiters —
# where the match must reach the closing delimiter — the label widened
# through Hiragana prose (``テストをご確認ください``) and glued the prose
# tail onto the domain as a phantom undeclared address. An explicit
# range cut cannot drift with the interpreter's Unicode tables, unlike
# the ambient ``\w`` membership it replaces.
_MIXED_FINAL_CHAR = rf"(?:[^\W_\u3040-\u309F]|-|{_MARK})"
_MIXED_FINAL_EDGE = r"[^\W_\u3040-\u309F]"
_MIXED_FINAL_LABEL = (
    rf"(?>(?={_MIXED_FINAL_CHAR}{{0,61}}{_CJK_PROSE})"
    rf"{_MIXED_FINAL_EDGE}(?:{_MIXED_FINAL_CHAR}{{0,61}}"
    rf"(?:{_MIXED_FINAL_EDGE}|{_MARK}))?)"
)
_NON_ASCII_LABEL_CHAR = rf"(?:[^\W_\x00-\x7F{_NOT_PROSE}]|{_MARK})"
_IDN_FINAL_LABEL = (
    rf"(?>(?:{_HIRAGANA}+(?:{_CJK_LABEL}|{_NON_CJK_LABEL})"
    rf"|{_HIRAGANA}*{_CJK_LABEL}"
    rf"|{_HIRAGANA}*(?={_NON_H_LABEL_CHAR}{{0,62}}{_NON_ASCII_LABEL_CHAR})"
    rf"{_NON_CJK_LABEL})|{_HIRAGANA}+)"
)

# Whole-domain bound: at most 253 label/dot characters ending at a real
# boundary (RFC 1035), checked once for every extractor below.
_DOMAIN_RUN = rf"(?:[-.]|[^\W_]|{_MARK})"
_DOMAIN_BOUND = (
    rf"(?={_DOMAIN_RUN}{{1,253}}"
    rf"(?!(?:[^\W_]|-|{_MARK})|\.(?:[^\W_]|-|{_MARK})))"
)
# A glued word character (except hiragana prose), %, +, or - is a near miss
# such as ``[email protected]`` rather than an address. A further dotted label
# is likewise not a boundary; ordinary URI and prose delimiters are.
_LABEL_START = rf"(?:[^\W_]|-|{_MARK})"
_FINAL_GUARD = rf"(?!(?:[^\W_{_NOT_PROSE}]|[%_+-])|\.{_LABEL_START})"

# One domain grammar for every email form: any labels, then a final label
# that is a U-label, all-ASCII, or mixed-script. Trying the U-label form
# first prevents phantom ASCII prefixes of mixed Unicode labels; the
# mixed final label is last so ASCII-label-plus-CJK-prose still prefers
# the shorter ASCII domain (the unspaced-prose rule).
_DOMAIN = (
    _DOMAIN_BOUND
    + rf"(?:{_MIXED_LABEL}\.)+"
    + rf"(?:{_IDN_FINAL_LABEL}|{_FINAL_LABEL}|{_MIXED_FINAL_LABEL})"
    + _FINAL_GUARD
)
# Local parts are Unicode-aware (SMTPUTF8-style ``müller@…``): any word
# character outside the CJK prose ranges, the ASCII specials ``._%+-``,
# and combining marks. The lookbehind uses the same class so a Unicode
# local part is never entered mid-token (``ller@…`` must not match when
# the declared author is ``müller``).
_LOCAL_PART_CHAR = rf"(?:[^\W_{_NOT_PROSE}]|[._%+-]|{_MARK})"
# Pure-CJK local parts (``用户@…`` / ``ユーザー@…``) are valid SMTPUTF8
# addresses too. A CJK run is admitted only when it contains no ASCII
# word character: a mixed run (``宛先はuser``) is prose around an ASCII
# address, which the dot-atom branch above already extracts whole.
# Hiragana stays out — the Hiragana prose boundary — so ``宛先はユーザー@…``
# yields ``ユーザー@…`` and nothing wider, and the lookbehind again
# forbids entering the run mid-token.
_CJK_LOCAL_PART = (
    rf"(?>(?<!{_CJK_PROSE}|{_MARK})"
    rf"{_CJK_PROSE}(?:{_CJK_PROSE}|{_MARK}){{0,63}})"
)
# Preserve interior apostrophes and exclamation marks without consuming a quote that
# merely opens a source string. Explicit delimiters below cover complete
# identities with leading punctuation or mixed-script local parts.
_LOCAL_CONTINUATION = rf"(?:{_LOCAL_PART_CHAR}|[!'])"
_HIRAGANA_LOCAL_PART = (
    rf"(?>(?<!{_HIRAGANA}|{_MARK})"
    rf"{_HIRAGANA}(?:{_HIRAGANA}|{_MARK}){{0,63}})"
)
EMAIL_RE = re.compile(
    rf"(?:(?<!{_LOCAL_PART_CHAR}){_LOCAL_PART_CHAR}{_LOCAL_CONTINUATION}{{0,63}}"
    rf"|{_CJK_LOCAL_PART}|{_HIRAGANA_LOCAL_PART})@" + _DOMAIN,
)
# RFC 5321 quoted-string local part ("john doe"@…): a valid address form
# the dot-atom pattern above cannot see.
QUOTED_EMAIL_RE = re.compile(r'"(?:[^"\\\n]|\\.){1,64}"@' + _DOMAIN)


# Angle brackets, quotes and Markdown code delimiters make the full address
# boundary explicit. Within them, Unicode local parts and final labels cannot
# be mistaken for surrounding prose — except that the Hiragana block never
# widens a label even here: a prose tail before the closing delimiter
# (``テストをご確認ください``) is prose, not a longer domain. Keep the
# remaining unspaced-prose rules outside these delimiters, where the text
# is inherently ambiguous.
#
# Two of the delimiters — the apostrophe and the backtick — are themselves
# legal local-part punctuation, so each wrapper DROPS its own character from
# the class it encloses. Otherwise a DOUBLED delimiter reads as delimiter
# plus a one-character local part: a Markdown-quoted decorator written with
# two backticks around "@router.get" parsed as an address whose local part
# was a lone backtick, on the non-reserved domain ``router.get`` — a pure
# false positive on ordinary prose. Tripled and mixed wrappers fall to the
# same rule together with the word-character requirement below: a delimited
# local part must contain at least one ``[^\W_]`` character, so a run of
# pure punctuation is never an identity. Real local parts (``user``,
# ``o'connor``, ``!lead``) all carry one, and the lookahead cannot cross the
# ``@`` that ends the local part, so the character it finds always lies
# inside the local part the class then matches.
_DELIMITED_LOCAL_PUNCTUATION = "._!#$%&'*+/=?^`{|}~-"
_DELIMITED_LOCAL_WORD = r"(?=[^@\n]{0,63}[^\W_])"


def _delimited_local(wrapper: str) -> str:
    """Delimited local-part pattern, minus ``wrapper``'s own character."""
    punctuation = re.escape(
        "".join(c for c in _DELIMITED_LOCAL_PUNCTUATION if c != wrapper)
    )
    return (
        _DELIMITED_LOCAL_WORD + rf"(?:[^\W_]|[{punctuation}]|{_MARK}){{1,64}}"
    )


def _delimited_address(wrapper: str) -> str:
    """Delimited address (dot-atom or RFC 5321 quoted) for one wrapper."""
    return (
        rf'(?:{_delimited_local(wrapper)}|"(?:[^"\\\n]|\\.){{1,64}}")@'
        + _DOMAIN
    )


# The same two delimiters occur INSIDE words (``can't``, a backtick in
# transliteration), where they open nothing. An opening apostrophe or
# backtick preceded by a local-part character is therefore not a delimiter:
# without this, dropping the wrapper from its own class merely moved the
# boundary one word inwards and ``'can'[email protected]'`` re-entered at the
# apostrophe as ``[email protected]``. The bare matcher already guards its local
# part with the same lookbehind. ``<`` and ``"`` are not local-part
# characters, so they keep the RFC 5322 ``Name<addr>`` form.
#
# That blanket rule is too blunt for the one preceding run that is not a
# word continuing into the quote: a Python string prefix. ``f'…'``,
# ``rf'…'``, ``u'…'``, ``b'…'`` all put a local-part character (the
# prefix letter) directly before the quote, so the plain lookbehind
# above also swallowed ``f'{user}@example.com'`` — the prefix, not a
# word, was what preceded the opener. A run of 1-2 letters from
# ``rRbBuUfFtT`` immediately before the quote, itself preceded by
# start-of-line or a non-word character, is therefore still an opener:
# ``_STRING_PREFIX_OPENER`` restates the exclusion as "not preceded by a
# local-part character UNLESS that preceding run is such a prefix".
# Python's ``re`` forbids differently-sized alternatives inside one
# lookbehind, so the three cases are three independent zero-width
# assertions joined by ordinary alternation, each internally fixed-width.
# This does not special-case a JS tagged template
# (``` tag`${u}@corp.example` ```): a tag name can be any identifier
# length, and admitting an opener after an arbitrary identifier would
# undo the ``can't`` guard for every prose word immediately followed by
# a backtick. What this carve-out reaches is narrower than "any address
# led by an interpolation": it only fires when the interpolation opens
# the literal AND the address is the whole literal — nothing before the
# interpolation, nothing after the domain but the closing delimiter.
# Leading text (``f"mailto:{u}@corp.example"``), trailing text
# (``f"{u}@corp.example, cc"``), an interpolation that is not the very
# first thing in the literal, tagged or not
# (``` html`Hi ${u}@corp.example` ```, untagged
# `` `Hi ${u}@corp.example` ``), and a ``str.format()`` call
# (``"{}@corp.example".format(u)``) are all unreported. A literal
# address anywhere in the template (``` tag`[email protected]` ```) is
# still caught by the bare matcher regardless of the tag, since it
# starts its own token right after the backtick. An UNTAGGED template
# opens like any other quote — the backtick itself is preceded by a
# non-local-part character — so `` `${u}@corp.example` `` (interpolation
# leading, filling the whole template) is caught with no carve-out
# needed; a 1-2 letter tag drawn from ``_STRING_PREFIX_CHARS`` reaches
# the same case via the carve-out above
# (``` f`${u}@corp.example` ```); any other tag
# (``` tag`${u}@corp.example` ```) does not.
_STRING_PREFIX_CHARS = "rRbBuUfFtT"
_STRING_PREFIX_OPENER = (
    rf"(?:(?<!{_LOCAL_PART_CHAR})"
    rf"|(?<=[{_STRING_PREFIX_CHARS}])(?<!\w[{_STRING_PREFIX_CHARS}])"
    rf"|(?<=[{_STRING_PREFIX_CHARS}]{{2}})(?<!\w[{_STRING_PREFIX_CHARS}]{{2}})"
    rf")"
)
_INTRAWORD_DELIMITERS = ("'", "`")
DELIMITED_EMAIL_RES = tuple(
    re.compile(
        (_STRING_PREFIX_OPENER if left in _INTRAWORD_DELIMITERS else "")
        + re.escape(left)
        + rf"(?P<email>{_delimited_address(left)})"
        + re.escape(right)
        + r"(?!@)"
    )
    for left, right in (("<", ">"), ('"', '"'), ("'", "'"), ("`", "`"))
)


def _delimited_email_spans(content: str) -> list[tuple[int, int, str, int]]:
    """Disjoint explicit boundaries, outermost first for nested matches."""
    candidates = sorted(
        (
            (m.start(), m.end(), m.group("email"), m.start("email"))
            for pattern in DELIMITED_EMAIL_RES
            for m in pattern.finditer(content)
        ),
        key=lambda item: (item[0], -item[1]),
    )
    spans: list[tuple[int, int, str, int]] = []
    for start, end, email, address_start in candidates:
        if spans and start < spans[-1][1]:
            continue
        spans.append((start, end, email, address_start))
    return spans


_URI_HOST = rf"(?:{_MIXED_LABEL}\.)+(?:{_FINAL_LABEL}|{_MIXED_LABEL})"
# RFC 3986 userinfo is pct-encoded, but real-world connection strings
# (MongoDB and friends) put raw "@" characters inside the password
# ("mongodb://admin:p@ss@db…"), so a raw "@" (and the ":" of
# "user:password") is allowed inside the userinfo span; the host still
# has to parse, which anchors the far end. Every other separator — ","
# ";" "&" whether bare or opening a "KEY=" assignment of any label
# length or shape, and bare "!" "$" "(" "*" "~" "'" "=" — ENDS the
# span. Crossing them let the span swallow a separately written real
# address as the URI host: in "postgres://[email protected],victim@…" the
# host parsed as the victim's domain and the hook failed open. The
# price is conscious: a password that itself contains one of those
# separators is no longer suppressed and needs the allow marker.
_URI_USERINFO_ATOM = (
    r"(?:%[0-9A-Fa-f]{2}|\$\{[A-Za-z_][A-Za-z0-9_]*\}|"
    r"[A-Za-z0-9._@:+-])"
)
URI_USERINFO_RE = re.compile(
    r"(?<![A-Za-z0-9+.-])[A-Za-z][A-Za-z0-9+.-]{0,254}://"
    r"(?P<userinfo>(?:"
    + _URI_USERINFO_ATOM
    + r"){1,193})@"
    + _URI_HOST
    + r"(?::[0-9]{1,5})?(?![^\W_.:-])"
)
# Pixel-density assets include fractional scales such as ``@1.5x``. The
# basename may contain ``+``/``%`` and interior ``!``/apostrophes — including
# a Python string-prefix letter and its opening quote, which the prefix
# carve-out above (``_STRING_PREFIX_OPENER``) can also read as opening a
# delimited address whose local part starts a few characters INTO this
# span, not at its start (``u'[email protected]'``: this pattern's own match
# starts at ``u``, but the delimited address it must suppress starts at
# ``i``). The suppression below therefore covers the whole span, not just
# its start offset — a start-only suppression left that inner offset
# unsuppressed and the address was reported. A sentence-final ``.`` right
# after the extension (``Use [email protected].``) is prose punctuation, not a
# longer filename — but a ``.`` followed by more basename characters
# still breaks the asset boundary.
#
# The same prefix carve-out also opens an f-string INTERPOLATED basename
# (``f'{stem}@3x.webp'``, ``f"{name}@2x.png"``): the delimited local part
# there includes ``{``/``}`` (they are legal RFC 5322 local-part
# punctuation, matched by ``_delimited_local`` above), so this pattern's
# own basename class matches them too — otherwise its match could not
# reach far enough left to cover that address's start, and it would stay
# a start-only-style gap even with the full-span fix. A double-quoted
# f-string needs ``"`` (``\x22``, to keep the enclosing raw string simple)
# in that same class for the same reason: only a single quote was needed
# before the prefix carve-out existed to consume it too.
#
# This widening only reaches as far left as the interpolation's own
# braces — neither ``/`` nor ``$`` is in this basename class, so a path
# qualifier ahead of the interpolation, or a JS ``${...}`` interpolation,
# is NOT covered: the asset match itself can only start after the ``/``
# or ``$``, so its span starts later than the delimited address does,
# that address's own start offset falls outside the suppressed range,
# and it is still reported. That is a false positive, not a missed real
# address — none of the examples below is genuine — and the hook fails
# toward reporting rather than staying silent about them:
# ``u'img/[email protected]'``  (email-domains: allow)
# ``f'{d}/[email protected]'``  (email-domains: allow)
# ```` f`${x}@2x.png` ````  (email-domains: allow)
HIDPI_ASSET_RE = re.compile(
    r"(?P<asset>(?<![A-Za-z0-9._%+-])[A-Za-z0-9._%+-][A-Za-z0-9._%+!'\x22{}-]{0,127}@[0-9]+(?:\.[0-9]+)?x\."
    r"(?:avif|gif|jpe?g|png|svg|webp|ico|bmp|tiff?|apng))(?![A-Za-z0-9_%+-]|\.[A-Za-z0-9_%+-])",
    re.IGNORECASE,
)
# scp-style remotes carry a colon and then a path that may be a single
# component ("[email protected]:myrepo.git") or a slash tree ("…:/srv/…").
SCP_REMOTE_RE = re.compile(
    r"(?P<remote>(?<![A-Za-z0-9._%+-])[A-Za-z0-9._%+-][A-Za-z0-9._%+!'-]{0,63}@"
    + _ASCII_DOMAIN
    + r"):(?:[^\s,;<>()[\]\"'`]*/[^\s,;<>()[\]\"'`]*"
    + r"|[^\s,;<>()[\]\"'`]*\.git(?![^\s,;<>()[\]\"'`]))"
)

# A ONE-character local part sitting immediately after a backslash is an
# escape sequence, not an identity: ``"\[email protected]"`` in a Python string,
# ``'\[email protected]'`` in YAML, ``\r``/``\\`` in JSON or Markdown code all read as
# escape letter plus a dotted attribute path, and the extractor used to
# report the escape letter as a local part on the non-reserved domain
# ``router.get``. Only the single-character shape is suppressed, and the
# offset it suppresses is added to ``suppressed_offsets`` before any
# finding is built: the match is never reported at all, not merely
# allow-listed, so ``email-domains: allow`` has nothing to act on — a
# genuine one-character local part directly after a backslash (``\a@…``)
# can only be written some other way. A real address written after a
# backslash (``\\user@…`` -> local part ``user``) is unaffected and is
# still extracted, because there ``user`` is more than the one character
# this pattern matches. That same width limit means a MULTI-character
# escape — ``\x41@…``, ``\u00e9@…``, an octal ``\101@…`` — is not
# suppressed either: the pattern only ever consumes one character between
# the backslash and the ``@``, so a longer escape's tail (``x41``,
# ``u00e9``, ``101``) is left to the ordinary local-part matcher and
# reported like any other address.
_ESCAPED_SINGLE_LOCAL_RE = re.compile(r"\\(?P<local>[^\\@\n])@")

# PEP 465 matrix multiplication: in Python source, an unspaced ``[email protected]``
# chain (dotted operands on either side of ``@``) is an operator
# expression, not an address. Applies only to ``.py``/``.pyi``
# lines, only to an operand in code context (after an operator or keyword),
# and only outside string literals and comments — quoted or commented
# addresses are still scanned, including inside multi-line triple-quoted
# literals whose state is carried across lines by ``_py_code_spans``.
_PY_MATMUL_RE = re.compile(
    r"(?:^|[\s=(,+\-*/%&|^!~<>\[\{}:]"
    r"|\b(?:and|or|not|in|is|if|else|elif|while|for|return|yield|await|"
    r"assert|del|lambda)[ \t]+)"
    r"(?P<expr>[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*"
    r"@[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)+)"
)


class _PyStringState:
    """Quote state carried across a Python file's post-image lines.

    Triple-quoted literals span newlines, and an ordinary single- or
    double-quoted string joined to the next physical line by a
    backslash-newline does too, so the facts a line needs from its
    predecessors are the quote character of an unclosed ``\"\"\"`` /
    ``'''`` opener and of a backslash-continued ordinary string (both
    ``\"\"`` when the line starts in code). Without them, each added
    diff line was classified in isolation: a docstring body line counted
    as code (matmul-suppressing an address inside it), and a
    backslash-continued ordinary string lost its state at the newline.
    ``fstring`` marks a carried quote opened on a prefixed f-string: its
    replacement fields may nest same-quote strings, so the continuation
    line is scanned with the f-string rules, not quote parity. PEP 701
    also lets a raw newline sit inside a replacement field (and a
    triple-quoted f-string span lines in its literal part), so an open
    f-string is carried whatever the line ends on — ``quote`` then holds
    the full closing delimiter (one quote character, or three), with
    ``depth``/``nested`` preserving the field-nesting and nested-string
    scan position for a backslash inside a field's string.
    """

    __slots__ = ("triple", "quote", "fstring", "depth", "nested")

    def __init__(self) -> None:
        self.triple = ""
        self.quote = ""
        self.fstring = False
        self.depth = 0
        self.nested = ""

    def copy(self) -> _PyStringState:
        clone = _PyStringState()
        clone.triple = self.triple
        clone.quote = self.quote
        clone.fstring = self.fstring
        clone.depth = self.depth
        clone.nested = self.nested
        return clone


# PEP 701 (Python 3.12) lets an interpolated replacement field reuse the
# enclosing quote character, so plain quote parity cannot find the
# closing quote of a prefixed string: in f"{" ... "}" the inner same
# quote would flip parity and the literal between the fields would be
# classified as code, matmul-suppressing an address written inside it.
# A quote preceded by one of these prefixes therefore switches to a
# brace-nesting scan that classifies the whole literal — replacement
# fields included — as string, never code (fail toward extraction).
# Template strings use the same replacement-field lexical rules as f-strings.
_FSTRING_PREFIXES = frozenset({"f", "fr", "rf", "t", "tr", "rt"})


def _fstring_prefix_at(content: str, quote_index: int) -> bool:
    """True if the quote opens an interpolated f-string or template string."""
    start = quote_index
    while start > 0 and (
        content[start - 1].isalnum() or content[start - 1] == "_"
    ):
        start -= 1
    return content[start:quote_index].lower() in _FSTRING_PREFIXES


def _scan_fstring_body(
    content: str,
    index: int,
    closer: str,
    depth: int = 0,
    nested: str = "",
) -> tuple[int, bool, bool, int, str]:
    """Scan an f-string literal starting inside its literal part.

    Tracks ``{``/``}`` replacement-field nesting and the string literals
    those fields may open (any quote character or triple-quote run,
    escapes included), so only a ``closer`` quote run in the literal
    part closes the f-string; ``{{`` and ``}}`` are escaped braces, not
    fields. ``closer`` is the full closing delimiter — one quote
    character, or the three of a triple-quoted f-string, so PEP 701's
    same-quote (even same-triple-quote) nesting cannot flip the parity
    either. ``depth`` and ``nested`` resume the scan mid-field (after a
    backslash continuation or a raw newline inside a replacement field).
    Returns the index just past the closing delimiter, whether the
    literal is still open at end of line, whether the line ends on an
    escaped character, and the field depth / open nested-string
    delimiter at that end.
    """
    length = len(content)
    escaped = False
    while index < length:
        char = content[index]
        if escaped:
            escaped = False
        elif char == "\\":
            escaped = True
        elif nested:
            if content.startswith(nested, index):
                index += len(nested)
                nested = ""
                continue
        elif depth:
            if char == "#":
                # Python 3.12 allows a comment inside a multi-line
                # replacement field; it runs to end of line (its ``}`` /
                # quotes are not field content), and the open field —
                # depth and nested string included — carries to the next
                # physical line instead of the comment tail being read
                # as the literal's code-span continuation.
                break
            if char == "{":
                depth += 1
            elif char == "}":
                depth -= 1
            elif char in "'\"":
                run = 1
                while (
                    run < 3
                    and index + run < length
                    and content[index + run] == char
                ):
                    run += 1
                if run >= 3:
                    nested = char * 3
                    index += 3
                    continue
                if run == 2:
                    index += 2  # an empty string token
                    continue
                nested = char
        elif content.startswith(closer, index):
            return index + len(closer), False, False, depth, nested
        elif char == "{":
            if content.startswith("{{", index):
                index += 1
            else:
                depth = 1
        elif char == "}" and content.startswith("}}", index):
            index += 1
        index += 1
    return length, True, escaped, depth, nested


def _py_code_spans(
    content: str, state: _PyStringState
) -> list[tuple[int, int]]:
    """Half-open [start, end) spans of ``content`` that are code, not string
    literal or comment.

    Tracks quote parity and backslash escapes within the line, ends the
    code region at a ``#`` outside a string, and carries an open
    triple-quoted string across lines through ``state``. An ordinary
    quote left open by a trailing backslash continues on the next
    physical line and is carried too; any other unterminated quote keeps
    only the tail classified as string. A quote opened on a prefixed
    f-string — single- or triple-quoted — switches to
    ``_scan_fstring_body``: PEP 701 same-quote nesting makes parity
    ambiguous there, so the whole literal — inner expressions included —
    is classified as string and an address inside it is never
    matmul-suppressed. An f-string left open at end of line (a raw
    newline inside a replacement field, or the multi-line literal part
    of a triple-quoted one) is carried through ``state`` along with its
    field depth and nested-string quote, so the continuation line is
    scanned with the f-string rules whatever the line ends on.
    """
    spans: list[tuple[int, int]] = []
    start = index = 0
    triple = state.triple
    quote = state.quote
    fstring = state.fstring
    depth = state.depth
    nested = state.nested
    escaped = False
    length = len(content)
    if quote and fstring:
        # A continued f-string resumes inside its literal part, field
        # nesting and nested-string quote included, so a backslash (or
        # raw newline) inside a field's string cannot close the literal
        # at the wrong quote.
        end, still_open, tail_escaped, depth, nested = _scan_fstring_body(
            content, 0, quote, depth, nested
        )
        if still_open:
            escaped = tail_escaped
            index = length  # the whole line stays literal text
        else:
            start = index = end
            quote = ""
            fstring = False
    while index < length:
        char = content[index]
        if triple or quote:
            if escaped:
                escaped = False
            elif char == "\\":
                escaped = True
            elif triple:
                if char == triple and content.startswith(triple * 3, index):
                    triple = ""
                    start = index = index + 3
                    continue
            elif char == quote:
                quote = ""
                start = index + 1
        elif char == "#":
            spans.append((start, index))
            break
        elif char in "'\"":
            # Only "is this at least a triple-quote run" matters, so the
            # count stops at three: re-counting a whole quote run per
            # character made a long run quadratic.
            run = 1
            while (
                run < 3
                and index + run < length
                and content[index + run] == char
            ):
                run += 1
            spans.append((start, index))
            if _fstring_prefix_at(content, index):
                # PEP 701 permits a same-quote (even same-triple-quote)
                # literal inside a replacement field, so the prefixed
                # run — of any length — is scanned brace-aware with its
                # own closing delimiter.
                closer = char * 3 if run >= 3 else char
                (
                    end,
                    still_open,
                    tail_escaped,
                    depth,
                    nested,
                ) = _scan_fstring_body(content, index + len(closer), closer)
                if still_open:
                    # Unterminated: the tail stays literal text, and the
                    # open literal continues on the next line — via a
                    # trailing backslash, or the raw newline PEP 701
                    # allows inside a replacement field (and any newline
                    # in a triple-quoted literal part).
                    quote = closer
                    fstring = True
                    escaped = tail_escaped
                    index = length
                else:
                    start = index = end
                continue
            if run >= 3:
                triple = char
                start = index = index + 3
                continue
            quote = char
        index += 1
    else:
        if not (triple or quote):
            spans.append((start, length))
    state.triple = triple
    # A trailing backslash escapes the newline of an open ordinary
    # string: the literal continues on the next physical line. An open
    # f-string continues whatever the line ends on (PEP 701 raw
    # newlines), carrying its closing delimiter, field depth and
    # nested-string quote.
    state.quote = quote if quote and (escaped or fstring) else ""
    state.fstring = fstring and bool(state.quote)
    state.depth = depth if state.fstring else 0
    state.nested = nested if state.fstring else ""
    return spans


def _git(*args: str) -> tuple[int, str]:
    """Run git; return (returncode, stdout). stderr is captured and discarded.

    ``errors="replace"`` keeps a non-UTF8 blob (e.g. a Latin-1 fixture) from
    crashing the decode; U+FFFD is a symbol rather than a letter, so a
    mangled blob cannot fabricate a domain label.
    """
    proc = subprocess.run(
        ["git", *args],
        capture_output=True,
        text=False,
    )
    return proc.returncode, proc.stdout.decode("utf-8", errors="replace")


def parse_author_emails(text: str) -> set[str]:
    """Return lower-cased emails declared exactly in ``[project].authors``."""
    try:
        metadata = tomllib.loads(text)
    except tomllib.TOMLDecodeError:
        raise RuntimeError("could not parse project metadata") from None

    project = metadata.get("project")
    if project is None:
        return set()
    if not isinstance(project, dict):
        raise RuntimeError("invalid project metadata")

    authors = project.get("authors")
    if authors is None:
        return set()
    if not isinstance(authors, list):
        raise RuntimeError("invalid project authors")

    emails: set[str] = set()
    for author in authors:
        if not isinstance(author, dict):
            raise RuntimeError("invalid project author")
        email = author.get("email")
        if email is None:
            continue
        if not isinstance(email, str):
            raise RuntimeError("invalid project author email")
        normalized_email = email.strip().lower()
        if normalized_email:
            emails.add(normalized_email)
    return emails


def is_allowed(email: str, declared: set[str]) -> bool:
    """True if ``email`` uses an allowed (reserved / service / declared) domain."""
    e = email.strip().lower()
    if "@" not in e:
        return True
    domain = e.rsplit("@", 1)[1]
    if e.endswith(NOREPLY_SUFFIX):
        return True
    if e in GITHUB_SERVICE_ADDRESSES:
        return True
    if domain in RESERVED_DOMAINS:
        return True
    if any(domain.endswith(sfx) for sfx in RESERVED_SUFFIXES):
        return True
    if e in declared:
        return True
    return False


def _git_unquote_path(path: str) -> str:
    """Decode a Git C-quoted diff path such as ``"b/caf\\303\\251.py"``.

    Git quotes paths containing non-ASCII (or otherwise-ambiguous)
    characters, emitting backslash escapes whose octal forms are BYTES
    of the filename. Undecoded, ``Path(path).suffix`` keeps the
    trailing quote (``.py\\"``), so Python-file scoping — and matmul
    suppression with it — silently fails for non-ASCII paths. Decoding
    with ``surrogateescape`` also round-trips names that are not valid
    UTF-8: subprocess argv re-encodes lone surrogates back to the
    original bytes, so the ``git show`` post-image lookup resolves such
    a path instead of failing on a U+FFFD stand-in.
    """
    if len(path) < 2 or path[0] != '"' or path[-1] != '"':
        return path
    simple = {
        '"': b'"',
        "\\": b"\\",
        "a": b"\a",
        "b": b"\b",
        "f": b"\f",
        "n": b"\n",
        "r": b"\r",
        "t": b"\t",
        "v": b"\v",
    }
    body = path[1:-1]
    raw = bytearray()
    index = 0
    while index < len(body):
        char = body[index]
        if char != "\\":
            # Git never leaves a literal non-ASCII char inside a quoted
            # path, but encoding it keeps the decode total.
            raw += char.encode()
            index += 1
            continue
        escape = body[index + 1] if index + 1 < len(body) else ""
        if escape in simple:
            raw += simple[escape]
            index += 2
        elif escape and escape in "01234567":
            digits = escape
            index += 1
            while (
                index + 1 < len(body)
                and len(digits) < 3
                and body[index + 1] in "01234567"
            ):
                digits += body[index + 1]
                index += 1
            raw.append(int(digits, 8) & 0xFF)
            index += 1
        else:
            # Unknown escape (Git never emits one): keep it verbatim.
            raw += b"\\"
            index += 1
    return raw.decode("utf-8", errors="surrogateescape")


_HUNK_HEADER = re.compile(r"@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@")


def iter_added_emails(
    diff: str, post_image: Callable[[str], str | None] | None = None
):
    """Yield (path, email) for every email on an added (``+``) diff line.

    The ``(path, email, line)`` form is :func:`iter_added_findings`; this
    wrapper drops the line number for callers that only need the address.
    """
    for path, email, _line in iter_added_findings(diff, post_image=post_image):
        yield path, email


def iter_added_findings(
    diff: str, post_image: Callable[[str], str | None] | None = None
):
    """Yield (path, email, line) for every email on an added (``+``) line.

    ``line`` is the 1-based line number of that added line in the file's
    post-image, counted from each hunk header's new-side start so a
    diagnostic can name ``path:line`` and a contributor can go straight to
    a false positive. Removed lines do not advance it; context lines do.
    A hunk header that does not parse yields ``0``, which the diagnostic
    renders as a bare path rather than a wrong location.

    Parses the unified diff with hunk-state tracking: ``diff --git`` starts a
    new file (headers follow), ``@@`` opens a hunk body. Inside a hunk, every
    ``+`` line is *content* — including a line whose text begins with ``++`` (it
    renders as ``+++…`` but is not a header there), which a naive
    ``startswith('+++ ')`` check would wrongly skip. ``+++`` header paths are
    C-unquoted (``_git_unquote_path``). Splits on ``\\n`` only (not
    ``str.splitlines``, which also breaks on U+2028 etc. and would let text
    after such a char slip past). Skips lockfiles and ``ALLOW_MARKER`` lines.

    For ``.py``/``.pyi`` files, string-literal state is carried across the
    post-image lines visible in the diff (context lines advance it too but
    are never scanned), so an address inside a multi-line triple-quoted
    literal — or a backslash-continued ordinary string — is never mistaken
    for code and matmul-suppressed. A hunk still does not START in a known
    lexical state: three context lines say nothing about an opener far
    above the hunk, and the gap between two hunks hides whatever closed
    the first one. When ``post_image`` maps a path to its post-image
    content (the staged index blob locally, the PR head blob in CI), the
    state at every hunk entry is rebuilt by scanning the real post-image
    above the hunk instead of assuming code. Each file's hunks arrive in
    ascending order, so one monotonic per-file scan serves them all and
    the whole reconstruction stays linear. A path whose post-image
    cannot be loaded (an index entry ``git show`` cannot resolve) fails
    toward extraction: matmul suppression is dropped for the whole file
    rather than guessed from an assumed-code entry state that would
    swallow an address sitting inside a docstring.
    """
    path = "?"
    in_hunk = False
    new_lineno = 0
    # False after an unparseable hunk header: ``new_lineno`` then stays at
    # its 0 sentinel instead of counting up from it, so EVERY added line
    # in that hunk reports the bare-path ``0`` the docstring promises, not
    # a wrong line number that happens to start from 0.
    new_lineno_known = True
    py_state = _PyStringState()
    # path -> (post-image lines, line count already scanned, state after
    # them); None marks a path the loader could not provide, which then
    # keeps the visible-lines approximation for state tracking but loses
    # matmul suppression entirely (py_unresolved) so it cannot fail open.
    py_scan: dict[str, tuple[list[str], int, _PyStringState] | None] = {}
    py_unresolved: set[str] = set()
    for raw in diff.split("\n"):
        line = raw[:-1] if raw.endswith("\r") else raw
        if line.startswith("diff --git "):
            in_hunk = False
            path = "?"
            new_lineno = 0
            new_lineno_known = True
            py_state = _PyStringState()
            continue
        if line.startswith("@@"):
            in_hunk = True
            header = _HUNK_HEADER.match(line)
            new_lineno_known = header is not None
            new_lineno = int(header.group(1)) if header is not None else 0
            if (
                header is not None
                and post_image is not None
                and Path(path).suffix in (".py", ".pyi")
            ):
                scan = py_scan.get(path)
                if scan is None and path not in py_scan:
                    text = post_image(path)
                    if text is None:
                        py_scan[path] = None
                        py_unresolved.add(path)
                    else:
                        scan = (
                            text.replace("\r\n", "\n").split("\n"),
                            0,
                            _PyStringState(),
                        )
                        py_scan[path] = scan
                if scan is not None:
                    lines, upto, cached = scan
                    target = max(
                        min(int(header.group(1)) - 1, len(lines)), upto
                    )
                    entry = cached.copy()
                    for index in range(upto, target):
                        _py_code_spans(lines[index], entry)
                    py_scan[path] = (lines, target, entry)
                    # The hunk body advances its own copy; the cache keeps
                    # the state exactly at ``target`` for the next entry.
                    py_state = entry.copy()
            continue
        if not in_hunk:
            # File-header region: capture the post-image path.
            if line.startswith("+++ "):
                # Strip only Git's unified-header separator tab, not arbitrary
                # trailing whitespace: a file literally named "uv.lock " must
                # not be collapsed to the "uv.lock" skip basename.
                p = line[4:].split("\t", 1)[0]
                p = _git_unquote_path(p)
                path = p[2:] if p.startswith("b/") else p
            continue
        if line.startswith("+"):
            content = line[1:]  # strip the single leading '+' diff marker
            lineno = new_lineno if new_lineno_known else 0
            if new_lineno_known:
                new_lineno += 1
        elif line.startswith(" "):
            # Context lines belong to the post-image: they still advance
            # the Python string-state so a literal opened on an unchanged
            # line closes correctly, but they are never scanned.
            if Path(path).suffix in (".py", ".pyi"):
                _py_code_spans(line[1:], py_state)
            if new_lineno_known:
                new_lineno += 1
            continue
        else:
            # A removed line or a "\\ No newline at end of file" marker: no
            # post-image line, so the new-side counter does not advance.
            continue
        # Advance the string-state past every Python post-image line, even
        # an opted-out one: a docstring body line still opens/closes literals.
        # An unresolvable post-image means the entry state is unknown, so
        # the file fails toward extraction: no span is code, matmul
        # suppression cannot apply, and addresses stay visible.
        code_spans = (
            _py_code_spans(content, py_state)
            if Path(path).suffix in (".py", ".pyi")
            and path not in py_unresolved
            else []
        )
        if ALLOW_MARKER in content.lower():
            continue  # explicit per-line opt-out
        if Path(path).name in SKIP_BASENAMES:
            continue
        suppressed_offsets = {
            offset
            for match in URI_USERINFO_RE.finditer(content)
            for offset in range(match.start("userinfo"), match.end("userinfo"))
        }
        suppressed_offsets.update(
            offset
            for match in HIDPI_ASSET_RE.finditer(content)
            for offset in range(*match.span("asset"))
        )
        suppressed_offsets.update(
            match.start("remote") for match in SCP_REMOTE_RE.finditer(content)
        )
        suppressed_offsets.update(
            match.start("local")
            for match in _ESCAPED_SINGLE_LOCAL_RE.finditer(content)
        )
        if code_spans:
            code_starts = [span[0] for span in code_spans]
            for match in _PY_MATMUL_RE.finditer(content):
                expr_start, expr_end = match.span("expr")
                index = bisect_right(code_starts, expr_start) - 1
                if index >= 0 and expr_end <= code_spans[index][1]:
                    suppressed_offsets.update(range(expr_start, expr_end))
        quoted_spans = [
            match.span() for match in QUOTED_EMAIL_RE.finditer(content)
        ]
        quoted_starts = [span[0] for span in quoted_spans]
        explicit_spans = []
        for span in _delimited_email_spans(content):
            quoted = bisect_right(quoted_starts, span[0]) - 1
            if quoted >= 0 and span[1] <= quoted_spans[quoted][1]:
                # Delimiters inside a quoted local part are literal text,
                # not a second address with a different domain.
                continue
            explicit_spans.append(span)
        explicit_starts = [span[0] for span in explicit_spans]
        emitted_explicit: set[int] = set()
        for pattern in (EMAIL_RE, QUOTED_EMAIL_RE):
            for m in pattern.finditer(content):
                if m.start() in suppressed_offsets:
                    continue
                explicit = bisect_right(explicit_starts, m.start()) - 1
                if explicit >= 0 and m.end() <= explicit_spans[explicit][1]:
                    _, _, email, address_start = explicit_spans[explicit]
                    if (
                        explicit not in emitted_explicit
                        and address_start not in suppressed_offsets
                    ):
                        emitted_explicit.add(explicit)
                        yield path, email, lineno
                    continue
                quoted = bisect_right(quoted_starts, m.start()) - 1
                if pattern is EMAIL_RE and quoted >= 0:
                    start, end = quoted_spans[quoted]
                    if start < m.start() and m.end() < end:
                        # A quoted local part shadows its inner address text.
                        continue
                yield path, m.group(0), lineno
        for index, (_, _, email, address_start) in enumerate(explicit_spans):
            if (
                index not in emitted_explicit
                and address_start not in suppressed_offsets
            ):
                yield path, email, lineno


def _resolve_merge_base(base: str, head: str) -> str:
    """Return merge-base(base, head). Raise on failure.

    Resolve from locally-available history FIRST and fetch only as a fallback,
    so the hook mutates no git state on the PR-CI path (full history is checked
    out there via ``fetch-depth: 0``).
    """
    rc, mb = _git("merge-base", base, head)
    if rc == 0 and mb.strip():
        return mb.strip()
    _git("fetch", "--quiet", "--depth=1000", "origin", head, base)
    rc, mb = _git("merge-base", base, head)
    if rc != 0 or not mb.strip():
        _git("fetch", "--quiet", "--unshallow", "origin")
        rc, mb = _git("merge-base", base, head)
    if rc != 0 or not mb.strip():
        raise RuntimeError("could not resolve the PR commit range")
    return mb.strip()


def _post_image_loader(ref: str | None) -> Callable[[str], str | None]:
    """Return a path -> post-image content callable for state rebuilds.

    ``ref`` is the PR head SHA in CI; ``None`` addresses the staged
    index locally (the exact blob the staged diff was computed from).
    One ``git show`` per path, cached: a file with many hunks asks
    once. A path git cannot show (deleted, synthetic) yields None so
    the caller disables matmul suppression for that file rather than
    guessing its Python string state.
    """
    cache: dict[str, str | None] = {}

    def load(path: str) -> str | None:
        if path not in cache:
            rc, content = _git("show", f"{ref}:{path}" if ref else f":0:{path}")
            cache[path] = content if rc == 0 else None
        return cache[path]

    return load


def _pr_context():
    """In PR CI: return (diff_text, base_pyproject_text, head_sha); the
    head SHA addresses the diff's post-image blobs so hunk-entry lexical
    state can be rebuilt. None if not PR CI.

    Raises RuntimeError on an unresolvable range (caller fails closed).
    """
    is_pr_event = os.environ.get("GITHUB_EVENT_NAME", "") in (
        "pull_request",
        "pull_request_target",
    )

    def give_up(reason: str):
        if is_pr_event:
            raise RuntimeError(reason)
        return

    event_path = os.environ.get("GITHUB_EVENT_PATH")
    if not (event_path and Path(event_path).exists()):
        return give_up("pull_request event but no event payload")
    try:
        with open(event_path, encoding="utf-8") as fh:
            payload = json.load(fh)
    except (OSError, ValueError):
        return give_up("could not read the PR event payload")
    pr = payload.get("pull_request") if isinstance(payload, dict) else None
    if not isinstance(pr, dict):
        return give_up("pull_request event missing its payload")
    base_ref = pr.get("base")
    head_ref = pr.get("head")
    if not isinstance(base_ref, dict) or not isinstance(head_ref, dict):
        return give_up("pull_request event missing base/head sha")
    base = base_ref.get("sha")
    head = head_ref.get("sha")
    if not (isinstance(base, str) and isinstance(head, str) and base and head):
        return give_up("pull_request event missing base/head sha")
    merge_base = _resolve_merge_base(base, head)  # may raise -> fail closed
    # --no-color: a runner/user git config forcing color would prefix added
    # lines with ANSI escapes and break the leading-'+' detection.
    rc, diff = _git("diff", *DIFF_FLAGS, merge_base, head)
    if rc != 0:
        raise RuntimeError(f"PR diff failed (exit status {rc})")
    rc, base_pyproject = _git("show", f"{base}:pyproject.toml")
    if rc != 0:
        raise RuntimeError(
            f"base pyproject.toml show failed (exit status {rc})"
        )
    return diff, base_pyproject, head


def _staged_context():
    """Local pre-commit: (staged diff, staged pyproject text, None ref)."""
    rc, diff = _git("diff", *DIFF_FLAGS, "--cached")
    if rc != 0:
        raise RuntimeError(f"staged diff failed (exit status {rc})")
    rc, pyproject = _git("show", ":pyproject.toml")
    if rc != 0:
        raise RuntimeError(
            f"staged pyproject.toml show failed (exit status {rc})"
        )
    return diff, pyproject, None


def main() -> int:
    try:
        ctx = _pr_context()
        diff, pyproject, ref = ctx if ctx is not None else _staged_context()
        declared = parse_author_emails(pyproject)
    except RuntimeError as exc:
        print(f"email-domains: {exc}; failing closed.", file=sys.stderr)
        return 1

    offenders = sorted(
        {
            (path, lineno)
            for path, email, lineno in iter_added_findings(
                diff, post_image=_post_image_loader(ref)
            )
            if not is_allowed(email, declared)
        }
    )
    if offenders:
        print("Email domain check failed:\n", file=sys.stderr)
        for path, lineno in offenders:
            # Redact conservatively: any "@" in the path treats it as
            # potentially email-bearing. The strict EMAIL_RE rejects
            # token-suffixed paths (e.g. "[email protected]") and would leak them.
            # Keep each filename on one line, with unambiguous escaped
            # controls and backslashes. Escape non-ASCII code points too so
            # directional formatting cannot reorder the diagnostic.
            display_path = (
                "<path containing email>"
                if "@" in path
                else json.dumps(path, ensure_ascii=True)[1:-1]
            )
            # The line number locates the offending ADDED line in the
            # post-image, so a false positive can be read in context and
            # marked; it reveals nothing about the address itself. A hunk
            # header that did not parse leaves it 0 -> print the bare path.
            location = f"{display_path}:{lineno}" if lineno else display_path
            print(
                f"  {location}: adds an email on a non-reserved domain",
                file=sys.stderr,
            )
        print(
            "\nUse a reserved example/test domain, declare a project author in "
            "pyproject.toml, use a GitHub `@users.noreply.github.com` alias, or "
            "add `email-domains: allow` on that line.\n"
            "(The address is not printed here.)",
            file=sys.stderr,
        )
        return 1
    return 0


if __name__ == "__main__":
    raise SystemExit(main())