README.md 9.0 KB
Newer Older
1
[![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)
2
[![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 Sottile's avatar
Anthony Sottile 已提交
3

Anthony Sottile's avatar
Anthony Sottile 已提交
4
pre-commit-hooks
5
================
Anthony Sottile's avatar
Anthony Sottile 已提交
6
7
8

Some out-of-the-box hooks for pre-commit.

Anthony Sottile's avatar
Anthony Sottile 已提交
9
See also: https://github.com/pre-commit/pre-commit
Anthony Sottile's avatar
Anthony Sottile 已提交
10
11
12
13
14
15


### Using pre-commit-hooks with pre-commit

Add this to your `.pre-commit-config.yaml`

16
17
```yaml
-   repo: https://github.com/pre-commit/pre-commit-hooks
Anthony Sottile's avatar
v4.4.0    
Anthony Sottile 已提交
18
    rev: v4.4.0  # Use the ref you want to point at
19
20
21
22
    hooks:
    -   id: trailing-whitespace
    # -   id: ...
```
Anthony Sottile's avatar
Anthony Sottile 已提交
23
24
25

### Hooks available

26
27
28
#### `check-added-large-files`
Prevent giant files from being committed.
  - 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
31
  - If `git-lfs` is installed, lfs files will be skipped
    (requires `git-lfs>=2.2.1`)
32
33
  - `--enforce-all` - Check all listed files not just those staged for
    addition.
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

#### `check-ast`
Simply check whether files parse as valid python.

#### `check-builtin-literals`
Require literal syntax when initializing empty or zero Python builtin types.
  - Allows calling constructors with positional arguments (e.g., `list('abc')`).
  - Allows calling constructors from the `builtins` (`__builtin__`) namespace (`builtins.list()`).
  - Ignore this requirement for specific builtin types with `--ignore=type1,type2,…`.
  - Forbid `dict` keyword syntax with `--no-allow-dict-kwargs`.

#### `check-case-conflict`
Check for files with names that would conflict on a case-insensitive filesystem like MacOS HFS+ or Windows FAT.

#### `check-docstring-first`
Checks for a common error of placing code before the docstring.

#### `check-executables-have-shebangs`
Checks that non-binary executables have a proper shebang.

#### `check-json`
Attempts to load all json files to verify syntax.

#### `check-merge-conflict`
Check for files that contain merge conflict strings.
59
  - `--assume-in-merge` - Allows running the hook when there is no ongoing merge operation
60

61
62
63
#### `check-shebang-scripts-are-executable`
Checks that scripts with shebangs are executable.

64
65
66
67
68
69
70
71
#### `check-symlinks`
Checks for symlinks which do not point to anything.

#### `check-toml`
Attempts to load all TOML files to verify syntax.

#### `check-vcs-permalinks`
Ensures that links to vcs websites are permalinks.
72
73
74
75
  - `--additional-github-domain DOMAIN` - Add check for specified domain.
    Can be repeated multiple times.  for example, if your company uses
    GitHub Enterprise you may use something like
    `--additional-github-domain github.example.com`
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

#### `check-xml`
Attempts to load all xml files to verify syntax.

#### `check-yaml`
Attempts to load all yaml files to verify syntax.
  - `--allow-multiple-documents` - allow yaml files which use the
    [multi-document syntax](http://www.yaml.org/spec/1.2/spec.html#YAML)
  - `--unsafe` - Instead of loading the files, simply parse them for syntax.
    A syntax-only check enables extensions and unsafe constructs which would
    otherwise be forbidden.  Using this option removes all guarantees of
    portability to other yaml implementations.
    Implies `--allow-multiple-documents`.

#### `debug-statements`
Check for debugger imports and py37+ `breakpoint()` calls in python source.

93
94
95
96
97
98
#### `destroyed-symlinks`
Detects symlinks which are changed to regular files with a content of a path
which that symlink was pointing to.
This usually happens on Windows when a user clones a repository that has
symlinks but they do not have the permission to create symlinks.

99
100
101
102
103
104
105
106
107
108
109
110
#### `detect-aws-credentials`
Checks for the existence of AWS secrets that you have set up with the AWS CLI.
The following arguments are available:
- `--credentials-file CREDENTIALS_FILE` - additional AWS CLI style
  configuration file in a non-standard location to fetch configured
  credentials from. Can be repeated multiple times.
- `--allow-missing-credentials` - Allow hook to pass when no credentials are detected.

#### `detect-private-key`
Checks for the existence of private keys.

#### `double-quote-string-fixer`
Jacob Bundgaard's avatar
Jacob Bundgaard 已提交
111
This hook replaces double quoted strings with single quoted strings.
112
113
114
115

#### `end-of-file-fixer`
Makes sure files end in a newline and only a newline.

116
117
#### `file-contents-sorter`
Sort the lines in specified files (defaults to alphabetical).
Nuz / Lovegood's avatar
Nuz / Lovegood 已提交
118
You must provide the target [`files`](https://pre-commit.com/#config-files) as input.
119
Note that this hook WILL remove blank lines and does NOT respect any comments.
120
All newlines will be converted to line feeds (`\n`).
121

122
123
124
125
The following arguments are available:
- `--ignore-case` - fold lower case to upper case characters.
- `--unique` - ensure each line is unique.

126
127
128
#### `fix-byte-order-marker`
removes UTF-8 byte order marker

129
130
131
132
133
134
135
#### `fix-encoding-pragma`
Add `# -*- coding: utf-8 -*-` to the top of python files.
  - To remove the coding pragma pass `--remove` (useful in a python3-only codebase)

#### `forbid-new-submodules`
Prevent addition of new git submodules.

136
137
138
139
140
141
This is intended as a helper to migrate away from submodules.  If you want to
ban them entirely use `forbid-submodules`

#### `forbid-submodules`
forbids any submodules in the repository.

142
143
144
145
146
147
148
149
150
#### `mixed-line-ending`
Replaces or checks mixed line ending.
  - `--fix={auto,crlf,lf,no}`
      - `auto` - Replaces automatically the most frequent line ending. This is the default argument.
      - `crlf`, `lf` - Forces to replace line ending by respectively CRLF and LF.
          - This option isn't compatible with git setup check-in LF check-out CRLF as git smudge this later than the hook is invoked.
      - `no` - Checks if there is any mixed line ending without modifying any file.

#### `name-tests-test`
151
152
153
154
verifies that test files are named correctly.
- `--pytest` (the default): ensure tests match `.*_test\.py`
- `--pytest-test-first`: ensure tests match `test_.*\.py`
- `--django` / `--unittest`: ensure tests match `test.*\.py`
155
156
157

#### `no-commit-to-branch`
Protect specific branches from direct checkins.
158
159
  - Use `args: [--branch, staging, --branch, main]` to set the branch.
    Both `main` and `master` are protected by default if no branch argument is set.
160
161
162
163
164
  - `-b` / `--branch` may be specified multiple times to protect multiple
    branches.
  - `-p` / `--pattern` can be used to protect branches that match a supplied regex
    (e.g. `--pattern, release/.*`). May be specified multiple times.

165
166
167
168
169
170
171
Note that `no-commit-to-branch` is configured by default to [`always_run`](https://pre-commit.com/#config-always_run).
As a result, it will ignore any setting of [`files`](https://pre-commit.com/#config-files),
[`exclude`](https://pre-commit.com/#config-exclude), [`types`](https://pre-commit.com/#config-types)
or [`exclude_types`](https://pre-commit.com/#config-exclude_types).
Set [`always_run: false`](https://pre-commit.com/#config-always_run) to allow this hook to be skipped according to these
file filters. Caveat: In this configuration, empty commits (`git commit --allow-empty`) would always be allowed by this hook.

172
173
174
175
176
#### `pretty-format-json`
Checks that all your JSON files are pretty.  "Pretty"
here means that keys are sorted and indented.  You can configure this with
the following commandline options:
  - `--autofix` - automatically format json files
177
  - `--indent ...` - Control the indentation (either a number for a number of spaces or a string of whitespace).  Defaults to 2 spaces.
178
179
180
181
182
  - `--no-ensure-ascii` preserve unicode characters instead of converting to escape sequences
  - `--no-sort-keys` - when autofixing, retain the original key ordering (instead of sorting the keys)
  - `--top-keys comma,separated,keys` - Keys to keep at the top of mappings.

#### `requirements-txt-fixer`
183
Sorts entries in requirements.txt and constraints.txt and removes incorrect entry for `pkg-resources==0.0.0`
184
185
186
187
188
189

#### `sort-simple-yaml`
Sorts simple YAML files which consist only of top-level
keys, preserving comments and blocks.

Note that `sort-simple-yaml` by default matches no `files` as it enforces a
190
very specific format.  You must opt in to this by setting [`files`](https://pre-commit.com/#config-files), for example:
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205

```yaml
    -   id: sort-simple-yaml
        files: ^config/simple/
```


#### `trailing-whitespace`
Trims trailing whitespace.
  - To preserve Markdown [hard linebreaks](https://github.github.com/gfm/#hard-line-break)
    use `args: [--markdown-linebreak-ext=md]` (or other extensions used
    by your markdownfiles).  If for some reason you want to treat all files
    as markdown, use `--markdown-linebreak-ext=*`.
  - By default, this hook trims all whitespace from the ends of lines.
    To specify a custom set of characters to trim instead, use `args: [--chars,"<chars to trim>"]`.
Anthony Sottile's avatar
Anthony Sottile 已提交
206

207
208
### Deprecated / replaced hooks

209
- `check-byte-order-marker`: instead use fix-byte-order-marker
210

Anthony Sottile's avatar
Anthony Sottile 已提交
211
212
### As a standalone package

213
If you'd like to use these hooks, they're also available as a standalone package.
Anthony Sottile's avatar
Anthony Sottile 已提交
214
215

Simply `pip install pre-commit-hooks`