|
| 1 | +[](https://asottile.visualstudio.com/asottile/_build/latest?definitionId=17&branchName=master) |
| 2 | +[](https://dev.azure.com/asottile/asottile/_build/latest?definitionId=17&branchName=master) |
| 3 | + |
| 4 | +pre-commit-hooks |
| 5 | +================ |
| 6 | + |
| 7 | +Some out-of-the-box hooks for pre-commit. |
| 8 | + |
| 9 | +See also: https://github.com/pre-commit/pre-commit |
| 10 | + |
| 11 | + |
| 12 | +### Using pre-commit-hooks with pre-commit |
| 13 | + |
| 14 | +Add this to your `.pre-commit-config.yaml` |
| 15 | + |
| 16 | +```yaml |
| 17 | +- repo: https://github.com/pre-commit/pre-commit-hooks |
| 18 | + rev: v3.2.0 # Use the ref you want to point at |
| 19 | + hooks: |
| 20 | + - id: trailing-whitespace |
| 21 | + # - id: ... |
| 22 | +``` |
| 23 | + |
| 24 | +### Hooks available |
| 25 | + |
| 26 | +#### `check-added-large-files` |
| 27 | +Prevent giant files from being committed. |
| 28 | + - Specify what is "too large" with `args: ['--maxkb=123']` (default=500kB). |
| 29 | + - Limits checked files to those indicated as staged for addition by git. |
| 30 | + - If `git-lfs` is installed, lfs files will be skipped |
| 31 | + (requires `git-lfs>=2.2.1`) |
| 32 | + - `--enforce-all` - Check all listed files not just those staged for |
| 33 | + addition. |
| 34 | + |
| 35 | +#### `check-ast` |
| 36 | +Simply check whether files parse as valid python. |
| 37 | + |
| 38 | +#### `check-builtin-literals` |
| 39 | +Require 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 | + |
| 45 | +#### `check-case-conflict` |
| 46 | +Check for files with names that would conflict on a case-insensitive filesystem like MacOS HFS+ or Windows FAT. |
| 47 | + |
| 48 | +#### `check-docstring-first` |
| 49 | +Checks for a common error of placing code before the docstring. |
| 50 | + |
| 51 | +#### `check-executables-have-shebangs` |
| 52 | +Checks that non-binary executables have a proper shebang. |
| 53 | + |
| 54 | +#### `check-json` |
| 55 | +Attempts to load all json files to verify syntax. |
| 56 | + |
| 57 | +#### `check-merge-conflict` |
| 58 | +Check for files that contain merge conflict strings. |
| 59 | + |
| 60 | +#### `check-symlinks` |
| 61 | +Checks for symlinks which do not point to anything. |
| 62 | + |
| 63 | +#### `check-toml` |
| 64 | +Attempts to load all TOML files to verify syntax. |
| 65 | + |
| 66 | +#### `check-vcs-permalinks` |
| 67 | +Ensures that links to vcs websites are permalinks. |
| 68 | + |
| 69 | +#### `check-xml` |
| 70 | +Attempts to load all xml files to verify syntax. |
| 71 | + |
| 72 | +#### `check-yaml` |
| 73 | +Attempts to load all yaml files to verify syntax. |
| 74 | + - `--allow-multiple-documents` - allow yaml files which use the |
| 75 | + [multi-document syntax](http://www.yaml.org/spec/1.2/spec.html#YAML) |
| 76 | + - `--unsafe` - Instead of loading the files, simply parse them for syntax. |
| 77 | + A syntax-only check enables extensions and unsafe constructs which would |
| 78 | + otherwise be forbidden. Using this option removes all guarantees of |
| 79 | + portability to other yaml implementations. |
| 80 | + Implies `--allow-multiple-documents`. |
| 81 | + |
| 82 | +#### `debug-statements` |
| 83 | +Check for debugger imports and py37+ `breakpoint()` calls in python source. |
| 84 | + |
| 85 | +#### `detect-aws-credentials` |
| 86 | +Checks for the existence of AWS secrets that you have set up with the AWS CLI. |
| 87 | +The following arguments are available: |
| 88 | +- `--credentials-file CREDENTIALS_FILE` - additional AWS CLI style |
| 89 | + configuration file in a non-standard location to fetch configured |
| 90 | + credentials from. Can be repeated multiple times. |
| 91 | +- `--allow-missing-credentials` - Allow hook to pass when no credentials are detected. |
| 92 | + |
| 93 | +#### `detect-private-key` |
| 94 | +Checks for the existence of private keys. |
| 95 | + |
| 96 | +#### `double-quote-string-fixer` |
| 97 | +This hook replaces double quoted strings with single quoted strings. |
| 98 | + |
| 99 | +#### `end-of-file-fixer` |
| 100 | +Makes sure files end in a newline and only a newline. |
| 101 | + |
| 102 | +#### `fix-byte-order-marker` |
| 103 | +removes UTF-8 byte order marker |
| 104 | + |
| 105 | +#### `fix-encoding-pragma` |
| 106 | +Add `# -*- coding: utf-8 -*-` to the top of python files. |
| 107 | + - To remove the coding pragma pass `--remove` (useful in a python3-only codebase) |
| 108 | + |
| 109 | +#### `file-contents-sorter` |
| 110 | +Sort the lines in specified files (defaults to alphabetical). |
| 111 | +You must provide list of target files as input to it. |
| 112 | +Note that this hook WILL remove blank lines and does NOT respect any comments. |
| 113 | + |
| 114 | +#### `forbid-new-submodules` |
| 115 | +Prevent addition of new git submodules. |
| 116 | + |
| 117 | +#### `mixed-line-ending` |
| 118 | +Replaces or checks mixed line ending. |
| 119 | + - `--fix={auto,crlf,lf,no}` |
| 120 | + - `auto` - Replaces automatically the most frequent line ending. This is the default argument. |
| 121 | + - `crlf`, `lf` - Forces to replace line ending by respectively CRLF and LF. |
| 122 | + - This option isn't compatible with git setup check-in LF check-out CRLF as git smudge this later than the hook is invoked. |
| 123 | + - `no` - Checks if there is any mixed line ending without modifying any file. |
| 124 | + |
| 125 | +#### `name-tests-test` |
| 126 | +Assert that files in tests/ end in `_test.py`. |
| 127 | + - Use `args: ['--django']` to match `test*.py` instead. |
| 128 | + |
| 129 | +#### `no-commit-to-branch` |
| 130 | +Protect specific branches from direct checkins. |
| 131 | + - Use `args: [--branch, staging, --branch, master]` to set the branch. |
| 132 | + `master` is the default if no branch argument is set. |
| 133 | + - `-b` / `--branch` may be specified multiple times to protect multiple |
| 134 | + branches. |
| 135 | + - `-p` / `--pattern` can be used to protect branches that match a supplied regex |
| 136 | + (e.g. `--pattern, release/.*`). May be specified multiple times. |
| 137 | + |
| 138 | +Note that `no-commit-to-branch` is configured by default to [`always_run`](https://pre-commit.com/#config-always_run). |
| 139 | +As a result, it will ignore any setting of [`files`](https://pre-commit.com/#config-files), |
| 140 | +[`exclude`](https://pre-commit.com/#config-exclude), [`types`](https://pre-commit.com/#config-types) |
| 141 | +or [`exclude_types`](https://pre-commit.com/#config-exclude_types). |
| 142 | +Set [`always_run: false`](https://pre-commit.com/#config-always_run) to allow this hook to be skipped according to these |
| 143 | +file filters. Caveat: In this configuration, empty commits (`git commit --allow-empty`) would always be allowed by this hook. |
| 144 | + |
| 145 | +#### `pretty-format-json` |
| 146 | +Checks that all your JSON files are pretty. "Pretty" |
| 147 | +here means that keys are sorted and indented. You can configure this with |
| 148 | +the following commandline options: |
| 149 | + - `--autofix` - automatically format json files |
| 150 | + - `--indent ...` - Control the indentation (either a number for a number of spaces or a string of whitespace). Defaults to 2 spaces. |
| 151 | + - `--no-ensure-ascii` preserve unicode characters instead of converting to escape sequences |
| 152 | + - `--no-sort-keys` - when autofixing, retain the original key ordering (instead of sorting the keys) |
| 153 | + - `--top-keys comma,separated,keys` - Keys to keep at the top of mappings. |
| 154 | + |
| 155 | +#### `requirements-txt-fixer` |
| 156 | +Sorts entries in requirements.txt and removes incorrect entry for `pkg-resources==0.0.0` |
| 157 | + |
| 158 | +#### `sort-simple-yaml` |
| 159 | +Sorts simple YAML files which consist only of top-level |
| 160 | +keys, preserving comments and blocks. |
| 161 | + |
| 162 | +Note that `sort-simple-yaml` by default matches no `files` as it enforces a |
| 163 | +very specific format. You must opt in to this by setting [`files`](https://pre-commit.com/#config-files), for example: |
| 164 | + |
| 165 | +```yaml |
| 166 | + - id: sort-simple-yaml |
| 167 | + files: ^config/simple/ |
| 168 | +``` |
| 169 | +
|
| 170 | +
|
| 171 | +#### `trailing-whitespace` |
| 172 | +Trims trailing whitespace. |
| 173 | + - To preserve Markdown [hard linebreaks](https://github.github.com/gfm/#hard-line-break) |
| 174 | + use `args: [--markdown-linebreak-ext=md]` (or other extensions used |
| 175 | + by your markdownfiles). If for some reason you want to treat all files |
| 176 | + as markdown, use `--markdown-linebreak-ext=*`. |
| 177 | + - By default, this hook trims all whitespace from the ends of lines. |
| 178 | + To specify a custom set of characters to trim instead, use `args: [--chars,"<chars to trim>"]`. |
| 179 | + |
| 180 | +### Deprecated / replaced hooks |
| 181 | + |
| 182 | +- `autopep8-wrapper`: instead use |
| 183 | + [mirrors-autopep8](https://github.com/pre-commit/mirrors-autopep8) |
| 184 | +- `pyflakes`: instead use `flake8` |
| 185 | +- `flake8`: instead use [upstream flake8](https://gitlab.com/pycqa/flake8) |
| 186 | +- `check-byte-order-marker`: instead use fix-byte-order-marker |
| 187 | + |
| 188 | +### As a standalone package |
| 189 | + |
| 190 | +If you'd like to use these hooks, they're also available as a standalone package. |
| 191 | + |
| 192 | +Simply `pip install pre-commit-hooks` |
0 commit comments