为 flake8 安装 pre-push hook python

Install pre-push hook for flake8 python

我已经阅读了 flake8 官方文档,它定义了安装 git 预提交挂钩的两步过程:

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-push 钩子。如果有人需要,这里是代码:

#!/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 测试。