Project Files
src / tools / git-branch-tool.ts
import { tool } from "@lmstudio/sdk"
import { simpleGit } from "simple-git"
import { z } from "zod"
import { formatGitError } from "../git/error-formatting"
import { resolveAndValidateRepoPath } from "../path/validation"
import type { Tool, ToolsProviderController } from "@lmstudio/sdk"
/**
* Create the git branch tool.
*
* @param ctl Tools provider controller supplied by the LM Studio SDK.
* @returns The configured git branch tool.
*/
export function createGitBranchTool(ctl: ToolsProviderController): Tool {
return tool({
name: "git_branch",
description:
"Get branch information for a cloned git repository, including the currently checked out branch and all local or remote branches.",
parameters: {
repoName: z.string().describe("Directory name of the cloned git repository."),
remote: z
.boolean()
.optional()
.default(false)
.describe("If true, list remote branches instead of local branches."),
},
/**
* Executes the git branch command.
*
* @param arguments_ Validated tool parameters.
* @param arguments_.repoName Repository directory name relative to the working directory.
* @param arguments_.remote When true, list remote branches instead of local branches.
* @param context Runtime tool context supplied by the SDK.
* @returns Branch information as JSON, or a user-facing error string.
*/
implementation: async (arguments_, context) => {
const { repoName } = arguments_
const remote = arguments_.remote === true
const repoPath = resolveAndValidateRepoPath(repoName, ctl.getWorkingDirectory())
const git = simpleGit(repoPath)
context.status(`Listing ${remote ? "remote" : "local"} branches in "${repoName}"…`)
try {
const [branch, root] = await Promise.all([
remote ? git.branch(["--remotes"]) : git.branch(),
git.revparse(["--show-toplevel"]),
])
return JSON.stringify({ current: branch.current, branches: branch.all, repoRoot: root.trim() }, undefined, 2)
} catch (error) {
return formatGitError("git_branch", repoName, error)
}
},
})
}