Skip to content

Add file-contents-sorter as builtin hook#1846

Merged
j178 merged 1 commit intomasterfrom
feat/file-contents-sorter-builtin
Mar 21, 2026
Merged

Add file-contents-sorter as builtin hook#1846
j178 merged 1 commit intomasterfrom
feat/file-contents-sorter-builtin

Conversation

@j178
Copy link
Owner

@j178 j178 commented Mar 21, 2026

Closes #1843

Copilot AI review requested due to automatic review settings March 21, 2026 15:27
@j178 j178 changed the title Add file-contents-sorter hook Add file-contents-sorter as builtin hook Mar 21, 2026
@j178 j178 added the enhancement New feature or request label Mar 21, 2026
@j178 j178 force-pushed the feat/file-contents-sorter-builtin branch from fca1a14 to e24a4c5 Compare March 21, 2026 15:30
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a Rust-native implementation of the file-contents-sorter hook and wires it into both the pre-commit-hooks fast path and the repo: builtin hook set, including schema/docs/test updates. It also introduces an internal FilePattern::Never to support builtin hooks that should match no files by default (requiring users to set files: explicitly).

Changes:

  • Implement file-contents-sorter (sorting + optional case-insensitive sort / unique mode) and hook it into the fast-path dispatcher.
  • Add file-contents-sorter to builtin hooks (including list-builtins output), docs, and the generated JSON schema.
  • Introduce FilePattern::Never for “match nothing by default” builtin-hook behavior.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
prek.schema.json Adds file-contents-sorter to the schema’s builtin hook id list.
docs/builtin.md Documents the new hook and its behavior/arguments.
crates/prek/tests/list_builtins.rs Updates list-builtins snapshots to include the new hook.
crates/prek/tests/builtin_hooks.rs Adds an integration test for the builtin file-contents-sorter behavior.
crates/prek/src/hooks/pre_commit_hooks/mod.rs Registers the new hook in the pre-commit-hooks fast path enum/dispatcher.
crates/prek/src/hooks/pre_commit_hooks/file_contents_sorter.rs New Rust implementation of the hook (with unit tests).
crates/prek/src/hooks/builtin_hooks/mod.rs Adds builtin hook metadata + defaults (files: Never).
crates/prek/src/config.rs Adds FilePattern::Never and tests for its matching behavior.
Comments suppressed due to low confidence (1)

crates/prek/src/hooks/builtin_hooks/mod.rs:275

  • The builtin hook description is user-facing (list-builtins) but is grammatically incorrect and misleading: it says users must provide a "list of target files" as input, but the hook is targeted via the files: regex/glob configuration. Please reword to accurately describe configuration (e.g., that users must set files: explicitly because the default matches nothing) and fix capitalization/grammar.
                priority: None,
                options: HookOptions {
                    description: Some(
                        "ensures that a file is either empty, or ends with one newline."

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fca1a14841

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@j178 j178 force-pushed the feat/file-contents-sorter-builtin branch from e24a4c5 to 7ea9388 Compare March 21, 2026 15:35
@prek-ci-bot
Copy link

prek-ci-bot bot commented Mar 21, 2026

📦 Cargo Bloat Comparison

Binary size change: +0.40% (25.0 MiB → 25.1 MiB)

Expand for cargo-bloat output

Head Branch Results

 File  .text     Size             Crate Name
 1.3%   2.7% 332.0KiB        aws_lc_sys aws_lc_0_39_0_aes_gcm_encrypt_avx512
 1.3%   2.7% 332.0KiB        aws_lc_sys aws_lc_0_39_0_aes_gcm_decrypt_avx512
 0.3%   0.7%  82.7KiB             prek? <prek::cli::Command as clap_builder::derive::Subcommand>::augment_subcommands
 0.3%   0.7%  81.1KiB              prek prek::languages::<impl prek::config::Language>::run::{{closure}}::{{closure}}
 0.3%   0.6%  74.1KiB              prek prek::languages::<impl prek::config::Language>::run::{{closure}}::{{closure}}
 0.2%   0.5%  56.9KiB              prek prek::languages::<impl prek::config::Language>::install::{{closure}}
 0.2%   0.4%  51.0KiB annotate_snippets annotate_snippets::renderer::render::render
 0.2%   0.4%  47.1KiB              prek prek::run::{{closure}}
 0.2%   0.3%  41.8KiB              prek prek::cli::run::run::run::{{closure}}
 0.1%   0.3%  32.0KiB             prek? <prek::cli::RunArgs as clap_builder::derive::Args>::augment_args
 0.1%   0.2%  28.0KiB        aws_lc_sys aws_lc_0_39_0_edwards25519_scalarmuldouble_alt
 0.1%   0.2%  27.8KiB      serde_saphyr saphyr_parser_bw::scanner::Scanner<T>::fetch_more_tokens
 0.1%   0.2%  27.5KiB        aws_lc_sys aws_lc_0_39_0_edwards25519_scalarmuldouble
 0.1%   0.2%  26.2KiB              prek prek::cli::try_repo::try_repo::{{closure}}
 0.1%   0.2%  25.4KiB             prek? <prek::config::_::<impl serde_core::de::Deserialize for prek::config::Config>::deserialize::__Visitor as serde_core::de::Visitor>::visit_map
 0.1%   0.2%  24.4KiB               std core::ptr::drop_in_place<prek::languages::<impl prek::config::Language>::install::{{closure}}>
 0.1%   0.2%  23.0KiB              prek prek::hooks::meta_hooks::MetaHooks::run::{{closure}}
 0.1%   0.2%  22.4KiB      serde_saphyr saphyr_parser_bw::scanner::Scanner<T>::fetch_more_tokens
 0.1%   0.2%  22.3KiB         [Unknown] Lp384_montjscalarmul_alt_p384_montjadd
 0.1%   0.2%  21.6KiB              prek prek::workspace::Project::init_hooks::{{closure}}
41.2%  85.9%  10.3MiB                   And 23547 smaller methods. Use -n N to show more.
48.0% 100.0%  12.0MiB                   .text section size, the file size is 25.1MiB

Base Branch Results

 File  .text     Size             Crate Name
 1.3%   2.7% 332.0KiB        aws_lc_sys aws_lc_0_39_0_aes_gcm_encrypt_avx512
 1.3%   2.7% 332.0KiB        aws_lc_sys aws_lc_0_39_0_aes_gcm_decrypt_avx512
 0.3%   0.7%  82.4KiB             prek? <prek::cli::Command as clap_builder::derive::Subcommand>::augment_subcommands
 0.3%   0.7%  80.6KiB              prek prek::languages::<impl prek::config::Language>::run::{{closure}}::{{closure}}
 0.3%   0.6%  73.6KiB              prek prek::languages::<impl prek::config::Language>::run::{{closure}}::{{closure}}
 0.2%   0.5%  56.9KiB              prek prek::languages::<impl prek::config::Language>::install::{{closure}}
 0.2%   0.4%  51.0KiB annotate_snippets annotate_snippets::renderer::render::render
 0.2%   0.4%  46.4KiB              prek prek::run::{{closure}}
 0.2%   0.3%  41.8KiB              prek prek::cli::run::run::run::{{closure}}
 0.1%   0.3%  32.0KiB             prek? <prek::cli::RunArgs as clap_builder::derive::Args>::augment_args
 0.1%   0.2%  28.0KiB        aws_lc_sys aws_lc_0_39_0_edwards25519_scalarmuldouble_alt
 0.1%   0.2%  27.8KiB      serde_saphyr saphyr_parser_bw::scanner::Scanner<T>::fetch_more_tokens
 0.1%   0.2%  27.5KiB        aws_lc_sys aws_lc_0_39_0_edwards25519_scalarmuldouble
 0.1%   0.2%  25.8KiB              prek prek::cli::try_repo::try_repo::{{closure}}
 0.1%   0.2%  24.4KiB               std core::ptr::drop_in_place<prek::languages::<impl prek::config::Language>::install::{{closure}}>
 0.1%   0.2%  23.2KiB             prek? <prek::config::_::<impl serde_core::de::Deserialize for prek::config::Config>::deserialize::__Visitor as serde_core::de::Visitor>::visit_map
 0.1%   0.2%  22.4KiB      serde_saphyr saphyr_parser_bw::scanner::Scanner<T>::fetch_more_tokens
 0.1%   0.2%  22.3KiB         [Unknown] Lp384_montjscalarmul_alt_p384_montjadd
 0.1%   0.2%  21.6KiB              prek prek::workspace::Project::init_hooks::{{closure}}
 0.1%   0.2%  21.5KiB      clap_builder clap_builder::parser::parser::Parser::get_matches_with
41.2%  85.9%  10.3MiB                   And 23471 smaller methods. Use -n N to show more.
47.9% 100.0%  12.0MiB                   .text section size, the file size is 25.0MiB

@prek-ci-bot
Copy link

prek-ci-bot bot commented Mar 21, 2026

⚡️ Hyperfine Benchmarks

Summary: 0 regressions, 0 improvements above the 10% threshold.

Environment
  • OS: Linux 6.14.0-1017-azure
  • CPU: 4 cores
  • prek version: prek 0.3.6+25 (fe45b38 2026-03-21)
  • Rust version: rustc 1.94.0 (4a4ef493e 2026-03-02)
  • Hyperfine version: hyperfine 1.20.0
CLI Commands

Benchmarking basic commands in the main repo:

prek --version

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base --version 2.4 ± 0.1 2.3 2.7 1.00
prek-head --version 2.4 ± 0.1 2.2 3.0 1.00 ± 0.06

prek list

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base list 9.1 ± 0.1 8.9 9.6 1.00 ± 0.02
prek-head list 9.1 ± 0.1 8.9 9.5 1.00

prek validate-config .pre-commit-config.yaml

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base validate-config .pre-commit-config.yaml 3.3 ± 0.1 3.1 3.4 1.00 ± 0.03
prek-head validate-config .pre-commit-config.yaml 3.3 ± 0.1 3.1 3.5 1.00

prek sample-config

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base sample-config 2.7 ± 0.3 2.6 4.7 1.00 ± 0.11
prek-head sample-config 2.7 ± 0.1 2.6 2.9 1.00
Cold vs Warm Runs

Comparing first run (cold) vs subsequent runs (warm cache):

prek run --all-files (cold - no cache)

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run --all-files 159.9 ± 2.8 154.9 165.5 1.02 ± 0.02
prek-head run --all-files 157.2 ± 2.4 154.4 162.1 1.00

prek run --all-files (warm - with cache)

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run --all-files 159.6 ± 2.9 155.2 165.5 1.01 ± 0.02
prek-head run --all-files 158.2 ± 2.3 154.4 162.8 1.00
Full Hook Suite

Running the builtin hook suite on the benchmark workspace:

prek run --all-files (full builtin hook suite)

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run --all-files 158.9 ± 2.5 155.0 165.7 1.00
prek-head run --all-files 161.1 ± 13.1 155.0 233.2 1.01 ± 0.08
Individual Hook Performance

Benchmarking each hook individually on the test repo:

prek run trailing-whitespace --all-files

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run trailing-whitespace --all-files 22.6 ± 0.6 21.3 23.8 1.02 ± 0.03
prek-head run trailing-whitespace --all-files 22.2 ± 0.4 21.6 23.1 1.00

prek run end-of-file-fixer --all-files

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run end-of-file-fixer --all-files 28.8 ± 1.8 25.9 32.4 1.02 ± 0.08
prek-head run end-of-file-fixer --all-files 28.3 ± 1.3 25.9 30.8 1.00

prek run check-json --all-files

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run check-json --all-files 12.8 ± 0.5 12.1 14.0 1.03 ± 0.04
prek-head run check-json --all-files 12.5 ± 0.3 12.1 13.2 1.00

prek run check-yaml --all-files

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run check-yaml --all-files 12.1 ± 0.2 11.8 12.8 1.00
prek-head run check-yaml --all-files 12.2 ± 0.3 11.8 13.3 1.01 ± 0.03

prek run check-toml --all-files

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run check-toml --all-files 12.5 ± 0.4 12.0 14.0 1.02 ± 0.04
prek-head run check-toml --all-files 12.2 ± 0.3 11.7 12.9 1.00

prek run check-xml --all-files

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run check-xml --all-files 12.3 ± 0.3 11.8 13.1 1.01 ± 0.03
prek-head run check-xml --all-files 12.1 ± 0.2 11.8 12.7 1.00

prek run detect-private-key --all-files

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run detect-private-key --all-files 19.3 ± 1.3 17.0 22.7 1.03 ± 0.10
prek-head run detect-private-key --all-files 18.8 ± 1.2 16.9 21.1 1.00

prek run fix-byte-order-marker --all-files

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run fix-byte-order-marker --all-files 24.4 ± 1.6 21.8 27.3 1.00 ± 0.09
prek-head run fix-byte-order-marker --all-files 24.4 ± 1.5 21.8 27.5 1.00
Installation Performance

Benchmarking hook installation (fast path hooks skip Python setup):

prek install-hooks (cold - no cache)

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base install-hooks 5.0 ± 0.1 4.9 5.2 1.02 ± 0.02
prek-head install-hooks 4.9 ± 0.1 4.9 5.0 1.00

prek install-hooks (warm - with cache)

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base install-hooks 4.9 ± 0.0 4.9 5.0 1.00 ± 0.01
prek-head install-hooks 4.9 ± 0.0 4.9 5.0 1.00
File Filtering/Scoping Performance

Testing different file selection modes:

prek run (staged files only)

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run 53.4 ± 0.7 52.0 55.4 1.00
prek-head run 54.0 ± 1.1 52.6 55.9 1.01 ± 0.02

prek run --files '*.json' (specific file type)

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run --files '*.json' 9.2 ± 0.1 9.0 9.6 1.00
prek-head run --files '*.json' 9.2 ± 0.1 9.0 9.4 1.00 ± 0.02
Workspace Discovery & Initialization

Benchmarking hook discovery and initialization overhead:

prek run --dry-run --all-files (measures init overhead)

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run --dry-run --all-files 14.3 ± 0.3 13.9 15.0 1.00 ± 0.02
prek-head run --dry-run --all-files 14.2 ± 0.2 14.0 14.6 1.00
Meta Hooks Performance

Benchmarking meta hooks separately:

prek run check-hooks-apply --all-files

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run check-hooks-apply --all-files 14.4 ± 0.1 14.1 14.7 1.00 ± 0.01
prek-head run check-hooks-apply --all-files 14.4 ± 0.1 14.2 14.7 1.00

prek run check-useless-excludes --all-files

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run check-useless-excludes --all-files 14.1 ± 0.7 12.6 14.8 1.11 ± 0.05
prek-head run check-useless-excludes --all-files 12.7 ± 0.2 12.5 13.1 1.00

prek run identity --all-files

Command Mean [ms] Min [ms] Max [ms] Relative
prek-base run identity --all-files 11.2 ± 0.1 11.1 11.5 1.00
prek-head run identity --all-files 11.2 ± 0.1 11.1 11.4 1.00 ± 0.01

@j178 j178 merged commit addddb8 into master Mar 21, 2026
51 checks passed
@j178 j178 deleted the feat/file-contents-sorter-builtin branch March 21, 2026 15:42
@codecov
Copy link

codecov bot commented Mar 21, 2026

Codecov Report

❌ Patch coverage is 97.72727% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.88%. Comparing base (cb3f71c) to head (7ea9388).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
crates/prek/src/config.rs 87.50% 1 Missing ⚠️
...src/hooks/pre_commit_hooks/file_contents_sorter.rs 99.07% 1 Missing ⚠️
crates/prek/src/hooks/pre_commit_hooks/mod.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1846      +/-   ##
==========================================
+ Coverage   91.85%   91.88%   +0.03%     
==========================================
  Files         103      104       +1     
  Lines       20788    20920     +132     
==========================================
+ Hits        19095    19223     +128     
- Misses       1693     1697       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add pre-commit file-contents-sorter as builtin hook

2 participants