P3-A: worktrees multi-repo & cycle de vie (backend)
Enregistrement de repos et gestion de leurs worktrees git, source de vérité = git (worktrees dérivés + cache court par repo), corrélation worktree ↔ sessions par cwd, mutations sérialisées par repo. - shared: RepoSummary, PostCreateHook, WorktreeSummary, WorktreeGitStatus ; messages WS repo_update/worktree_update/*_removed ; topic sub 'worktrees' (+ parseClientMessage) ; DTOs REST repos/worktrees. - db: migration id:3 (table repos). - core/git.ts: couche git sûre (execFile, jamais de shell, -- avant chemins, GIT_OPTIONAL_LOCKS=0), parseWorktreePorcelain, list/add/remove/prune/status/ ahead-behind, validation branche + chemin. - core/claude-trust.ts: pré-trust atomique de ~/.claude.json (spike S3). - core/worktree-manager.ts: repos CRUD, create (worktree add + pré-trust + hooks post-create + startSession optionnel), adopt, delete (garde-fous 409 dirty / 400 main / 409 session live), prune ; events. - routes/repos.ts + routes/worktrees.ts, câblage app.ts, gateway topic worktrees. - tests: git (repos tmp réels), claude-trust, worktree-manager (146 verts).
This commit is contained in:
88
packages/server/src/routes/worktrees.ts
Normal file
88
packages/server/src/routes/worktrees.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
import type {
|
||||
AdoptWorktreeRequest,
|
||||
CreateWorktreeRequest,
|
||||
CreateWorktreeResponse,
|
||||
WorktreeResponse,
|
||||
WorktreesListResponse,
|
||||
} from '@arboretum/shared';
|
||||
import type { WorktreeManager } from '../core/worktree-manager.js';
|
||||
import { sendManagerError } from './repos.js';
|
||||
|
||||
export function registerWorktreeRoutes(app: FastifyInstance, wt: WorktreeManager): void {
|
||||
app.get('/api/v1/worktrees', async (): Promise<WorktreesListResponse> => ({ worktrees: await wt.listAllWorktrees() }));
|
||||
|
||||
app.get('/api/v1/repos/:id/worktrees', async (req): Promise<WorktreesListResponse> => {
|
||||
const { id } = req.params as { id: string };
|
||||
return { worktrees: await wt.listRepoWorktrees(id) };
|
||||
});
|
||||
|
||||
app.post('/api/v1/repos/:id/worktrees', async (req, reply) => {
|
||||
const { id } = req.params as { id: string };
|
||||
const body = req.body as Partial<CreateWorktreeRequest> | null;
|
||||
if (!body || typeof body.branch !== 'string' || body.branch.trim() === '') {
|
||||
return reply.status(400).send({ error: { code: 'BAD_REQUEST', message: 'branch is required' } });
|
||||
}
|
||||
if (body.startSession != null && body.startSession !== 'claude' && body.startSession !== 'bash') {
|
||||
return reply.status(400).send({ error: { code: 'BAD_REQUEST', message: 'startSession must be claude, bash or null' } });
|
||||
}
|
||||
try {
|
||||
const out = await wt.createWorktree(id, {
|
||||
branch: body.branch,
|
||||
newBranch: body.newBranch !== false, // défaut : créer la branche
|
||||
...(body.baseRef !== undefined ? { baseRef: body.baseRef } : {}),
|
||||
...(body.path !== undefined ? { path: body.path } : {}),
|
||||
...(body.runHooks !== undefined ? { runHooks: body.runHooks } : {}),
|
||||
...(body.preTrust !== undefined ? { preTrust: body.preTrust } : {}),
|
||||
...(body.startSession !== undefined ? { startSession: body.startSession } : {}),
|
||||
});
|
||||
const res: CreateWorktreeResponse = out;
|
||||
return reply.status(201).send(res);
|
||||
} catch (err) {
|
||||
return sendManagerError(reply, err);
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/v1/repos/:id/worktrees/adopt', async (req, reply) => {
|
||||
const { id } = req.params as { id: string };
|
||||
const body = req.body as Partial<AdoptWorktreeRequest> | null;
|
||||
if (!body || typeof body.path !== 'string') {
|
||||
return reply.status(400).send({ error: { code: 'BAD_REQUEST', message: 'path is required' } });
|
||||
}
|
||||
try {
|
||||
const out = await wt.adoptWorktree(id, {
|
||||
path: body.path,
|
||||
...(body.runHooks !== undefined ? { runHooks: body.runHooks } : {}),
|
||||
...(body.preTrust !== undefined ? { preTrust: body.preTrust } : {}),
|
||||
});
|
||||
const res: WorktreeResponse & { hookResults: typeof out.hookResults } = { worktree: out.worktree, hookResults: out.hookResults };
|
||||
return reply.send(res);
|
||||
} catch (err) {
|
||||
return sendManagerError(reply, err);
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/v1/repos/:id/worktrees/prune', async (req, reply) => {
|
||||
const { id } = req.params as { id: string };
|
||||
try {
|
||||
await wt.prune(id);
|
||||
return reply.send({ ok: true });
|
||||
} catch (err) {
|
||||
return sendManagerError(reply, err);
|
||||
}
|
||||
});
|
||||
|
||||
app.delete('/api/v1/repos/:id/worktrees', async (req, reply) => {
|
||||
const { id } = req.params as { id: string };
|
||||
const query = req.query as { path?: string; force?: string };
|
||||
if (typeof query.path !== 'string') {
|
||||
return reply.status(400).send({ error: { code: 'BAD_REQUEST', message: 'path query param is required' } });
|
||||
}
|
||||
try {
|
||||
await wt.deleteWorktree(id, query.path, query.force === 'true');
|
||||
return reply.send({ ok: true });
|
||||
} catch (err) {
|
||||
return sendManagerError(reply, err);
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user