|
| 1 | +"""A filter or merge driver with an empty name is overridden like any other. |
| 2 | +
|
| 3 | +Git accepts `[filter ""]` and `[merge ""]` sections, whose keys read back as |
| 4 | +`filter..clean` and `merge..driver`, and `.gitattributes` selects them with |
| 5 | +`filter=` and `merge=`. `harden_repo` must list and blank those keys too, or |
| 6 | +the repository's own configuration runs a command on git_status, git_add, |
| 7 | +git_checkout or git_merge. The payload and helpers are those of |
| 8 | +`test_repository_config.py`, which covers drivers with ordinary names. |
| 9 | +""" |
| 10 | + |
| 11 | +import os |
| 12 | +from pathlib import Path |
| 13 | +from typing import Any |
| 14 | + |
| 15 | +import git |
| 16 | +import pytest |
| 17 | +from conftest import connect, wire |
| 18 | +from test_repository_config import Payload, commit_file, posix_only, root_of |
| 19 | + |
| 20 | +from mcp_server_git.server import harden_repo |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def payload(tmp_path: Path) -> Payload: |
| 25 | + return Payload(tmp_path / "payload") |
| 26 | + |
| 27 | + |
| 28 | +def set_empty_driver(repo: git.Repo, key: str, value: str) -> None: |
| 29 | + """Set `<section>..<key>`, a key in a subsection with an empty name.""" |
| 30 | + repo.git.config(key, value) |
| 31 | + |
| 32 | + |
| 33 | +def make_stale(path: Path) -> None: |
| 34 | + """Move a tracked file's mtime so git re-hashes it, as after an unpack.""" |
| 35 | + stat = path.stat() |
| 36 | + os.utime(path, (stat.st_atime, stat.st_mtime + 10)) |
| 37 | + |
| 38 | + |
| 39 | +def test_harden_repo_blanks_an_empty_named_driver(repo: git.Repo): |
| 40 | + set_empty_driver(repo, "filter..clean", "false") |
| 41 | + set_empty_driver(repo, "filter..smudge", "false") |
| 42 | + set_empty_driver(repo, "filter..process", "false") |
| 43 | + set_empty_driver(repo, "merge..driver", "false") |
| 44 | + |
| 45 | + harden_repo(repo) |
| 46 | + |
| 47 | + for key in ("filter..clean", "filter..smudge", "filter..process", "merge..driver"): |
| 48 | + assert repo.git.config("--get", key) == "", key |
| 49 | + |
| 50 | + |
| 51 | +@posix_only |
| 52 | +@pytest.mark.parametrize("kind", ["clean", "smudge", "process"]) |
| 53 | +async def test_empty_named_filter_driver_never_runs( |
| 54 | + repo: git.Repo, payload: Payload, kind: str |
| 55 | +): |
| 56 | + commit_file(repo, ".gitattributes", "*.txt filter=\n") |
| 57 | + repo.git.checkout("-b", "other") |
| 58 | + commit_file(repo, "other.txt", "other\n") |
| 59 | + repo.git.checkout("main") |
| 60 | + set_empty_driver(repo, f"filter..{kind}", payload.command("filter")) |
| 61 | + make_stale(root_of(repo) / "test.txt") |
| 62 | + (root_of(repo) / "new.txt").write_text("new\n") |
| 63 | + path = str(root_of(repo)) |
| 64 | + |
| 65 | + for repository in (None, root_of(repo)): |
| 66 | + async with connect(repository) as session: |
| 67 | + for tool, arguments in ( |
| 68 | + ("git_status", {}), |
| 69 | + ("git_diff_unstaged", {}), |
| 70 | + ("git_add", {"files": ["new.txt"]}), |
| 71 | + ("git_checkout", {"branch_name": "other"}), |
| 72 | + ("git_checkout", {"branch_name": "main"}), |
| 73 | + ): |
| 74 | + result: dict[str, Any] = wire( |
| 75 | + await session.call_tool(tool, {"repo_path": path, **arguments}) |
| 76 | + ) |
| 77 | + assert result["isError"] is False, (tool, result) |
| 78 | + |
| 79 | + assert payload.runs() == [] |
| 80 | + assert (root_of(repo) / "test.txt").exists() |
| 81 | + |
| 82 | + |
| 83 | +@posix_only |
| 84 | +async def test_empty_named_merge_driver_never_runs(repo: git.Repo, payload: Payload): |
| 85 | + # With the driver disabled, the conflicting change is a plain conflict, |
| 86 | + # which git_merge aborts. |
| 87 | + commit_file(repo, ".gitattributes", "*.txt merge=\n") |
| 88 | + repo.git.checkout("-b", "feature") |
| 89 | + commit_file(repo, "test.txt", "feature\n") |
| 90 | + repo.git.checkout("main") |
| 91 | + commit_file(repo, "test.txt", "main\n") |
| 92 | + set_empty_driver(repo, "merge..driver", payload.command("merge")) |
| 93 | + |
| 94 | + async with connect(None) as session: |
| 95 | + result = wire( |
| 96 | + await session.call_tool( |
| 97 | + "git_merge", |
| 98 | + { |
| 99 | + "repo_path": str(root_of(repo)), |
| 100 | + "source_branch": "feature", |
| 101 | + "fast_forward_only": False, |
| 102 | + }, |
| 103 | + ) |
| 104 | + ) |
| 105 | + |
| 106 | + assert result["isError"] is True |
| 107 | + assert "stopped on conflicts in: test.txt" in result["content"][0]["text"] |
| 108 | + assert payload.runs() == [] |
0 commit comments