首页 > 解决方案 > git-pylint-commit-hook 不检查所有项目文件

问题描述

我有一个 python 2.7 项目,它有一个主文件main.py和一个src文件夹,其中包含 main.py 中使用的其他 python 文件:

project folder
|
+ main.py
|
+-src(folder)
   +-file1.py
   |
   +....

现在,我想使用 git-pylint-commit-hook 来在我的所有 python 文件上运行 lint。我使用pip install git-pylint-commit-hook. 我添加了一个包含

#!/usr/bin/env bash
git-pylint-commit-hook

在 /project/root/.git/hooks/pre-commit 文件夹中并使其可运行。

问题是当我提交更改时, git-pylint-commit-hook 只检查 main.py 而不会检查 src 文件夹中的所有文件。

我该如何解决?

如果我pylint直接使用参数运行,*.py **/*.py它会检查所有文件。

这是我尝试过的,我设置了pylintrc文件来设置参数。我在项目文件夹中添加了一个 pylintrc 文件

[pre-commit-hook]
params=*.py **/*.py

但它不起作用。

如何解决问题?

标签: pythonpython-2.7pylint

解决方案


您的设置在我的环境中正常工作,不需要任何.pylintrc文件或指定任何特殊的 pylint 配置。我认为钩子只是检查main.py 因为只有main.py包含在git add. 确保您暂存了整个src文件夹并且它没有被 git 忽略,以便它将包含在git commit.

$ tree -a -L 2
.
├── .git
│   ├── HEAD
│   ├── ...
│   └── refs
├── main.py
└── src
    ├── file1.py
    └── file2.py

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   main.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    src/

在这里,git status报告只有main.py被暂存,并且src(以及它下面的所有内容)尚未跟踪,这意味着在执行时不会包含它git commit

$ git commit -m "test"
Running pylint on main.py (file 1/1)..  3.3/10.00   FAILED
************* Module main
C:  1, 0: Missing module docstring (missing-docstring)
...

------------------------------------------------------------------
Your code has been rated at 3.33/10 (previous run: 3.33/10, +0.00)

正如预期的那样,只有main.py被提交,并且只有main.pygit-pylint-commit-hook. 一旦我git add进入src文件夹,它将包含在下一次提交尝试中。

$ git add src
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   main.py
    new file:   src/file1.py
    new file:   src/file2.py

$ git commit -m "test"
Running pylint on main.py (file 1/3)..  3.3/10.00   FAILED
...


Running pylint on src/file1.py (file 2/3).. 3.3/10.00   FAILED
...


Running pylint on src/file2.py (file 3/3).. 3.3/10.00   FAILED
...

------------------------------------------------------------------
Your code has been rated at 3.33/10 (previous run: 3.33/10, +0.00)

推荐阅读