From 9d77513ed7690ce77a14805a388d8cff3f029b74 Mon Sep 17 00:00:00 2001 From: itguruhaseeb Date: Mon, 13 Jul 2026 22:08:00 -0500 Subject: [PATCH] fix(git): correct field misalignment in date-filtered git_log The date-filtered branch of git_log used the pretty-format '%H%n%an%n%ad%n%s%n'. The trailing %n makes git emit a blank line after every commit, so each record occupies 5 slots after split('\n') while the parser advances in groups of 4. Only the first commit lands on a correct boundary; every subsequent commit is shifted by one field (the hash is labeled Author, the date labeled Message) and a commit can be dropped or a spurious blank entry emitted. Drop the trailing %n so each record is exactly 4 lines and the group-of-4 parser stays aligned. The unfiltered branch (iter_commits) was already correct and is unchanged. Adds a regression test that filters by start_timestamp and asserts every commit's fields map correctly across multiple commits. --- src/git/src/mcp_server_git/server.py | 2 +- src/git/tests/test_server.py | 32 ++++++++++++++++++++++++++++ 2 files changed, 33 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..6c6e50b249 100644 --- a/src/git/src/mcp_server_git/server.py +++ b/src/git/src/mcp_server_git/server.py @@ -169,7 +169,7 @@ 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']) + 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..b2e448135e 100644 --- a/src/git/tests/test_server.py +++ b/src/git/tests/test_server.py @@ -234,6 +234,38 @@ def test_git_log_default(test_repository): assert len(result) >= 1 assert "initial commit" in result[0] +def test_git_log_with_timestamp_filter(test_repository): + """Date-filtered git_log must map fields correctly for every commit, not + just the first. Regression for a trailing ``%n`` in the pretty-format that + emitted a blank line per commit, shifting every commit after the first by + one field (the hash showed up under ``Author:``, the date under + ``Message:``, and a commit could be dropped).""" + for i in range(3): + file_path = Path(test_repository.working_dir) / f"ts_log_{i}.txt" + file_path.write_text(f"content {i}") + test_repository.index.add([f"ts_log_{i}.txt"]) + test_repository.index.commit(f"timestamped commit {i}") + + # initial commit + 3 new commits all fall after this floor + result = git_log(test_repository, max_count=10, start_timestamp="2000-01-01") + + assert isinstance(result, list) + assert len(result) == 4 + + messages = [] + for entry in result: + lines = entry.splitlines() + assert lines[0].startswith("Commit: ") + assert lines[1].startswith("Author: ") + assert lines[2].startswith("Date: ") + assert lines[3].startswith("Message: ") + messages.append(lines[3][len("Message: "):]) + + # Under the bug these messages land in the wrong field for commits 2..n. + assert "timestamped commit 2" in messages + assert "timestamped commit 1" in messages + assert "initial commit" in messages + def test_git_create_branch(test_repository): result = git_create_branch(test_repository, "new-feature-branch")