-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitLanguageHistory.py
More file actions
77 lines (68 loc) · 3.01 KB
/
gitLanguageHistory.py
File metadata and controls
77 lines (68 loc) · 3.01 KB
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
import git
import subprocess
import datetime
import csv
import argparse
def main():
begin_time = datetime.datetime.now()
parser = argparse.ArgumentParser(
description='Create a history of the programming languages used in your git repo.')
parser.add_argument('--path', nargs='?', default='.',
help='path to your git repo, defaults to the current directory')
args = parser.parse_args()
repo = git.Repo(args.path)
commitCount = len(list(repo.iter_commits()))
branch = repo.active_branch
writtenLines = 0
print('analysing', commitCount, 'commits. This is going to take some time...')
# setup the result CSV File
result = csv.writer(
open('gitLanguageHistory.csv', 'w'),
delimiter=';',
quotechar='|',
quoting=csv.QUOTE_MINIMAL
)
headers = ['commitSha', 'commitMessage', 'commitDate', 'language', 'percent']
result.writerow(headers)
yearsAndWeeks = []
for index, commit in enumerate(list(repo.iter_commits()), start=1):
# filter out semcolons and linebreaks for parsing
commitMsg = commit.message.rstrip().replace(';', ',').replace('\n', ',')
if "Merge branch" not in commitMsg:
# get commit date and check if there is already a commit for this week
date = datetime.datetime.fromtimestamp(commit.committed_date)
yearWeek = str(date.year) + '-' + str(date.isocalendar()[1])
if yearWeek not in yearsAndWeeks:
yearsAndWeeks.append(yearWeek)
# checkout the commit
repo.git.checkout(commit.hexsha)
# run linguist as subprocess
linguistResult = subprocess.check_output(
'github-linguist',
cwd=args.path.encode('utf-8')
).decode("utf-8").split('\n')
# parse the results
for language in linguistResult:
row = [
commit.hexsha,
commitMsg,
commit.committed_datetime.strftime("%d.%m.%Y"),
"", ""
]
percentAndLanguage = language.split()
if len(percentAndLanguage) > 1:
row[3] = percentAndLanguage[1]
row[4] = percentAndLanguage[0].replace('%', '')
result.writerow(row)
writtenLines = writtenLines + 1
if (index > 0 and index % 10 == 0):
timePassed = datetime.datetime.now() - begin_time
print('- done with', index, 'commits (', timePassed.seconds,
')s, wrote', writtenLines, 'lines')
# reset the repository
repo.git.checkout(branch)
timePassed = datetime.datetime.now() - begin_time
print('analysed', commitCount, 'commits (100%) in ', timePassed.seconds, 's')
print('saved', writtenLines, 'lines to gitLanguageHistory.csv')
if __name__ == "__main__":
main()