|
| 1 | +import { existsSync, rmSync } from 'node:fs' |
1 | 2 | import { executeCommand } from '../utils/process' |
2 | 3 | import { ensureDirectoryExists } from './file-operations' |
3 | 4 | import * as db from '../db/queries' |
@@ -304,9 +305,9 @@ export async function cloneRepo( |
304 | 305 | } |
305 | 306 |
|
306 | 307 | await ensureDirectoryExists(getReposPath()) |
307 | | - const baseRepoExists = await executeCommand(['bash', '-c', `test -d ${baseRepoDirName} && echo exists || echo missing`], path.resolve(getReposPath())) |
| 308 | + const baseRepoExists = existsSync(path.join(path.resolve(getReposPath()), baseRepoDirName)) |
308 | 309 |
|
309 | | - const shouldUseWorktree = useWorktree && branch && baseRepoExists.trim() === 'exists' |
| 310 | + const shouldUseWorktree = useWorktree && branch && baseRepoExists |
310 | 311 |
|
311 | 312 | const createRepoInput: CreateRepoInput = { |
312 | 313 | repoUrl: normalizedRepoUrl, |
@@ -340,26 +341,24 @@ export async function cloneRepo( |
340 | 341 |
|
341 | 342 | await createWorktreeSafely(baseRepoPath, worktreePath, branch, env) |
342 | 343 |
|
343 | | - const worktreeVerified = await executeCommand(['test', '-d', worktreePath]) |
344 | | - .then(() => true) |
345 | | - .catch(() => false) |
| 344 | + const worktreeVerified = existsSync(worktreePath) |
346 | 345 |
|
347 | 346 | if (!worktreeVerified) { |
348 | 347 | throw new Error(`Worktree directory was not created at: ${worktreePath}`) |
349 | 348 | } |
350 | 349 |
|
351 | 350 | logger.info(`Worktree verified at: ${worktreePath}`) |
352 | 351 |
|
353 | | - } else if (branch && baseRepoExists.trim() === 'exists' && useWorktree) { |
| 352 | + } else if (branch && baseRepoExists && useWorktree) { |
354 | 353 | logger.info(`Base repo exists but worktree creation failed, cloning branch separately`) |
355 | 354 |
|
356 | | - const worktreeExists = await executeCommand(['bash', '-c', `test -d ${worktreeDirName} && echo exists || echo missing`], path.resolve(getReposPath())) |
357 | | - if (worktreeExists.trim() === 'exists') { |
| 355 | + const worktreeExists = existsSync(path.join(path.resolve(getReposPath()), worktreeDirName)) |
| 356 | + if (worktreeExists) { |
358 | 357 | logger.info(`Workspace directory exists, removing it: ${worktreeDirName}`) |
359 | 358 | try { |
360 | | - await executeCommand(['rm', '-rf', worktreeDirName], getReposPath()) |
361 | | - const verifyRemoved = await executeCommand(['bash', '-c', `test -d ${worktreeDirName} && echo exists || echo removed`], getReposPath()) |
362 | | - if (verifyRemoved.trim() === 'exists') { |
| 359 | + rmSync(path.join(path.resolve(getReposPath()), worktreeDirName), { recursive: true, force: true }) |
| 360 | + const verifyRemoved = !existsSync(path.join(path.resolve(getReposPath()), worktreeDirName)) |
| 361 | + if (!verifyRemoved) { |
363 | 362 | throw new Error(`Failed to remove existing directory: ${worktreeDirName}`) |
364 | 363 | } |
365 | 364 | } catch (cleanupError: unknown) { |
@@ -402,7 +401,7 @@ export async function cloneRepo( |
402 | 401 | } |
403 | 402 | } |
404 | 403 | } else { |
405 | | - if (baseRepoExists.trim() === 'exists') { |
| 404 | + if (baseRepoExists) { |
406 | 405 | logger.info(`Repository directory already exists, verifying it's a valid git repo: ${baseRepoDirName}`) |
407 | 406 | const isValidRepo = await executeCommand(['git', '-C', path.resolve(getReposPath(), baseRepoDirName), 'rev-parse', '--git-dir'], path.resolve(getReposPath())).then(() => 'valid').catch(() => 'invalid') |
408 | 407 |
|
@@ -446,26 +445,26 @@ export async function cloneRepo( |
446 | 445 | return { ...repo, cloneStatus: 'ready' } |
447 | 446 | } else { |
448 | 447 | logger.warn(`Invalid repository directory found, removing and recloning: ${baseRepoDirName}`) |
449 | | - await executeCommand(['rm', '-rf', baseRepoDirName], getReposPath()) |
| 448 | + rmSync(path.join(getReposPath(), baseRepoDirName), { recursive: true, force: true }) |
450 | 449 | } |
451 | 450 | } |
452 | 451 |
|
453 | 452 | logger.info(`Cloning repo: ${normalizedRepoUrl}${branch ? ` to branch ${branch}` : ''}`) |
454 | 453 |
|
455 | | - const worktreeExists = await executeCommand(['bash', '-c', `test -d ${worktreeDirName} && echo exists || echo missing`], getReposPath()) |
456 | | - if (worktreeExists.trim() === 'exists') { |
| 454 | + const worktreeExists = existsSync(path.join(getReposPath(), worktreeDirName)) |
| 455 | + if (worktreeExists) { |
457 | 456 | logger.info(`Workspace directory exists, removing it: ${worktreeDirName}`) |
458 | 457 | try { |
459 | | - await executeCommand(['rm', '-rf', worktreeDirName], getReposPath()) |
460 | | - const verifyRemoved = await executeCommand(['bash', '-c', `test -d ${worktreeDirName} && echo exists || echo removed`], getReposPath()) |
461 | | - if (verifyRemoved.trim() === 'exists') { |
462 | | - throw new Error(`Failed to remove existing directory: ${worktreeDirName}`) |
| 458 | + rmSync(path.join(getReposPath(), worktreeDirName), { recursive: true, force: true }) |
| 459 | + const verifyRemoved = !existsSync(path.join(getReposPath(), worktreeDirName)) |
| 460 | + if (!verifyRemoved) { |
| 461 | + throw new Error(`Failed to remove existing directory: ${worktreeDirName}`) |
| 462 | + } |
| 463 | + } catch (cleanupError: unknown) { |
| 464 | + logger.error(`Failed to clean up existing directory: ${worktreeDirName}`, cleanupError) |
| 465 | + throw new Error(`Cannot clone: directory ${worktreeDirName} exists and could not be removed`) |
463 | 466 | } |
464 | | - } catch (cleanupError: unknown) { |
465 | | - logger.error(`Failed to clean up existing directory: ${worktreeDirName}`, cleanupError) |
466 | | - throw new Error(`Cannot clone: directory ${worktreeDirName} exists and could not be removed`) |
467 | 467 | } |
468 | | - } |
469 | 468 |
|
470 | 469 | try { |
471 | 470 | const cloneCmd = branch |
|
0 commit comments