| |
| |
| |
|
|
| import type { Request, Response, NextFunction } from 'express'; |
| import { isGitRepo } from '@automaker/git-utils'; |
| import { hasCommits } from './common.js'; |
|
|
| interface ValidationOptions { |
| |
| requireGitRepo?: boolean; |
| |
| requireCommits?: boolean; |
| |
| pathField?: 'worktreePath' | 'projectPath'; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function requireValidGitRepo(options: ValidationOptions = {}) { |
| const { requireGitRepo = true, requireCommits = true, pathField = 'worktreePath' } = options; |
|
|
| return async (req: Request, res: Response, next: NextFunction): Promise<void> => { |
| const repoPath = req.body[pathField] as string | undefined; |
|
|
| if (!repoPath) { |
| |
| next(); |
| return; |
| } |
|
|
| if (requireGitRepo && !(await isGitRepo(repoPath))) { |
| res.status(400).json({ |
| success: false, |
| error: 'Not a git repository', |
| code: 'NOT_GIT_REPO', |
| }); |
| return; |
| } |
|
|
| if (requireCommits && !(await hasCommits(repoPath))) { |
| res.status(400).json({ |
| success: false, |
| error: 'Repository has no commits yet', |
| code: 'NO_COMMITS', |
| }); |
| return; |
| } |
|
|
| next(); |
| }; |
| } |
|
|
| |
| |
| |
| export const requireValidWorktree = requireValidGitRepo({ pathField: 'worktreePath' }); |
|
|
| |
| |
| |
| export const requireValidProject = requireValidGitRepo({ pathField: 'projectPath' }); |
|
|
| |
| |
| |
| export const requireGitRepoOnly = requireValidGitRepo({ |
| pathField: 'worktreePath', |
| requireCommits: false, |
| }); |
|
|