Skip to content

Commit 00abecc

Browse files
cliffhallclaude
andcommitted
fix(git): link a linked worktree's objects by their resolved path
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent a2ae0ae commit 00abecc

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

‎src/git/src/mcp_server_git/server.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,9 @@ def _own_object_store(repo: git.Repo, *, writes: bool = False) -> Iterator[None]
11201120
GitPython's persistent `git cat-file` processes are stopped before and
11211121
after, so none reads with the environment it does not belong to.
11221122
"""
1123-
objects = Path(repo.common_dir) / "objects"
1123+
# Resolved: a linked worktree's common directory reads `<git dir>/../..`,
1124+
# and a junction's target is not a path Windows collapses `..` in.
1125+
objects = Path(repo.common_dir).resolve() / "objects"
11241126
own = Path(tempfile.mkdtemp(prefix="mcp-git-objects-"))
11251127
links: list[Path] = []
11261128
placeholders: list[Path] = []

‎src/git/tests/test_alternates_race.py‎

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def alternates_planted_after_check(alternates: Path, entry: str) -> Iterator[Non
6262

6363
def check_then_plant(*args: Any, **kwargs: Any) -> None:
6464
check(*args, **kwargs)
65-
alternates.write_text(entry)
65+
alternates.write_text(entry, newline="\n")
6666

6767
with mock.patch.object(server, "_check_git_dir_contents", check_then_plant):
6868
yield
@@ -126,7 +126,7 @@ async def test_restricted_tools_ignore_alternates_written_after_the_check(
126126

127127
# The entry really was in place when the tool read objects.
128128
assert alternates.read_text() == f"{objects_of(other)}\n"
129-
assert result["isError"] is True
129+
assert result["isError"] is True, result
130130
text = result["content"][0]["text"]
131131
assert "ONLY_IN_THE_OTHER_REPOSITORY" not in text
132132
assert MESSAGE not in text
@@ -179,7 +179,8 @@ async def test_restricted_tools_still_read_the_repositorys_own_objects(
179179
async def test_restricted_writes_land_in_the_repositorys_own_store(tmp_path: Path):
180180
# A repository with no loose-object directories yet, so every object the
181181
# tools write is the first in its directory.
182-
repo = git.Repo.init(tmp_path / "fresh")
182+
# Named, since git's default branch name is configurable.
183+
repo = git.Repo.init(tmp_path / "fresh", initial_branch="main")
183184
root = root_of(repo)
184185
for config in [("user.name", "t"), ("user.email", "t@example.com")]:
185186
repo.git.config(*config)
@@ -297,6 +298,37 @@ async def test_restricted_private_store_links_only_what_the_call_can_use(
297298
}
298299

299300

301+
async def test_restricted_private_store_of_a_linked_worktree_names_real_paths(
302+
tmp_path: Path,
303+
):
304+
# GitPython gives a linked worktree's common directory as
305+
# `<git dir>/../..`. A junction's target is not a path Windows collapses
306+
# `..` in, so each link names the main repository's objects directly.
307+
main = make_repo(tmp_path / "main")
308+
worktree = tmp_path / "worktree"
309+
main.git.worktree("add", str(worktree))
310+
expected = (Path(main.git_dir).resolve() / "objects").resolve()
311+
targets: list[Path] = []
312+
original = server.git_status
313+
314+
def recording(repo: git.Repo) -> str:
315+
own = Path(repo.git.environment()["GIT_OBJECT_DIRECTORY"])
316+
targets.extend(Path(os.readlink(p)) for p in own.iterdir())
317+
return original(repo)
318+
319+
try:
320+
with mock.patch.object(server, "git_status", recording):
321+
result = await call(worktree, "git_status", {"repo_path": str(worktree)})
322+
finally:
323+
main.close()
324+
325+
assert result["isError"] is False, result
326+
assert targets
327+
for target in targets:
328+
assert ".." not in target.parts, target
329+
assert Path(str(target).removeprefix("\\\\?\\")).parent == expected
330+
331+
300332
@pytest.mark.skipif(
301333
sys.platform == "win32" or os.geteuid() == 0,
302334
reason="needs a directory that chmod can make unwritable",
@@ -363,13 +395,13 @@ async def test_unrestricted_git_show_follows_alternates(
363395
root = root_of(test_repository)
364396
alternates = objects_of(test_repository) / "info" / "alternates"
365397
alternates.parent.mkdir(parents=True, exist_ok=True)
366-
alternates.write_text(f"{objects_of(other)}\n")
398+
alternates.write_text(f"{objects_of(other)}\n", newline="\n")
367399

368400
result = await call(
369401
None,
370402
"git_show",
371403
{"repo_path": str(root), "revision": other.git.rev_parse("HEAD:only-here.txt")},
372404
)
373405

374-
assert result["isError"] is False
406+
assert result["isError"] is False, result
375407
assert result["content"][0]["text"] == MARKER

0 commit comments

Comments
 (0)