Created
November 25, 2023 17:35
-
-
Save samwho/be946d246da4437f53d415536092e99d to your computer and use it in GitHub Desktop.
git_repo_size.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| import subprocess | |
| DIVIDER_RE = r"^.+\s+[0-9a-f]+$" | |
| ITEM_RE = r"^.+\s+.+\s+.*$" | |
| def get_line_changes_over_time(): | |
| process = subprocess.Popen( | |
| [ | |
| "git", | |
| "log", | |
| "--author-date-order", | |
| "--reverse", | |
| "--date=format:%Y-%m", | |
| "--pretty=format:%ad %H", | |
| "--numstat", | |
| ], | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.STDOUT, | |
| text=True, | |
| ) | |
| stats_by_date = {} | |
| for line in process.stdout: | |
| line = line.strip() | |
| if not line: | |
| continue | |
| if re.match(DIVIDER_RE, line): | |
| date, *_ = line.split() | |
| if date not in stats_by_date: | |
| stats_by_date[date] = {"added": 0, "deleted": 0} | |
| stats = stats_by_date[date] | |
| elif re.match(ITEM_RE, line): | |
| a, d, *_ = line.split() | |
| if a.isdigit(): | |
| stats["added"] += int(a) | |
| if d.isdigit(): | |
| stats["deleted"] += int(d) | |
| else: | |
| raise ValueError(line) | |
| if process.wait() != 0: | |
| raise ValueError(process.stderr.read()) | |
| total = 0 | |
| for date in stats_by_date: | |
| stats = stats_by_date[date] | |
| total += stats["added"] | |
| total -= stats["deleted"] | |
| yield date, stats["added"], stats["deleted"], total | |
| print("month,added,removed,total") | |
| for month, added, deleted, total in get_line_changes_over_time(): | |
| print(f"{month},{added},{deleted},{total}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment