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