Git钩子脚本对于在提交代码审查之前识别简单问题很有用。我们在每次提交时都运行钩子,以自动指出代码中的问题,例如缺少分号,尾随空白和调试语句。通过在代码审阅之前指出这些问题,代码审阅者可以专注于更改的体系结构,而不会浪费琐碎的样式问题。

我们建立了预提交来解决钩子问题。它是用于预提交挂钩的多语言包管理器。您可以指定所需的挂钩列表,并且在每次提交之前,预提交可以管理用任何语言编写的任何挂钩的安装和执行。 pre-commit是专门为不需要root访问而设计的。如果您的开发人员之一未安装节点,但修改了JavaScript文件,则预提交会自动处理下载和构建节点,以在没有root的情况下运行eslint。
在此介绍的pre-commit只是git hook的一部分, git hook分客户端和服务端的,pre-commit属于客户端的。

下面我将以我在linux 下安装pre-commit 检查python代码为例子讲解 。官网:https://pre-commit.com/

1: 安装

Using pip:

pip install pre-commit
如果你的项目中有requirements.txt, 把pre-commit加入到里面。

然后命令行输入查看是否安装成功:

$ pre-commit --version
pre-commit 2.1.1

配置

我们需要添加个pre-commit的配置文件,在git项目的根目录创建一个.pre-commit-config.yaml 文件

下面是我设置python 检查的一个文件举例:

# 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: v2.4.0
    hooks:
    -   id: check-xml
    -   id: check-added-large-files
    -   id: check-byte-order-marker
-   repo: https://gitlab.com/pycqa/flake8
    rev: '3.7.9'
    hooks:
    -   id: flake8
-   repo: https://github.com/codespell-project/codespell
    rev: v1.16.0
    hooks:
    -   id: codespell

在命令行执行下面命令:

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

我们就把pre-commit安装到了项目.git的hook目录下面,我们在执行git commit 的时候就会先调用这个文件,当然还可以在git操作的很多步骤前面做一些工作,pre-merge pre-update…

$ ls .git/hooks/
applypatch-msg.sample      post-update.sample         pre-commit.sample          pre-push.sample            update.sample
commit-msg.sample          pre-applypatch.sample      pre-merge-commit.sample    pre-rebase.sample
fsmonitor-watchman.sample  pre-commit                 prepare-commit-msg.sample  pre-receive.sample

然后我们来看下这个pre-commit文件

$ cat .git/hooks/pre-commit
#!/usr/bin/env python.exe
# File generated by pre-commit: https://pre-commit.com
# ID: 138fd403232d2ddd5efb44317e38bf03
import os
import sys

# we try our best, but the shebang of this script is difficult to determine:
# - macos doesn't ship with python3
# - windows executables are almost always `python.exe`
# therefore we continue to support python2 for this small script
if sys.version_info < (3, 3):
    from distutils.spawn import find_executable as which
else:
    from shutil import which

# work around https://github.com/Homebrew/homebrew-core/issues/30445
os.environ.pop('__PYVENV_LAUNCHER__', None)

# start templated
INSTALL_PYTHON = 'c:\\users\\wuh17\\appdata\\local\\programs\\python\\python37-32\\python.exe'
ARGS = ['hook-impl', '--config=.pre-commit-config.yaml', '--hook-type=pre-commit']
# end templated
ARGS.extend(('--hook-dir', os.path.realpath(os.path.dirname(__file__))))
ARGS.append('--')
ARGS.extend(sys.argv[1:])

DNE = '`pre-commit` not found.  Did you forget to activate your virtualenv?'
if os.access(INSTALL_PYTHON, os.X_OK):
    CMD = [INSTALL_PYTHON, '-mpre_commit']
elif which('pre-commit'):
    CMD = ['pre-commit']
else:
    raise SystemExit(DNE)

CMD.extend(ARGS)
if sys.platform == 'win32':  # https://bugs.python.org/issue19124
    import subprocess

    if sys.version_info < (3, 7):  # https://bugs.python.org/issue25942
        raise SystemExit(subprocess.Popen(CMD).wait())
    else:
        raise SystemExit(subprocess.call(CMD))
else:
    os.execvp(CMD[0], CMD)

通过pdb 调试这个文件我们可以看到

(Pdb) p CMD
['c:\\users\\wuh17\\appdata\\local\\programs\\python\\python37-32\\python.exe', '-mpre_commit', 'hook-impl', '--config=.pre-commit-config.yaml', '--hook-type=pre-commit', '--hook-dir', 'C:\\Users\\wuh17\\Documents\\pythontest\\.git\\hooks', '--']

命令执行的全部参数,开始我们创建的.pre-commit-config.yaml 文件就会被传递进去。

执行

安装配置好后,最好做个全文的检查,修复问题。

$ pre-commit run --all-files
[INFO] Initializing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Initializing environment for https://github.com/psf/black.
[INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
[INFO] Installing environment for https://github.com/psf/black.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
Check Yaml...............................................................Passed
Fix End of Files.........................................................Passed
Trim Trailing Whitespace.................................................Failed
- hook id: trailing-whitespace
- exit code: 1

Files were modified by this hook. Additional output:

Fixing sample.py

black....................................................................Passed

当然我们在做git commit的时候只会对我我们git add的文件进行检查。

其他用途

还可以不再git commit 的时候做检查,可以单独检查某个文件,检查某次commit的提交等等用法。

$ pre-commit run --help
usage: pre-commit run [-h] [--color {auto,always,never}] [-c CONFIG]
                      [--verbose] [--origin ORIGIN] [--source SOURCE]
                      [--commit-msg-filename COMMIT_MSG_FILENAME]
                      [--remote-name REMOTE_NAME] [--remote-url REMOTE_URL]
                      [--hook-stage {commit,merge-commit,prepare-commit-msg,commit-msg,manual,push}]
                      [--show-diff-on-failure]
                      [--all-files | --files [FILES [FILES ...]]]
                      [hook]

positional arguments:
  hook                  A single hook-id to run

optional arguments:
  -h, --help            show this help message and exit
  --color {auto,always,never}
                        Whether to use color in output. Defaults to `auto`.
  -c CONFIG, --config CONFIG
                        Path to alternate config file
  --verbose, -v
  --origin ORIGIN, -o ORIGIN
                        The origin branch's commit_id when using `git push`.
  --source SOURCE, -s SOURCE
                        The remote branch's commit_id when using `git push`.
  --commit-msg-filename COMMIT_MSG_FILENAME
                        Filename to check when running during `commit-msg`
  --remote-name REMOTE_NAME
                        Remote name used by `git push`.
  --remote-url REMOTE_URL
                        Remote url used by `git push`.
  --hook-stage {commit,merge-commit,prepare-commit-msg,commit-msg,manual,push}
                        The stage during which the hook is fired. One of
                        commit, merge-commit, prepare-commit-msg, commit-msg,
                        manual, push
  --show-diff-on-failure
                        When hooks fail, run `git diff` directly afterward.
  --all-files, -a       Run on all the files in the repo.
  --files [FILES [FILES ...]]
                        Specific filenames to run hooks on.

总结

pre-commit 变化比较多的在于用户的需求,根据需求进行配置文件的修改,例如修改行的长度值:

-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v1.2.3
    hooks:
    -   id: flake8
        args: [--max-line-length=131]

欢迎关注交流学习,to be better

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐