【问题标题】:Install pre-push hook for flake8 python为 flake8 python 安装 pre-push 钩子
【发布时间】:2018-06-12 13:44:04
【问题描述】:

我已经阅读了 flake8 官方文档,它定义了安装 git pre-commit hook 的两步过程:

flake8 --install-hook git
git config --bool flake8.strict true

但它似乎没有提供任何其他类型的钩子。有没有办法安装预推钩。将pre-commit.exe 重命名为pre-push.exe 是否可行?

这是pre-commit 文件:

import sys
from flake8.main import git
if __name__ == '__main__':
    sys.exit(
        git.hook(
            strict=git.config_for('strict'),
            lazy=git.config_for('lazy'),
        )
    )

【问题讨论】:

  • pre-commitpre-push 钩子完全不同。 pre-commit 不接受参数——它适用于工作树; pre-push 接受很多参数。对于flake8 pre-commit 就足够了。
  • 其实我不想做pre-commit,我只想用pre-push钩子。
  • pre-pushflake8 毫无意义,因为 flake8 测试文件,而不是提交;当然,这样的测试只有在提交之前才有意义。如果你坚持——你必须自己写钩子,flake8 不提供它也不应该提供。
  • 这差不多一岁了...解决了吗?如果没有,这里有很多关于 githooks 的信息 githooks.com 这可能会对您有所帮助。

标签: git githooks flake8


【解决方案1】:

终于写出了自己的 pre-push hook。如果有人想要,这里是代码:

#!/usr/local/bin/python3

import os
import subprocess
import sys

# Pre-push hook that executes the Python/JS linters on all files that
# deviate from develop.
# To bypass the validation upon `git push` use the following command:
# `git push REMOTE BRANCH --no-verify`
# Change the first line of this file if your python3 installation
# is elsewhere.


def main():

    # flake8 linting tests for backend.
    # Make sure flake8 is installed in the virtual environment or give
    # the path where flake8 is installed.
    print('Running flake8 tests...')
    result = subprocess.run([os.getcwd() + '/venv/bin/flake8', 'api/'], stdout=subprocess.PIPE)
    check = result.stdout.decode('utf-8')
    if check:
        print('Please correct the linting errors: ')
        print(result.stdout.decode('utf-8'))
        sys.exit(1)
    print('Flake8 tests passed.')

    # ember tests for frontend.
    print('Running ember tests...')
    result_front = subprocess.run(['ember', 'test'], cwd='frontend', stdout=subprocess.PIPE)
    if result_front.returncode:
        print(result_front.stdout.decode('utf-8'))
        sys.exit(1)
    print('Ember tests passed.')


if __name__ == '__main__':
    main()

在执行此之前,您必须先安装 pre-push 挂钩。这个钩子测试 flake8 和 ember 测试。

【讨论】:

    猜你喜欢
    • 2014-02-15
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    • 2017-07-16
    • 2016-07-15
    • 2016-04-16
    • 2020-07-20
    相关资源
    最近更新 更多