Skip to content

mmphego/pre-commit-hooks

Repository files navigation

Pre-Commit Hooks

A personal collection of pre-commit hooks that can makes my life easier :)

I created a blog post Why You Need To Stop Using Git-Hooks, if you would like to read more about pre-commit.

Option 1

Using pre-commit-hooks with pre-commit

Add this to your .pre-commit-config.yaml

-   repo: https://github.com/mmphego/pre-commit-hooks
    rev: master  # Use the ref you want to point at
    hooks:
    -   id: flake8
    -   id: black
    -   id: pytest

Option 2

Installation

On the root directory, you will find a script: setup_hooks.sh and the script will:

  1. Find all directories under the /home which contain any .git directory
  2. Install the hooks into them
  3. Sets init.templateDir to point to templates/git-commit-template.txt

Pre-commit Installation

# clone this repository and install pre-commit hooks
git clone git@github.com:mmphego/pre-commit-hooks.git
# change directory
cd pre-commit-hooks.git

# [Blind Install]
# Install pre-commit package and current hooks across all packages in your /home
./setup_hooks.sh install

# [Self-Aware install]
# Install pre-commit package
pip install pre-commit

# verify and modify `.pre-commit-config.yaml` for your python enviroment (default Python3.6)
vim .pre-commit-config.yaml

# Install pre-commit to your repository
pre-commit install

Skip Checker to Commit

Skip the rule checker pytest with the following command:

SKIP=pytest pre-commit run --all-files

Pre-commit contents

If you want to "skip", "ignore" or "add" the git hook rule, trying to modify pre-commit config file and take off the checker id in .pre-commit-config.yaml and run pre-commit install again.

Usage

Use git commit command to run the hooks and commit staged files. Use pre-commit run --all to run the hooks without commiting. Use pre-commit autoupdate to update the repo rev if you experience any problems running the hooks.

List of hooks available

  • Black: Formats your staged files with Black formatter (black should be installed and configured in your project).
  • Flake8: Runs flake8 linter to find any problems with file formatting according to specified rules flake8.
  • autopep8: Automatically formats Python code to conform to the PEP 8 style guide with autopep8
  • Pytest: Runs pytest with flags -sv. It means that tests will be run with no capture and verbose.
  • shellcheck: Runs shellcheck which lints shell/bash scripts.
  • isort: Automatically sort Python imports.
  • pydocstyle: Static analysis tool for checking compliance with Python docstring conventions.
  • and many more - checkout the .pre-commit-config.yaml files.

Notes

  • Be careful using different linters / formatters at the same time (like black, autopep8 and flake8`). They may have different conflicts so in order to use them together you should solve them first or have a configuration file handy.

Additional info

Find more about pre-commit hooks here.

Features

  • Use black to autoformat
  • Use flake8 to run static analysis tests (black doesn't fix everything)
  • Have your CI separated into 2 pipelines
    1. Run flake8 and pytest
    2. To publish
  • Have CI tests pass to be able to merge to master

Reference

About

A personal collection of pre-commit hooks that can makes my life easier :)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published