Skip to main content

pre_commit_models/
config.rs

1//! Pre-commit configuration models, i.e. for `.pre-commit-config.yml`.
2//!
3//! See: <https://pre-commit.com/#plugins>
4
5use crate::common;
6use indexmap::IndexMap;
7
8/// A single pre-commit configuration, containing one or more repositories,
9/// each of which may reference one or more hooks.
10#[derive(Debug, serde::Deserialize)]
11#[serde(rename_all = "snake_case")]
12pub struct Config {
13    /// A list of repository mappings.
14    #[serde(deserialize_with = "common::non_empty_vec")]
15    pub repos: Vec<Repo>,
16    /// An optional list of `--hook-types`.
17    ///
18    /// If not supplied, this defaults to `[pre-commit]`.
19    ///
20    // TODO: Model that?
21    pub default_install_hook_types: Option<Vec<String>>,
22
23    /// A mapping from language to the default language version that
24    /// should be used for that language, if a hook does not supply its
25    /// own `language_version`.
26    #[serde(default)]
27    pub default_language_version: IndexMap<String, String>,
28
29    /// The `stages` property for a hook, if a hook does not supply its
30    /// own `stages`.
31    pub default_stages: Option<Vec<String>>,
32
33    /// The global file include pattern.
34    #[serde(default)]
35    pub files: String,
36
37    /// The global file exclude pattern.
38    #[serde(default)]
39    pub exclude: String,
40
41    /// Whether to have pre-commit stop running hooks after the first
42    /// failure.
43    #[serde(default)]
44    pub fail_fast: bool,
45
46    /// The minimum version of pre-commit required.
47    #[serde(default = "common::default_minimum_pre_commit_version")]
48    pub minimum_pre_commit_version: String,
49}
50
51/// A repository, i.e. where to get one or more hooks from.
52///
53/// This concept is slightly overloaded in pre-commit, as there are
54/// two special sentinel "repository" types, `local`, and `meta`, which
55/// have a different shape than a normal Git repository.
56#[derive(Debug, serde::Deserialize)]
57#[serde(
58    rename_all = "snake_case",
59    rename_all_fields = "snake_case",
60    tag = "repo"
61)]
62pub enum Repo {
63    /// A special 'local' repository, for hooks defined within the current Git repository.
64    ///
65    /// See: <https://pre-commit.com/#repository-local-hooks>
66    // TODO: Fill this in. It's seemingly identical to a normal hook,
67    // except without `rev`.
68    Local {},
69    /// A special 'meta' repository, for hooks defined by pre-commit itself.
70    ///
71    /// See: <https://pre-commit.com/#meta-hooks>
72    // TODO: Fill this in, it's a fixed set of IDs for hooks.
73    Meta {},
74    #[serde(untagged)]
75    Repo {
76        repo: String,
77        rev: String,
78        #[serde(deserialize_with = "common::non_empty_vec")]
79        hooks: Vec<Hook>,
80    },
81}
82
83/// A single hook.
84#[derive(Debug, serde::Deserialize)]
85#[serde(rename_all = "snake_case")]
86pub struct Hook {
87    /// The ID of the hook to use.
88    pub id: String,
89
90    /// An optional additional ID to use when referring to the hook.
91    pub alias: Option<String>,
92
93    /// Overrides the name of the hook, as shown during execution.
94    pub name: Option<String>,
95
96    /// Overrides the language version for the hook.
97    pub language_version: Option<String>,
98
99    /// Overrides the files pattern for the hook.
100    pub files: Option<String>,
101
102    /// Overrides the exclude pattern for the hook.
103    pub exclude: Option<String>,
104
105    /// Overrides the default file types to run on for the hook (AND).
106    pub types: Option<Vec<String>>,
107
108    /// Overrides the default file types to run on for the hook (OR).
109    pub types_or: Option<Vec<String>>,
110
111    /// Overrides the types to exclude for the hook.
112    pub exclude_types: Option<Vec<String>>,
113
114    /// Optional list of additional args to supply to the hook.
115    #[serde(default)]
116    pub args: Vec<String>,
117
118    /// Overrides the set of stages to run the hook for.
119    pub stages: Option<Vec<String>>,
120
121    /// Additional dependencies to install into the hook's environment.
122    #[serde(default)]
123    pub additional_dependencies: Vec<String>,
124
125    /// If true, run the hook even when there are no matching files.
126    #[serde(default)]
127    pub always_run: bool,
128
129    /// If true, force the hook's output to be printed even if it passes.
130    #[serde(default)]
131    pub verbose: bool,
132
133    /// If present, additionally append the hook's log output to this file.
134    #[serde(default)]
135    pub log_file: Option<String>,
136}