-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_add_authors.py
executable file
·99 lines (78 loc) · 2.63 KB
/
auto_add_authors.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
#!/usr/bin/env python3
import os
import sys
import git
import yaml
def format_email(formatted_email):
if formatted_email[0] != '<' or formatted_email[-1] != '>':
return None
return formatted_email[1:-1]
def process_line(line):
tokens = line.split()
if len(tokens) < 3:
return None, None
email = tokens.pop()
email = format_email(email)
if email is None:
return None, None
# Throw away first token(number of commits)
tokens.pop(0)
initials = None
if len(tokens) >= 2:
# Guess initials
initials = "".join([name[0].lower() for name in tokens])
name = " ".join(tokens)
return initials, email, name
def process_gitlog():
cwd = os.getcwd()
if not os.path.exists(cwd + "/.git"):
print("No git repo found here")
sys.exit(1)
r = git.Repo.init(cwd)
log = r.git.shortlog(s=True, e=True)
lines = log.split('\n')
return [process_line(l) for l in lines]
if __name__ == '__main__':
seen_initials = set()
seen_emails = set()
distinct_users = []
# For now just alter git-authors
# TODO: create .mailmap
lines = process_gitlog()
for initials, email, name in lines:
print(initials, email, name)
# TODO: fuzzy matching names
if initials is None:
continue
if email in seen_emails:
continue
if initials in seen_initials:
continue
seen_emails.add(email)
seen_initials.add(initials)
distinct_users.append((initials, email, name))
print("\nfound these unique initials")
for u in distinct_users:
print(u)
print("\nAdding them to .git-authors")
git_author_path = os.path.expanduser("~/.git-authors")
if not os.path.exists(git_author_path):
git_authors = yaml.safe_load(open(git_author_path, "w+"))
authors = {}
email_addresses = {}
for initials, email, name in distinct_users:
authors[initials] = name
email_addresses[initials] = email
git_authors = dict(
authors=authors, email_addresses=email_addresses)
else:
git_authors = yaml.safe_load(open(git_author_path, "r"))
for initials, email, name in distinct_users:
if initials in git_authors['authors']:
continue
if initials in git_authors['email_addresses']:
continue
git_authors['authors'][initials] = name
git_authors['email_addresses'][initials] = email
yaml.safe_dump(git_authors, open(git_author_path, "w+"),
default_flow_style=False, explicit_start=False)