Skip to content

Commit 9dc29d1

Browse files
authored
Merge pull request #5023 from modelcontextprotocol/v2/fix/4804-git-checkout-detached-head
fix(git): report a detached HEAD from git_checkout (#4804)
2 parents 0e729d3 + 6bc2f92 commit 9dc29d1

4 files changed

Lines changed: 58 additions & 8 deletions

File tree

‎src/git/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Please note that mcp-server-git is currently in early development. The functiona
8383
- Inputs:
8484
- `repo_path` (string): Path to Git repository
8585
- `branch_name` (string): Name of branch to checkout
86-
- Returns: Confirmation of branch switch
86+
- Returns: Confirmation of branch switch, or `HEAD is now detached at <short sha>` when `branch_name` is not a branch (a sha, tag or other revision)
8787
11. `git_show`
8888
- Shows the contents of a commit
8989
- Inputs:

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,12 @@ def git_checkout(repo: git.Repo, branch_name: str) -> str:
293293
branch_name
294294
) # Validates branch_name is a real git ref, throws BadName if not
295295
repo.git.checkout(branch_name)
296-
return f"Switched to branch '{branch_name}'"
296+
# rev_parse accepts any revision, so branch_name may have been a sha, tag or
297+
# remote-tracking ref rather than a branch. Report what actually happened instead of
298+
# claiming a branch switch; a detached HEAD is easy to commit onto by mistake.
299+
if repo.head.is_detached:
300+
return f"HEAD is now detached at {repo.head.commit.hexsha[:7]}"
301+
return f"Switched to branch '{repo.active_branch.name}'"
297302

298303

299304
def git_show(repo: git.Repo, revision: str) -> str:

‎src/git/tests/test_protocol.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -674,26 +674,26 @@ async def test_git_checkout_branch(repo: git.Repo):
674674
assert repo.active_branch.name == "feature"
675675

676676

677-
# KNOWN BUG #4804: pins current (wrong) behavior; the fix changes this assertion.
678677
@pytest.mark.parametrize("revision", ["sha", "tag", "HEAD~1", "refs/heads/feature"])
679-
async def test_git_checkout_non_branch_detaches_head_but_claims_switch(
678+
async def test_git_checkout_non_branch_reports_detached_head(
680679
repo: git.Repo, revision: str
681680
):
682-
# Pins #4804: any revision rev_parse accepts is checked out, detaching
683-
# HEAD, and the reply still says "Switched to branch". Fixing #4804 changes
684-
# this test.
681+
# #4804: any revision rev_parse accepts is checked out, detaching HEAD, and
682+
# the reply says so with the short sha instead of claiming a branch switch.
685683
add_commits(repo, 1)
686684
repo.git.branch("feature")
687685
repo.create_tag("v1", ref="HEAD~1")
688686
name = {
689687
"sha": repo.head.commit.hexsha[:7],
690688
"tag": "v1",
691689
}.get(revision, revision)
690+
target = repo.commit(name).hexsha
692691
result = await call(
693692
None, "git_checkout", {"repo_path": str(root_of(repo)), "branch_name": name}
694693
)
695-
assert result == text_result(f"Switched to branch '{name}'")
696694
assert repo.head.is_detached
695+
assert repo.head.commit.hexsha == target
696+
assert result == text_result(f"HEAD is now detached at {target[:7]}")
697697

698698

699699
async def test_git_checkout_unknown_branch_is_error(repo: git.Repo):

‎src/git/tests/test_server.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,51 @@ def test_git_checkout_nonexistent_branch(test_repository):
3434
git_checkout(test_repository, "nonexistent-branch")
3535

3636

37+
def test_git_checkout_sha_reports_detached_head(test_repository):
38+
"""rev_parse accepts a sha, so the reply must not claim a branch switch."""
39+
sha = test_repository.head.commit.hexsha
40+
result = git_checkout(test_repository, sha)
41+
42+
assert test_repository.head.is_detached
43+
assert "detached" in result
44+
assert "Switched to branch" not in result
45+
46+
47+
@pytest.mark.parametrize("revision", ["HEAD~1", "refs/heads/feature", "origin/main"])
48+
def test_git_checkout_non_branch_revisions_report_detached_head(
49+
test_repository, tmp_path, revision
50+
):
51+
"""The revisions an agent is most likely to send: a relative ref, a full ref name, and a
52+
remote-tracking ref. None of them is a branch, so none may be reported as a branch switch."""
53+
test_repository.git.branch("feature")
54+
test_repository.index.commit("second commit")
55+
git.Repo.init(tmp_path / "remote.git", bare=True)
56+
test_repository.create_remote("origin", str(tmp_path / "remote.git"))
57+
test_repository.git.push("origin", "HEAD:refs/heads/main")
58+
59+
result = git_checkout(test_repository, revision)
60+
61+
assert test_repository.head.is_detached
62+
assert result.startswith("HEAD is now detached at ")
63+
64+
65+
def test_git_checkout_tag_reports_detached_head(test_repository):
66+
test_repository.create_tag("v1")
67+
result = git_checkout(test_repository, "v1")
68+
69+
assert test_repository.head.is_detached
70+
assert "detached" in result
71+
assert "Switched to branch" not in result
72+
73+
74+
def test_git_checkout_branch_name_still_reports_branch(test_repository):
75+
test_repository.git.branch("attached-checkout")
76+
result = git_checkout(test_repository, "attached-checkout")
77+
78+
assert not test_repository.head.is_detached
79+
assert result == "Switched to branch 'attached-checkout'"
80+
81+
3782
def test_git_branch_local(test_repository):
3883
test_repository.git.branch("new-branch-local")
3984
result = git_branch(test_repository, "local")

0 commit comments

Comments
 (0)