#!/usr/bin/env python3 # gitlog - pretty log with a graph and per-commit file stats, filtered by author/date # Usage: gitlog [path] [-n N] [--author NAME] [--since DATE] [--until DATE] [--stat] [-- path] import argparse, os, subprocess, sys def selftest(): import tempfile from pathlib import Path with tempfile.TemporaryDirectory() as d: env = dict(os.environ, GIT_AUTHOR_NAME="Alice", GIT_AUTHOR_EMAIL="a@t", GIT_COMMITTER_NAME="Alice", GIT_COMMITTER_EMAIL="a@t") def g(*a): subprocess.run(["git"] + list(a), cwd=d, env=env, check=True, capture_output=True) g("init", "-q", "-b", "main") Path(d, "a.txt").write_text("one\n"); g("add", "."); g("commit", "-qm", "first by alice") Path(d, "b.txt").write_text("two\n"); g("add", "."); g("commit", "-qm", "second by alice") me = os.path.abspath(__file__) out = subprocess.run([sys.executable, me, d], capture_output=True, text=True) assert out.returncode == 0 and "first by alice" in out.stdout, out.stderr out = subprocess.run([sys.executable, me, "--author", "Nobody", d], capture_output=True, text=True) assert out.returncode == 0 and "first by alice" not in out.stdout out = subprocess.run([sys.executable, me, "--stat", "-n", "1", d], capture_output=True, text=True) assert "b.txt" in out.stdout print("gitlog selftest: PASS") return 0 def main(): ap = argparse.ArgumentParser(prog="gitlog", description="Pretty git log with a graph and per-commit file stats, filtered by author/date.") ap.add_argument("path", nargs="?", default=".", help="path to the repo (default: .)") ap.add_argument("-n", "--number", type=int, default=15, help="commits to show (default 15)") ap.add_argument("--author", help="filter by author name/email (substring)") ap.add_argument("--since", help="only commits after this date (e.g. 2026-01-01, '2 weeks ago')") ap.add_argument("--until", help="only commits before this date") ap.add_argument("--stat", action="store_true", help="show per-commit file stats (default: on)") ap.add_argument("--no-stat", dest="stat", action="store_false", help="hide file stats") ap.add_argument("--paths", nargs="*", default=[], help="only commits touching these paths") ap.add_argument("--selftest", action="store_true", help="run built-in self-test and exit") ap.set_defaults(stat=True) a = ap.parse_args() if a.selftest: return selftest() r = subprocess.run(["git", "rev-parse", "--show-toplevel"], cwd=a.path, capture_output=True, text=True) if r.returncode != 0: print("gitlog: not a git repository: %s" % a.path, file=sys.stderr); return 1 cmd = ["git", "log", "--graph", "--color=never", "--pretty=format:%h | %ad | %an | %s%d", "--date=short", "-n", str(a.number)] if a.author: cmd += ["--author=" + a.author] if a.since: cmd += ["--since=" + a.since] if a.until: cmd += ["--until=" + a.until] if a.stat: cmd += ["--stat"] if a.paths: cmd += ["--"] + a.paths out = subprocess.run(cmd, cwd=r.stdout.strip(), capture_output=True, text=True) print(out.stdout, end="") if out.stderr: print(out.stderr, file=sys.stderr) return out.returncode if __name__ == "__main__": sys.exit(main())