$ 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
项目要使用git进行代码提交时,使用叫pre-commit的git钩子,在调用git commit 命令时自动执行某些脚本检测代码,若检测出错,则阻止commit代码,也就无法push,保证了出错代码只在我们本地,不会把问题提交到远程仓库
【1】安装husky
npm i husky -save-dev
【2】安装list-staged
npm i lint-stage...
在多人协作项目中,如果代码风格统一、代码提交信息的说明准确,那么在后期协作以及Bug处理时会更加方便。
因此,在本文章中,我会介绍怎么使用下面这个工具,在git push 代码之前检测commit messages:
commitlint
husky
commitlint介绍
2、先来介绍博主采用的commit规范
Commit message格式
&lt;ty...
您可以将以下代码段复制/粘贴到.pre-commit-config.yaml文件中。
注意可以在找到更加充实的版本
# ==========================================================================
# Golang Pre-Commit Hooks | https://github.com/tekwizely/pre-commit-golang
# !! ALL Hooks enabled by default - Comment out hooks you are not using
# Visit the project home page
使用这些钩子
将此添加到您的.pre-commit-config.yaml
- repo: git://github.com/joker8023/pre-commit-golang
rev: master
hooks:
- id: go-fmt
- id: go-vet
- id: go-lint
- id: go-imports
- id: go-cyclo
args: [-over=15]
- id: validate-toml
- id: no-go-testing
- id: gometalinter
- id: golangci-lint
- id: go-critic
- id: go-unit-tests
pre-commit介绍
自定义 Git - Git 钩子,pre-commit 钩子在键入提交信息前运行。 它用于检查即将提交的快照,例如,检查是否有所遗漏,确保测试运行,以及核查代码。 如果该钩子以非零值退出,Git 将放弃此次提交,可以用 git commit --no-verify 来绕过这个环节。
安装git校验
lint-staged和yorkie介绍
lint-staged,lint-staged会对当前add到git stage区的文件进行扫描操作。
youkie,实际是fork husk
写 .pre-commit-config.yaml配置文件
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
# pre-commit==2.13.0
repos:
- repo: https://github.com/pre-commit/pre-commit-hook
如果你使用Git,那你一定懂得纯文本的魅力并喜爱上shell这样的脚本语言。
在很多时候,我更喜欢能够通过脚本语言进行配置的工具,而不是直接安装到编辑器的工具。一是因为脚本可以放在项目中与更多的人共享,以保持规范一直;二是脚本自动触发的操作无需要记更多的快捷键或者点击一点鼠标;再来则是脚本语言可以做更多灵活的操作,而不受软件开发者的约束。这大概也是我一直喜欢用Git指令,而不是编译器.
在项目中初始化 .husky,会在 packgae.json 中添加 prepare 脚本
npm set-script prepare "husky install" && npm run prepare
package.json
"scripts": {
"prepare": "husky install"
这是脑语言v0.5.8版的2500个单字(也称为“令”与“一令”),通过【单字编程】(并不仅是中文编程,而是混合英文关键字,但以单字为主的命名)也许是英文不太好时又希望能写代码的其中一种方式。
我在做脑语言【单字编程】系列的教程,先是《JavaScript单字编程》欢迎参与! ...
前面两节,我们已经能控制git提交对应的规范,但是还存在一个问题就是代码规范问题在项目中使用ESlint和Prettier配合解决代码规范问题时,我们可以通过vscode,但也很有可能存在团队中存在有人没配置,或者在提交前,没有保存自动格式化使用到的插件pre-commit提交时,检测代码规范自动修复格式错误。...