Skip to content

Instantly share code, notes, and snippets.

@peterotool
Last active July 17, 2023 15:48
Show Gist options
  • Save peterotool/806ee5cb47862404c42720b4410dee2c to your computer and use it in GitHub Desktop.
Save peterotool/806ee5cb47862404c42720b4410dee2c to your computer and use it in GitHub Desktop.
Pre-Commit Hook Setup

Pre-commit (catch errors before a commit happens)

Alternative 1

  1. Install pre-commit package
pip install pre-commit
  1. Create and edit a .pre-commit-config.yaml configuration file
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
-   repo: https://github.com/psf/black
    rev: 21.9b0
    hooks:
    -   id: black
        args: [--safe]

-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v3.2.0
    hooks:
    -   id: trailing-whitespace
    -   id: end-of-file-fixer
    -   id: check-yaml
    -   id: check-added-large-files
    -   id: debug-statements
        language_version: python3

-   repo: https://github.com/PyCQA/flake8
    rev: 3.9.2
    hooks:
    -   id: flake8
        language_version: python3

-   repo: https://github.com/asottile/reorder_python_imports
    rev: v2.6.0
    hooks:
    -   id: reorder-python-imports
        args: [--application-directories=.:src, --py36-plus]

-   repo: https://github.com/asottile/pyupgrade
    rev: v2.29.0
    hooks:
    -   id: pyupgrade
        args: [--py36-plus]

-   repo: https://github.com/pre-commit/mirrors-mypy
    rev: v0.910
    hooks:
    -   id: mypy
        files: ^src/
        args: []

or

# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.0.1
    hooks:
    -   id: trailing-whitespace
    -   id: end-of-file-fixer
    -   id: check-yaml
-   repo: https://github.com/psf/black
    rev: 22.3.0
    hooks:
    - id: black
-   repo: https://github.com/pycqa/flake8
    rev: 3.9.2
    hooks:
    - id: flake8
  1. Run pre-commit command to install the git hooks into .git/hooks folder
pre-commit install

Alternative 2

  1. Create a pre-commit file inside .git/hooks folder
#!/bin/sh

flake8 --max-line-lenght 80 ./src/

black --check ./src/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment