blob: 3b3b3301ac326efc09f2bf42bb31bc6ae27d9061 [file] [log] [blame] [view]
Anthony Sottilea13cf642022-12-29 18:11:25 -05001[![build status](https://github.com/pre-commit/pre-commit-hooks/actions/workflows/main.yml/badge.svg)](https://github.com/pre-commit/pre-commit-hooks/actions/workflows/main.yml)
Anthony Sottile202ec6b2022-03-24 14:53:14 -04002[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/pre-commit/pre-commit-hooks/main.svg)](https://results.pre-commit.ci/latest/github/pre-commit/pre-commit-hooks/main)
Anthony Sottilebe8b2962014-03-22 18:40:47 -07003
Anthony Sottile27291ff2014-03-13 08:41:35 -07004pre-commit-hooks
Anthony Sottile8bb94f02019-03-10 13:42:05 -07005================
Anthony Sottile27291ff2014-03-13 08:41:35 -07006
7Some out-of-the-box hooks for pre-commit.
8
Anthony Sottilecd74dc12014-03-13 09:26:06 -07009See also: https://github.com/pre-commit/pre-commit
Anthony Sottile24609de2014-06-18 06:47:14 -070010
11
12### Using pre-commit-hooks with pre-commit
13
14Add this to your `.pre-commit-config.yaml`
15
Daiki Katsuragawa74024a92020-03-24 02:30:14 +090016```yaml
17- repo: https://github.com/pre-commit/pre-commit-hooks
Anthony Sottilef71fa2c2022-11-23 14:35:47 -050018 rev: v4.4.0 # Use the ref you want to point at
Daiki Katsuragawa74024a92020-03-24 02:30:14 +090019 hooks:
20 - id: trailing-whitespace
21 # - id: ...
22```
Anthony Sottile24609de2014-06-18 06:47:14 -070023
24### Hooks available
25
Jacob Bundgaard3c614e72020-02-04 23:23:23 +010026#### `check-added-large-files`
27Prevent giant files from being committed.
28 - Specify what is "too large" with `args: ['--maxkb=123']` (default=500kB).
Marcus Shawcroft012bb062020-09-16 06:26:11 +010029 - Limits checked files to those indicated as staged for addition by git.
Jacob Bundgaard3c614e72020-02-04 23:23:23 +010030 - If `git-lfs` is installed, lfs files will be skipped
31 (requires `git-lfs>=2.2.1`)
Marcus Shawcroft012bb062020-09-16 06:26:11 +010032 - `--enforce-all` - Check all listed files not just those staged for
33 addition.
Jacob Bundgaard3c614e72020-02-04 23:23:23 +010034
35#### `check-ast`
36Simply check whether files parse as valid python.
37
38#### `check-builtin-literals`
39Require literal syntax when initializing empty or zero Python builtin types.
40 - Allows calling constructors with positional arguments (e.g., `list('abc')`).
41 - Allows calling constructors from the `builtins` (`__builtin__`) namespace (`builtins.list()`).
42 - Ignore this requirement for specific builtin types with `--ignore=type1,type2,…`.
43 - Forbid `dict` keyword syntax with `--no-allow-dict-kwargs`.
44
Jacob Bundgaard3c614e72020-02-04 23:23:23 +010045#### `check-case-conflict`
46Check for files with names that would conflict on a case-insensitive filesystem like MacOS HFS+ or Windows FAT.
47
48#### `check-docstring-first`
49Checks for a common error of placing code before the docstring.
50
51#### `check-executables-have-shebangs`
52Checks that non-binary executables have a proper shebang.
53
54#### `check-json`
55Attempts to load all json files to verify syntax.
56
57#### `check-merge-conflict`
58Check for files that contain merge conflict strings.
Bastien Gérardaa56da82022-04-25 12:20:30 +020059 - `--assume-in-merge` - Allows running the hook when there is no ongoing merge operation
Jacob Bundgaard3c614e72020-02-04 23:23:23 +010060
Ville Skyttä391ae302021-01-08 17:36:55 +020061#### `check-shebang-scripts-are-executable`
62Checks that scripts with shebangs are executable.
63
Jacob Bundgaard3c614e72020-02-04 23:23:23 +010064#### `check-symlinks`
65Checks for symlinks which do not point to anything.
66
67#### `check-toml`
68Attempts to load all TOML files to verify syntax.
69
70#### `check-vcs-permalinks`
71Ensures that links to vcs websites are permalinks.
Youngmin Koo1f8151a2020-11-17 02:33:47 +090072 - `--additional-github-domain DOMAIN` - Add check for specified domain.
73 Can be repeated multiple times. for example, if your company uses
74 GitHub Enterprise you may use something like
75 `--additional-github-domain github.example.com`
Jacob Bundgaard3c614e72020-02-04 23:23:23 +010076
77#### `check-xml`
78Attempts to load all xml files to verify syntax.
79
80#### `check-yaml`
81Attempts to load all yaml files to verify syntax.
82 - `--allow-multiple-documents` - allow yaml files which use the
83 [multi-document syntax](http://www.yaml.org/spec/1.2/spec.html#YAML)
84 - `--unsafe` - Instead of loading the files, simply parse them for syntax.
85 A syntax-only check enables extensions and unsafe constructs which would
86 otherwise be forbidden. Using this option removes all guarantees of
87 portability to other yaml implementations.
88 Implies `--allow-multiple-documents`.
89
90#### `debug-statements`
91Check for debugger imports and py37+ `breakpoint()` calls in python source.
92
Mikhail Khvoinitsky1e87d592020-08-02 21:25:07 +030093#### `destroyed-symlinks`
94Detects symlinks which are changed to regular files with a content of a path
95which that symlink was pointing to.
96This usually happens on Windows when a user clones a repository that has
97symlinks but they do not have the permission to create symlinks.
98
Jacob Bundgaard3c614e72020-02-04 23:23:23 +010099#### `detect-aws-credentials`
100Checks for the existence of AWS secrets that you have set up with the AWS CLI.
101The following arguments are available:
102- `--credentials-file CREDENTIALS_FILE` - additional AWS CLI style
103 configuration file in a non-standard location to fetch configured
104 credentials from. Can be repeated multiple times.
105- `--allow-missing-credentials` - Allow hook to pass when no credentials are detected.
106
107#### `detect-private-key`
108Checks for the existence of private keys.
109
110#### `double-quote-string-fixer`
Jacob Bundgaard96efcc62020-02-04 23:24:19 +0100111This hook replaces double quoted strings with single quoted strings.
Anthony Sottiled2a2cde2019-10-07 08:33:23 -0700112
Jacob Bundgaard3c614e72020-02-04 23:23:23 +0100113#### `end-of-file-fixer`
114Makes sure files end in a newline and only a newline.
Anthony Sottiled2a2cde2019-10-07 08:33:23 -0700115
Nuz / Lovegood8fe064d2022-10-08 10:58:42 -0700116#### `file-contents-sorter`
117Sort the lines in specified files (defaults to alphabetical).
Nuz / Lovegood14ac91f2022-10-08 11:16:50 -0700118You must provide the target [`files`](https://pre-commit.com/#config-files) as input.
Nuz / Lovegood8fe064d2022-10-08 10:58:42 -0700119Note that this hook WILL remove blank lines and does NOT respect any comments.
120
Jeremiah Gowdyd18bd5b2020-10-04 18:45:54 -0700121#### `fix-byte-order-marker`
122removes UTF-8 byte order marker
123
Jacob Bundgaard3c614e72020-02-04 23:23:23 +0100124#### `fix-encoding-pragma`
125Add `# -*- coding: utf-8 -*-` to the top of python files.
126 - To remove the coding pragma pass `--remove` (useful in a python3-only codebase)
Anthony Sottiled2a2cde2019-10-07 08:33:23 -0700127
Hugo van Kemenadee7cf4952022-02-21 18:44:06 +0200128The following arguments are available:
129- `--ignore-case` - fold lower case to upper case characters.
130- `--unique` - ensure each line is unique.
131
Jacob Bundgaard3c614e72020-02-04 23:23:23 +0100132#### `forbid-new-submodules`
133Prevent addition of new git submodules.
134
Anthony Sottilea9b6a7e2022-10-04 15:56:38 -0400135This is intended as a helper to migrate away from submodules. If you want to
136ban them entirely use `forbid-submodules`
137
138#### `forbid-submodules`
139forbids any submodules in the repository.
140
Jacob Bundgaard3c614e72020-02-04 23:23:23 +0100141#### `mixed-line-ending`
142Replaces or checks mixed line ending.
143 - `--fix={auto,crlf,lf,no}`
144 - `auto` - Replaces automatically the most frequent line ending. This is the default argument.
145 - `crlf`, `lf` - Forces to replace line ending by respectively CRLF and LF.
146 - This option isn't compatible with git setup check-in LF check-out CRLF as git smudge this later than the hook is invoked.
147 - `no` - Checks if there is any mixed line ending without modifying any file.
148
149#### `name-tests-test`
Anthony Sottile412564f2022-06-07 09:10:42 -0700150verifies that test files are named correctly.
151- `--pytest` (the default): ensure tests match `.*_test\.py`
152- `--pytest-test-first`: ensure tests match `test_.*\.py`
153- `--django` / `--unittest`: ensure tests match `test.*\.py`
Jacob Bundgaard3c614e72020-02-04 23:23:23 +0100154
155#### `no-commit-to-branch`
156Protect specific branches from direct checkins.
Anthony Sottile202ec6b2022-03-24 14:53:14 -0400157 - Use `args: [--branch, staging, --branch, main]` to set the branch.
158 Both `main` and `master` are protected by default if no branch argument is set.
Jacob Bundgaard3c614e72020-02-04 23:23:23 +0100159 - `-b` / `--branch` may be specified multiple times to protect multiple
160 branches.
161 - `-p` / `--pattern` can be used to protect branches that match a supplied regex
162 (e.g. `--pattern, release/.*`). May be specified multiple times.
163
Marc Jayb4694342020-06-19 18:34:16 +0100164Note that `no-commit-to-branch` is configured by default to [`always_run`](https://pre-commit.com/#config-always_run).
165As a result, it will ignore any setting of [`files`](https://pre-commit.com/#config-files),
166[`exclude`](https://pre-commit.com/#config-exclude), [`types`](https://pre-commit.com/#config-types)
167or [`exclude_types`](https://pre-commit.com/#config-exclude_types).
168Set [`always_run: false`](https://pre-commit.com/#config-always_run) to allow this hook to be skipped according to these
169file filters. Caveat: In this configuration, empty commits (`git commit --allow-empty`) would always be allowed by this hook.
170
Jacob Bundgaard3c614e72020-02-04 23:23:23 +0100171#### `pretty-format-json`
172Checks that all your JSON files are pretty. "Pretty"
173here means that keys are sorted and indented. You can configure this with
174the following commandline options:
175 - `--autofix` - automatically format json files
Douglas Thor10915742020-02-27 15:04:33 -0800176 - `--indent ...` - Control the indentation (either a number for a number of spaces or a string of whitespace). Defaults to 2 spaces.
Jacob Bundgaard3c614e72020-02-04 23:23:23 +0100177 - `--no-ensure-ascii` preserve unicode characters instead of converting to escape sequences
178 - `--no-sort-keys` - when autofixing, retain the original key ordering (instead of sorting the keys)
179 - `--top-keys comma,separated,keys` - Keys to keep at the top of mappings.
180
181#### `requirements-txt-fixer`
182Sorts entries in requirements.txt and removes incorrect entry for `pkg-resources==0.0.0`
183
184#### `sort-simple-yaml`
185Sorts simple YAML files which consist only of top-level
186keys, preserving comments and blocks.
187
188Note that `sort-simple-yaml` by default matches no `files` as it enforces a
Marc Jayb4694342020-06-19 18:34:16 +0100189very specific format. You must opt in to this by setting [`files`](https://pre-commit.com/#config-files), for example:
Jacob Bundgaard3c614e72020-02-04 23:23:23 +0100190
191```yaml
192 - id: sort-simple-yaml
193 files: ^config/simple/
194```
195
196
197#### `trailing-whitespace`
198Trims trailing whitespace.
199 - To preserve Markdown [hard linebreaks](https://github.github.com/gfm/#hard-line-break)
200 use `args: [--markdown-linebreak-ext=md]` (or other extensions used
201 by your markdownfiles). If for some reason you want to treat all files
202 as markdown, use `--markdown-linebreak-ext=*`.
203 - By default, this hook trims all whitespace from the ends of lines.
204 To specify a custom set of characters to trim instead, use `args: [--chars,"<chars to trim>"]`.
Anthony Sottile24609de2014-06-18 06:47:14 -0700205
Anthony Sottile526904b2018-10-11 17:19:35 -0700206### Deprecated / replaced hooks
207
Jeremiah Gowdyd18bd5b2020-10-04 18:45:54 -0700208- `check-byte-order-marker`: instead use fix-byte-order-marker
Anthony Sottile526904b2018-10-11 17:19:35 -0700209
Anthony Sottile24609de2014-06-18 06:47:14 -0700210### As a standalone package
211
Jacob Bundgaard3c614e72020-02-04 23:23:23 +0100212If you'd like to use these hooks, they're also available as a standalone package.
Anthony Sottile24609de2014-06-18 06:47:14 -0700213
214Simply `pip install pre-commit-hooks`