feat: support creating local branch during session creation

- Add new_branch field to CreateInstanceRequest
- Run git checkout -b after cloning when new_branch is provided
- Store new branch name in ToolInstance record
This commit is contained in:
2026-05-24 09:21:29 +00:00
parent c5f117e5b1
commit 3672312028
+22 -1
View File
@@ -2,6 +2,7 @@
import logging
import os
import subprocess
import uuid
from datetime import datetime
@@ -61,6 +62,7 @@ class CreateInstanceRequest(BaseModel):
display_name: str | None = Field(default=None, description="Optional display name for the instance")
clone_mode: str = Field(default="mount", description="Repository access mode: 'mount' or 'clone'")
branch: str | None = Field(default="main", description="Branch to clone (when clone_mode='clone')")
new_branch: str | None = Field(default=None, description="Create a new local branch after cloning")
def _modify_compose_file(
@@ -256,6 +258,25 @@ async def create_instance(
else:
repo_path = repo.path
# Create new local branch if requested
if data.clone_mode == "clone" and data.new_branch:
try:
result = subprocess.run(
["git", "-C", repo_path, "checkout", "-b", data.new_branch],
capture_output=True,
text=True,
)
if result.returncode != 0:
logger.error("Failed to create branch %s: %s", data.new_branch, result.stderr)
raise RuntimeError(f"Failed to create branch: {result.stderr}")
logger.info("Created local branch %s in cloned repository", data.new_branch)
except Exception as exc:
logger.exception("Failed to create local branch: %s", exc)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to create local branch: {exc}"
)
# Handle based on definition type
if tool_type.definition_type == "dockerfile":
# Build image from Dockerfile
@@ -318,7 +339,7 @@ services:
compose_path=compose_path,
port=tool_port,
clone_mode=data.clone_mode,
branch=data.branch if data.clone_mode == "clone" else None,
branch=data.new_branch if data.new_branch else (data.branch if data.clone_mode == "clone" else None),
)
session.add(instance)
await session.commit()