Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 72 lines (57 sloc) 2.07 kb
f5f431e @SmileyChris Add an AUTHORS file
authored
1 #!/usr/bin/env python
2 """
3 Get a git project's authors (ordered by most contributions).
4 """
5
6 import re
7 import subprocess
8 from operator import itemgetter
9
fbae877 @SmileyChris Use Django's bundled six util
authored
10 from django.utils import six
11
dbc3950 @SmileyChris Python 3 compatibility
authored
12 re_line = re.compile(six.b(r'(\d+)\s+(\d+)\s+[^<]+$'))
13 re_author = re.compile(six.b(r'.+<(.+)>$'))
f5f431e @SmileyChris Add an AUTHORS file
authored
14
15
16 def get_authors(exclude_primary_author=True):
dbc3950 @SmileyChris Python 3 compatibility
authored
17 git_log = subprocess.Popen(
18 ["git", "log", "--format=%aN <%aE>", "--numstat"],
19 stdout=subprocess.PIPE)
f5f431e @SmileyChris Add an AUTHORS file
authored
20
21 output = git_log.communicate()[0]
22
23 authors = {}
24 author = None
25 for line in output.splitlines():
26 match = re_line.match(line)
27 if not match:
28 if line:
29 author = line
30 continue
dbc3950 @SmileyChris Python 3 compatibility
authored
31 authors[author] = authors.get(author, 0) + max([
32 int(num) for num in match.groups()])
f5f431e @SmileyChris Add an AUTHORS file
authored
33
34 # Combine duplicate authors (by email).
35 emails = {}
dbc3950 @SmileyChris Python 3 compatibility
authored
36 for author, changes in list(authors.items()):
f5f431e @SmileyChris Add an AUTHORS file
authored
37 match = re_author.match(author)
38 if not match:
39 continue
40 author_emails = match.group(1)
dbc3950 @SmileyChris Python 3 compatibility
authored
41 for email in author_emails.split(six.b(',')):
42 if six.b('@') not in email:
f5f431e @SmileyChris Add an AUTHORS file
authored
43 continue
44 if email in emails:
45 remove_author = emails[email]
46 if remove_author not in authors:
47 continue
48 if changes < authors[remove_author]:
49 author, remove_author = remove_author, author
50 authors[author] = authors[author] + authors[remove_author]
51 del authors[remove_author]
52 else:
53 emails[email] = author
54
55 # Sort authors list.
dbc3950 @SmileyChris Python 3 compatibility
authored
56 list_authors = sorted(authors.items(), key=itemgetter(1), reverse=True)
f5f431e @SmileyChris Add an AUTHORS file
authored
57
58 total = float(sum(authors.values()))
59
60 if exclude_primary_author:
61 top_author = list_authors.pop(0)
62 total -= top_author[1]
63
dbc3950 @SmileyChris Python 3 compatibility
authored
64 return [
65 (author.decode(), changes, changes / total * 100)
f5f431e @SmileyChris Add an AUTHORS file
authored
66 for author, changes in list_authors]
67
68
69 if __name__ == '__main__':
70 for author, changes, percent in get_authors():
dbc3950 @SmileyChris Python 3 compatibility
authored
71 print(author)
Something went wrong with that request. Please try again.