Skip to main content

recent_git_commits.py (Source)

"""
    Find commits in git repositories under this directory since the given date.
    Used to compile the list of changes for the Numbas development log.
    Example:
        python recent_changes.py 2023-10-01
"""
import argparse
from pathlib import Path
import re
import subprocess
ignore_dirs = ['node_modules', 'media']
def find_git_directories(p):
    if (p / '.git').exists():
        yield p
    for sp in p.iterdir():
        if (not sp.is_dir()) or sp.name.startswith('.') or sp.name in ignore_dirs:
            continue
        yield from find_git_directories(sp)
parser = argparse.ArgumentParser()
parser.add_argument('date', help="Date to list commits since")
args = parser.parse_args()
git_directories = sorted(find_git_directories(Path()))
for d in git_directories:
    def run(cmd, **kwargs):
        return subprocess.run(cmd, cwd=d, encoding='utf-8', capture_output=True, **kwargs)
    origin = run(['git','remote','get-url','origin']).stdout.strip()
    origin_url = re.sub(r'^\w+@([^:]+):(.*?)(.git)?$', r'https://\1/\2', origin)
    commits = run(['git','log',f'--since="{args.date}"', '--oneline', f'--pretty=format:%as\n  %cn\n\t%s\n\t{origin_url}/commit/%h\n','--reverse'], env={'GIT_PAGER': ''}).stdout
    if commits:
        contributors = set(run(['git','log','--pretty=format:%cn',f'--since="{args.date}"']).stdout.split('\n'))
        print(f"""
{d}
{origin_url}
Contributors: {', '.join(sorted(contributors))}
{'-'*80}
""")
        print(commits)