Write a function that parses raw git log output and builds a summary report.
The input is a list of strings lines, one per line of a git log. Each commit block starts with a line commit <hash>, followed by Author: <name> <<email>>, followed by Date: <date_string>, followed by one or more blank or message lines, and optionally followed by file change lines in the format <additions>\t<deletions>\t<filename>.
Return a dictionary with two keys:
-
commits_per_author
: a dictionary mapping each author name to their total number of commits.
-
changes_per_extension
: a dictionary mapping each file extension (including the dot, for example
.py
) to a dictionary with
additions
and
deletions
totals.
Authors are identified by the name portion before the email in angle brackets. Files with no extension are grouped under the key (none). If additions or deletions are - (git prints this for binary files), treat them as 0.
Function Signature
def parse_git_log(lines: list[str]) -> dict:
Rules
-
The line right after each
commit <hash>
line is the
Author:
line. The author name is the text between
Author:
and the first
<
, with leading and trailing spaces removed. Each
commit
line adds one commit for that author.
-
A file change line has exactly three tab-separated fields, and the first two are each a string of digits or
-
. A
-
counts as 0. All other lines are ignored: the
Date:
line, blank lines, and commit message lines (git indents these by four spaces).
-
The extension comes from the last path component of the file name, which is the text after the last
/
. It runs from the last
.
in that component to the end, dot included, so
pkg/archive.tar.gz
gives
.gz
and
config/.env
gives
.env
. A last component with no
.
, as in
Makefile
or
docs.v2/README
, goes under
(none)
.
-
Extensions are compared exactly as written, so
.PY
and
.py
are separate keys.
-
An extension appears in
changes_per_extension
only if some file change line has it. A commit with no file change lines still counts for its author. Key order inside the dictionaries does not matter.
-
For empty input, return
{"commits_per_author": {}, "changes_per_extension": {}}
.
Constraints
-
0 <= len(lines) <= 10^4
-
Each commit block follows the format described above.
-
File change lines are tab-separated:
<additions>\t<deletions>\t<filename>
.
-
Additions and deletions are non-negative integers, or
-
for binary files.
Examples
In the inputs below, \t stands for a tab character.
Input: lines = ["commit abc123", "Author: Alice <alice@example.com>", "Date: Mon Jan 1 12:00:00 2024 +0000", "", " Initial commit", "", "10\t5\tsrc/main.py", "3\t1\tREADME.md", "commit def456", "Author: Bob <bob@example.com>", "Date: Tue Jan 2 12:00:00 2024 +0000", "", " Add tests", "", "20\t0\ttests/test_main.py", "5\t2\tsrc/main.py"]
Output: {"commits_per_author": {"Alice": 1, "Bob": 1}, "changes_per_extension": {".py": {"additions": 35, "deletions": 7}, ".md": {"additions": 3, "deletions": 1}}}
Alice and Bob each made one commit. The three .py lines add 10 + 20 + 5 = 35 lines and delete 5 + 0 + 2 = 7, and README.md puts 3 additions and 1 deletion under .md.
Input: lines = ["commit aaa111", "Author: Charlie <charlie@dev.org>", "Date: Wed Jan 3 09:00:00 2024 +0000", "", " Binary file update", "", "-\t-\tassets/logo.png", "1\t0\tMakefile"]
Output: {"commits_per_author": {"Charlie": 1}, "changes_per_extension": {".png": {"additions": 0, "deletions": 0}, "(none)": {"additions": 1, "deletions": 0}}}
Charlie made one commit. The binary logo.png shows - for both counts, so .png gets 0 and 0. Makefile has no dot, so its 1 addition goes under (none).
Input: lines = ["commit 111aaa", "Author: Dana Lee <dana@corp.io>", "Date: Thu Jan 4 10:00:00 2024 +0000", "", " Update config", "", "4\t4\tconfig/.env", "2\t0\tdocs.v2/README", "commit 222bbb", "Author: Dana Lee <dana@corp.io>", "Date: Fri Jan 5 10:00:00 2024 +0000", "", " Fix typo"]
Output: {"commits_per_author": {"Dana Lee": 2}, "changes_per_extension": {".env": {"additions": 4, "deletions": 4}, "(none)": {"additions": 2, "deletions": 0}}}
Both commits belong to Dana Lee (the name keeps its inner space), and the second commit has no file change lines but still counts. The last component of config/.env is .env, so that is its extension. The dot in docs.v2/README is in a directory name, so that file goes under (none).