Skip to content

Commit a25a409

Browse files
cliffhallclaude
andcommitted
fix(git): report unknown branch_type and existing branch names as errors
git_branch returned an unknown branch_type as a successful result ("Invalid branch type: ..." with isError false); it now raises, so the client gets isError true (modelcontextprotocol#4995). git_create_branch reported "Created branch 'x' from 'x'" when the name already existed at the base commit, because GitPython's create_head accepts that case silently. It now checks for an existing head first and returns an error (modelcontextprotocol#4996). The pinning tests assert the corrected behavior; their KNOWN BUG markers are removed. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 571e2b3 commit a25a409

2 files changed

Lines changed: 15 additions & 13 deletions

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,12 @@ def git_create_branch(
219219
else:
220220
base = repo.active_branch
221221

222+
# GitPython's create_head accepts a name that already points at the base
223+
# commit, so check first rather than report a branch that was not created.
224+
if branch_name in repo.heads:
225+
raise ValueError(
226+
f"Cannot create branch '{branch_name}': refs/heads/{branch_name} already exists"
227+
)
222228
repo.create_head(branch_name, base)
223229
return f"Created branch '{branch_name}' from '{base.name}'"
224230

@@ -318,7 +324,7 @@ def git_branch(
318324
case "all":
319325
b_type = "-a"
320326
case _:
321-
return f"Invalid branch type: {branch_type}"
327+
raise ValueError(f"Invalid branch type: {branch_type}")
322328

323329
# None value will be auto deleted by GitPython
324330
branch_info = repo.git.branch(b_type, *contains_sha, *not_contains_sha)

‎src/git/tests/test_protocol.py‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -603,18 +603,18 @@ async def test_git_create_branch_unknown_base_is_error(repo: git.Repo):
603603
) == text_result("No item found with id nope", is_error=True)
604604

605605

606-
# KNOWN BUG #4996: git_create_branch reports "Created branch" for an existing branch already at the base commit; the fix changes this assertion.
607-
async def test_git_create_branch_existing_name_at_same_commit_reports_success(
606+
async def test_git_create_branch_existing_name_at_same_commit_is_error(
608607
repo: git.Repo,
609608
):
610-
# Characterization: GitPython's create_head accepts an existing branch that
611-
# already points at the base commit, so this "creates" `main` from `main`
612-
# and reports success although nothing was created.
609+
# GitPython's create_head accepts an existing branch that already points
610+
# at the base commit; the server rejects it rather than report a creation.
613611
assert await call(
614612
None,
615613
"git_create_branch",
616614
{"repo_path": str(root_of(repo)), "branch_name": "main"},
617-
) == text_result("Created branch 'main' from 'main'")
615+
) == text_result(
616+
"Cannot create branch 'main': refs/heads/main already exists", is_error=True
617+
)
618618

619619

620620
async def test_git_create_branch_existing_name_at_other_commit_is_error(
@@ -885,14 +885,10 @@ async def test_git_branch_contains_and_not_contains(repo: git.Repo):
885885
assert not_contains == text_result("* main")
886886

887887

888-
# KNOWN BUG #4995: git_branch returns an unknown branch_type as a successful result, not an error; the fix changes this assertion.
889-
async def test_git_branch_unknown_type_is_not_an_error(repo: git.Repo):
890-
# Characterization: an unknown branch_type is reported in the text of a
891-
# *successful* result (isError false), so a client cannot tell it apart
892-
# from a branch listing.
888+
async def test_git_branch_unknown_type_is_error(repo: git.Repo):
893889
assert await call(
894890
None, "git_branch", {"repo_path": str(root_of(repo)), "branch_type": "bogus"}
895-
) == text_result("Invalid branch type: bogus")
891+
) == text_result("Invalid branch type: bogus", is_error=True)
896892

897893

898894
# KNOWN BUG #4999: the flag-injection guard's message reaches the client wrapped as "Ref '...' did not resolve to an object"; the fix changes this assertion.

0 commit comments

Comments
 (0)