在我的python项目中,我有pre-commit-config.YAML,我想在其中创建我的自定义文件。
如果python lint错误大于某些数字,则此文件的目的是使git提交失败。以下命令将用于对行进行计数
pylint api/ | wc -l
有没有人能给我一些建议。我是MAC和Python生态系统的新手?
编辑sh文件如下所示。
#!/bin/sh
a=$(pylint source/ | wc -l)
b=20
errorsCount="$(echo "${a}" | tr -d '[:space:]')"
if [ $errorsCount -gt $b ]
then
exit 1
fi
我试过了
repos:
- repo: local
hooks:
- id: custom-script-file
name: custom-script-file
entry: hooks/pre-commit.sh
language: script
types: [python]
pass_filenames: false
但它不会起作用。
发布于 2019-12-27 18:42:46
下面是使用内联bash命令作为预提交钩子条目方法
- repo: local
hooks:
- id: pylint-error-count
name: pylint-error-count
entry: bash -c 'lines=$(pylint api/ | wc -l) && (( lines > 10)) && exit 1'
language: system
types: [python]
pass_filenames: false
您还可以编写脚本并以这种方式调用它:
entry: path/relavite/to/repo/root/pylint_validator.sh
language: script
注意:wc -l
不是错误的准确计数。
编辑:添加更多选项
- repo: local
hooks:
- id: simple-pylint
name: simple-pylint
entry: pylint
args: ["api/"]
language: system
types: [python]
pass_filenames: false
- id: inline-pylint-with-bash
name: inline-pylint-with-bash
entry: bash -c 'lines=$(pylint api/ | wc -l) && (( lines > 10)) && exit 1'
language: system
types: [python]
pass_filenames: false
- id: custom-script-file
name: custom-script-file
entry: relative/path/to/repo/root/check_pylint.sh
language: script
types: [python]
pass_filenames: false
https://stackoverflow.com/questions/59499061
复制相似问题