README.md 8.1 KB
Newer Older
1
2
[![Build Status](https://asottile.visualstudio.com/asottile/_apis/build/status/pre-commit.pre-commit-hooks?branchName=master)](https://asottile.visualstudio.com/asottile/_build/latest?definitionId=17&branchName=master)
[![Azure DevOps coverage](https://img.shields.io/azure-devops/coverage/asottile/asottile/17/master.svg)](https://dev.azure.com/asottile/asottile/_build/latest?definitionId=17&branchName=master)
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
    -   repo: https://github.com/pre-commit/pre-commit-hooks
Anthony Sottile's avatar
v2.4.0    
Anthony Sottile 已提交
17
        rev: v2.4.0  # Use the ref you want to point at
Anthony Sottile's avatar
Anthony Sottile 已提交
18
19
20
21
22
23
24
        hooks:
        -   id: trailing-whitespace
        # -   id: ...


### Hooks available

25
- <a name="check-added-large-files">`check-added-large-files`</a> - Prevent giant files from being committed.
26
    - Specify what is "too large" with `args: ['--maxkb=123']` (default=500kB).
27
28
    - If `git-lfs` is installed, lfs files will be skipped
      (requires `git-lfs>=2.2.1`)
29
30
- <a name="check-ast">`check-ast`</a> - Simply check whether files parse as valid python.
- <a name="check-builtin-literals">`check-builtin-literals`</a> - Require literal syntax when initializing empty or zero Python builtin types.
31
    - Allows calling constructors with positional arguments (e.g., `list('abc')`).
32
    - Allows calling constructors from the `builtins` (`__builtin__`) namespace (`builtins.list()`).
33
34
    - Ignore this requirement for specific builtin types with `--ignore=type1,type2,…`.
    - Forbid `dict` keyword syntax with `--no-allow-dict-kwargs`.
35
36
- <a name="check-byte-order-marker">`check-byte-order-marker`</a> - Forbid files which have a UTF-8 byte-order marker
- <a name="check-case-conflict">`check-case-conflict`</a> - Check for files with names that would conflict on a
37
  case-insensitive filesystem like MacOS HFS+ or Windows FAT.
38
- <a name="check-docstring-first">`check-docstring-first`</a> - Checks for a common error of placing code before
39
  the docstring.
40
- <a name="check-executables-have-shebangs">`check-executables-have-shebangs`</a> - Checks that non-binary executables have a
41
  proper shebang.
42
43
44
45
46
47
48
- <a name="check-json">`check-json`</a> - Attempts to load all json files to verify syntax.
- <a name="check-merge-conflict">`check-merge-conflict`</a> - Check for files that contain merge conflict strings.
- <a name="check-symlinks">`check-symlinks`</a> - Checks for symlinks which do not point to anything.
- <a name="check-toml">`check-toml`</a> - Attempts to load all TOML files to verify syntax.
- <a name="check-vcs-permalinks">`check-vcs-permalinks`</a> - Ensures that links to vcs websites are permalinks.
- <a name="check-xml">`check-xml`</a> - Attempts to load all xml files to verify syntax.
- <a name="check-yaml">`check-yaml`</a> - Attempts to load all yaml files to verify syntax.
49
50
    - `--allow-multiple-documents` - allow yaml files which use the
      [multi-document syntax](http://www.yaml.org/spec/1.2/spec.html#YAML)
Anthony Sottile's avatar
Anthony Sottile 已提交
51
    - `--unsafe` - Instead of loading the files, simply parse them for syntax.
52
53
54
55
      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`.
56
- <a name="debug-statements">`debug-statements`</a> - Check for debugger imports and py37+ `breakpoint()`
57
  calls in python source.
58
- <a name="detect-aws-credentials">`detect-aws-credentials`</a> - Checks for the existence of AWS secrets that you
59
60
  have set up with the AWS CLI.
  The following arguments are available:
61
  - `--credentials-file CREDENTIALS_FILE` - additional AWS CLI style
62
63
    configuration file in a non-standard location to fetch configured
    credentials from. Can be repeated multiple times.
64
65
  - `--allow-missing-credentials` - Allow hook to pass when no credentials are
    detected.
66
67
- <a name="detect-private-key">`detect-private-key`</a> - Checks for the existence of private keys.
- <a name="double-quote-string-fixer">`double-quote-string-fixer`</a> - This hook replaces double quoted strings
68
  with single quoted strings.
69
70
- <a name="end-of-file-fixer">`end-of-file-fixer`</a> - Makes sure files end in a newline and only a newline.
- <a name="fix-encoding-pragma">`fix-encoding-pragma`</a> - Add `# -*- coding: utf-8 -*-` to the top of python files.
71
    - To remove the coding pragma pass `--remove` (useful in a python3-only codebase)
72
73
74
75
- <a name="file-contents-sorter">`file-contents-sorter`</a> - Sort the lines in specified files (defaults to alphabetical). You must provide list of target files as input to it. Note that this hook WILL remove blank lines and does NOT respect any comments.
- <a name="flake8">`flake8`</a> - Run flake8 on your python files.
- <a name="forbid-new-submodules">`forbid-new-submodules`</a> - Prevent addition of new git submodules.
- <a name="mixed-line-ending">`mixed-line-ending`</a> - Replaces or checks mixed line ending.
76
77
78
    - `--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.
79
            - This option isn't compatible with git setup check-in LF check-out CRLF as git smudge this later than the hook is invoked.
80
        - `no` - Checks if there is any mixed line ending without modifying any file.
81
- <a name="name-tests-test">`name-tests-test`</a> - Assert that files in tests/ end in `_test.py`.
82
    - Use `args: ['--django']` to match `test*.py` instead.
83
- <a name="no-commit-to-branch">`no-commit-to-branch`</a> - Protect specific branches from direct checkins.
84
    - Use `args: [--branch, staging, --branch, master]` to set the branch.
85
      `master` is the default if no branch argument is set.
86
87
    - `-b` / `--branch` may be specified multiple times to protect multiple
      branches.
88
    - `-p` / `--pattern` can be used to protect branches that match a supplied regex
89
      (e.g. `--pattern, release/.*`). May be specified multiple times.
90
- <a name="pretty-format-json">`pretty-format-json`</a> - Checks that all your JSON files are pretty.  "Pretty"
91
92
  here means that keys are sorted and indented.  You can configure this with
  the following commandline options:
93
    - `--autofix` - automatically format json files
94
    - `--indent ...` - Control the indentation (either a number for a number of spaces or a string of whitespace).  Defaults to 4 spaces.
95
    - `--no-ensure-ascii` preserve unicode characters instead of converting to escape sequences
96
    - `--no-sort-keys` - when autofixing, retain the original key ordering (instead of sorting the keys)
97
    - `--top-keys comma,separated,keys` - Keys to keep at the top of mappings.
98
99
- <a name="requirements-txt-fixer">`requirements-txt-fixer`</a> - Sorts entries in requirements.txt and removes incorrect entry for `pkg-resources==0.0.0`
- <a name="sort-simple-yaml">`sort-simple-yaml`</a> - Sorts simple YAML files which consist only of top-level
100
101
102
103
104
105
106
107
108
109
110
  keys, preserving comments and blocks.

  Note that `sort-simple-yaml` by default matches no `files` as it enforces a
  very specific format.  You must opt in to this by setting `files`, for
  example:

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

111
- <a name="trailing-whitespace">`trailing-whitespace`</a> - Trims trailing whitespace.
112
113
114
115
    - 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=*`.
iconmaster5326's avatar
iconmaster5326 已提交
116
117
    - 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 已提交
118

119
120
121
122
123
124
### Deprecated / replaced hooks

- `autopep8-wrapper`: instead use
  [mirrors-autopep8](https://github.com/pre-commit/mirrors-autopep8)
- `pyflakes`: instead use `flake8`

Anthony Sottile's avatar
Anthony Sottile 已提交
125
126
127
128
129
130
### As a standalone package

If you'd like to use these hooks, they're also available as a standalone
package.

Simply `pip install pre-commit-hooks`