pre-commit/pre_commit/commands
Sharmila Jesupaul 635912514d add pass_filenames_via_stdin for large changesets
pre-commit currently passes selected filenames to hooks via argv.
For large changesets (or --all-files), argv length limits are hit and
filenames are partitioned, causing multiple hook invocations.

This means there is currently no built-in way to pass filenames to an
underlying hook in one shot without chunking / re-running. The only practical
workaround is to set pass_filenames: false and run custom git operations in
hook code to reconstruct the file set, which is expensive and duplicates
pre-commit's own file-selection logic.

This change adds a hook option:

    pass_filenames_via_stdin: true

When enabled, pre-commit sends filenames as NUL-delimited bytes on stdin and
runs the hook in a single invocation (no argv chunking).

Why NUL-delimited stdin:
- safe for filenames containing spaces/newlines
- matches established -0 conventions in unix tooling

Usage for hook authors:
- shell:

    while IFS= read -r -d '' filename; do
        ...
    done

- python:

    data = sys.stdin.buffer.read()
    filenames = [os.fsdecode(p) for p in data.split(b'\0') if p]

Behavior notes:
- default remains argv-based passing
- pass_filenames: false still disables filename passing entirely

Implementation includes schema/runtime wiring, shared NUL encode/decode
helpers, and tests covering defaulting and runtime behavior.
2026-02-18 18:06:34 -08:00
..
__init__.py Move commands into their own files. 2014-06-13 19:52:14 -07:00
autoupdate.py python3.9+ 2023-10-28 14:20:37 -04:00
clean.py drop python 3.6 support 2022-01-18 18:44:20 -05:00
gc.py move logic for gc back to commands.gc 2025-11-19 14:32:09 -05:00
hazmat.py add pre-commit hazmat 2025-11-22 13:53:53 -05:00
hook_impl.py Add fail-fast argument for run command 2025-09-06 14:14:23 -04:00
init_templatedir.py implement default_install_hook_types 2022-04-02 15:11:31 -04:00
install_uninstall.py shlex.join is always available in 3.8+ 2023-07-01 17:22:42 -04:00
migrate_config.py py310+ 2025-10-09 17:44:05 -04:00
run.py add pass_filenames_via_stdin for large changesets 2026-02-18 18:06:34 -08:00
sample_config.py remove obsolete comment 2022-11-27 16:30:58 -05:00
try_repo.py add pre_commit.yaml module 2023-01-02 18:42:34 -05:00
validate_config.py python3.9+ 2023-10-28 14:20:37 -04:00
validate_manifest.py python3.9+ 2023-10-28 14:20:37 -04:00