Skip to content

Commit 9b4e938

Browse files
authored
feat(daemon): add POST /git/checkout endpoint for switching to existing branches (#1164)
Split the checkout logic into two explicit APIs: - POST /checkout-branch: creates and switches to a new branch (git checkout -b) - POST /checkout: switches to an existing branch, returns 404 if not found Made-with: Cursor
1 parent c741b38 commit 9b4e938

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

daemon/git.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,15 @@ export interface CheckoutBranchAPI {
324324
};
325325
}
326326

327+
export interface CheckoutExistingBranchAPI {
328+
body: {
329+
branchName: string;
330+
};
331+
response: {
332+
branch: string;
333+
};
334+
}
335+
327336
// ─── /git/raw ─────────────────────────────────────────────────────────────────
328337

329338
export interface GitRawAPI {
@@ -858,6 +867,28 @@ export const checkoutBranch: Handler = async (c) => {
858867
return Response.json({ branch: branchName });
859868
};
860869

870+
export const checkoutExistingBranch: Handler = async (c) => {
871+
const { branchName } =
872+
(await c.req.json()) as CheckoutExistingBranchAPI["body"];
873+
874+
if (!branchName || !/^[a-zA-Z0-9_\-./]+$/.test(branchName)) {
875+
return new Response("Invalid branch name", { status: 400 });
876+
}
877+
878+
const branches = await git.branch(["-a"]);
879+
const exists = Object.keys(branches.branches).some(
880+
(b) => b === branchName || b === `remotes/origin/${branchName}`,
881+
);
882+
883+
if (!exists) {
884+
return new Response(`Branch "${branchName}" not found`, { status: 404 });
885+
}
886+
887+
await git.checkout(branchName);
888+
889+
return Response.json({ branch: branchName });
890+
};
891+
861892
export const createGitAPIS = (options: Options) => {
862893
const app = new Hono();
863894

@@ -869,6 +900,7 @@ export const createGitAPIS = (options: Options) => {
869900
app.post("/discard", discard);
870901
app.post("/rebase", rebase);
871902
app.post("/checkout-branch", checkoutBranch);
903+
app.post("/checkout", checkoutExistingBranch);
872904
app.post("/raw", gitRaw);
873905

874906
return app;

0 commit comments

Comments
 (0)