Ale*_*rov 3 git pylint eslint git-husky pre-commit.com
我有一个 JS 前端和 Python 后端的项目。前端已经配置了 husky pre-commit hook。今天我已经用预提交库配置了 Pylint,但是 husky hooks 已经被这个举动覆盖了。是否可以结合 pre-commit 和 husky 库?如果没有......解决问题的最佳方法是什么?
预提交具有用于运行其他现有钩子框架的“迁移模式”。看起来 husky 在检测你正在运行的钩子方面的钩子实现有点太聪明了——他们基于正在执行的文件名
pre-commit 的迁移模式的工作方式是采用任何现有的钩子脚本(在这种情况下,是由 husky 编写的钩子脚本.git/hooks/pre-commit)并添加扩展名.legacy. 然后在执行期间它运行该脚本。
但对哈士奇来说,看起来pre-commit.legacy钩子正在运行(!)
一个小黑客是定义pre-commit.legacy在package.json这似乎工作:
{
"husky": {
"hooks": {
"pre-commit.legacy": "echo hello world"
}
},
"dependencies": {
"husky": "^4.3.0"
}
}
Run Code Online (Sandbox Code Playgroud)
# 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: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
$ git commit -m "both"
husky > pre-commit.legacy (node v12.18.3)
hello world
Trim Trailing Whitespace.................................................Passed
Fix End of Files.........................................................Passed
Check Yaml...........................................(no files to check)Skipped
Check for added large files..............................................Passed
[master 7bd8807] both
1 file changed, 1 insertion(+), 1 deletion(-)
Run Code Online (Sandbox Code Playgroud)
也就是说,这似乎很脆弱。pre-commit 旨在支持多种不同的编程语言(尽管它是用 python 编写的,但它对10 多种编程语言(包括 javascript)具有本机支持)
第一个替代方法可能是从local预提交钩子中调用 husky :
{
"husky": {
"hooks": {
"pre-commit": "echo hello world"
}
},
"dependencies": {
"husky": "^4.3.0"
}
}
Run Code Online (Sandbox Code Playgroud)
.pre-commit-config.yaml# 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: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: local
hooks:
- id: husky-run-pre-commit
name: husky
language: system
entry: node_modules/.bin/husky-run pre-commit
pass_filenames: false
always_run: true
Run Code Online (Sandbox Code Playgroud)
$ pre-commit run --all-files --verbose
Trim Trailing Whitespace.................................................Passed
- hook id: trailing-whitespace
- duration: 0.03s
Fix End of Files.........................................................Passed
- hook id: end-of-file-fixer
- duration: 0.03s
Check Yaml...............................................................Passed
- hook id: check-yaml
- duration: 0.05s
Check for added large files..............................................Passed
- hook id: check-added-large-files
- duration: 0.05s
husky....................................................................Passed
- hook id: husky-run-pre-commit
- duration: 0.07s
husky > pre-commit (node v12.18.3)
hello world
Run Code Online (Sandbox Code Playgroud)
然而,这个解决方案没有利用 pre-commit 的 js 支持,它只是调用已经存在的 husky hooks
更原生的解决方案是直接使用预提交安装 js 钩子,例如,如果您使用的是 eslint:
$ pre-commit run --all-files --verbose
Trim Trailing Whitespace.................................................Passed
- hook id: trailing-whitespace
- duration: 0.03s
Fix End of Files.........................................................Passed
- hook id: end-of-file-fixer
- duration: 0.03s
Check Yaml...............................................................Passed
- hook id: check-yaml
- duration: 0.05s
Check for added large files..............................................Passed
- hook id: check-added-large-files
- duration: 0.05s
husky....................................................................Passed
- hook id: husky-run-pre-commit
- duration: 0.07s
husky > pre-commit (node v12.18.3)
hello world
Run Code Online (Sandbox Code Playgroud)
$ pre-commit run --all-files
[INFO] Initializing environment for https://github.com/pre-commit/mirrors-eslint.
[INFO] Initializing environment for https://github.com/pre-commit/mirrors-eslint:eslint@7.9.0.
[INFO] Installing environment for https://github.com/pre-commit/mirrors-eslint.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
eslint...................................................................Passed
Run Code Online (Sandbox Code Playgroud)
免责声明:我是 pre-commit 的作者
小智 6
最新版本的预提交(2.14.0)和 husky(7.0.2)对我来说更有效的解决方案是将预提交集成到 husky 中,而不是相反。
在./husky/_/pre-commit文件中,我添加了pre-commit run.
见下文:
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npx lint-staged
npx pretty-quick --staged
pre-commit run
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1386 次 |
| 最近记录: |