Skip to content

Commit 2b2e2a2

Browse files
cliffhallclaude
andcommitted
fix(git): report a detached HEAD in git_create_branch instead of catching TypeError
Catching TypeError for every git tool would show a real bug's text to the model as if it were an anticipated failure. The only TypeError the git tools anticipated was GitPython's `active_branch` on a detached HEAD, so git_create_branch now checks for that and asks for base_branch. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent 916cdc9 commit 2b2e2a2

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,12 @@ def git_create_branch(
258258
)
259259
if base_branch:
260260
base = repo.references[base_branch]
261+
elif repo.head.is_detached:
262+
# GitPython's `active_branch` raises TypeError on a detached HEAD.
263+
raise ValueError(
264+
"HEAD is detached, so there is no current branch to branch from; "
265+
"pass base_branch"
266+
)
261267
else:
262268
base = repo.active_branch
263269

@@ -412,15 +418,15 @@ def git_branch(
412418
# command, a missing or invalid repository), gitdb's lookup errors (`ODBError`:
413419
# `BadName` for an unknown revision, `BadObject`), and the builtins GitPython
414420
# raises for a failed lookup: `KeyError` for a path not in a tree, `IndexError`
415-
# for a missing ref in `repo.references[...]`, `TypeError` for
416-
# `active_branch` on a detached HEAD, `OSError` for an unreadable path.
421+
# for a missing ref in `repo.references[...]`, `OSError` for an unreadable
422+
# path. A detached HEAD is checked before `active_branch` (whose TypeError
423+
# would otherwise be a crash), so TypeError stays a crash, as a bug should.
417424
# Anything else escapes as a crash, which the SDK reports only as
418425
# "Error executing tool <name>", logging the traceback.
419426
ANTICIPATED_ERRORS = (
420427
ValueError,
421428
KeyError,
422429
IndexError,
423-
TypeError,
424430
OSError,
425431
git.GitError,
426432
ODBError,

‎src/git/tests/test_protocol.py‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -674,13 +674,12 @@ async def test_git_create_branch_existing_name_at_other_commit_is_error(
674674
assert "already exist" in result["content"][0]["text"]
675675

676676

677-
async def test_git_create_branch_on_detached_head_reports_gitpython_message(
677+
async def test_git_create_branch_on_detached_head_asks_for_a_base(
678678
repo: git.Repo,
679679
):
680-
# With no base_branch the base is the active branch, and GitPython raises
681-
# TypeError for `active_branch` on a detached HEAD. #5174: that is an
682-
# anticipated failure, so the model still sees GitPython's message rather
683-
# than MCPServer's generic "Error executing tool git_create_branch".
680+
# With no base_branch the base is the active branch, which a detached HEAD
681+
# does not have (GitPython would raise TypeError, a crash under MCPServer).
682+
# #5174: the tool says so instead, as an anticipated failure.
684683
repo.git.checkout("--detach")
685684
result = await call(
686685
None,
@@ -689,9 +688,8 @@ async def test_git_create_branch_on_detached_head_reports_gitpython_message(
689688
)
690689
assert result == tool_error(
691690
"git_create_branch",
692-
f"HEAD is a detached symbolic reference as it points to "
693-
f"'{repo.head.commit.hexsha}'. Use .commit or .object to access the "
694-
"target directly.",
691+
"HEAD is detached, so there is no current branch to branch from; "
692+
"pass base_branch",
695693
)
696694

697695

0 commit comments

Comments
 (0)