2014-03-23 09:40:47 +08:00
[![Build Status ](https://travis-ci.org/pre-commit/pre-commit-hooks.svg?branch=master )](https://travis-ci.org/pre-commit/pre-commit-hooks)
2017-02-02 00:30:58 +08:00
[![Coverage Status ](https://coveralls.io/repos/github/pre-commit/pre-commit-hooks/badge.svg?branch=master )](https://coveralls.io/github/pre-commit/pre-commit-hooks?branch=master)
2015-02-27 23:58:59 +08:00
[![Build status ](https://ci.appveyor.com/api/projects/status/dfcpng35u4g0r0t1/branch/master?svg=true )](https://ci.appveyor.com/project/asottile/pre-commit-hooks/branch/master)
2014-03-23 09:40:47 +08:00
2014-03-13 23:41:35 +08:00
pre-commit-hooks
==========
Some out-of-the-box hooks for pre-commit.
2014-03-14 00:26:06 +08:00
See also: https://github.com/pre-commit/pre-commit
2014-06-18 21:47:14 +08:00
### Using pre-commit-hooks with pre-commit
Add this to your `.pre-commit-config.yaml`
2018-05-29 01:58:28 +08:00
- repo: https://github.com/pre-commit/pre-commit-hooks
2018-12-27 01:48:02 +08:00
rev: v2.1.0 # Use the ref you want to point at
2014-06-18 21:47:14 +08:00
hooks:
- id: trailing-whitespace
# - id: ...
### Hooks available
2015-01-05 05:06:21 +08:00
- `check-added-large-files` - Prevent giant files from being committed.
2015-05-10 16:00:54 +08:00
- Specify what is "too large" with `args: ['--maxkb=123']` (default=500kB).
2017-12-05 09:28:46 +08:00
- If `git-lfs` is installed, lfs files will be skipped
(requires `git-lfs>=2.2.1` )
2015-08-05 04:45:41 +08:00
- `check-ast` - Simply check whether files parse as valid python.
2017-11-26 08:17:47 +08:00
- `check-builtin-literals` - Require literal syntax when initializing empty or zero Python builtin types.
- Allows calling constructors with positional arguments (e.g., `list('abc')` ).
2017-12-01 02:27:16 +08:00
- Allows calling constructors from the `builtins` (`__builtin__`) namespace (`builtins.list()`).
2017-11-26 08:17:47 +08:00
- Ignore this requirement for specific builtin types with `--ignore=type1,type2,…` .
- Forbid `dict` keyword syntax with `--no-allow-dict-kwargs` .
2016-03-19 01:59:31 +08:00
- `check-byte-order-marker` - Forbid files which have a UTF-8 byte-order marker
2015-05-10 16:00:54 +08:00
- `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.
2017-07-03 12:00:28 +08:00
- `check-executables-have-shebangs` - Checks that non-binary executables have a
proper shebang.
2014-08-20 08:33:08 +08:00
- `check-json` - Attempts to load all json files to verify syntax.
2015-03-14 07:30:14 +08:00
- `check-merge-conflict` - Check for files that contain merge conflict strings.
2016-01-15 23:47:33 +08:00
- `check-symlinks` - Checks for symlinks which do not point to anything.
2017-10-07 14:32:11 +08:00
- `check-vcs-permalinks` - Ensures that links to vcs websites are permalinks.
2015-01-18 02:21:44 +08:00
- `check-xml` - Attempts to load all xml files to verify syntax.
2014-06-18 21:47:14 +08:00
- `check-yaml` - Attempts to load all yaml files to verify syntax.
2017-10-13 06:47:20 +08:00
- `--allow-multiple-documents` - allow yaml files which use the
[multi-document syntax ](http://www.yaml.org/spec/1.2/spec.html#YAML )
2018-03-20 01:13:18 +08:00
- `--unsafe` - Instead of loading the files, simply parse them for syntax.
2018-03-20 00:28:18 +08:00
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` .
2018-05-15 00:16:37 +08:00
- `debug-statements` - Check for debugger imports and py37+ `breakpoint()`
calls in python source.
Improve searching for configured AWS credentials
The previous approach for finding AWS credentials was pretty naive and
only covered contents of a single file (~/.aws/credentials by
default).
The AWS CLI documentation states various other ways to configure
credentials which weren't covered:
https://docs.aws.amazon.com/cli/latest/topic/config-vars.html#credentials
Even that aren't all ways, a look into the code shows:
https://github.com/boto/botocore/blob/develop/botocore/credentials.py
This commit changes the behavior so the hook will behave in a way
that if the AWS CLI is able to obtain credentials from local files,
the hook will find them as well.
The changes in detail are:
- detect AWS session tokens and handle them like secret keys.
- always search credentials in the default AWS CLI file locations
( ~/.aws/config, ~/.aws/credentials, /etc/boto.cfg and ~/.boto)
- detect AWS credentials configured via environment variables in
AWS_SECRET_ACCESS_KEY, AWS_SECURITY_TOKEN and AWS_SESSION_TOKEN
- check additional configuration files configured via environment
variables (AWS_CREDENTIAL_FILE, AWS_SHARED_CREDENTIALS_FILE and
BOTO_CONFIG)
- print out the first four characters of each secret found in files to
be checked in, to make it easier to figure out, what the secrets
were, which were going to be checked in
- improve error handling for parsing ini-files
- improve tests
There is a major functional change introduced by this commit:
Locations the AWS CLI gets credentials from are always searched and
there is no way to disable them. --credentials-file is still there to
specify one or more additional files to search credentials in. It's
the purpose of this hook to find and check files for found
credentials, so it should work in any case. As this commit also
improves error handling for not-existing or malformed configuration
files, it should be no big deal.
Receiving credentials via the EC2 and ECS meta data services is not
covered intentionally, to not further increase the amount of changes
in this commit and as it's probably an edge case anyway to have this
hook running in such an environment.
2016-12-30 15:41:24 +08:00
- `detect-aws-credentials` - Checks for the existence of AWS secrets that you
have set up with the AWS CLI.
The following arguments are available:
2018-10-29 06:58:14 +08:00
- `--credentials-file CREDENTIALS_FILE` - additional AWS CLI style
2018-10-29 03:07:03 +08:00
configuration file in a non-standard location to fetch configured
credentials from. Can be repeated multiple times.
2018-10-29 03:05:42 +08:00
- `--allow-missing-credentials` - Allow hook to pass when no credentials are
detected.
2015-05-10 16:00:54 +08:00
- `detect-private-key` - Checks for the existence of private keys.
- `double-quote-string-fixer` - This hook replaces double quoted strings
with single quoted strings.
2014-06-18 21:47:14 +08:00
- `end-of-file-fixer` - Makes sure files end in a newline and only a newline.
2016-04-28 02:18:14 +08:00
- `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)
2017-07-28 06:09:31 +08:00
- `file-contents-sorter` - 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.
2015-05-10 16:00:54 +08:00
- `flake8` - Run flake8 on your python files.
2016-12-01 02:10:29 +08:00
- `forbid-new-submodules` - Prevent addition of new git submodules.
2017-06-14 03:38:14 +08:00
- `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.
- `no` - Checks if there is any mixed line ending without modifying any file.
2015-05-10 16:00:54 +08:00
- `name-tests-test` - Assert that files in tests/ end in `_test.py` .
- Use `args: ['--django']` to match `test*.py` instead.
2017-03-21 01:36:51 +08:00
- `no-commit-to-branch` - Protect specific branches from direct checkins.
2018-10-11 02:15:13 +08:00
- Use `args: [--branch, staging, --branch, master]` to set the branch.
`master` is the default if no argument is set.
2018-06-10 02:16:14 +08:00
- `-b` / `--branch` may be specified multiple times to protect multiple
branches.
2016-12-08 02:44:07 +08:00
- `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:
2016-08-23 05:23:23 +08:00
- `--autofix` - automatically format json files
2016-12-08 02:44:07 +08:00
- `--indent ...` - Control the indentation (either a number for a number of spaces or a string of whitespace). Defaults to 4 spaces.
2016-08-23 05:23:23 +08:00
- `--no-sort-keys` - when autofixing, retain the original key ordering (instead of sorting the keys)
2016-12-08 02:44:07 +08:00
- `--top-keys comma,separated,keys` - Keys to keep at the top of mappings.
2018-03-26 06:02:23 +08:00
- `requirements-txt-fixer` - Sorts entries in requirements.txt and removes incorrect entry for `pkg-resources==0.0.0`
2017-06-24 07:26:00 +08:00
- `sort-simple-yaml` - Sorts simple YAML files which consist only of top-level keys, preserving comments and blocks.
2014-06-18 21:47:14 +08:00
- `trailing-whitespace` - Trims trailing whitespace.
2018-10-13 09:10:02 +08:00
- 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=*` .
2014-06-18 21:47:14 +08:00
2018-10-12 08:19:35 +08:00
### Deprecated / replaced hooks
- `autopep8-wrapper` : instead use
[mirrors-autopep8 ](https://github.com/pre-commit/mirrors-autopep8 )
- `pyflakes` : instead use `flake8`
2014-06-18 21:47:14 +08:00
### 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`