Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/git/src/mcp_server_git/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,17 @@ def git_create_branch(repo: git.Repo, branch_name: str, base_branch: str | None
raise BadName(f"Invalid base branch: '{base_branch}' - cannot start with '-'")
if base_branch:
base = repo.references[base_branch]
base_name = base.name
elif repo.head.is_detached:
# No active branch to start from; branch from the current commit.
base = repo.head.commit.hexsha
base_name = base
else:
base = repo.active_branch
base_name = base.name

repo.create_head(branch_name, base)
return f"Created branch '{branch_name}' from '{base.name}'"
return f"Created branch '{branch_name}' from '{base_name}'"

def git_checkout(repo: git.Repo, branch_name: str) -> str:
# Defense in depth: reject branch names starting with '-' to prevent flag injection,
Expand Down
11 changes: 11 additions & 0 deletions src/git/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,17 @@ def test_git_create_branch_from_base(test_repository):

assert "Created branch 'derived-branch' from 'base-branch'" in result

def test_git_create_branch_detached_head(test_repository):
# `git switch -c <name>` from a detached HEAD branches from the current commit.
detached_sha = test_repository.head.commit.hexsha
test_repository.git.checkout(detached_sha)
assert test_repository.head.is_detached

result = git_create_branch(test_repository, "rescued-branch")

assert "Created branch 'rescued-branch'" in result
assert test_repository.heads["rescued-branch"].commit.hexsha == detached_sha

def test_git_show(test_repository):
file_path = Path(test_repository.working_dir) / "show_test.txt"
file_path.write_text("show content")
Expand Down
Loading