diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml deleted file mode 100644 index 9408e44d..00000000 --- a/.github/FUNDING.yml +++ /dev/null @@ -1,2 +0,0 @@ -github: asottile -open_collective: pre-commit diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2bd89e0d..4639bc6b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.0.1 + rev: v4.1.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer @@ -13,7 +13,7 @@ repos: - id: double-quote-string-fixer - id: requirements-txt-fixer - repo: https://github.com/PyCQA/flake8 - rev: 3.9.2 + rev: 4.0.1 hooks: - id: flake8 additional_dependencies: [flake8-typing-imports==1.7.0] @@ -22,25 +22,26 @@ repos: hooks: - id: autopep8 - repo: https://github.com/asottile/reorder_python_imports - rev: v2.5.0 + rev: v2.6.0 hooks: - id: reorder-python-imports args: [--py3-plus] - repo: https://github.com/asottile/pyupgrade - rev: v2.15.0 + rev: v2.29.1 hooks: - id: pyupgrade args: [--py36-plus] - repo: https://github.com/asottile/add-trailing-comma - rev: v2.1.0 + rev: v2.2.1 hooks: - id: add-trailing-comma args: [--py36-plus] - repo: https://github.com/asottile/setup-cfg-fmt - rev: v1.17.0 + rev: v1.20.0 hooks: - id: setup-cfg-fmt - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.812 + rev: v0.920 hooks: - id: mypy + additional_dependencies: [types-all] diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 91dbdf0b..56906603 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -1,192 +1,193 @@ - id: check-added-large-files - name: Check for added large files - description: Prevent giant files from being committed + name: check for added large files + description: prevents giant files from being committed. entry: check-added-large-files language: python - id: check-ast - name: Check python ast - description: Simply check whether the files parse as valid python. + name: check python ast + description: simply checks whether the files parse as valid python. entry: check-ast language: python types: [python] - id: check-byte-order-marker name: 'check BOM - deprecated: use fix-byte-order-marker' - description: forbid files which have a UTF-8 byte-order marker + description: forbids files which have a utf-8 byte-order marker. entry: check-byte-order-marker language: python types: [text] - id: check-builtin-literals - name: Check builtin type constructor use - description: Require literal syntax when initializing empty or zero Python builtin types. + name: check builtin type constructor use + description: requires literal syntax when initializing empty or zero python builtin types. entry: check-builtin-literals language: python types: [python] - id: check-case-conflict - name: Check for case conflicts - description: Check for files that would conflict in case-insensitive filesystems + name: check for case conflicts + description: checks for files that would conflict in case-insensitive filesystems. entry: check-case-conflict language: python - id: check-docstring-first - name: Check docstring is first - description: Checks a common error of defining a docstring after code. + name: check docstring is first + description: checks a common error of defining a docstring after code. entry: check-docstring-first language: python types: [python] - id: check-executables-have-shebangs - name: Check that executables have shebangs - description: Ensures that (non-binary) executables have a shebang. + name: check that executables have shebangs + description: ensures that (non-binary) executables have a shebang. entry: check-executables-have-shebangs language: python types: [text, executable] stages: [commit, push, manual] - id: check-json - name: Check JSON - description: This hook checks json files for parseable syntax. + name: check json + description: checks json files for parseable syntax. entry: check-json language: python types: [json] - id: check-shebang-scripts-are-executable - name: Check that scripts with shebangs are executable - description: Ensures that (non-binary) files with a shebang are executable. + name: check that scripts with shebangs are executable + description: ensures that (non-binary) files with a shebang are executable. entry: check-shebang-scripts-are-executable language: python types: [text] stages: [commit, push, manual] - id: pretty-format-json - name: Pretty format JSON - description: This hook sets a standard for formatting JSON files. + name: pretty format json + description: sets a standard for formatting json files. entry: pretty-format-json language: python types: [json] - id: check-merge-conflict - name: Check for merge conflicts - description: Check for files that contain merge conflict strings. + name: check for merge conflicts + description: checks for files that contain merge conflict strings. entry: check-merge-conflict language: python types: [text] - id: check-symlinks - name: Check for broken symlinks - description: Checks for symlinks which do not point to anything. + name: check for broken symlinks + description: checks for symlinks which do not point to anything. entry: check-symlinks language: python types: [symlink] - id: check-toml - name: Check Toml - description: This hook checks toml files for parseable syntax. + name: check toml + description: checks toml files for parseable syntax. entry: check-toml language: python types: [toml] - id: check-vcs-permalinks - name: Check vcs permalinks - description: Ensures that links to vcs websites are permalinks. + name: check vcs permalinks + description: ensures that links to vcs websites are permalinks. entry: check-vcs-permalinks language: python types: [text] - id: check-xml - name: Check Xml - description: This hook checks xml files for parseable syntax. + name: check xml + description: checks xml files for parseable syntax. entry: check-xml language: python types: [xml] - id: check-yaml - name: Check Yaml - description: This hook checks yaml files for parseable syntax. + name: check yaml + description: checks yaml files for parseable syntax. entry: check-yaml language: python types: [yaml] - id: debug-statements - name: Debug Statements (Python) - description: Check for debugger imports and py37+ `breakpoint()` calls in python source. + name: debug statements (python) + description: checks for debugger imports and py37+ `breakpoint()` calls in python source. entry: debug-statement-hook language: python types: [python] - id: destroyed-symlinks - name: Detect Destroyed Symlinks - description: Detects symlinks which are changed to regular files with a content of a path which that symlink was pointing to. + name: detect destroyed symlinks + description: detects symlinks which are changed to regular files with a content of a path which that symlink was pointing to. entry: destroyed-symlinks language: python types: [file] - id: detect-aws-credentials - name: Detect AWS Credentials - description: Detects *your* aws credentials from the aws cli credentials file + name: detect aws credentials + description: detects *your* aws credentials from the aws cli credentials file. entry: detect-aws-credentials language: python types: [text] - id: detect-private-key - name: Detect Private Key - description: Detects the presence of private keys + name: detect private key + description: detects the presence of private keys. entry: detect-private-key language: python types: [text] - id: double-quote-string-fixer - name: Fix double quoted strings - description: This hook replaces double quoted strings with single quoted strings + name: fix double quoted strings + description: replaces double quoted strings with single quoted strings. entry: double-quote-string-fixer language: python types: [python] - id: end-of-file-fixer - name: Fix End of Files - description: Ensures that a file is either empty, or ends with one newline. + name: fix end of files + description: ensures that a file is either empty, or ends with one newline. entry: end-of-file-fixer language: python types: [text] stages: [commit, push, manual] - id: file-contents-sorter - name: File Contents Sorter - description: Sort the lines in specified files (defaults to alphabetical). You must provide list of target files as input in your .pre-commit-config.yaml file. + name: file contents sorter + description: sorts the lines in specified files (defaults to alphabetical). you must provide list of target files as input in your .pre-commit-config.yaml file. entry: file-contents-sorter language: python files: '^$' - id: fix-byte-order-marker - name: fix UTF-8 byte order marker - description: removes UTF-8 byte order marker + name: fix utf-8 byte order marker + description: removes utf-8 byte order marker. entry: fix-byte-order-marker language: python types: [text] - id: fix-encoding-pragma - name: Fix python encoding pragma + name: fix python encoding pragma + description: 'adds # -*- coding: utf-8 -*- to the top of python files.' language: python entry: fix-encoding-pragma - description: 'Add # -*- coding: utf-8 -*- to the top of python files' types: [python] - id: forbid-new-submodules - name: Forbid new submodules + name: forbid new submodules + description: prevents addition of new git submodules. language: python entry: forbid-new-submodules - description: Prevent addition of new git submodules + types: [directory] - id: mixed-line-ending - name: Mixed line ending - description: Replaces or checks mixed line ending + name: mixed line ending + description: replaces or checks mixed line ending. entry: mixed-line-ending language: python types: [text] - id: name-tests-test - name: Tests should end in _test.py - description: This verifies that test files are named correctly + name: tests should end in _test.py + description: this verifies that test files are named correctly. entry: name-tests-test language: python files: (^|/)tests/.+\.py$ - id: no-commit-to-branch - name: "Don't commit to branch" + name: "don't commit to branch" entry: no-commit-to-branch language: python pass_filenames: false always_run: true - id: requirements-txt-fixer - name: Fix requirements.txt - description: Sorts entries in requirements.txt + name: fix requirements.txt + description: sorts entries in requirements.txt. entry: requirements-txt-fixer language: python files: requirements.*\.txt$ - id: sort-simple-yaml - name: Sort simple YAML files + name: sort simple yaml files + description: sorts simple yaml files which consist only of top-level keys, preserving comments and blocks. language: python entry: sort-simple-yaml - description: Sorts simple YAML files which consist only of top-level keys, preserving comments and blocks. files: '^$' - id: trailing-whitespace - name: Trim Trailing Whitespace - description: This hook trims trailing whitespace. + name: trim trailing whitespace + description: trims trailing whitespace. entry: trailing-whitespace-fixer language: python types: [text] diff --git a/CHANGELOG.md b/CHANGELOG.md index 530c4c8d..50e72022 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,34 @@ +4.1.0 - 2021-12-22 +================== + +### Features +- `debug-statements`: add `pdbr` debugger. + - #614 PR by @cansarigol. +- `detect-private-key`: add detection for additional key types. + - #658 PR by @ljmf00. +- `check-executables-have-shebangs`: improve messaging on windows. + - #689 PR by @pujitm. + - #686 issue by @jmerdich. +- `check-added-large-files`: support `--enforce-all` with `git-lfs`. + - #674 PR by @amartani. + - #560 issue by @jeremy-coulon. + +### Fixes +- `check-case-conflict`: improve performance. + - #626 PR by @guykisel. + - #625 issue by @guykisel. +- `forbid-new-submodules`: fix false-negatives for `pre-push`. + - #619 PR by @m-khvoinitsky. + - #609 issue by @m-khvoinitsky. +- `check-merge-conflict`: fix execution in git worktrees. + - #662 PR by @errsyn. + - #638 issue by @daschuer. + +### Misc. +- Normalize case of hook names and descriptions. + - #671 PR by @dennisroche. + - #673 PR by @revolter. + 4.0.1 - 2021-05-16 ================== diff --git a/README.md b/README.md index 7486aba2..c5a199e1 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Add this to your `.pre-commit-config.yaml` ```yaml - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.0.1 # Use the ref you want to point at + rev: v4.1.0 # Use the ref you want to point at hooks: - id: trailing-whitespace # - id: ... diff --git a/pre_commit_hooks/check_added_large_files.py b/pre_commit_hooks/check_added_large_files.py index cb646d7b..e1beb4ef 100644 --- a/pre_commit_hooks/check_added_large_files.py +++ b/pre_commit_hooks/check_added_large_files.py @@ -1,24 +1,33 @@ import argparse -import json import math import os +import subprocess from typing import Optional from typing import Sequence from typing import Set from pre_commit_hooks.util import added_files -from pre_commit_hooks.util import CalledProcessError -from pre_commit_hooks.util import cmd_output +from pre_commit_hooks.util import zsplit -def lfs_files() -> Set[str]: - try: - # Introduced in git-lfs 2.2.0, first working in 2.2.1 - lfs_ret = cmd_output('git', 'lfs', 'status', '--json') - except CalledProcessError: # pragma: no cover (with git-lfs) - lfs_ret = '{"files":{}}' +def filter_lfs_files(filenames: Set[str]) -> None: # pragma: no cover (lfs) + """Remove files tracked by git-lfs from the set.""" + if not filenames: + return - return set(json.loads(lfs_ret)['files']) + check_attr = subprocess.run( + ('git', 'check-attr', 'filter', '-z', '--stdin'), + stdout=subprocess.PIPE, + stderr=subprocess.DEVNULL, + encoding='utf-8', + check=True, + input='\0'.join(filenames), + ) + stdout = zsplit(check_attr.stdout) + for i in range(0, len(stdout), 3): + filename, filter_tag = stdout[i], stdout[i + 2] + if filter_tag == 'lfs': + filenames.remove(filename) def find_large_added_files( @@ -30,7 +39,9 @@ def find_large_added_files( # Find all added files that are also in the list of files pre-commit tells # us about retv = 0 - filenames_filtered = set(filenames) - lfs_files() + filenames_filtered = set(filenames) + filter_lfs_files(filenames_filtered) + if not enforce_all: filenames_filtered &= added_files() @@ -55,7 +66,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int: ) parser.add_argument( '--maxkb', type=int, default=500, - help='Maxmimum allowable KB for added files', + help='Maximum allowable KB for added files', ) args = parser.parse_args(argv) @@ -67,4 +78,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/check_ast.py b/pre_commit_hooks/check_ast.py index 2be6e1af..ab5661dc 100644 --- a/pre_commit_hooks/check_ast.py +++ b/pre_commit_hooks/check_ast.py @@ -29,4 +29,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/check_builtin_literals.py b/pre_commit_hooks/check_builtin_literals.py index 6bcd8387..3fbae3e8 100644 --- a/pre_commit_hooks/check_builtin_literals.py +++ b/pre_commit_hooks/check_builtin_literals.py @@ -103,4 +103,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/check_byte_order_marker.py b/pre_commit_hooks/check_byte_order_marker.py index c0c2969c..fda05e84 100644 --- a/pre_commit_hooks/check_byte_order_marker.py +++ b/pre_commit_hooks/check_byte_order_marker.py @@ -20,4 +20,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/check_case_conflict.py b/pre_commit_hooks/check_case_conflict.py index 024c1c3c..c3f39db5 100644 --- a/pre_commit_hooks/check_case_conflict.py +++ b/pre_commit_hooks/check_case_conflict.py @@ -1,5 +1,4 @@ import argparse -import os.path from typing import Iterable from typing import Iterator from typing import Optional @@ -15,10 +14,11 @@ def lower_set(iterable: Iterable[str]) -> Set[str]: def parents(file: str) -> Iterator[str]: - file = os.path.dirname(file) - while file: - yield file - file = os.path.dirname(file) + path_parts = file.split('/') + path_parts.pop() + while path_parts: + yield '/'.join(path_parts) + path_parts.pop() def directories_for(files: Set[str]) -> Set[str]: @@ -69,4 +69,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/check_executables_have_shebangs.py b/pre_commit_hooks/check_executables_have_shebangs.py index e271c662..34af5cac 100644 --- a/pre_commit_hooks/check_executables_have_shebangs.py +++ b/pre_commit_hooks/check_executables_have_shebangs.py @@ -64,6 +64,8 @@ def _message(path: str) -> None: f'{path}: marked executable but has no (or invalid) shebang!\n' f" If it isn't supposed to be executable, try: " f'`chmod -x {shlex.quote(path)}`\n' + f' If on Windows, you may also need to: ' + f'`git add --chmod=-x {shlex.quote(path)}`\n' f' If it is supposed to be executable, double-check its shebang.', file=sys.stderr, ) @@ -78,4 +80,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/check_json.py b/pre_commit_hooks/check_json.py index db589d01..96ba024e 100644 --- a/pre_commit_hooks/check_json.py +++ b/pre_commit_hooks/check_json.py @@ -37,4 +37,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/check_merge_conflict.py b/pre_commit_hooks/check_merge_conflict.py index c20a8af7..aed1e9ca 100644 --- a/pre_commit_hooks/check_merge_conflict.py +++ b/pre_commit_hooks/check_merge_conflict.py @@ -3,6 +3,8 @@ from typing import Optional from typing import Sequence +from pre_commit_hooks.util import cmd_output + CONFLICT_PATTERNS = [ b'<<<<<<< ', @@ -12,13 +14,14 @@ ] -def is_in_merge() -> int: +def is_in_merge() -> bool: + git_dir = cmd_output('git', 'rev-parse', '--git-dir').rstrip() return ( - os.path.exists(os.path.join('.git', 'MERGE_MSG')) and + os.path.exists(os.path.join(git_dir, 'MERGE_MSG')) and ( - os.path.exists(os.path.join('.git', 'MERGE_HEAD')) or - os.path.exists(os.path.join('.git', 'rebase-apply')) or - os.path.exists(os.path.join('.git', 'rebase-merge')) + os.path.exists(os.path.join(git_dir, 'MERGE_HEAD')) or + os.path.exists(os.path.join(git_dir, 'rebase-apply')) or + os.path.exists(os.path.join(git_dir, 'rebase-merge')) ) ) @@ -48,4 +51,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/check_shebang_scripts_are_executable.py b/pre_commit_hooks/check_shebang_scripts_are_executable.py index dce8c59d..50bc9c0c 100644 --- a/pre_commit_hooks/check_shebang_scripts_are_executable.py +++ b/pre_commit_hooks/check_shebang_scripts_are_executable.py @@ -50,4 +50,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/check_symlinks.py b/pre_commit_hooks/check_symlinks.py index f014714a..db3b4fea 100644 --- a/pre_commit_hooks/check_symlinks.py +++ b/pre_commit_hooks/check_symlinks.py @@ -23,4 +23,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/check_toml.py b/pre_commit_hooks/check_toml.py index 87496753..f623e688 100644 --- a/pre_commit_hooks/check_toml.py +++ b/pre_commit_hooks/check_toml.py @@ -21,4 +21,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/check_vcs_permalinks.py b/pre_commit_hooks/check_vcs_permalinks.py index 5231d7af..1c77b9a3 100644 --- a/pre_commit_hooks/check_vcs_permalinks.py +++ b/pre_commit_hooks/check_vcs_permalinks.py @@ -57,4 +57,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/check_xml.py b/pre_commit_hooks/check_xml.py index 59b4d59e..0fa6bb23 100644 --- a/pre_commit_hooks/check_xml.py +++ b/pre_commit_hooks/check_xml.py @@ -22,4 +22,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/check_yaml.py b/pre_commit_hooks/check_yaml.py index 7453f6fb..5e86b73a 100644 --- a/pre_commit_hooks/check_yaml.py +++ b/pre_commit_hooks/check_yaml.py @@ -68,4 +68,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/debug_statement_hook.py b/pre_commit_hooks/debug_statement_hook.py index 794f7080..f78d6d62 100644 --- a/pre_commit_hooks/debug_statement_hook.py +++ b/pre_commit_hooks/debug_statement_hook.py @@ -10,6 +10,7 @@ DEBUG_STATEMENTS = { 'ipdb', 'pdb', + 'pdbr', 'pudb', 'pydevd_pycharm', 'q', @@ -81,4 +82,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/destroyed_symlinks.py b/pre_commit_hooks/destroyed_symlinks.py old mode 100755 new mode 100644 index cfaf4e53..a3f122ff --- a/pre_commit_hooks/destroyed_symlinks.py +++ b/pre_commit_hooks/destroyed_symlinks.py @@ -93,4 +93,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/detect_aws_credentials.py b/pre_commit_hooks/detect_aws_credentials.py index 1663cfd6..ba1d789a 100644 --- a/pre_commit_hooks/detect_aws_credentials.py +++ b/pre_commit_hooks/detect_aws_credentials.py @@ -149,4 +149,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/detect_private_key.py b/pre_commit_hooks/detect_private_key.py index 7bbc2f91..18f95394 100644 --- a/pre_commit_hooks/detect_private_key.py +++ b/pre_commit_hooks/detect_private_key.py @@ -11,6 +11,8 @@ b'PuTTY-User-Key-File-2', b'BEGIN SSH2 ENCRYPTED PRIVATE KEY', b'BEGIN PGP PRIVATE KEY BLOCK', + b'BEGIN ENCRYPTED PRIVATE KEY', + b'BEGIN OpenVPN Static key V1', ] @@ -36,4 +38,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/end_of_file_fixer.py b/pre_commit_hooks/end_of_file_fixer.py index 1c07379d..40e8821d 100644 --- a/pre_commit_hooks/end_of_file_fixer.py +++ b/pre_commit_hooks/end_of_file_fixer.py @@ -67,4 +67,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/file_contents_sorter.py b/pre_commit_hooks/file_contents_sorter.py index ebbcd206..392e2261 100644 --- a/pre_commit_hooks/file_contents_sorter.py +++ b/pre_commit_hooks/file_contents_sorter.py @@ -81,4 +81,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/fix_byte_order_marker.py b/pre_commit_hooks/fix_byte_order_marker.py index 1ffe047d..51b94e16 100644 --- a/pre_commit_hooks/fix_byte_order_marker.py +++ b/pre_commit_hooks/fix_byte_order_marker.py @@ -27,4 +27,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/fix_encoding_pragma.py b/pre_commit_hooks/fix_encoding_pragma.py index 88d72ed7..c704774f 100644 --- a/pre_commit_hooks/fix_encoding_pragma.py +++ b/pre_commit_hooks/fix_encoding_pragma.py @@ -145,4 +145,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/forbid_new_submodules.py b/pre_commit_hooks/forbid_new_submodules.py index c144d728..02758089 100644 --- a/pre_commit_hooks/forbid_new_submodules.py +++ b/pre_commit_hooks/forbid_new_submodules.py @@ -1,3 +1,5 @@ +import argparse +import os from typing import Optional from typing import Sequence @@ -5,10 +7,23 @@ def main(argv: Optional[Sequence[str]] = None) -> int: - # `argv` is ignored, pre-commit will send us a list of files that we - # don't care about + parser = argparse.ArgumentParser() + parser.add_argument('filenames', nargs='*') + args = parser.parse_args(argv) + + if ( + 'PRE_COMMIT_FROM_REF' in os.environ and + 'PRE_COMMIT_TO_REF' in os.environ + ): + diff_arg = '...'.join(( + os.environ['PRE_COMMIT_FROM_REF'], + os.environ['PRE_COMMIT_TO_REF'], + )) + else: + diff_arg = '--staged' added_diff = cmd_output( - 'git', 'diff', '--staged', '--diff-filter=A', '--raw', + 'git', 'diff', '--diff-filter=A', '--raw', diff_arg, '--', + *args.filenames, ) retv = 0 for line in added_diff.splitlines(): @@ -29,4 +44,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/mixed_line_ending.py b/pre_commit_hooks/mixed_line_ending.py index 0ef8e2c0..4e07ed96 100644 --- a/pre_commit_hooks/mixed_line_ending.py +++ b/pre_commit_hooks/mixed_line_ending.py @@ -85,4 +85,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/no_commit_to_branch.py b/pre_commit_hooks/no_commit_to_branch.py index 49ffecf7..db848507 100644 --- a/pre_commit_hooks/no_commit_to_branch.py +++ b/pre_commit_hooks/no_commit_to_branch.py @@ -44,4 +44,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/pretty_format_json.py b/pre_commit_hooks/pretty_format_json.py index 61b01698..33ad5a1c 100644 --- a/pre_commit_hooks/pretty_format_json.py +++ b/pre_commit_hooks/pretty_format_json.py @@ -132,4 +132,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/removed.py b/pre_commit_hooks/removed.py index 60df0963..236cbf83 100644 --- a/pre_commit_hooks/removed.py +++ b/pre_commit_hooks/removed.py @@ -12,4 +12,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/requirements_txt_fixer.py b/pre_commit_hooks/requirements_txt_fixer.py index 351e5b15..63f891f7 100644 --- a/pre_commit_hooks/requirements_txt_fixer.py +++ b/pre_commit_hooks/requirements_txt_fixer.py @@ -150,4 +150,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/sort_simple_yaml.py b/pre_commit_hooks/sort_simple_yaml.py old mode 100755 new mode 100644 index 8ebc84ff..39f683e4 --- a/pre_commit_hooks/sort_simple_yaml.py +++ b/pre_commit_hooks/sort_simple_yaml.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python """Sort a simple YAML file, keeping blocks of comments and definitions together. @@ -123,4 +122,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/string_fixer.py b/pre_commit_hooks/string_fixer.py index 3fdb6e2f..a08a5f76 100644 --- a/pre_commit_hooks/string_fixer.py +++ b/pre_commit_hooks/string_fixer.py @@ -77,4 +77,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/tests_should_end_in_test.py b/pre_commit_hooks/tests_should_end_in_test.py index b8cf9152..bffb0c42 100644 --- a/pre_commit_hooks/tests_should_end_in_test.py +++ b/pre_commit_hooks/tests_should_end_in_test.py @@ -30,4 +30,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/pre_commit_hooks/trailing_whitespace_fixer.py b/pre_commit_hooks/trailing_whitespace_fixer.py index 05ed9994..82faa2dc 100644 --- a/pre_commit_hooks/trailing_whitespace_fixer.py +++ b/pre_commit_hooks/trailing_whitespace_fixer.py @@ -99,4 +99,4 @@ def main(argv: Optional[Sequence[str]] = None) -> int: if __name__ == '__main__': - exit(main()) + raise SystemExit(main()) diff --git a/setup.cfg b/setup.cfg index fc579392..45498ebe 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = pre_commit_hooks -version = 4.0.1 +version = 4.1.0 description = Some out-of-the-box hooks for pre-commit. long_description = file: README.md long_description_content_type = text/markdown @@ -17,6 +17,7 @@ classifiers = Programming Language :: Python :: 3.7 Programming Language :: Python :: 3.8 Programming Language :: Python :: 3.9 + Programming Language :: Python :: 3.10 Programming Language :: Python :: Implementation :: CPython Programming Language :: Python :: Implementation :: PyPy @@ -80,6 +81,8 @@ disallow_any_generics = true disallow_incomplete_defs = true disallow_untyped_defs = true no_implicit_optional = true +warn_redundant_casts = true +warn_unused_ignores = true [mypy-testing.*] disallow_untyped_defs = false diff --git a/testing/util.py b/testing/util.py index 8e468d60..50437544 100644 --- a/testing/util.py +++ b/testing/util.py @@ -1,4 +1,5 @@ import os.path +import subprocess TESTING_DIR = os.path.abspath(os.path.dirname(__file__)) @@ -6,3 +7,8 @@ def get_resource_path(path): return os.path.join(TESTING_DIR, 'resources', path) + + +def git_commit(*args, **kwargs): + cmd = ('git', 'commit', '--no-gpg-sign', '--no-verify', '--no-edit', *args) + subprocess.check_call(cmd, **kwargs) diff --git a/tests/check_added_large_files_test.py b/tests/check_added_large_files_test.py index ff53b05b..c16bf5a9 100644 --- a/tests/check_added_large_files_test.py +++ b/tests/check_added_large_files_test.py @@ -1,10 +1,11 @@ -import distutils.spawn +import shutil import pytest from pre_commit_hooks.check_added_large_files import find_large_added_files from pre_commit_hooks.check_added_large_files import main from pre_commit_hooks.util import cmd_output +from testing.util import git_commit def test_nothing_added(temp_git_dir): @@ -75,7 +76,7 @@ def test_integration(temp_git_dir): def has_gitlfs(): - return distutils.spawn.find_executable('git-lfs') is not None + return shutil.which('git-lfs') is not None xfailif_no_gitlfs = pytest.mark.xfail( @@ -84,10 +85,9 @@ def has_gitlfs(): @xfailif_no_gitlfs -def test_allows_gitlfs(temp_git_dir, monkeypatch): # pragma: no cover +def test_allows_gitlfs(temp_git_dir): # pragma: no cover with temp_git_dir.as_cwd(): - monkeypatch.setenv('HOME', str(temp_git_dir)) - cmd_output('git', 'lfs', 'install') + cmd_output('git', 'lfs', 'install', '--local') temp_git_dir.join('f.py').write('a' * 10000) cmd_output('git', 'lfs', 'track', 'f.py') cmd_output('git', 'add', '--', '.') @@ -96,27 +96,37 @@ def test_allows_gitlfs(temp_git_dir, monkeypatch): # pragma: no cover @xfailif_no_gitlfs -def test_moves_with_gitlfs(temp_git_dir, monkeypatch): # pragma: no cover +def test_moves_with_gitlfs(temp_git_dir): # pragma: no cover with temp_git_dir.as_cwd(): - monkeypatch.setenv('HOME', str(temp_git_dir)) - cmd_output('git', 'lfs', 'install') + cmd_output('git', 'lfs', 'install', '--local') cmd_output('git', 'lfs', 'track', 'a.bin', 'b.bin') # First add the file we're going to move temp_git_dir.join('a.bin').write('a' * 10000) cmd_output('git', 'add', '--', '.') - cmd_output('git', 'commit', '--no-gpg-sign', '-am', 'foo') + git_commit('-am', 'foo') # Now move it and make sure the hook still succeeds cmd_output('git', 'mv', 'a.bin', 'b.bin') assert main(('--maxkb', '9', 'b.bin')) == 0 @xfailif_no_gitlfs -def test_enforce_allows_gitlfs(temp_git_dir, monkeypatch): # pragma: no cover +def test_enforce_allows_gitlfs(temp_git_dir): # pragma: no cover with temp_git_dir.as_cwd(): - monkeypatch.setenv('HOME', str(temp_git_dir)) - cmd_output('git', 'lfs', 'install') + cmd_output('git', 'lfs', 'install', '--local') temp_git_dir.join('f.py').write('a' * 10000) cmd_output('git', 'lfs', 'track', 'f.py') cmd_output('git', 'add', '--', '.') # With --enforce-all large files on git lfs should succeed assert main(('--enforce-all', '--maxkb', '9', 'f.py')) == 0 + + +@xfailif_no_gitlfs +def test_enforce_allows_gitlfs_after_commit(temp_git_dir): # pragma: no cover + with temp_git_dir.as_cwd(): + cmd_output('git', 'lfs', 'install', '--local') + temp_git_dir.join('f.py').write('a' * 10000) + cmd_output('git', 'lfs', 'track', 'f.py') + cmd_output('git', 'add', '--', '.') + git_commit('-am', 'foo') + # With --enforce-all large files on git lfs should succeed + assert main(('--enforce-all', '--maxkb', '9', 'f.py')) == 0 diff --git a/tests/check_case_conflict_test.py b/tests/check_case_conflict_test.py index c8c9d122..d9211b57 100644 --- a/tests/check_case_conflict_test.py +++ b/tests/check_case_conflict_test.py @@ -6,6 +6,7 @@ from pre_commit_hooks.check_case_conflict import main from pre_commit_hooks.check_case_conflict import parents from pre_commit_hooks.util import cmd_output +from testing.util import git_commit skip_win32 = pytest.mark.skipif( sys.platform == 'win32', @@ -85,7 +86,7 @@ def test_file_conflicts_with_committed_file(temp_git_dir): with temp_git_dir.as_cwd(): temp_git_dir.join('f.py').write("print('hello world')") cmd_output('git', 'add', 'f.py') - cmd_output('git', 'commit', '--no-gpg-sign', '-n', '-m', 'Add f.py') + git_commit('-m', 'Add f.py') temp_git_dir.join('F.py').write("print('hello world')") cmd_output('git', 'add', 'F.py') @@ -98,7 +99,7 @@ def test_file_conflicts_with_committed_dir(temp_git_dir): with temp_git_dir.as_cwd(): temp_git_dir.mkdir('dir').join('x').write('foo') cmd_output('git', 'add', '-A') - cmd_output('git', 'commit', '--no-gpg-sign', '-n', '-m', 'Add f.py') + git_commit('-m', 'Add f.py') temp_git_dir.join('DIR').write('foo') cmd_output('git', 'add', '-A') diff --git a/tests/check_merge_conflict_test.py b/tests/check_merge_conflict_test.py index fccf41ff..79c1b118 100644 --- a/tests/check_merge_conflict_test.py +++ b/tests/check_merge_conflict_test.py @@ -6,6 +6,7 @@ from pre_commit_hooks.check_merge_conflict import main from pre_commit_hooks.util import cmd_output from testing.util import get_resource_path +from testing.util import git_commit @pytest.fixture @@ -20,19 +21,19 @@ def f1_is_a_conflict_file(tmpdir): with repo1.as_cwd(): repo1_f1.ensure() cmd_output('git', 'add', '.') - cmd_output('git', 'commit', '--no-gpg-sign', '-m', 'commit1') + git_commit('-m', 'commit1') cmd_output('git', 'clone', str(repo1), str(repo2)) # Commit in master with repo1.as_cwd(): repo1_f1.write('parent\n') - cmd_output('git', 'commit', '--no-gpg-sign', '-am', 'master commit2') + git_commit('-am', 'master commit2') # Commit in clone and pull with repo2.as_cwd(): repo2_f1.write('child\n') - cmd_output('git', 'commit', '--no-gpg-sign', '-am', 'clone commit2') + git_commit('-am', 'clone commit2') cmd_output('git', 'pull', '--no-rebase', retcode=None) # We should end up in a merge conflict! f1 = repo2_f1.read() @@ -75,20 +76,20 @@ def repository_pending_merge(tmpdir): with repo1.as_cwd(): repo1_f1.ensure() cmd_output('git', 'add', '.') - cmd_output('git', 'commit', '--no-gpg-sign', '-m', 'commit1') + git_commit('-m', 'commit1') cmd_output('git', 'clone', str(repo1), str(repo2)) # Commit in master with repo1.as_cwd(): repo1_f1.write('parent\n') - cmd_output('git', 'commit', '--no-gpg-sign', '-am', 'master commit2') + git_commit('-am', 'master commit2') # Commit in clone and pull without committing with repo2.as_cwd(): repo2_f2.write('child\n') cmd_output('git', 'add', '.') - cmd_output('git', 'commit', '--no-gpg-sign', '-m', 'clone commit2') + git_commit('-m', 'clone commit2') cmd_output('git', 'pull', '--no-commit', '--no-rebase') # We should end up in a pending merge assert repo2_f1.read() == 'parent\n' @@ -134,3 +135,15 @@ def test_care_when_assumed_merge(tmpdir): f = tmpdir.join('README.md') f.write_binary(b'problem\n=======\n') assert main([str(f.realpath()), '--assume-in-merge']) == 1 + + +def test_worktree_merge_conflicts(f1_is_a_conflict_file, tmpdir): + worktree = tmpdir.join('worktree') + cmd_output('git', 'worktree', 'add', str(worktree)) + with worktree.as_cwd(): + cmd_output( + 'git', 'pull', '--no-rebase', 'origin', 'master', retcode=None, + ) + msg = f1_is_a_conflict_file.join('.git/worktrees/worktree/MERGE_MSG') + assert msg.exists() + test_merge_conflicts_git() diff --git a/tests/destroyed_symlinks_test.py b/tests/destroyed_symlinks_test.py index d2c90310..cde06cfb 100644 --- a/tests/destroyed_symlinks_test.py +++ b/tests/destroyed_symlinks_test.py @@ -5,6 +5,7 @@ from pre_commit_hooks.destroyed_symlinks import find_destroyed_symlinks from pre_commit_hooks.destroyed_symlinks import main +from testing.util import git_commit TEST_SYMLINK = 'test_symlink' TEST_SYMLINK_TARGET = '/doesnt/really/matters' @@ -23,9 +24,7 @@ def repo_with_destroyed_symlink(tmpdir): with open(TEST_FILE, 'w') as f: print('some random content', file=f) subprocess.check_call(('git', 'add', '.')) - subprocess.check_call( - ('git', 'commit', '--no-gpg-sign', '-m', 'initial'), - ) + git_commit('-m', 'initial') assert b'120000 ' in subprocess.check_output( ('git', 'cat-file', '-p', 'HEAD^{tree}'), ) diff --git a/tests/detect_private_key_test.py b/tests/detect_private_key_test.py index 72810008..d2c724f0 100644 --- a/tests/detect_private_key_test.py +++ b/tests/detect_private_key_test.py @@ -10,6 +10,8 @@ (b'-----BEGIN OPENSSH PRIVATE KEY-----', 1), (b'PuTTY-User-Key-File-2: ssh-rsa', 1), (b'---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----', 1), + (b'-----BEGIN ENCRYPTED PRIVATE KEY-----', 1), + (b'-----BEGIN OpenVPN Static key V1-----', 1), (b'ssh-rsa DATA', 0), (b'ssh-dsa DATA', 0), # Some arbitrary binary data diff --git a/tests/forbid_new_submodules_test.py b/tests/forbid_new_submodules_test.py index 4871ae7f..0326d941 100644 --- a/tests/forbid_new_submodules_test.py +++ b/tests/forbid_new_submodules_test.py @@ -1,22 +1,20 @@ +import os import subprocess +from unittest import mock import pytest from pre_commit_hooks.forbid_new_submodules import main +from testing.util import git_commit @pytest.fixture def git_dir_with_git_dir(tmpdir): with tmpdir.as_cwd(): subprocess.check_call(('git', 'init', '.')) - subprocess.check_call(( - 'git', 'commit', '-m', 'init', '--allow-empty', '--no-gpg-sign', - )) + git_commit('--allow-empty', '-m', 'init') subprocess.check_call(('git', 'init', 'foo')) - subprocess.check_call( - ('git', 'commit', '-m', 'init', '--allow-empty', '--no-gpg-sign'), - cwd=str(tmpdir.join('foo')), - ) + git_commit('--allow-empty', '-m', 'init', cwd=str(tmpdir.join('foo'))) yield @@ -31,7 +29,24 @@ def git_dir_with_git_dir(tmpdir): ) def test_main_new_submodule(git_dir_with_git_dir, capsys, cmd): subprocess.check_call(cmd) - assert main() == 1 + assert main(('random_non-related_file',)) == 0 + assert main(('foo',)) == 1 + out, _ = capsys.readouterr() + assert out.startswith('foo: new submodule introduced\n') + + +def test_main_new_submodule_committed(git_dir_with_git_dir, capsys): + rev_parse_cmd = ('git', 'rev-parse', 'HEAD') + from_ref = subprocess.check_output(rev_parse_cmd).decode().strip() + subprocess.check_call(('git', 'submodule', 'add', './foo')) + git_commit('-m', 'new submodule') + to_ref = subprocess.check_output(rev_parse_cmd).decode().strip() + with mock.patch.dict( + os.environ, + {'PRE_COMMIT_FROM_REF': from_ref, 'PRE_COMMIT_TO_REF': to_ref}, + ): + assert main(('random_non-related_file',)) == 0 + assert main(('foo',)) == 1 out, _ = capsys.readouterr() assert out.startswith('foo: new submodule introduced\n') @@ -39,4 +54,4 @@ def test_main_new_submodule(git_dir_with_git_dir, capsys, cmd): def test_main_no_new_submodule(git_dir_with_git_dir): open('test.py', 'a+').close() subprocess.check_call(('git', 'add', 'test.py')) - assert main() == 0 + assert main(('test.py',)) == 0 diff --git a/tests/no_commit_to_branch_test.py b/tests/no_commit_to_branch_test.py index 610e660e..9fcb5808 100644 --- a/tests/no_commit_to_branch_test.py +++ b/tests/no_commit_to_branch_test.py @@ -3,6 +3,7 @@ from pre_commit_hooks.no_commit_to_branch import is_on_branch from pre_commit_hooks.no_commit_to_branch import main from pre_commit_hooks.util import cmd_output +from testing.util import git_commit def test_other_branch(temp_git_dir): @@ -62,7 +63,7 @@ def test_main_default_call(temp_git_dir): def test_not_on_a_branch(temp_git_dir): with temp_git_dir.as_cwd(): - cmd_output('git', 'commit', '--no-gpg-sign', '--allow-empty', '-m1') + git_commit('--allow-empty', '-m1') head = cmd_output('git', 'rev-parse', 'HEAD').strip() cmd_output('git', 'checkout', head) # we're not on a branch!