Git hook 之 pre-commit 代码检查使用介绍

发布时间: 更新时间: 总字数:914 阅读时间:2m 作者: IP上海 分享 网址

在执行git命令时,通过添加hook执行各种动作进行代码检查。文本介绍如何使用git pre-commit在提交时进行代码格式检查。

什么是 Git hooks

Git hooks 是Git在事件之前或之后执行的脚本,例如:commitpushreceiveGit hooks 是一个内置功能 - 无需下载任何东西。Git hooks 在本地运行。

一些 sample hooks 脚本如下:

  • pre-commit: 检查提交消息是否有拼写错误
  • pre-receive: Enforce project coding standards.
  • post-commit: Email/SMS team members of a new commit.
  • post-receive: Push the code to production.

Git hooks如何工作?

每个Git存储库都有一个.git/hooks文件夹,其中包含可以绑定到的每个钩子的脚本。您可以根据需要随意更改或更新这些脚本,Git将在这些事件发生时执行它们。

pre-commit 简介

pre-commitgit hooks 的一个子集,实现在提交代码审查之前,Git钩子脚本可用于处理简单问题。我们在每次提交时运行我们的钩子,以自动指出代码中的问题,例如缺少分号,尾随空格和调试语句。

本文以python 项目为例,介绍在开发Python代码时,如何使用pre-commit解决代码格式不统一的问题

安装

使用 pip 安装:

pip install pre-commit

使用 brew 安装:

brew install pre-commit

依赖

python 项目根目录下,在 requirements.txt (或 requirements-dev.txt) 填加依赖:

pre-commit

安装

pip install -r requirements-dev.txt

配置

在项目根目录填加 .pre-commit-config.yaml 文件

repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v2.2.3
    hooks:
    -   id: trailing-whitespace
    -   id: end-of-file-fixer
    -   id: check-docstring-first
    -   id: check-json
    -   id: check-added-large-files
    -   id: check-yaml
    -   id: debug-statements
    -   id: name-tests-test
    -   id: double-quote-string-fixer
    -   id: requirements-txt-fixer
-   repo: https://gitlab.com/pycqa/flake8
    rev: 3.7.1
    hooks:
    -   id: flake8
-   repo: https://github.com/pre-commit/mirrors-autopep8
    rev: v1.4.3
    hooks:
    -   id: autopep8
-   repo: https://github.com/pre-commit/pre-commit
    rev: v1.14.2
    hooks:
    -   id: validate_manifest
-   repo: https://github.com/asottile/reorder_python_imports
    rev: v1.3.5
    hooks:
    -   id: reorder-python-imports
        language_version: python3
-   repo: https://github.com/asottile/pyupgrade
    rev: v1.11.1
    hooks:
    -   id: pyupgrade
-   repo: https://github.com/asottile/add-trailing-comma
    rev: v0.7.1
    hooks:
    -   id: add-trailing-comma
-   repo: https://github.com/pre-commit/mirrors-mypy
    rev: v0.660
    hooks:
    -   id: mypy
        language_version: python3

自定义的:

# .pre-commit-config.yaml
default_language_version:
    python: python2.7
fail_fast: true
repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v2.2.3
    hooks:
    -   id: check-merge-conflict
-   repo: https://github.com/asottile/reorder_python_imports
    rev: v1.4.0
    hooks:
    -   id: reorder-python-imports
-   repo: https://github.com/pre-commit/pre-commit
    rev: v1.16.1
    hooks:
    -   id: validate_manifest
-   repo: https://github.com/asottile/pyupgrade
    rev: v1.17.1
    hooks:
    -   id: pyupgrade
-   repo: https://github.com/pre-commit/mirrors-autopep8
    rev: v1.4.4
    hooks:
    -   id: autopep8
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v2.2.3
    hooks:
    # -   id: trailing-whitespace
    # -   id: end-of-file-fixer
    -   id: check-json
    -   id: check-added-large-files
    -   id: check-yaml
    -   id: debug-statements
    # -   id: name-tests-test
    # -   id: double-quote-string-fixer
    -   id: requirements-txt-fixer
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v2.2.3
    hooks:
    -   id: flake8

其中 flake8 根据 .flake8 给出的代码规则检查代码,需要在项目根目录下创建,.flake8 示例:

[flake8]
ignore = C901, W503, F405, E731, F401, F403, F841, F9
# C901: is too complex
# W503: line break before binary operator
# F405: may be undefined, or defined from star imports
exclude =
    *migrations*,
    *.pyc,
    .git,
    .cover,
    __pycache__,
    */node_modules/*,
    */templates_module*,
    */bin/*,
    packages/*
max-line-length = 120
max-complexity = 12
format = pylint
show_source = True
statistics = True
count = True

pre-commit 使用

启用

pre-commit install
# pre-commit install
pre-commit installed at .git/hooks/pre-commit

pre-commit 将会在每次提交前执行,每次 clone 代码后,都需要执行 pre-commit install

手动触发

第一次,需要触发全部:

pre-commit run --all-files

触发指定hook

pre-commit run <hook_id>

第一次 pre-commit 运行时,将会自动下载、安装并且运行 hook

自动合并配置

pre-commit migrate-config

自动更新 hooks

pre-commit autoupdate

支持语言

  • docker
  • docker_image
  • fail
  • golang
  • node
  • python
  • python_venv
  • ruby
  • rust
  • swift
  • pcre
  • pygrep
  • script
  • system

参考

  1. Git hooks
  2. pre-commit
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数