what is pre commit looks like and how to implement it
pre-commit can find and fix common issues before changes are submitted for code review;
Code reviewers should pay attention to the architecture of a change and not worry about trivial errors like
not following code style,
How to implement pre-commit [TL;DR]
- write a python script(
pre-commit.py) to check all the checkable files and print error messages;
- get all committed files;
- filter with exclude files;
- split files into
- check with customize pep8, pylint, isort, jshint, jscs etc;
- commit continue if all check pass, else we will have more details in report file.
- write a shell script(
pre_commit)(name should not change) which actually called by git command;
- active the specific env(which create by step 3);
- execute the real checker script.
- write another shell script(
install_pre_commit.sh) to install pre-commit;
- make a independent env with virtualenv;
- install third libs in this env, such as pep8, pylint, isort etc(make sure they have specific version number);
- copy the
pre_commitshell script to git directory and make it executable.
Actually you should not reinvent the wheel
- pip install pre-commit;
- pre-commit install;
- create file
.pre-commit-hooks.yamlat the root of your repo and configure your hooks.
the simplest hooks configuration like this:
- repo: git://github.com/pre-commit/pre-commit-hooks sha: v0.4.2 hooks: - id: trailing-whitespace