Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions crates/prek/src/hooks/builtin_hooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@ use crate::cli::run::HookRunReporter;
use crate::config::{BuiltinHook, FilePattern, HookOptions, PassFilenames, Stage};
use crate::hook::Hook;
use crate::hooks::pre_commit_hooks::{
check_added_large_files, check_case_conflict, check_executables_have_shebangs, check_json,
check_merge_conflict, check_shebang_scripts_are_executable, check_symlinks, check_toml,
check_vcs_permalinks, check_xml, check_yaml, destroyed_symlinks, detect_private_key,
file_contents_sorter, fix_byte_order_marker, fix_end_of_file, fix_trailing_whitespace,
forbid_new_submodules, mixed_line_ending, no_commit_to_branch, pretty_format_json,
requirements_txt_fixer,
check_added_large_files, check_case_conflict, check_executables_have_shebangs,
check_illegal_windows_names, check_json, check_merge_conflict,
check_shebang_scripts_are_executable, check_symlinks, check_toml, check_vcs_permalinks,
check_xml, check_yaml, destroyed_symlinks, detect_private_key, file_contents_sorter,
fix_byte_order_marker, fix_end_of_file, fix_trailing_whitespace, forbid_new_submodules,
mixed_line_ending, no_commit_to_branch, pretty_format_json, requirements_txt_fixer,
};
use crate::store::Store;

use super::{HookFuture, HookOutput};

mod check_illegal_windows_names;
mod check_json5;
mod pattern;

Expand Down Expand Up @@ -104,9 +103,9 @@ impl BuiltinHooks {
Self::CheckExecutablesHaveShebangs => {
Box::pin(check_executables_have_shebangs::run(hook, filenames))
}
Self::CheckIllegalWindowsNames => Box::pin(std::future::ready(Ok(
check_illegal_windows_names::check_illegal_windows_names(hook, filenames),
))),
Self::CheckIllegalWindowsNames => {
Box::pin(check_illegal_windows_names::run(hook, filenames))
}
Self::CheckJson => Box::pin(check_json::run(hook, filenames)),
Self::CheckJson5 => Box::pin(check_json5::check_json5(hook, filenames)),
Self::CheckMergeConflict => Box::pin(check_merge_conflict::run(hook, filenames)),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
use std::io::Write;
use std::path::Path;

use anyhow::Result;

use crate::hook::Hook;
use crate::hooks::HookOutput;

pub(super) const ILLEGAL_WINDOWS_PATTERN: &str = r"(?i)((^|/)(CON|PRN|AUX|NUL|COM[\d\x{00B9}\x{00B2}\x{00B3}]|LPT[\d\x{00B9}\x{00B2}\x{00B3}])(\.|/|$)|[<>:\x22\\|?*\x00-\x1F]|/[^/]*[\.\s]/|[^/]*[\.\s]$)";
pub(crate) const ILLEGAL_WINDOWS_PATTERN: &str = r"(?i)((^|/)(CON|PRN|AUX|NUL|COM[\d\x{00B9}\x{00B2}\x{00B3}]|LPT[\d\x{00B9}\x{00B2}\x{00B3}])(\.|/|$)|[<>:\x22\\|?*\x00-\x1F]|/[^/]*[\.\s]/|[^/]*[\.\s]$)";

// Keep this hook in `builtin_hooks` instead of `pre_commit_hooks`.
//
// Upstream implements `check-illegal-windows-names` as a `fail` hook with a
// `files` regex. Our pre-commit-hooks fast path already handles that generic
// `fail` language in Rust, so there is no dedicated fast-path implementation to
// add here. This module only exists to provide the builtin-hook equivalent:
// reuse the same regex for matching, then emit a simple fail-style message.
pub(crate) fn check_illegal_windows_names(_hook: &Hook, filenames: &[&Path]) -> HookOutput {
#[expect(
clippy::unused_async,
reason = "pre-commit hook runners share an async interface"
)]
pub(crate) async fn run(_hook: &Hook, filenames: &[&Path]) -> Result<HookOutput> {
if filenames.is_empty() {
return HookOutput::unchanged(0, Vec::new());
return Ok(HookOutput::unchanged(0, Vec::new()));
Comment thread
j178 marked this conversation as resolved.
Resolved
}

HookOutput::unchanged(1, illegal_windows_names_output(filenames))
Ok(HookOutput::unchanged(
1,
illegal_windows_names_output(filenames),
))
}

fn illegal_windows_names_output(filenames: &[&Path]) -> Vec<u8> {
Expand Down
5 changes: 5 additions & 0 deletions crates/prek/src/hooks/pre_commit_hooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::HookFuture;
pub(crate) mod check_added_large_files;
pub(crate) mod check_case_conflict;
pub(crate) mod check_executables_have_shebangs;
pub(crate) mod check_illegal_windows_names;
pub(crate) mod check_json;
pub(crate) mod check_merge_conflict;
pub(crate) mod check_shebang_scripts_are_executable;
Expand Down Expand Up @@ -101,6 +102,7 @@ pub(crate) enum PreCommitHooks {
CheckAddedLargeFiles,
CheckCaseConflict,
CheckExecutablesHaveShebangs,
CheckIllegalWindowsNames,
Comment thread
j178 marked this conversation as resolved.
Resolved
CheckShebangScriptsAreExecutable,
CheckVcsPermalinks,
FileContentsSorter,
Expand Down Expand Up @@ -142,6 +144,9 @@ impl PreCommitHooks {
Self::CheckExecutablesHaveShebangs => {
Box::pin(check_executables_have_shebangs::run(hook, filenames))
}
Self::CheckIllegalWindowsNames => {
Box::pin(check_illegal_windows_names::run(hook, filenames))
}
Self::CheckShebangScriptsAreExecutable => {
Box::pin(check_shebang_scripts_are_executable::run(hook, filenames))
}
Expand Down
37 changes: 17 additions & 20 deletions crates/prek/src/languages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::sync::Arc;
use anyhow::Result;
use prek_consts::env_vars::{EnvVars, EnvVarsRead};
use prek_identify::parse_shebang;
use tracing::{Instrument, trace, trace_span};
use tracing::{instrument, trace};

use crate::cli::reporter::HookInstallReporter;
use crate::cli::run::HookRunReporter;
Expand Down Expand Up @@ -327,47 +327,44 @@ impl Language {
self.backend().check_health(info)
}

pub(crate) fn run<'a, 'p>(
#[instrument(skip_all, fields(hook_id=%hook.id, language=%hook.language))]
pub(crate) async fn run<'a, 'p>(
&'a self,
store: &'a Store,
hook: &'a InstalledHook,
filenames: &'a [&'p Path],
reporter: &'a HookRunReporter,
) -> impl Future<Output = Result<HookOutput>> + 'a
) -> Result<HookOutput>
where
'p: 'a,
{
let future: LanguageFuture<'a, HookOutput> = match hook.repo() {
Repo::Meta { .. } => Box::pin(
match hook.repo() {
Repo::Meta { .. } => {
hooks::MetaHooks::from_str(&hook.id)
.unwrap()
.run(store, hook, filenames, reporter),
),
Repo::Builtin { .. } => Box::pin(
.run(store, hook, filenames, reporter)
.await
}
Repo::Builtin { .. } => {
hooks::BuiltinHooks::from_str(&hook.id)
.unwrap()
.run(store, hook, filenames, reporter),
),
.run(store, hook, filenames, reporter)
.await
}
// Fast path for hooks implemented in Rust
Repo::Remote { .. } if hooks::check_fast_path(hook) => {
Box::pin(hooks::run_fast_path(store, hook, filenames, reporter))
hooks::run_fast_path(store, hook, filenames, reporter).await
}
Repo::Remote { .. } | Repo::Local { .. } => Box::pin(async move {
Repo::Remote { .. } | Repo::Local { .. } => {
let (exit_status, output) =
self.backend().run(store, hook, filenames, reporter).await?;
if self.can_modify_files() {
Ok(HookOutput::unknown(exit_status, output))
} else {
Ok(HookOutput::unchanged(exit_status, output))
}
}),
};

future.instrument(trace_span!(
"run",
hook_id = %hook.id,
language = %hook.language,
))
}
}
}
}

Expand Down
109 changes: 63 additions & 46 deletions docs/builtin.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,28 @@ Currently, only part of hooks from `https://github.com/pre-commit/pre-commit-hoo

### <https://github.com/pre-commit/pre-commit-hooks>

- [`trailing-whitespace`](https://github.com/pre-commit/pre-commit-hooks#trailing-whitespace) (Trim trailing whitespace)
- [`check-added-large-files`](https://github.com/pre-commit/pre-commit-hooks#check-added-large-files) (Prevent committing large files)
- [`check-case-conflict`](https://github.com/pre-commit/pre-commit-hooks#check-case-conflict) (Check for files that would conflict in case-insensitive filesystems)
- [`end-of-file-fixer`](https://github.com/pre-commit/pre-commit-hooks#end-of-file-fixer) (Ensure newline at EOF)
- [`file-contents-sorter`](https://github.com/pre-commit/pre-commit-hooks#file-contents-sorter) (Sort lines in explicitly targeted files)
- [`requirements-txt-fixer`](https://github.com/pre-commit/pre-commit-hooks#requirements-txt-fixer) (Sort Python requirement and constraint files)
- [`fix-byte-order-marker`](https://github.com/pre-commit/pre-commit-hooks#fix-byte-order-marker) (Remove UTF-8 byte order marker)
- [`forbid-new-submodules`](https://github.com/pre-commit/pre-commit-hooks#forbid-new-submodules) (Prevent addition of new git submodules.)
- [`check-json`](https://github.com/pre-commit/pre-commit-hooks#check-json) (Validate JSON files)
- [`check-toml`](https://github.com/pre-commit/pre-commit-hooks#check-toml) (Validate TOML files)
- [`check-vcs-permalinks`](https://github.com/pre-commit/pre-commit-hooks#check-vcs-permalinks) (Check that VCS links are permalinks)
- [`check-yaml`](https://github.com/pre-commit/pre-commit-hooks#check-yaml) (Validate YAML files)
- [`check-xml`](https://github.com/pre-commit/pre-commit-hooks#check-xml) (Validate XML files)
- [`mixed-line-ending`](https://github.com/pre-commit/pre-commit-hooks#mixed-line-ending) (Normalize or check line endings)
- [`check-symlinks`](https://github.com/pre-commit/pre-commit-hooks#check-symlinks) (Check for broken symlinks)
- [`destroyed-symlinks`](https://github.com/pre-commit/pre-commit-hooks#destroyed-symlinks) (Detect destroyed symlinks)
- [`check-merge-conflict`](https://github.com/pre-commit/pre-commit-hooks#check-merge-conflict) (Check for merge conflicts)
- [`detect-private-key`](https://github.com/pre-commit/pre-commit-hooks#detect-private-key) (Detect private keys)
- [`no-commit-to-branch`](https://github.com/pre-commit/pre-commit-hooks#no-commit-to-branch) (Prevent committing to protected branches)
- [`check-shebang-scripts-are-executable`](https://github.com/pre-commit/pre-commit-hooks#check-shebang-scripts-are-executable) (Ensures that (non-binary) files with a shebang are executable)
- [`check-executables-have-shebangs`](https://github.com/pre-commit/pre-commit-hooks#check-executables-have-shebangs) (Ensures that (non-binary) executables have a shebang)
- [`trailing-whitespace`](https://github.com/pre-commit/pre-commit-hooks#trailing-whitespace) (Trims trailing whitespace.)
- [`check-added-large-files`](https://github.com/pre-commit/pre-commit-hooks#check-added-large-files) (Prevents giant files from being committed.)
- [`check-case-conflict`](https://github.com/pre-commit/pre-commit-hooks#check-case-conflict) (Checks for files that would conflict in case-insensitive filesystems.)
- [`check-illegal-windows-names`](https://github.com/pre-commit/pre-commit-hooks#check-illegal-windows-names) (Checks for filenames which cannot be created on Windows.)
- [`end-of-file-fixer`](https://github.com/pre-commit/pre-commit-hooks#end-of-file-fixer) (Ensures that a file is either empty, or ends with one newline.)
- [`file-contents-sorter`](https://github.com/pre-commit/pre-commit-hooks#file-contents-sorter) (Sorts the lines in specified files (defaults to alphabetical).)
- [`requirements-txt-fixer`](https://github.com/pre-commit/pre-commit-hooks#requirements-txt-fixer) (Sorts entries in requirements.txt.)
- [`fix-byte-order-marker`](https://github.com/pre-commit/pre-commit-hooks#fix-byte-order-marker) (Removes UTF-8 byte order marker.)
- [`forbid-new-submodules`](https://github.com/pre-commit/pre-commit-hooks#forbid-new-submodules) (Prevents the addition of new Git submodules.)
- [`check-json`](https://github.com/pre-commit/pre-commit-hooks#check-json) (Checks JSON files for parseable syntax.)
- [`check-toml`](https://github.com/pre-commit/pre-commit-hooks#check-toml) (Checks TOML files for parseable syntax.)
- [`check-vcs-permalinks`](https://github.com/pre-commit/pre-commit-hooks#check-vcs-permalinks) (Ensures that links to VCS websites are permalinks.)
- [`check-yaml`](https://github.com/pre-commit/pre-commit-hooks#check-yaml) (Checks YAML files for parseable syntax.)
- [`check-xml`](https://github.com/pre-commit/pre-commit-hooks#check-xml) (Checks XML files for parseable syntax.)
- [`mixed-line-ending`](https://github.com/pre-commit/pre-commit-hooks#mixed-line-ending) (Replaces or checks mixed line endings.)
- [`check-symlinks`](https://github.com/pre-commit/pre-commit-hooks#check-symlinks) (Checks for symlinks which do not point to anything.)
- [`destroyed-symlinks`](https://github.com/pre-commit/pre-commit-hooks#destroyed-symlinks) (Detects symlinks that were replaced with regular files whose contents are the original symlink target path.)
- [`check-merge-conflict`](https://github.com/pre-commit/pre-commit-hooks#check-merge-conflict) (Checks for files that contain merge conflict strings.)
- [`detect-private-key`](https://github.com/pre-commit/pre-commit-hooks#detect-private-key) (Detects the presence of private keys.)
- [`no-commit-to-branch`](https://github.com/pre-commit/pre-commit-hooks#no-commit-to-branch) (Protects specific branches from direct commits.)
- [`check-shebang-scripts-are-executable`](https://github.com/pre-commit/pre-commit-hooks#check-shebang-scripts-are-executable) (Ensures that (non-binary) files with a shebang are executable.)
- [`check-executables-have-shebangs`](https://github.com/pre-commit/pre-commit-hooks#check-executables-have-shebangs) (Ensures that (non-binary) executables have a shebang.)

#### Notes

Expand Down Expand Up @@ -96,31 +97,32 @@ repos:

For `repo: builtin`, the following hooks are supported:

- [`trailing-whitespace`](#trailing-whitespace) (Trim trailing whitespace)
- [`check-added-large-files`](#check-added-large-files) (Prevent committing large files)
- [`check-case-conflict`](#check-case-conflict) (Check for files that would conflict in case-insensitive filesystems)
- [`check-illegal-windows-names`](#check-illegal-windows-names) (Check for filenames invalid on Windows)
- [`end-of-file-fixer`](#end-of-file-fixer) (Ensure newline at EOF)
- [`file-contents-sorter`](#file-contents-sorter) (Sort lines in explicitly targeted files)
- [`requirements-txt-fixer`](#requirements-txt-fixer) (Sort Python requirement and constraint files)
- [`fix-byte-order-marker`](#fix-byte-order-marker) (Remove UTF-8 byte order marker)
- [`check-json`](#check-json) (Validate JSON files)
- [`check-json5`](#check-json5) (Validate JSON5 files)
- [`pretty-format-json`](#pretty-format-json) (Pretty format JSON files)
- [`check-toml`](#check-toml) (Validate TOML files)
- [`check-vcs-permalinks`](#check-vcs-permalinks) (Check that VCS links are permalinks)
- [`check-yaml`](#check-yaml) (Validate YAML files)
- [`check-xml`](#check-xml) (Validate XML files)
- [`deny-pattern`](#deny-pattern) (Reject configured regular expression matches)
- [`require-pattern`](#require-pattern) (Require each file to match a configured regular expression)
- [`mixed-line-ending`](#mixed-line-ending) (Normalize or check line endings)
- [`check-symlinks`](#check-symlinks) (Check for broken symlinks)
- [`destroyed-symlinks`](#destroyed-symlinks) (Detect destroyed symlinks)
- [`check-merge-conflict`](#check-merge-conflict) (Check for merge conflicts)
- [`detect-private-key`](#detect-private-key) (Detect private keys)
- [`no-commit-to-branch`](#no-commit-to-branch) (Prevent committing to protected branches)
- [`check-shebang-scripts-are-executable`](#check-shebang-scripts-are-executable) (Ensures that (non-binary) files with a shebang are executable)
- [`check-executables-have-shebangs`](#check-executables-have-shebangs) (Ensures that (non-binary) executables have a shebang)
- [`trailing-whitespace`](#trailing-whitespace) (Trims trailing whitespace.)
- [`check-added-large-files`](#check-added-large-files) (Prevents giant files from being committed.)
- [`check-case-conflict`](#check-case-conflict) (Checks for files that would conflict in case-insensitive filesystems.)
- [`check-illegal-windows-names`](#check-illegal-windows-names) (Checks for filenames which cannot be created on Windows.)
- [`end-of-file-fixer`](#end-of-file-fixer) (Ensures that a file is either empty, or ends with one newline.)
- [`file-contents-sorter`](#file-contents-sorter) (Sorts the lines in specified files (defaults to alphabetical).)
- [`requirements-txt-fixer`](#requirements-txt-fixer) (Sorts entries in requirements.txt.)
- [`fix-byte-order-marker`](#fix-byte-order-marker) (Removes UTF-8 byte order marker.)
- [`forbid-new-submodules`](#forbid-new-submodules) (Prevents the addition of new Git submodules.)
- [`check-json`](#check-json) (Checks JSON files for parseable syntax.)
- [`check-json5`](#check-json5) (Checks JSON5 files for parseable syntax.)
- [`pretty-format-json`](#pretty-format-json) (Checks that JSON files are pretty-formatted.)
- [`check-toml`](#check-toml) (Checks TOML files for parseable syntax.)
- [`check-vcs-permalinks`](#check-vcs-permalinks) (Ensures that links to VCS websites are permalinks.)
- [`check-yaml`](#check-yaml) (Checks YAML files for parseable syntax.)
- [`check-xml`](#check-xml) (Checks XML files for parseable syntax.)
- [`deny-pattern`](#deny-pattern) (Fails if any file contains a matching regular expression.)
- [`require-pattern`](#require-pattern) (Fails if any file does not contain a matching regular expression.)
- [`mixed-line-ending`](#mixed-line-ending) (Replaces or checks mixed line endings.)
- [`check-symlinks`](#check-symlinks) (Checks for symlinks which do not point to anything.)
- [`destroyed-symlinks`](#destroyed-symlinks) (Detects symlinks that were replaced with regular files whose contents are the original symlink target path.)
- [`check-merge-conflict`](#check-merge-conflict) (Checks for files that contain merge conflict strings.)
- [`detect-private-key`](#detect-private-key) (Detects the presence of private keys.)
- [`no-commit-to-branch`](#no-commit-to-branch) (Protects specific branches from direct commits.)
- [`check-shebang-scripts-are-executable`](#check-shebang-scripts-are-executable) (Ensures that (non-binary) files with a shebang are executable.)
- [`check-executables-have-shebangs`](#check-executables-have-shebangs) (Ensures that (non-binary) executables have a shebang.)

### Hook Reference

Expand Down Expand Up @@ -289,6 +291,21 @@ Removes a UTF-8 byte order marker (BOM) from the beginning of a file.

---

#### `forbid-new-submodules`

Prevents the addition of new Git submodules.

**Supported arguments**

- None.

**Behavior / caveats**

- Existing submodules are allowed; only submodules newly added by the checked changes are reported.
- Staged changes are checked by default. When `PRE_COMMIT_FROM_REF` and `PRE_COMMIT_TO_REF` are both set, their revision range is checked instead.

---

#### `check-json`

Attempts to load all JSON files to verify syntax.
Expand Down
Loading