Skip to content

Commit ffb7a76

Browse files
cliffhallclaude
andcommitted
fix(git): compare head names in git_create_branch's existence check
`branch_name in repo.heads` uses GitPython's IterableList.__contains__, which falls back to getattr, so a new branch named like a list attribute ("append", "count", "index") read as existing and was rejected. Compare the heads' names instead, and cover "append" in the protocol tests. Found by Copilot review on #5026. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent a25a409 commit ffb7a76

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ def git_create_branch(
221221

222222
# GitPython's create_head accepts a name that already points at the base
223223
# commit, so check first rather than report a branch that was not created.
224-
if branch_name in repo.heads:
224+
# Compare names: `in repo.heads` also matches IterableList attributes
225+
# such as "append".
226+
if any(head.name == branch_name for head in repo.heads):
225227
raise ValueError(
226228
f"Cannot create branch '{branch_name}': refs/heads/{branch_name} already exists"
227229
)

‎src/git/tests/test_protocol.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,17 @@ async def test_git_create_branch_existing_name_at_same_commit_is_error(
617617
)
618618

619619

620+
async def test_git_create_branch_name_matching_list_attribute(repo: git.Repo):
621+
# The existence check compares head names, so a new branch named like an
622+
# attribute of GitPython's IterableList ("append") is still created.
623+
assert await call(
624+
None,
625+
"git_create_branch",
626+
{"repo_path": str(root_of(repo)), "branch_name": "append"},
627+
) == text_result("Created branch 'append' from 'main'")
628+
assert repo.commit("refs/heads/append") == repo.heads["main"].commit
629+
630+
620631
async def test_git_create_branch_existing_name_at_other_commit_is_error(
621632
repo: git.Repo,
622633
):

0 commit comments

Comments
 (0)