|
| 1 | +"""No tool starts git's automatic maintenance in a repository it opens. |
| 2 | +
|
| 3 | +`git merge` ends in `git maintenance run --auto`, which runs `git gc` when |
| 4 | +the repository's own configuration says it is due (`gc.auto`, |
| 5 | +`gc.autoPackLimit`). That gc's cruft repack runs every command the |
| 6 | +repository's `gc.recentObjectsHook` names, so a repository could run a |
| 7 | +program as the server's user through git_merge. `HARDENED_CONFIG` pins |
| 8 | +`maintenance.auto` and `gc.auto` off; these tests plant a repository that |
| 9 | +forces a gc on the next merge, with a hook that records its own run in a |
| 10 | +canary file, and assert the canary is never written. |
| 11 | +
|
| 12 | +The hook is a POSIX shell script, so the tests that run it skip on Windows; |
| 13 | +the check of the pinned values runs everywhere. |
| 14 | +""" |
| 15 | + |
| 16 | +import os |
| 17 | +import shlex |
| 18 | +import stat |
| 19 | +import subprocess |
| 20 | +import sys |
| 21 | +from pathlib import Path |
| 22 | + |
| 23 | +import git |
| 24 | +import pytest |
| 25 | +from conftest import connect, wire |
| 26 | + |
| 27 | +from mcp_server_git.server import harden_repo |
| 28 | + |
| 29 | +posix_only = pytest.mark.skipif( |
| 30 | + sys.platform == "win32", reason="the hook is a POSIX shell script" |
| 31 | +) |
| 32 | +# gc.recentObjectsHook arrived in git 2.42; an older git never runs it. |
| 33 | +needs_hook = pytest.mark.skipif( |
| 34 | + git.Git().version_info < (2, 42), reason="gc.recentObjectsHook needs git 2.42" |
| 35 | +) |
| 36 | + |
| 37 | + |
| 38 | +def root_of(repo: git.Repo) -> Path: |
| 39 | + assert repo.working_dir is not None |
| 40 | + return Path(repo.working_dir) |
| 41 | + |
| 42 | + |
| 43 | +def commit_file(repo: git.Repo, name: str, content: str) -> None: |
| 44 | + (root_of(repo) / name).write_text(content, newline="\n") |
| 45 | + repo.git.add(name) |
| 46 | + repo.git.commit("-m", f"write {name}") |
| 47 | + |
| 48 | + |
| 49 | +def plant_due_gc(repo: git.Repo, canary: Path, *, diverge: bool) -> None: |
| 50 | + """Make the next merge of `feature` run a gc that asks the hook. |
| 51 | +
|
| 52 | + Two packs make gc due under `gc.auto=1` and `gc.autoPackLimit=1`, and |
| 53 | + their sizes (6 or 3 objects against 4) are within a factor of two, so a |
| 54 | + git whose maintenance repacks geometrically finds them due too. An old |
| 55 | + unreachable loose object makes the cruft repack consult |
| 56 | + `gc.recentObjectsHook`. `diverge` adds a commit on `main`, so the merge |
| 57 | + is not a fast-forward. |
| 58 | + """ |
| 59 | + # Each commit comes before the pack it lands in: a commit runs automatic |
| 60 | + # maintenance too, which could consolidate packs already made. |
| 61 | + repo.git.branch("feature") |
| 62 | + if diverge: |
| 63 | + commit_file(repo, "main.txt", "main\n") |
| 64 | + repo.git.repack("-a", "-d") |
| 65 | + repo.git.checkout("feature") |
| 66 | + (root_of(repo) / "feature-1.txt").write_text("one\n", newline="\n") |
| 67 | + (root_of(repo) / "feature-2.txt").write_text("two\n", newline="\n") |
| 68 | + repo.git.add("feature-1.txt", "feature-2.txt") |
| 69 | + repo.git.commit("-m", "feature") |
| 70 | + repo.git.checkout("main") |
| 71 | + repo.git.repack("-d") |
| 72 | + |
| 73 | + hook = canary.parent / "hook.sh" |
| 74 | + hook.write_text(f"#!/bin/sh\necho ran >> {shlex.quote(str(canary))}\n") |
| 75 | + hook.chmod(hook.stat().st_mode | stat.S_IEXEC) |
| 76 | + with repo.config_writer() as config: |
| 77 | + config.set_value("gc", "auto", "1") |
| 78 | + config.set_value("gc", "autoPackLimit", "1") |
| 79 | + config.set_value("gc", "autoDetach", "false") |
| 80 | + config.set_value("gc", "recentObjectsHook", shlex.quote(str(hook))) |
| 81 | + |
| 82 | + sha = ( |
| 83 | + subprocess.run( |
| 84 | + ["git", "hash-object", "-w", "--stdin"], |
| 85 | + cwd=root_of(repo), |
| 86 | + input=b"stale\n", |
| 87 | + capture_output=True, |
| 88 | + check=True, |
| 89 | + ) |
| 90 | + .stdout.decode() |
| 91 | + .strip() |
| 92 | + ) |
| 93 | + loose = Path(repo.git_dir) / "objects" / sha[:2] / sha[2:] |
| 94 | + os.utime(loose, (0, 0)) |
| 95 | + |
| 96 | + |
| 97 | +@posix_only |
| 98 | +@pytest.mark.parametrize("fast_forward_only", [True, False]) |
| 99 | +@pytest.mark.parametrize("pinned", [False, True], ids=["roots", "repository"]) |
| 100 | +async def test_merge_never_runs_the_recent_objects_hook( |
| 101 | + tmp_path: Path, repo: git.Repo, fast_forward_only: bool, pinned: bool |
| 102 | +): |
| 103 | + canary = tmp_path / "canary" |
| 104 | + plant_due_gc(repo, canary, diverge=not fast_forward_only) |
| 105 | + |
| 106 | + async with connect(root_of(repo) if pinned else None) as session: |
| 107 | + result = wire( |
| 108 | + await session.call_tool( |
| 109 | + "git_merge", |
| 110 | + { |
| 111 | + "repo_path": str(root_of(repo)), |
| 112 | + "source_branch": "feature", |
| 113 | + "fast_forward_only": fast_forward_only, |
| 114 | + }, |
| 115 | + ) |
| 116 | + ) |
| 117 | + |
| 118 | + assert result["isError"] is False, result |
| 119 | + assert (root_of(repo) / "feature-1.txt").exists() |
| 120 | + assert not canary.exists() |
| 121 | + |
| 122 | + |
| 123 | +@posix_only |
| 124 | +@needs_hook |
| 125 | +@pytest.mark.parametrize("diverge", [False, True], ids=["fast-forward", "merge"]) |
| 126 | +def test_the_planted_repository_runs_the_hook_without_the_overrides( |
| 127 | + tmp_path: Path, repo: git.Repo, diverge: bool |
| 128 | +): |
| 129 | + # The control: the same merge under plain git does run the hook, so the |
| 130 | + # test above fails if the overrides go missing. |
| 131 | + canary = tmp_path / "canary" |
| 132 | + plant_due_gc(repo, canary, diverge=diverge) |
| 133 | + repo.git.merge("--ff", "--no-edit", "feature") |
| 134 | + assert canary.read_text() == "ran\n" |
| 135 | + |
| 136 | + |
| 137 | +def test_harden_repo_turns_automatic_maintenance_off(repo: git.Repo): |
| 138 | + with repo.config_writer() as config: |
| 139 | + config.set_value("gc", "auto", "1") |
| 140 | + config.set_value("maintenance", "auto", "true") |
| 141 | + harden_repo(repo) |
| 142 | + assert repo.git.config("--show-scope", "--get", "gc.auto") == "command\t0" |
| 143 | + assert ( |
| 144 | + repo.git.config("--show-scope", "--get", "maintenance.auto") == "command\tfalse" |
| 145 | + ) |
0 commit comments