blob: 411529a05be969d5bef2ccadb718deef326fd246 [file] [log] [blame] [view]
Anthony Sottile202ec6b2022-03-24 14:53:14 -04001[![Build Status](https://asottile.visualstudio.com/asottile/_apis/build/status/pre-commit.pre-commit-hooks?branchName=main)](https://asottile.visualstudio.com/asottile/_build/latest?definitionId=17&branchName=main)
2[![Azure DevOps coverage](https://img.shields.io/azure-devops/coverage/asottile/asottile/17/main.svg)](https://dev.azure.com/asottile/asottile/_build/latest?definitionId=17&branchName=main)
3[![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 -07004
Anthony Sottile27291ff2014-03-13 08:41:35 -07005pre-commit-hooks
Anthony Sottile8bb94f02019-03-10 13:42:05 -07006================
Anthony Sottile27291ff2014-03-13 08:41:35 -07007
8Some out-of-the-box hooks for pre-commit.
9
Anthony Sottilecd74dc12014-03-13 09:26:06 -070010See also: https://github.com/pre-commit/pre-commit
Anthony Sottile24609de2014-06-18 06:47:14 -070011
12
13### Using pre-commit-hooks with pre-commit
14
15Add this to your `.pre-commit-config.yaml`
16
Daiki Katsuragawa74024a92020-03-24 02:30:14 +090017```yaml
18- repo: https://github.com/pre-commit/pre-commit-hooks
Anthony Sottile3298dda2022-06-07 10:08:29 -070019 rev: v4.3.0 # Use the ref you want to point at
Daiki Katsuragawa74024a92020-03-24 02:30:14 +090020 hooks:
21 - id: trailing-whitespace
22 # - id: ...
23```
Anthony Sottile24609de2014-06-18 06:47:14 -070024
25### Hooks available
26
Jacob Bundgaard3c614e72020-02-04 23:23:23 +010027#### `check-added-large-files`
28Prevent giant files from being committed.
29 - Specify what is "too large" with `args: ['--maxkb=123']` (default=500kB).
Marcus Shawcroft012bb062020-09-16 06:26:11 +010030 - Limits checked files to those indicated as staged for addition by git.
Jacob Bundgaard3c614e72020-02-04 23:23:23 +010031 - If `git-lfs` is installed, lfs files will be skipped
32 (requires `git-lfs>=2.2.1`)
Marcus Shawcroft012bb062020-09-16 06:26:11 +010033 - `--enforce-all` - Check all listed files not just those staged for
34 addition.
Jacob Bundgaard3c614e72020-02-04 23:23:23 +010035
36#### `check-ast`
37Simply check whether files parse as valid python.
38
39#### `check-builtin-literals`
40Require literal syntax when initializing empty or zero Python builtin types.
41 - Allows calling constructors with positional arguments (e.g., `list('abc')`).
42 - Allows calling constructors from the `builtins` (`__builtin__`) namespace (`builtins.list()`).
43 - Ignore this requirement for specific builtin types with `--ignore=type1,type2,…`.
44 - Forbid `dict` keyword syntax with `--no-allow-dict-kwargs`.
45
Jacob Bundgaard3c614e72020-02-04 23:23:23 +010046#### `check-case-conflict`
47Check for files with names that would conflict on a case-insensitive filesystem like MacOS HFS+ or Windows FAT.
48
49#### `check-docstring-first`
50Checks for a common error of placing code before the docstring.
51
52#### `check-executables-have-shebangs`
53Checks that non-binary executables have a proper shebang.
54
55#### `check-json`
56Attempts to load all json files to verify syntax.
57
58#### `check-merge-conflict`
59Check for files that contain merge conflict strings.
Bastien Gérardaa56da82022-04-25 12:20:30 +020060 - `--assume-in-merge` - Allows running the hook when there is no ongoing merge operation
Jacob Bundgaard3c614e72020-02-04 23:23:23 +010061
Ville Skyttä391ae302021-01-08 17:36:55 +020062#### `check-shebang-scripts-are-executable`
63Checks that scripts with shebangs are executable.
64
Jacob Bundgaard3c614e72020-02-04 23:23:23 +010065#### `check-symlinks`
66Checks for symlinks which do not point to anything.
67
68#### `check-toml`
69Attempts to load all TOML files to verify syntax.
70
71#### `check-vcs-permalinks`
72Ensures that links to vcs websites are permalinks.
Youngmin Koo1f8151a2020-11-17 02:33:47 +090073 - `--additional-github-domain DOMAIN` - Add check for specified domain.
74 Can be repeated multiple times. for example, if your company uses
75 GitHub Enterprise you may use something like
76 `--additional-github-domain github.example.com`
Jacob Bundgaard3c614e72020-02-04 23:23:23 +010077
78#### `check-xml`
79Attempts to load all xml files to verify syntax.
80
81#### `check-yaml`
82Attempts to load all yaml files to verify syntax.
83 - `--allow-multiple-documents` - allow yaml files which use the
84 [multi-document syntax](http://www.yaml.org/spec/1.2/spec.html#YAML)
85 - `--unsafe` - Instead of loading the files, simply parse them for syntax.
86 A syntax-only check enables extensions and unsafe constructs which would
87 otherwise be forbidden. Using this option removes all guarantees of
88 portability to other yaml implementations.
89 Implies `--allow-multiple-documents`.
90
91#### `debug-statements`
92Check for debugger imports and py37+ `breakpoint()` calls in python source.
93
Mikhail Khvoinitsky1e87d592020-08-02 21:25:07 +030094#### `destroyed-symlinks`
95Detects symlinks which are changed to regular files with a content of a path
96which that symlink was pointing to.
97This usually happens on Windows when a user clones a repository that has
98symlinks but they do not have the permission to create symlinks.
99
Jacob Bundgaard3c614e72020-02-04 23:23:23 +0100100#### `detect-aws-credentials`
101Checks for the existence of AWS secrets that you have set up with the AWS CLI.
102The following arguments are available:
103- `--credentials-file CREDENTIALS_FILE` - additional AWS CLI style
104 configuration file in a non-standard location to fetch configured
105 credentials from. Can be repeated multiple times.
106- `--allow-missing-credentials` - Allow hook to pass when no credentials are detected.
107
108#### `detect-private-key`
109Checks for the existence of private keys.
110
111#### `double-quote-string-fixer`
Jacob Bundgaard96efcc62020-02-04 23:24:19 +0100112This hook replaces double quoted strings with single quoted strings.
Anthony Sottiled2a2cde2019-10-07 08:33:23 -0700113
Jacob Bundgaard3c614e72020-02-04 23:23:23 +0100114#### `end-of-file-fixer`
115Makes sure files end in a newline and only a newline.
Anthony Sottiled2a2cde2019-10-07 08:33:23 -0700116
Jeremiah Gowdyd18bd5b2020-10-04 18:45:54 -0700117#### `fix-byte-order-marker`
118removes UTF-8 byte order marker
119
Jacob Bundgaard3c614e72020-02-04 23:23:23 +0100120#### `fix-encoding-pragma`
121Add `# -*- coding: utf-8 -*-` to the top of python files.
122 - To remove the coding pragma pass `--remove` (useful in a python3-only codebase)
Anthony Sottiled2a2cde2019-10-07 08:33:23 -0700123
Jacob Bundgaard3c614e72020-02-04 23:23:23 +0100124#### `file-contents-sorter`
125Sort the lines in specified files (defaults to alphabetical).
126You must provide list of target files as input to it.
127Note that this hook WILL remove blank lines and does NOT respect any comments.
128
Hugo van Kemenadee7cf4952022-02-21 18:44:06 +0200129The following arguments are available:
130- `--ignore-case` - fold lower case to upper case characters.
131- `--unique` - ensure each line is unique.
132
Jacob Bundgaard3c614e72020-02-04 23:23:23 +0100133#### `forbid-new-submodules`
134Prevent addition of new git submodules.
135
136#### `mixed-line-ending`
137Replaces or checks mixed line ending.
138 - `--fix={auto,crlf,lf,no}`
139 - `auto` - Replaces automatically the most frequent line ending. This is the default argument.
140 - `crlf`, `lf` - Forces to replace line ending by respectively CRLF and LF.
141 - This option isn't compatible with git setup check-in LF check-out CRLF as git smudge this later than the hook is invoked.
142 - `no` - Checks if there is any mixed line ending without modifying any file.
143
144#### `name-tests-test`
Anthony Sottile412564f2022-06-07 09:10:42 -0700145verifies that test files are named correctly.
146- `--pytest` (the default): ensure tests match `.*_test\.py`
147- `--pytest-test-first`: ensure tests match `test_.*\.py`
148- `--django` / `--unittest`: ensure tests match `test.*\.py`
Jacob Bundgaard3c614e72020-02-04 23:23:23 +0100149
150#### `no-commit-to-branch`
151Protect specific branches from direct checkins.
Anthony Sottile202ec6b2022-03-24 14:53:14 -0400152 - Use `args: [--branch, staging, --branch, main]` to set the branch.
153 Both `main` and `master` are protected by default if no branch argument is set.
Jacob Bundgaard3c614e72020-02-04 23:23:23 +0100154 - `-b` / `--branch` may be specified multiple times to protect multiple
155 branches.
156 - `-p` / `--pattern` can be used to protect branches that match a supplied regex
157 (e.g. `--pattern, release/.*`). May be specified multiple times.
158
Marc Jayb4694342020-06-19 18:34:16 +0100159Note that `no-commit-to-branch` is configured by default to [`always_run`](https://pre-commit.com/#config-always_run).
160As a result, it will ignore any setting of [`files`](https://pre-commit.com/#config-files),
161[`exclude`](https://pre-commit.com/#config-exclude), [`types`](https://pre-commit.com/#config-types)
162or [`exclude_types`](https://pre-commit.com/#config-exclude_types).
163Set [`always_run: false`](https://pre-commit.com/#config-always_run) to allow this hook to be skipped according to these
164file filters. Caveat: In this configuration, empty commits (`git commit --allow-empty`) would always be allowed by this hook.
165
Jacob Bundgaard3c614e72020-02-04 23:23:23 +0100166#### `pretty-format-json`
167Checks that all your JSON files are pretty. "Pretty"
168here means that keys are sorted and indented. You can configure this with
169the following commandline options:
170 - `--autofix` - automatically format json files
Douglas Thor10915742020-02-27 15:04:33 -0800171 - `--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 +0100172 - `--no-ensure-ascii` preserve unicode characters instead of converting to escape sequences
173 - `--no-sort-keys` - when autofixing, retain the original key ordering (instead of sorting the keys)
174 - `--top-keys comma,separated,keys` - Keys to keep at the top of mappings.
175
176#### `requirements-txt-fixer`
177Sorts entries in requirements.txt and removes incorrect entry for `pkg-resources==0.0.0`
178
179#### `sort-simple-yaml`
180Sorts simple YAML files which consist only of top-level
181keys, preserving comments and blocks.
182
183Note that `sort-simple-yaml` by default matches no `files` as it enforces a
Marc Jayb4694342020-06-19 18:34:16 +0100184very 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 +0100185
186```yaml
187 - id: sort-simple-yaml
188 files: ^config/simple/
189```
190
191
192#### `trailing-whitespace`
193Trims trailing whitespace.
194 - To preserve Markdown [hard linebreaks](https://github.github.com/gfm/#hard-line-break)
195 use `args: [--markdown-linebreak-ext=md]` (or other extensions used
196 by your markdownfiles). If for some reason you want to treat all files
197 as markdown, use `--markdown-linebreak-ext=*`.
198 - By default, this hook trims all whitespace from the ends of lines.
199 To specify a custom set of characters to trim instead, use `args: [--chars,"<chars to trim>"]`.
Anthony Sottile24609de2014-06-18 06:47:14 -0700200
Anthony Sottile526904b2018-10-11 17:19:35 -0700201### Deprecated / replaced hooks
202
Jeremiah Gowdyd18bd5b2020-10-04 18:45:54 -0700203- `check-byte-order-marker`: instead use fix-byte-order-marker
Anthony Sottile526904b2018-10-11 17:19:35 -0700204
Anthony Sottile24609de2014-06-18 06:47:14 -0700205### As a standalone package
206
Jacob Bundgaard3c614e72020-02-04 23:23:23 +0100207If you'd like to use these hooks, they're also available as a standalone package.
Anthony Sottile24609de2014-06-18 06:47:14 -0700208
209Simply `pip install pre-commit-hooks`