From a6b09708b24cc45bc0a4f7ccf0272016cb57dc81 Mon Sep 17 00:00:00 2001 From: Tony <1250043+tao-hpu@users.noreply.github.com> Date: Wed, 8 Jul 2026 01:06:57 +0800 Subject: [PATCH] fix(git): git_log date-filtered path misparses every commit after the first The --format string ended with a trailing %n. Together with the newline git prints between log entries, each commit emitted 5 lines while the parser walks the output in strides of 4, so field alignment broke from the second commit on (hash column blank, author column holding the hash, and so on). Drop the trailing %n so each commit is exactly 4 lines, and add a regression test covering the date-filtered path, which previously had no coverage. Co-Authored-By: Claude Fable 5 --- src/git/src/mcp_server_git/server.py | 5 ++++- src/git/tests/test_server.py | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/git/src/mcp_server_git/server.py b/src/git/src/mcp_server_git/server.py index 84188d8fd7..1587f546ee 100644 --- a/src/git/src/mcp_server_git/server.py +++ b/src/git/src/mcp_server_git/server.py @@ -169,7 +169,10 @@ def git_log(repo: git.Repo, max_count: int = 10, start_timestamp: Optional[str] args.extend(['--since', start_timestamp]) if end_timestamp: args.extend(['--until', end_timestamp]) - args.extend(['--format=%H%n%an%n%ad%n%s%n']) + # No trailing %n: git already separates entries with a newline, so a + # trailing %n would emit 5 lines per commit and break the groups-of-4 + # parsing below for every commit after the first. + args.extend(['--format=%H%n%an%n%ad%n%s']) log_output = repo.git.log(*args).split('\n') diff --git a/src/git/tests/test_server.py b/src/git/tests/test_server.py index 893195d414..ac4a350f2d 100644 --- a/src/git/tests/test_server.py +++ b/src/git/tests/test_server.py @@ -227,6 +227,25 @@ def test_git_log(test_repository): assert "Date:" in result[0] assert "Message:" in result[0] +def test_git_log_with_date_filter(test_repository): + """Every commit on the date-filtered path parses into aligned fields.""" + for i in range(3): + file_path = Path(test_repository.working_dir) / f"log_filter_{i}.txt" + file_path.write_text(f"content {i}") + test_repository.index.add([f"log_filter_{i}.txt"]) + test_repository.index.commit(f"commit {i}") + + result = git_log(test_repository, start_timestamp="2000-01-01") + + # initial commit fixture + 3 above + assert len(result) == 4 + for i, entry in enumerate(reversed(result[:3])): + lines = entry.splitlines() + assert lines[0].startswith("Commit: ") and len(lines[0]) == len("Commit: ") + 40 + assert lines[1].startswith("Author: ") and lines[1] != "Author: " + assert lines[2].startswith("Date: ") + assert lines[3] == f"Message: commit {i}" + def test_git_log_default(test_repository): result = git_log(test_repository)