+server.ts
1import { json } from '@sveltejs/kit';
2import { getStatus, getWorktreeStatus, getBaseBranchInfo, approveFile, unapproveFile, resetFile, resetHunk } from '$lib/server/vcs';
3import type { DiffScope } from '$lib/types/index';
4import type { RequestHandler } from './$types.ts';
5
6export const GET: RequestHandler = async ({ url }) => {
7 try {
8 const scope = (url.searchParams.get('scope') || 'uncommitted') as DiffScope;
9
10 if (scope === 'worktree') {
11 // getWorktreeStatus internally calls getBaseBranchInfo and populates the cache
12 const files = await getWorktreeStatus();
13 const branchInfo = await getBaseBranchInfo();
14 return json({ files, baseBranch: branchInfo.baseBranch, mergeBase: branchInfo.mergeBase });
15 }
16
17 const files = await getStatus();
18 return json(files);
19 } catch (err) {
20 return json({ error: String(err) }, { status: 500 });
21 }
22};
23
24export const POST: RequestHandler = async ({ request }) => {
25 try {
26 const body = await request.json();
27 const { path, action, scope } = body;
28
29 if (!path || !action) {
30 return json({ error: 'Missing path or action' }, { status: 400 });
31 }
32
33 if (action === 'approve') {
34 await approveFile(path, scope);
35 } else if (action === 'unapprove') {
36 await unapproveFile(path, scope);
37 } else if (action === 'reset') {
38 await resetFile(path);
39 } else if (action === 'reset-hunk') {
40 const { hunkHeader } = body;
41 if (!hunkHeader) return json({ error: 'Missing hunkHeader' }, { status: 400 });
42 await resetHunk(path, hunkHeader);
43 } else {
44 return json({ error: 'Unknown action' }, { status: 400 });
45 }
46
47 // Return updated file list
48 const files = await getStatus();
49 return json(files);
50 } catch (err) {
51 return json({ error: String(err) }, { status: 500 });
52 }
53};