pre-commit-hooks/README.md

111 lines
6.4 KiB
Markdown
Raw Normal View History

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`
- repo: https://github.com/pre-commit/pre-commit-hooks
2018-07-23 00:32:56 +08:00
rev: v1.4.0 # Use the ref you want to point at
2014-06-18 21:47:14 +08:00
hooks:
- id: trailing-whitespace
# - id: ...
### Hooks available
- `autopep8-wrapper` - Runs autopep8 over python source.
- Ignore PEP 8 violation types with `args: ['-i', '--ignore=E000,...']` or
2017-03-14 05:32:43 +08:00
through configuration of the `[pycodestyle]` section in
setup.cfg / tox.ini.
2015-01-05 05:06:21 +08:00
- `check-added-large-files` - Prevent giant files from being committed.
- Specify what is "too large" with `args: ['--maxkb=123']` (default=500kB).
- 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.
- `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`.
2016-03-19 01:59:31 +08:00
- `check-byte-order-marker` - Forbid files which have a UTF-8 byte-order marker
- `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.
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.
- `--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.
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:
2016-12-30 23:53:09 +08:00
- `--credentials-file` - additional AWS CLI style configuration file in a
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
non-standard location to fetch configured credentials from. Can be repeated
multiple times.
- `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.
- `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)
- `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.
- `flake8` - Run flake8 on your python files.
- `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.
- `name-tests-test` - Assert that files in tests/ end in `_test.py`.
- Use `args: ['--django']` to match `test*.py` instead.
- `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.
- `-b` / `--branch` may be specified multiple times to protect multiple
branches.
- `pyflakes` - Run pyflakes on your python files.
- `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
- `--indent ...` - Control the indentation (either a number for a number of spaces or a string of whitespace). Defaults to 4 spaces.
- `--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.
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`
- `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.
- Markdown linebreak trailing spaces preserved for `.md` and`.markdown`;
use `args: ['--markdown-linebreak-ext=txt,text']` to add other extensions,
`args: ['--markdown-linebreak-ext=*']` to preserve them for all files,
or `args: ['--no-markdown-linebreak-ext']` to disable and always trim.
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`