Skip to main content

pre_commit_models/
hooks.rs

1//! Pre-commit hook definition models, i.e. for `.pre-commit-hooks.yml`.
2//!
3//! See: <https://pre-commit.com/#new-hooks>
4
5use crate::common;
6
7/// One or more hook definitions.
8#[derive(Debug, serde::Deserialize)]
9pub struct Hooks(#[serde(deserialize_with = "common::non_empty_vec")] pub Vec<HookDefinition>);
10
11/// A single hook definition within a `.pre-commit-hooks.yml` file.
12#[derive(Debug, serde::Deserialize)]
13#[serde(rename_all = "snake_case")]
14pub struct HookDefinition {
15    /// The ID of the hook, as used in `.pre-commit-config.yml`.
16    pub id: String,
17
18    /// The name of the hook, shown during execution.
19    pub name: String,
20
21    /// The entrypoint for the hook, i.e. the executable to run.
22    ///
23    /// This can also contain arguments that aren't overridable, e.g.
24    /// `entry: autopep8 -i`.
25    pub entry: String,
26
27    /// The hook's language.
28    pub language: String,
29
30    /// The pattern of files to run the hook on.
31    pub files: Option<String>,
32
33    /// Excludes files matches by `files` from the hook.
34    pub exclude: Option<String>,
35
36    /// Default list of file times to run the hook on (AND).
37    pub types: Option<Vec<String>>,
38
39    /// Default list of file times to run the hook on (OR).
40    pub types_or: Option<Vec<String>>,
41
42    /// Default list of file times to exclude.
43    pub exclude_types: Option<Vec<String>>,
44
45    /// If `true`, run the hook even when there are no matching files.
46    #[serde(default)]
47    pub always_run: bool,
48
49    /// If `true`, pre-commit will stop running hooks if this hook fails.
50    #[serde(default)]
51    pub fail_fast: bool,
52
53    /// If `true`, force the hook's output to be printed even if it passes.
54    #[serde(default)]
55    pub verbose: bool,
56
57    /// If `false`, no filenames will be passed to the hook.
58    #[serde(default = "default_true")]
59    pub pass_filenames: bool,
60
61    /// If `true`, this hook will execute using a single process instead of in parallel.
62    #[serde(default)]
63    pub require_serial: bool,
64
65    /// A description of the hook, or `''` if not given.
66    #[serde(default)]
67    pub description: String,
68
69    /// The default version to use for [`Self::language`].
70    #[serde(default = "default_language_version")]
71    pub language_version: String,
72
73    /// The minimum version of pre-commit required.
74    #[serde(default = "common::default_minimum_pre_commit_version")]
75    pub minimum_pre_commit_version: String,
76
77    /// The default list of additional parameters to pass to the hook.
78    #[serde(default)]
79    pub args: Vec<String>,
80
81    /// The default set of stages to run the hook for.
82    pub stages: Option<Vec<String>>,
83}
84
85const fn default_true() -> bool {
86    true
87}
88
89fn default_language_version() -> String {
90    "default".into()
91}