From f1944e37d85718795450bbf4577ba196ea3d0c84 Mon Sep 17 00:00:00 2001 From: Evan Pratten Date: Thu, 19 Oct 2023 14:29:13 -0400 Subject: [PATCH] Add a script for multirepo author stats --- configs/scripts/git-authors-multirepo | 60 +++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100755 configs/scripts/git-authors-multirepo diff --git a/configs/scripts/git-authors-multirepo b/configs/scripts/git-authors-multirepo new file mode 100755 index 0000000..648bada --- /dev/null +++ b/configs/scripts/git-authors-multirepo @@ -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())