Anthony Sottile | 202ec6b | 2022-03-24 14:53:14 -0400 | [diff] [blame] | 1 | [](https://asottile.visualstudio.com/asottile/_build/latest?definitionId=17&branchName=main) |
| 2 | [](https://dev.azure.com/asottile/asottile/_build/latest?definitionId=17&branchName=main) |
| 3 | [](https://results.pre-commit.ci/latest/github/pre-commit/pre-commit-hooks/main) |
Anthony Sottile | be8b296 | 2014-03-22 18:40:47 -0700 | [diff] [blame] | 4 | |
Anthony Sottile | 27291ff | 2014-03-13 08:41:35 -0700 | [diff] [blame] | 5 | pre-commit-hooks |
Anthony Sottile | 8bb94f0 | 2019-03-10 13:42:05 -0700 | [diff] [blame] | 6 | ================ |
Anthony Sottile | 27291ff | 2014-03-13 08:41:35 -0700 | [diff] [blame] | 7 | |
| 8 | Some out-of-the-box hooks for pre-commit. |
| 9 | |
Anthony Sottile | cd74dc1 | 2014-03-13 09:26:06 -0700 | [diff] [blame] | 10 | See also: https://github.com/pre-commit/pre-commit |
Anthony Sottile | 24609de | 2014-06-18 06:47:14 -0700 | [diff] [blame] | 11 | |
| 12 | |
| 13 | ### Using pre-commit-hooks with pre-commit |
| 14 | |
| 15 | Add this to your `.pre-commit-config.yaml` |
| 16 | |
Daiki Katsuragawa | 74024a9 | 2020-03-24 02:30:14 +0900 | [diff] [blame] | 17 | ```yaml |
| 18 | - repo: https://github.com/pre-commit/pre-commit-hooks |
Anthony Sottile | 3298dda | 2022-06-07 10:08:29 -0700 | [diff] [blame] | 19 | rev: v4.3.0 # Use the ref you want to point at |
Daiki Katsuragawa | 74024a9 | 2020-03-24 02:30:14 +0900 | [diff] [blame] | 20 | hooks: |
| 21 | - id: trailing-whitespace |
| 22 | # - id: ... |
| 23 | ``` |
Anthony Sottile | 24609de | 2014-06-18 06:47:14 -0700 | [diff] [blame] | 24 | |
| 25 | ### Hooks available |
| 26 | |
Jacob Bundgaard | 3c614e7 | 2020-02-04 23:23:23 +0100 | [diff] [blame] | 27 | #### `check-added-large-files` |
| 28 | Prevent giant files from being committed. |
| 29 | - Specify what is "too large" with `args: ['--maxkb=123']` (default=500kB). |
Marcus Shawcroft | 012bb06 | 2020-09-16 06:26:11 +0100 | [diff] [blame] | 30 | - Limits checked files to those indicated as staged for addition by git. |
Jacob Bundgaard | 3c614e7 | 2020-02-04 23:23:23 +0100 | [diff] [blame] | 31 | - If `git-lfs` is installed, lfs files will be skipped |
| 32 | (requires `git-lfs>=2.2.1`) |
Marcus Shawcroft | 012bb06 | 2020-09-16 06:26:11 +0100 | [diff] [blame] | 33 | - `--enforce-all` - Check all listed files not just those staged for |
| 34 | addition. |
Jacob Bundgaard | 3c614e7 | 2020-02-04 23:23:23 +0100 | [diff] [blame] | 35 | |
| 36 | #### `check-ast` |
| 37 | Simply check whether files parse as valid python. |
| 38 | |
| 39 | #### `check-builtin-literals` |
| 40 | Require 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 Bundgaard | 3c614e7 | 2020-02-04 23:23:23 +0100 | [diff] [blame] | 46 | #### `check-case-conflict` |
| 47 | Check for files with names that would conflict on a case-insensitive filesystem like MacOS HFS+ or Windows FAT. |
| 48 | |
| 49 | #### `check-docstring-first` |
| 50 | Checks for a common error of placing code before the docstring. |
| 51 | |
| 52 | #### `check-executables-have-shebangs` |
| 53 | Checks that non-binary executables have a proper shebang. |
| 54 | |
| 55 | #### `check-json` |
| 56 | Attempts to load all json files to verify syntax. |
| 57 | |
| 58 | #### `check-merge-conflict` |
| 59 | Check for files that contain merge conflict strings. |
Bastien Gérard | aa56da8 | 2022-04-25 12:20:30 +0200 | [diff] [blame] | 60 | - `--assume-in-merge` - Allows running the hook when there is no ongoing merge operation |
Jacob Bundgaard | 3c614e7 | 2020-02-04 23:23:23 +0100 | [diff] [blame] | 61 | |
Ville Skyttä | 391ae30 | 2021-01-08 17:36:55 +0200 | [diff] [blame] | 62 | #### `check-shebang-scripts-are-executable` |
| 63 | Checks that scripts with shebangs are executable. |
| 64 | |
Jacob Bundgaard | 3c614e7 | 2020-02-04 23:23:23 +0100 | [diff] [blame] | 65 | #### `check-symlinks` |
| 66 | Checks for symlinks which do not point to anything. |
| 67 | |
| 68 | #### `check-toml` |
| 69 | Attempts to load all TOML files to verify syntax. |
| 70 | |
| 71 | #### `check-vcs-permalinks` |
| 72 | Ensures that links to vcs websites are permalinks. |
Youngmin Koo | 1f8151a | 2020-11-17 02:33:47 +0900 | [diff] [blame] | 73 | - `--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 Bundgaard | 3c614e7 | 2020-02-04 23:23:23 +0100 | [diff] [blame] | 77 | |
| 78 | #### `check-xml` |
| 79 | Attempts to load all xml files to verify syntax. |
| 80 | |
| 81 | #### `check-yaml` |
| 82 | Attempts 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` |
| 92 | Check for debugger imports and py37+ `breakpoint()` calls in python source. |
| 93 | |
Mikhail Khvoinitsky | 1e87d59 | 2020-08-02 21:25:07 +0300 | [diff] [blame] | 94 | #### `destroyed-symlinks` |
| 95 | Detects symlinks which are changed to regular files with a content of a path |
| 96 | which that symlink was pointing to. |
| 97 | This usually happens on Windows when a user clones a repository that has |
| 98 | symlinks but they do not have the permission to create symlinks. |
| 99 | |
Jacob Bundgaard | 3c614e7 | 2020-02-04 23:23:23 +0100 | [diff] [blame] | 100 | #### `detect-aws-credentials` |
| 101 | Checks for the existence of AWS secrets that you have set up with the AWS CLI. |
| 102 | The 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` |
| 109 | Checks for the existence of private keys. |
| 110 | |
| 111 | #### `double-quote-string-fixer` |
Jacob Bundgaard | 96efcc6 | 2020-02-04 23:24:19 +0100 | [diff] [blame] | 112 | This hook replaces double quoted strings with single quoted strings. |
Anthony Sottile | d2a2cde | 2019-10-07 08:33:23 -0700 | [diff] [blame] | 113 | |
Jacob Bundgaard | 3c614e7 | 2020-02-04 23:23:23 +0100 | [diff] [blame] | 114 | #### `end-of-file-fixer` |
| 115 | Makes sure files end in a newline and only a newline. |
Anthony Sottile | d2a2cde | 2019-10-07 08:33:23 -0700 | [diff] [blame] | 116 | |
Jeremiah Gowdy | d18bd5b | 2020-10-04 18:45:54 -0700 | [diff] [blame] | 117 | #### `fix-byte-order-marker` |
| 118 | removes UTF-8 byte order marker |
| 119 | |
Jacob Bundgaard | 3c614e7 | 2020-02-04 23:23:23 +0100 | [diff] [blame] | 120 | #### `fix-encoding-pragma` |
| 121 | Add `# -*- coding: utf-8 -*-` to the top of python files. |
| 122 | - To remove the coding pragma pass `--remove` (useful in a python3-only codebase) |
Anthony Sottile | d2a2cde | 2019-10-07 08:33:23 -0700 | [diff] [blame] | 123 | |
Jacob Bundgaard | 3c614e7 | 2020-02-04 23:23:23 +0100 | [diff] [blame] | 124 | #### `file-contents-sorter` |
| 125 | Sort the lines in specified files (defaults to alphabetical). |
| 126 | You must provide list of target files as input to it. |
| 127 | Note that this hook WILL remove blank lines and does NOT respect any comments. |
| 128 | |
Hugo van Kemenade | e7cf495 | 2022-02-21 18:44:06 +0200 | [diff] [blame] | 129 | The following arguments are available: |
| 130 | - `--ignore-case` - fold lower case to upper case characters. |
| 131 | - `--unique` - ensure each line is unique. |
| 132 | |
Jacob Bundgaard | 3c614e7 | 2020-02-04 23:23:23 +0100 | [diff] [blame] | 133 | #### `forbid-new-submodules` |
| 134 | Prevent addition of new git submodules. |
| 135 | |
| 136 | #### `mixed-line-ending` |
| 137 | Replaces 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 Sottile | 412564f | 2022-06-07 09:10:42 -0700 | [diff] [blame] | 145 | verifies 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 Bundgaard | 3c614e7 | 2020-02-04 23:23:23 +0100 | [diff] [blame] | 149 | |
| 150 | #### `no-commit-to-branch` |
| 151 | Protect specific branches from direct checkins. |
Anthony Sottile | 202ec6b | 2022-03-24 14:53:14 -0400 | [diff] [blame] | 152 | - 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 Bundgaard | 3c614e7 | 2020-02-04 23:23:23 +0100 | [diff] [blame] | 154 | - `-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 Jay | b469434 | 2020-06-19 18:34:16 +0100 | [diff] [blame] | 159 | Note that `no-commit-to-branch` is configured by default to [`always_run`](https://pre-commit.com/#config-always_run). |
| 160 | As 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) |
| 162 | or [`exclude_types`](https://pre-commit.com/#config-exclude_types). |
| 163 | Set [`always_run: false`](https://pre-commit.com/#config-always_run) to allow this hook to be skipped according to these |
| 164 | file filters. Caveat: In this configuration, empty commits (`git commit --allow-empty`) would always be allowed by this hook. |
| 165 | |
Jacob Bundgaard | 3c614e7 | 2020-02-04 23:23:23 +0100 | [diff] [blame] | 166 | #### `pretty-format-json` |
| 167 | Checks that all your JSON files are pretty. "Pretty" |
| 168 | here means that keys are sorted and indented. You can configure this with |
| 169 | the following commandline options: |
| 170 | - `--autofix` - automatically format json files |
Douglas Thor | 1091574 | 2020-02-27 15:04:33 -0800 | [diff] [blame] | 171 | - `--indent ...` - Control the indentation (either a number for a number of spaces or a string of whitespace). Defaults to 2 spaces. |
Jacob Bundgaard | 3c614e7 | 2020-02-04 23:23:23 +0100 | [diff] [blame] | 172 | - `--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` |
| 177 | Sorts entries in requirements.txt and removes incorrect entry for `pkg-resources==0.0.0` |
| 178 | |
| 179 | #### `sort-simple-yaml` |
| 180 | Sorts simple YAML files which consist only of top-level |
| 181 | keys, preserving comments and blocks. |
| 182 | |
| 183 | Note that `sort-simple-yaml` by default matches no `files` as it enforces a |
Marc Jay | b469434 | 2020-06-19 18:34:16 +0100 | [diff] [blame] | 184 | very specific format. You must opt in to this by setting [`files`](https://pre-commit.com/#config-files), for example: |
Jacob Bundgaard | 3c614e7 | 2020-02-04 23:23:23 +0100 | [diff] [blame] | 185 | |
| 186 | ```yaml |
| 187 | - id: sort-simple-yaml |
| 188 | files: ^config/simple/ |
| 189 | ``` |
| 190 | |
| 191 | |
| 192 | #### `trailing-whitespace` |
| 193 | Trims 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 Sottile | 24609de | 2014-06-18 06:47:14 -0700 | [diff] [blame] | 200 | |
Anthony Sottile | 526904b | 2018-10-11 17:19:35 -0700 | [diff] [blame] | 201 | ### Deprecated / replaced hooks |
| 202 | |
Jeremiah Gowdy | d18bd5b | 2020-10-04 18:45:54 -0700 | [diff] [blame] | 203 | - `check-byte-order-marker`: instead use fix-byte-order-marker |
Anthony Sottile | 526904b | 2018-10-11 17:19:35 -0700 | [diff] [blame] | 204 | |
Anthony Sottile | 24609de | 2014-06-18 06:47:14 -0700 | [diff] [blame] | 205 | ### As a standalone package |
| 206 | |
Jacob Bundgaard | 3c614e7 | 2020-02-04 23:23:23 +0100 | [diff] [blame] | 207 | If you'd like to use these hooks, they're also available as a standalone package. |
Anthony Sottile | 24609de | 2014-06-18 06:47:14 -0700 | [diff] [blame] | 208 | |
| 209 | Simply `pip install pre-commit-hooks` |