♻️ refactor: update index.ts
swadhinbiswas committed 3 months ago
8ccc4ba
1 parent c8223ec
commit 8ccc4ba
1 file changed
+14 -1
Showing 1 changed file+14-1
▼src/pages/api/repos/index.ts
+14-1
| @@ -300,9 +300,10 @@ export const POST: APIRoute = withErrorHandler(async ({ request }) => { | ||
300 | 300 | |
301 | 301 | // Initialize git repository immediately (fast, ~100ms) |
302 | 302 | // Upload to S3 happens asynchronously to not block response |
303 | + let localGitPath: string | null = null; | |
303 | 304 | try { |
304 | 305 | // Get local path for git operations (temp path for cloud storage, direct path for local) |
305 | - const localGitPath = await initRepoInStorage(user.username, slug); | |
306 | + localGitPath = await initRepoInStorage(user.username, slug); | |
306 | 307 | |
307 | 308 | await initRepository(localGitPath, { |
308 | 309 | defaultBranch, |
| @@ -328,6 +329,18 @@ export const POST: APIRoute = withErrorHandler(async ({ request }) => { | ||
328 | 329 | } catch (error) { |
329 | 330 | // Rollback database entry if git init fails |
330 | 331 | await db.delete(repositories).where(eq(repositories.id, repoId)); |
332 | + // Clean up the git repo directory from disk | |
333 | + if (localGitPath) { | |
334 | + try { | |
335 | + const { rmSync, existsSync } = await import('fs'); | |
336 | + if (existsSync(localGitPath)) { | |
337 | + rmSync(localGitPath, { recursive: true, force: true }); | |
338 | + logger.info({ localGitPath }, 'Cleaned up failed repo directory'); | |
339 | + } | |
340 | + } catch (cleanupErr) { | |
341 | + logger.warn({ err: cleanupErr, localGitPath }, 'Failed to clean up repo directory'); | |
342 | + } | |
343 | + } | |
331 | 344 | logger.error({ err: error, repoId, diskPath }, 'Git init error'); |
332 | 345 | return serverError('Failed to initialize repository'); |
333 | 346 | } |
334 | 347 | |