Skip to content

Commit f4d53ec

Browse files
authored
Add files via upload
0 parents  commit f4d53ec

40 files changed

+2965
-0
lines changed

CHANGELOG.md

+498
Large diffs are not rendered by default.

LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2014 pre-commit dev team: Anthony Sottile, Ken Struys
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

+192
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
[![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)
2+
[![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)
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`

azure-pipelines.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
trigger:
2+
branches:
3+
include: [master, test-me-*]
4+
tags:
5+
include: ['*']
6+
7+
resources:
8+
repositories:
9+
- repository: asottile
10+
type: github
11+
endpoint: github
12+
name: asottile/azure-pipeline-templates
13+
ref: refs/tags/v1.0.0
14+
15+
jobs:
16+
- template: job--pre-commit.yml@asottile
17+
- template: job--python-tox.yml@asottile
18+
parameters:
19+
toxenvs: [py38]
20+
os: windows
21+
- template: job--python-tox.yml@asottile
22+
parameters:
23+
toxenvs: [pypy3, py36, py37, py38]
24+
os: linux

check_added_large_files.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import argparse
2+
import json
3+
import math
4+
import os
5+
from typing import Optional
6+
from typing import Sequence
7+
from typing import Set
8+
9+
from pre_commit_hooks.util import added_files
10+
from pre_commit_hooks.util import CalledProcessError
11+
from pre_commit_hooks.util import cmd_output
12+
13+
14+
def lfs_files() -> Set[str]:
15+
try:
16+
# Introduced in git-lfs 2.2.0, first working in 2.2.1
17+
lfs_ret = cmd_output('git', 'lfs', 'status', '--json')
18+
except CalledProcessError: # pragma: no cover (with git-lfs)
19+
lfs_ret = '{"files":{}}'
20+
21+
return set(json.loads(lfs_ret)['files'])
22+
23+
24+
def find_large_added_files(
25+
filenames: Sequence[str],
26+
maxkb: int,
27+
*,
28+
enforce_all: bool = False,
29+
) -> int:
30+
# Find all added files that are also in the list of files pre-commit tells
31+
# us about
32+
retv = 0
33+
filenames_filtered = set(filenames) - lfs_files()
34+
if not enforce_all:
35+
filenames_filtered &= added_files()
36+
37+
for filename in filenames_filtered:
38+
kb = int(math.ceil(os.stat(filename).st_size / 1024))
39+
if kb > maxkb:
40+
print(f'{filename} ({kb} KB) exceeds {maxkb} KB.')
41+
retv = 1
42+
43+
return retv
44+
45+
46+
def main(argv: Optional[Sequence[str]] = None) -> int:
47+
parser = argparse.ArgumentParser()
48+
parser.add_argument(
49+
'filenames', nargs='*',
50+
help='Filenames pre-commit believes are changed.',
51+
)
52+
parser.add_argument(
53+
'--enforce-all', action='store_true',
54+
help='Enforce all files are checked, not just staged files.',
55+
)
56+
parser.add_argument(
57+
'--maxkb', type=int, default=500,
58+
help='Maxmimum allowable KB for added files',
59+
)
60+
args = parser.parse_args(argv)
61+
62+
return find_large_added_files(
63+
args.filenames,
64+
args.maxkb,
65+
enforce_all=args.enforce_all,
66+
)
67+
68+
69+
if __name__ == '__main__':
70+
exit(main())

check_ast.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import argparse
2+
import ast
3+
import platform
4+
import sys
5+
import traceback
6+
from typing import Optional
7+
from typing import Sequence
8+
9+
10+
def main(argv: Optional[Sequence[str]] = None) -> int:
11+
parser = argparse.ArgumentParser()
12+
parser.add_argument('filenames', nargs='*')
13+
args = parser.parse_args(argv)
14+
15+
retval = 0
16+
for filename in args.filenames:
17+
18+
try:
19+
with open(filename, 'rb') as f:
20+
ast.parse(f.read(), filename=filename)
21+
except SyntaxError:
22+
impl = platform.python_implementation()
23+
version = sys.version.split()[0]
24+
print(f'{filename}: failed parsing with {impl} {version}:')
25+
tb = ' ' + traceback.format_exc().replace('\n', '\n ')
26+
print(f'\n{tb}')
27+
retval = 1
28+
return retval
29+
30+
31+
if __name__ == '__main__':
32+
exit(main())

0 commit comments

Comments
 (0)