From d78fe02411440a2d9c0b68bfa57ff443bebfb8de Mon Sep 17 00:00:00 2001 From: Frederico Castro Date: Sat, 28 Feb 2026 06:00:21 -0300 Subject: [PATCH] =?UTF-8?q?Tratar=20diret=C3=B3rio=20existente=20inv=C3=A1?= =?UTF-8?q?lido=20(worktree=20=C3=B3rf=C3=A3o)=20no=20import?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Se o diretório em /home/projetos/ já existe mas não é um repo git válido (ex: worktree órfão), remove e clona do zero ao invés de falhar com erro. --- src/routes/api.js | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/routes/api.js b/src/routes/api.js index d7e1c48..316cdd4 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -1436,10 +1436,17 @@ router.post('/projects/import', async (req, res) => { const projectsDir = '/home/projetos'; const targetDir = join(projectsDir, sanitizedName); if (existsSync(targetDir)) { - await exec(`git remote set-url origin "${repoUrl}"`, targetDir); - await exec('git fetch origin', targetDir); - await exec('git reset --hard origin/main', targetDir); - steps.push('Projeto atualizado em /home/projetos/'); + try { + await exec('git rev-parse --git-dir', targetDir); + await exec(`git remote set-url origin "${repoUrl}"`, targetDir); + await exec('git fetch origin', targetDir); + await exec('git reset --hard origin/main', targetDir); + steps.push('Projeto atualizado em /home/projetos/'); + } catch { + rmSync(targetDir, { recursive: true, force: true }); + await exec(`git clone "${repoUrl}" "${targetDir}"`, projectsDir); + steps.push('Diretório anterior removido e projeto clonado em /home/projetos/'); + } } else { await exec(`git clone "${repoUrl}" "${targetDir}"`, projectsDir); steps.push('Projeto clonado em /home/projetos/'); @@ -1536,10 +1543,17 @@ router.post('/projects/upload', (req, res, next) => { const projectsDir = '/home/projetos'; const targetDir = join(projectsDir, repoName); if (existsSync(targetDir)) { - await exec(`git remote set-url origin "${repoUrl}"`, targetDir); - await exec('git fetch origin', targetDir); - await exec('git reset --hard origin/main', targetDir); - steps.push('Projeto atualizado em /home/projetos/'); + try { + await exec('git rev-parse --git-dir', targetDir); + await exec(`git remote set-url origin "${repoUrl}"`, targetDir); + await exec('git fetch origin', targetDir); + await exec('git reset --hard origin/main', targetDir); + steps.push('Projeto atualizado em /home/projetos/'); + } catch { + rmSync(targetDir, { recursive: true, force: true }); + await exec(`git clone "${repoUrl}" "${targetDir}"`, projectsDir); + steps.push('Diretório anterior removido e projeto clonado em /home/projetos/'); + } } else { await exec(`git clone "${repoUrl}" "${targetDir}"`, projectsDir); steps.push('Projeto clonado em /home/projetos/');