-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-activity-old.py
executable file
·202 lines (164 loc) · 5.2 KB
/
git-activity-old.py
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python3
"""
Show a daily log of activity accross all of your local git repos.
"""
import argparse
import colorama
from collections import namedtuple
import contextlib
import datetime
import logging
import os
import pathlib
from pprint import pprint as pp
import re
import subprocess
import sys
logger = logging.getLogger(__name__)
@contextlib.contextmanager
def chdir(folder):
"""
Context manager to temporarily change the current working directory.
"""
curdir = os.getcwd()
os.chdir(folder)
yield
os.chdir(curdir)
def git_repo_folders(parent):
"""
Yield folders containing git repos.
This is any regular folder containing in turn the folder '.git'.
"""
logger.debug("Looking for Git repos under: %s", parent)
parent = str(parent)
examined = 0
found = 0
for root, dirs, files in os.walk(parent, topdown=True):
examined += 1
dirs.sort()
if '.git' in dirs:
found += 1
yield root
# Don't look any further inside repo
dirs.clear()
logger.info("{:,} Git repos found. {:,} folders searched.".format(found, examined))
class MultiGit:
def __init__(self, parent, args):
"""
Initialiser.
"""
self.args = args if args else ['diff', '--numstat', '--color']
self.encoding = 'utf-8'
self.parent = self.clean_path(parent)
def clean_path(self, path):
"""
Return cannonical version of given path.
"""
path = pathlib.Path(path)
path = path.expanduser()
path = path.resolve()
return path
def run(self):
"""
Main script.
"""
for repo in git_repo_folders(self.parent):
with chdir(repo):
self.run_git(repo, self.args)
def run_git(self, repo, args):
"""
Run git 'inside' repo and print output, if not empty.
"""
args = ['git'] + args
process = subprocess.run(args, stdout=subprocess.PIPE)
if process.stdout:
heading = "{:<80}".format(repo)
print(colorama.Style.BRIGHT + colorama.Back.BLUE + heading)
stdout = process.stdout.decode(self.encoding).strip()
print(stdout)
print()
def setup_logging(level):
logging.basicConfig(
datefmt="%d-%b-%Y %H:%M:%S",
format='{message}',
level=level,
style='{'
)
Numstat = namedtuple('Numstat', 'name email timestamp files_changed insertions deletions')
class ParseError(RuntimeError):
pass
class ParseNumStat:
def parse(self, string):
for commit in self.commits(string):
record = self.make_record(commit)
yield record
def commits(self, string):
"""
Break full log string into individual commit strings.
"""
parts = re.split(r'commit [0-9a-f]{40}', string)
for commit in parts:
commit = commit.strip()
if commit:
yield commit
def get_name(self, lines):
"""
Pull name and email address out of
"""
prefix = 'Author: '
for line in lines:
if line.startswith(prefix):
line = line.strip(prefix)
pattern = r'([^<]*) <([^>]*)>'
match = re.match(pattern, line)
if match is None:
self.prefix_error(pattern, line)
return match.groups()
self.prefix_error(prefix, lines)
def get_timestamp(self, lines):
prefix = 'Date: '
for line in lines:
if line.startswith(prefix):
line = line.strip(prefix)
timestamp = datetime.datetime.strptime(line, "%Y-%m-%d %H:%M:%S %z")
return timestamp
self.prefix_error(prefix, lines)
def get_changes(self, lines):
pp(lines)
return (12, 234, 45)
def make_record(self, commit):
lines = commit.splitlines()
name, email = self.get_name(lines)
timestamp = self.get_timestamp(lines)
files_changed, insertions, deletions = self.get_changes(lines)
return Numstat(name, email, timestamp, files_changed, insertions, deletions)
def prefix_error(self, prefix, lines):
lines = '\n'.join(lines)
raise ParseError(f"Prefix {prefix!r} not found in: {lines!s}")
def regex_error(self, regex, line):
raise ParseError(f"No match for '{regex!s}' found in: {line!r}")
if __name__ == '__main__':
with open('output.txt') as f:
OUTPUT = f.read()
parser = ParseNumStat()
for record in parser.parse(OUTPUT):
pp(record)
break
sys.exit(0)
colorama.init(autoreset=True)
setup_logging(logging.INFO)
if len(sys.argv) > 1:
parent = sys.argv[1]
else:
logger.debug('No parent folder given, defaulting to home folder.')
parent = '~'
delta = datetime.timedelta(days=3)
date = datetime.date.today() - delta
date = date.isoformat()
args = [
"log", "--all", "--numstat", "--color=always",
f"--after='{date} 00:00:00'",
f"--before='{date} 23:59:59'",
]
multi_git = MultiGit(parent, args)
sys.exit(multi_git.run())