1

I have read the official flake8 docs, it has defined a 2 step process to install git pre-commit hook:

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

But it doesn't seem to provide any other type of hooks. Is there a way to install pre-push hook. Will just renaming pre-commit.exe to pre-push.exe work?

This is the pre-commit file:

import sys
from flake8.main import git
if __name__ == '__main__':
    sys.exit(
        git.hook(
            strict=git.config_for('strict'),
            lazy=git.config_for('lazy'),
        )
    )
4
  • pre-commit and pre-push hooks are completely different. pre-commit doesn't accept parameters — it works with the worktree; pre-push accepts a lot of parameters. For flake8 pre-commit is enough.
    – phd
    Commented Jun 12, 2018 at 15:40
  • Actually i don't want to do pre-commit, i only want to use pre-push hook. Commented Jun 12, 2018 at 15:46
  • pre-push is meaningless with flake8 because flake8 tests files, not commits; and of course such test is only meaningful before commit. If you insist — you have to write the hook yourself, flake8 doesn't provide it and it shouldn't.
    – phd
    Commented Jun 12, 2018 at 15:49
  • 1
    This almost an year old... Is it solved? If not, there is lots of information about githooks here githooks.com which might help you.
    – Lovato
    Commented Mar 27, 2019 at 19:47

1 Answer 1

2

Finally wrote my own pre-push hook. Here is the code if anyone wants:

#!/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()

You have to first install the pre-push hook before executing this. This hook tests for flake8 and ember tests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.