summaryrefslogtreecommitdiff
path: root/pygitchecker.py
blob: 200627e664b526799467fdf3ecb5fc381e0d8d88 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python3

import argparse
import os
import sys

import pygit2

from flake8.api import legacy as flake8_legacy


MODIFIED_STATUSES = (pygit2.GIT_STATUS_WT_MODIFIED,
                     pygit2.GIT_STATUS_INDEX_NEW,
                     pygit2.GIT_STATUS_INDEX_MODIFIED)
MODIFIED_STATUSES_FULL = (pygit2.GIT_STATUS_WT_MODIFIED,
                          pygit2.GIT_STATUS_WT_NEW,
                          pygit2.GIT_STATUS_INDEX_NEW,
                          pygit2.GIT_STATUS_INDEX_MODIFIED)


def is_git_repo(abspath):
    """
    True of abspath points to a git repo, False otherwise
    """
    if not os.path.isdir(abspath):
        return False
    try:
        pygit2.Repository(abspath)
        return True
    except pygit2.GitError:
        return False


def modifications_exist(abspath, args):
    """
    True if modifications exist for git repo representing abspath
    False if no modifications exist

    pygit2.GitError raised if abspath points to illegal repo
    """
    repo = pygit2.Repository(abspath)
    status = repo.status()
    if args.full_search:
        statuses = MODIFIED_STATUSES_FULL
    else:
        statuses = MODIFIED_STATUSES
    for gfile, state in status.items():
        if state in statuses:
            return True
    return False


def print_modification_status(abspath, status, args):
    """
    Prints the modification status for the give abspath
    """
    if status:
        if args.verbose:
            print(f'{abspath} -> modified')
        else:
            print(abspath)
        return status
    if args.verbose:
        print(f'{abspath} -> unchanged')
    return status


def fetch_git_repos(abspath):
    """
    Returns a list of git repo paths under abspath
    """
    repo_list = list()
    for root, dirs, files in os.walk(abspath):
        if '.git' in dirs:
            try:
                pygit2.Repository(root)
                repo_list.append(root)
            except pygit2.GitError:
                continue
    return repo_list


def fetch_git_files(args):
    """
    Fetches new and modified .py-files using pygit2
    """
    git_files_list = list()
    try:
        repo = pygit2.Repository(args.repo_path)
        status = repo.status()
    except pygit2.GitError:
        print('{repo_path} is not a valid git repository'.format(
            repo_path=args.repo_path),
              file=sys.stderr,
              flush=True)
        sys.exit(1)
    if not status:
        return git_files_list
    for gfile, state in status.items():
        if state in MODIFIED_STATUSES and gfile.lower().endswith('.py'):
            if args.verbose:
                print(gfile)
            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=os.getcwd(),
        type=str,
        help='Git repo path (default: cwd)')
    parser.add_argument(
        '-f', '--full',
        action='store_true',
        dest='full_search',
        default=False,
        help='Consider repos that contain untracked files (used with -m)')
    parser.add_argument(
        '-m', '--modifications-only',
        action='store_true',
        dest='mod_only',
        default=False,
        help='Only check for git modifications')
    parser.add_argument(
        '-r', '--recursive',
        action='store_true',
        dest='recursive',
        default=False,
        help='Look for several repos recursively')
    parser.add_argument(
        '-v', '--verbose',
        action='store_true',
        dest='verbose',
        default=False,
        help='Verbosity')
    args = parser.parse_args()
    abs_repo_path = os.path.abspath(os.path.expanduser(args.repo_path))
    if args.mod_only:
        if not args.recursive:
            if not is_git_repo(abs_repo_path):
                print(f'{abs_repo_path} is not a valid git repository',
                      file=sys.stderr,
                      flush=True)
                sys.exit(1)
            print_modification_status(abs_repo_path,
                                      modifications_exist(abs_repo_path, args),
                                      args)
            sys.exit(0)
        # resursive
        repo_list = fetch_git_repos(abs_repo_path)
        if not repo_list:
            print('No git-repos found')
            sys.exit(0)
        repo_list.sort()
        for repo_path in repo_list:
            print_modification_status(repo_path,
                                      modifications_exist(repo_path, args),
                                      args)
        sys.exit(0)
    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))