1

Add a script for multirepo author stats

This commit is contained in:
Evan Pratten 2023-10-19 14:29:13 -04:00
parent 44c8767c77
commit f1944e37d8

View File

@ -0,0 +1,60 @@
#! /usr/bin/env python3
import subprocess
import argparse
import sys
from pathlib import Path
def main() -> int:
# Handle program arguments
ap = argparse.ArgumentParser(
description="git authors, but for multiple repos at once"
)
ap.add_argument(
"walk_start_dir",
help="Directory to start walking from",
default=Path("."),
type=Path,
)
args = ap.parse_args()
# Find every subdirectory that is a git repo
git_dirs = []
for path in args.walk_start_dir.iterdir():
if path.is_dir() and (path / ".git").is_dir():
git_dirs.append(path)
# Collect the results of `git authors` from each repo
authors = []
for git_dir in git_dirs:
output = subprocess.check_output(["git", "authors"], cwd=git_dir)
lines = output.split(b"\n")
for line in lines:
try:
line = line.decode("utf-8")
except UnicodeDecodeError:
continue
if line and len(line.split("\t")) >1:
commits, author = line.split("\t", 1)
authors.append((int(commits.strip()), author))
# Combine the results
combined = {}
for author in authors:
if author[1] not in combined:
combined[author[1]] = 0
combined[author[1]] += author[0]
# Convert back to a list
authors = [(combined[author], author) for author in combined]
authors.sort(reverse=True)
# Print
for author in authors:
print(f"{author[0]}\t{author[1]}")
return 0
if __name__ == "__main__":
sys.exit(main())