【问题标题】:flake8, only on diff and excludeflake8,仅在 diff 和 exclude 上
【发布时间】:2014-10-30 14:54:54
【问题描述】:

我正在尝试在我的 git diff 中仅更改文件的预提交挂钩中运行 flake8,同时也排除我的配置文件中的文件。

files=$(git diff --cached --name-only --diff-filter=ACM);
if flake8 --config=/path/to/config/flake8-hook.ini $files; then
    exit 1;
fi

我本质上是想做的:

flake8 --exclude=/foo/ /foo/stuff.py

然后让 flake8 跳过我传入的文件,因为它在排除变量中。

我还希望它排除不是 .py 文件的文件。例如:

flake8 example.js

现在我正在测试,这些都不起作用。有人有什么想法吗?

【问题讨论】:

标签: git pep8 flake8


【解决方案1】:

如果您要在未提交和暂存的 python 文件上运行 flake8 并进行更改,则此单行程序可以解决问题:

flake8 $(git status -s | grep -E '\.py$' | cut -c 4-)

git status 列出改变的文件,grep for python,去掉开头的M/S位用cut。

要使其成为预提交钩子,您需要添加shell hashbang:

#!/bin/sh flake8 $(git status -s | grep -E '\.py$' | cut -c 4-)

另存为 .git/hooks/pre-commit, chmod +x.

【讨论】:

    【解决方案2】:

    您的问题分为两部分:

    1。仅在我的 git > diff

    中更改的文件上在预提交挂钩中运行 flake8

    为了仅在差异文件(已暂存)上运行 flake8,我将 .git/hooks/pre-commit 脚本修改为如下:

    #!/bin/sh
    export PATH=/usr/local/bin:$PATH  
    export files=$(git diff --staged --name-only HEAD)  
    echo $files  
    if [ $files != "" ]  
    then  
        flake8 $files  
    fi
    

    我使用的是export PATH=/usr/local/bin:$PATH,因为我通常从 sourceTree 提交,它不会选择 flake8 所在的路径

    --staged 选项只允许提取暂存区中的文件

    第二部分:

    2。排除我的配置文件中的文件

    您可以在存储库根目录中创建一个.flake8 文件来处理这个问题。我的.flake8 文件如下所示:

    [flake8]  
    ignore = E501  
    exclude =  
            .git,  
            docs/*,  
            tests/*,  
            build/*  
    max-complexity = 16
    

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      如果你想在提交前使用flake8检查文件,只需使用

      $ flake8 --install-hook git
      

      参考:https://flake8.pycqa.org/en/latest/user/using-hooks.html

      【讨论】:

        【解决方案4】:

        有一个项目 lint-diffs 仅对 diff 文件运行 linter,并将结果匹配到 diff 输出。

        它适用于 flake8 和 pylint。此外,它只能在 diffs 中的行上报告 linter 错误...或在更改的文件中的所有行上。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-24
          • 1970-01-01
          • 2011-05-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多