Allow to check out branches

The git server currently lacks branch switching capabilities, limiting both
LLMs and developers. This adds branch checkout so LLMs can
help developers add new functionality in a new feature branch.
This commit is contained in:
Mike Gehard
2024-12-01 17:00:35 -05:00
parent 2ecb382a02
commit 021a95c904
4 changed files with 139 additions and 3 deletions

View File

@@ -44,6 +44,10 @@ class GitCreateBranch(BaseModel):
branch_name: str
base_branch: str | None = None
class GitCheckout(BaseModel):
repo_path: str
branch_name: str
class GitTools(str, Enum):
STATUS = "git_status"
DIFF_UNSTAGED = "git_diff_unstaged"
@@ -53,6 +57,7 @@ class GitTools(str, Enum):
RESET = "git_reset"
LOG = "git_log"
CREATE_BRANCH = "git_create_branch"
CHECKOUT = "git_checkout"
def git_status(repo: git.Repo) -> str:
return repo.git.status()
@@ -96,6 +101,10 @@ def git_create_branch(repo: git.Repo, branch_name: str, base_branch: str | None
repo.create_head(branch_name, base)
return f"Created branch '{branch_name}' from '{base.name}'"
def git_checkout(repo: git.Repo, branch_name: str) -> str:
repo.git.checkout(branch_name)
return f"Switched to branch '{branch_name}'"
async def serve(repository: Path | None) -> None:
logger = logging.getLogger(__name__)
@@ -152,6 +161,11 @@ async def serve(repository: Path | None) -> None:
description="Creates a new branch from an optional base branch",
inputSchema=GitCreateBranch.schema(),
),
Tool(
name=GitTools.CHECKOUT,
description="Switches branches",
inputSchema=GitCheckout.schema(),
),
]
async def list_repos() -> Sequence[str]:
@@ -249,6 +263,13 @@ async def serve(repository: Path | None) -> None:
text=result
)]
case GitTools.CHECKOUT:
result = git_checkout(repo, arguments["branch_name"])
return [TextContent(
type="text",
text=result
)]
case _:
raise ValueError(f"Unknown tool: {name}")