From 367231202805dd8af3f9881ac6b8ff108b7a6ca2 Mon Sep 17 00:00:00 2001 From: Alex Blank Date: Sun, 24 May 2026 09:21:29 +0000 Subject: [PATCH] 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 --- apps/api/src/api/tool_instances.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/apps/api/src/api/tool_instances.py b/apps/api/src/api/tool_instances.py index 859c87f..7774823 100644 --- a/apps/api/src/api/tool_instances.py +++ b/apps/api/src/api/tool_instances.py @@ -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()