Skip to content

Commit be2e80e

Browse files
committed
Add --contributor-stats option
1 parent e597e42 commit be2e80e

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

analytics/github_analyze.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22

3-
from datetime import datetime
3+
from datetime import datetime, timedelta
44
from typing import Any, Dict, List, Iterable, Optional, Union
55
from urllib.request import urlopen, Request
66
from urllib.error import HTTPError
@@ -138,7 +138,7 @@ def __init__(self, path, remote='upstream'):
138138
self.repo_dir = path
139139
self.remote = remote
140140

141-
def _run_git_log(self, revision_range):
141+
def _run_git_log(self, revision_range) -> List[GitCommit]:
142142
log = _check_output(["git", "-C", self.repo_dir, "log", '--format=fuller', '--date=unix', revision_range]).split("\n")
143143
rc = []
144144
cur_msg = []
@@ -285,6 +285,25 @@ def analyze_reverts(commits: List[GitCommit]):
285285
print(f"{k} {orig_sm[k]}->{revert_sm[k]}")
286286

287287

288+
def print_contributor_stats(commits, delta: Optional[timedelta] = None) -> None:
289+
authors: Dict[str, int] = {}
290+
now = datetime.now()
291+
# Default delta is one non-leap year
292+
if delta is None:
293+
delta = timedelta(days=365)
294+
for commit in commits:
295+
date, author = commit.commit_date, commit.author
296+
if now - date > delta:
297+
break
298+
if author not in authors:
299+
authors[author] = 0
300+
authors[author] += 1
301+
302+
print(f"{len(authors)} contributors made {sum(authors.values())} commits in last {delta.days} days")
303+
for count, author in sorted(((commit, author) for author, commit in authors.items()), reverse=True):
304+
print(f"{author}: {count}")
305+
306+
288307
def parse_arguments():
289308
from argparse import ArgumentParser
290309
parser = ArgumentParser(description="Print GitHub repo stats")
@@ -293,6 +312,7 @@ def parse_arguments():
293312
help="Path to PyTorch git checkout",
294313
default=os.path.expanduser("~/git/pytorch/pytorch"))
295314
parser.add_argument("--analyze-reverts", action="store_true")
315+
parser.add_argument("--contributor-stats", action="store_true")
296316
return parser.parse_args()
297317

298318

@@ -313,6 +333,8 @@ def main():
313333
print(f"done in {time.time()-start_time:.1f} sec")
314334
if args.analyze_reverts:
315335
analyze_reverts(x)
336+
elif args.contributor_stats:
337+
print_contributor_stats(x)
316338
else:
317339
print_monthly_stats(x)
318340

0 commit comments

Comments
 (0)