项目管理:代码检查 pre-commit 使用详解

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

  • 8
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: git:husky>pre-commit是一个Git钩子,它在提交代码前执行一些操作。通常用于代码风格检查、单元测试等操作,以确保代码质量和稳定性。Husky是一个Git钩子管理工具,可以方便地配置和管理Git钩子。 ### 回答2: Git是目前最流行的版本控制系统。在大多数项目中,代码的质量和一致性非常重要。在Git中,可以使用钩子(Hooks)来确保代码的质量和一致性。其中,pre-commit是常用的钩子之一。而Git: Husky则是Git中常用的钩子管理工具。 Git: Husky是一个钩子管理工具,它允许开发者进行钩子配置,包括在提交代码之前运行何种指令。 具体地说,Git: Husky支持pre-commit钩子,使开发者可以在代码提交之前运行一些脚本,以确保提交的代码的质量和一致性。 pre-commit钩子可以被用于在代码提交之前运行某些指令。例如:静态代码分析工具,或者在代码提交之前运行的测试。因此,pre-commit钩子可以帮助开发者检查代码错误、格式、风格等。其过程是:在代码提交之前,pre-commit钩子运行拖的脚本,检查指定的问题并生成反馈,然后提交成功或失败。 总之,Git: Husky中的pre-commit钩子是非常实用的工具。它可以让开发者在代码提交之前运行各种指令,以检查代码错误、格式、风格等。这有助于确保代码的质量和一致性,提高软件开发的效率和质量。所以,建议开发者在Git中应用Git: Husky这一钩子管理工具,并使用pre-commit钩子确保代码的质量。 ### 回答3: Git是一款非常流行的版本控制工具,而husky>pre-commit是Git中的一个功能。Husky是一款JavaScript库,可以通过它来为Git的hooks添加JavaScript脚本,而pre-commit是Git hook的一种类型,可以在commit前触发,并且可以运行一些脚本来帮助我们检查代码或其它工作。 在Git中,pre-commit可以帮助我们做很多事情。例如,在代码提交之前,我们可以用pre-commit来对代码进行语法检查、格式化、代码审查、代码测试等。如果代码不符合某些规范,pre-commit还可以阻止代码提交,从而让我们更加规范地管理代码使用husky>pre-commit也非常简单。首先,我们需要在项目中安装husky,然后在package.json文件中设置pre-commit属性来配置pre-commit hook。在pre-commit属性中,我们可以定义一个或多个需要执行的脚本,这些脚本会在commit之前执行。例如,我们可以在pre-commit中加入代码格式化、代码审查和代码测试三个脚本,来确保我们提交的代码符合团队的代码规范和质量要求。 总之,使用husky>pre-commit可以很好地帮助我们管理代码,确保代码的质量和一致性。这对于团队协作和代码维护都非常重要。同时,husky>pre-commit也可以提高我们的编码效率,让我们集中精力在代码逻辑的编写上,而不是在代码格式、代码规范等上浪费时间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值