Add git branch creation functionality
This commit is contained in:
parent
2578d6f668
commit
6135c62c69
1 changed files with 31 additions and 0 deletions
|
|
@ -39,6 +39,11 @@ class GitLog(BaseModel):
|
||||||
repo_path: str
|
repo_path: str
|
||||||
max_count: int = 10
|
max_count: int = 10
|
||||||
|
|
||||||
|
class GitCreateBranch(BaseModel):
|
||||||
|
repo_path: str
|
||||||
|
branch_name: str
|
||||||
|
base_branch: str | None = None
|
||||||
|
|
||||||
class GitTools(str, Enum):
|
class GitTools(str, Enum):
|
||||||
STATUS = "git_status"
|
STATUS = "git_status"
|
||||||
DIFF_UNSTAGED = "git_diff_unstaged"
|
DIFF_UNSTAGED = "git_diff_unstaged"
|
||||||
|
|
@ -47,6 +52,7 @@ class GitTools(str, Enum):
|
||||||
ADD = "git_add"
|
ADD = "git_add"
|
||||||
RESET = "git_reset"
|
RESET = "git_reset"
|
||||||
LOG = "git_log"
|
LOG = "git_log"
|
||||||
|
CREATE_BRANCH = "git_create_branch"
|
||||||
|
|
||||||
def git_status(repo: git.Repo) -> str:
|
def git_status(repo: git.Repo) -> str:
|
||||||
return repo.git.status()
|
return repo.git.status()
|
||||||
|
|
@ -81,6 +87,15 @@ def git_log(repo: git.Repo, max_count: int = 10) -> list[str]:
|
||||||
)
|
)
|
||||||
return log
|
return log
|
||||||
|
|
||||||
|
def git_create_branch(repo: git.Repo, branch_name: str, base_branch: str | None = None) -> str:
|
||||||
|
if base_branch:
|
||||||
|
base = repo.refs[base_branch]
|
||||||
|
else:
|
||||||
|
base = repo.active_branch
|
||||||
|
|
||||||
|
repo.create_head(branch_name, base)
|
||||||
|
return f"Created branch '{branch_name}' from {base.name}"
|
||||||
|
|
||||||
async def serve(repository: Path | None) -> None:
|
async def serve(repository: Path | None) -> None:
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
@ -132,6 +147,11 @@ async def serve(repository: Path | None) -> None:
|
||||||
description="Shows the commit logs",
|
description="Shows the commit logs",
|
||||||
inputSchema=GitLog.schema(),
|
inputSchema=GitLog.schema(),
|
||||||
),
|
),
|
||||||
|
Tool(
|
||||||
|
name=GitTools.CREATE_BRANCH,
|
||||||
|
description="Creates a new branch from an optional base branch",
|
||||||
|
inputSchema=GitCreateBranch.schema(),
|
||||||
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
async def list_repos() -> Sequence[str]:
|
async def list_repos() -> Sequence[str]:
|
||||||
|
|
@ -218,6 +238,17 @@ async def serve(repository: Path | None) -> None:
|
||||||
text="Commit history:\n" + "\n".join(log)
|
text="Commit history:\n" + "\n".join(log)
|
||||||
)]
|
)]
|
||||||
|
|
||||||
|
case GitTools.CREATE_BRANCH:
|
||||||
|
result = git_create_branch(
|
||||||
|
repo,
|
||||||
|
arguments["branch_name"],
|
||||||
|
arguments.get("base_branch")
|
||||||
|
)
|
||||||
|
return [TextContent(
|
||||||
|
type="text",
|
||||||
|
text=result
|
||||||
|
)]
|
||||||
|
|
||||||
case _:
|
case _:
|
||||||
raise ValueError(f"Unknown tool: {name}")
|
raise ValueError(f"Unknown tool: {name}")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue