summaryrefslogtreecommitdiff
path: root/pygitchecker.py
blob: 77f85ecc45d719f5a6c6e6adf3d837b4d28d1e23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3

import argparse
import sys

import pygit2

from flake8.api import legacy as flake8_legacy


def fetch_git_files(args):
    """
    Fetches new and modified .py-files using pygit2
    """
    repo = pygit2.Repository(args.repo_path)
    status = repo.status()
    git_files_list = list()
    if not status:
        return git_files_list
    for gfile, state in status.items():
        if (
                state in (pygit2.GIT_STATUS_WT_MODIFIED,
                          pygit2.GIT_STATUS_WT_NEW,
                          pygit2.GIT_STATUS_INDEX_NEW,
                          pygit2.GIT_STATUS_INDEX_MODIFIED) and
                gfile.lower().endswith('.py')
        ):
            git_files_list.append(gfile)
    return git_files_list


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description='The following options are available')
    parser.add_argument(
        'repo_path',
        metavar='REPO',
        default='.',
        type=str,
        help='Git repo path (default: cwd)')
    parser.add_argument(
        '--disable-pep8-checks',
        action='store_true',
        dest='disable_pep8',
        default=False,
        help='Disable pep8 checks')
    args = parser.parse_args()
    git_files = fetch_git_files(args)
    if not git_files:
        print('No modifications')
        sys.exit(0)
    report = flake8_legacy.get_style_guide().check_files(git_files)
    if report.total_errors:
        print('Total errors: {errors}'.format(errors=report.total_errors))