Tratar diretório existente inválido (worktree órfão) no import

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.
This commit is contained in:
Frederico Castro
2026-02-28 06:00:21 -03:00
parent 7f8bf5e3a9
commit d78fe02411

View File

@@ -1436,10 +1436,17 @@ router.post('/projects/import', async (req, res) => {
const projectsDir = '/home/projetos'; const projectsDir = '/home/projetos';
const targetDir = join(projectsDir, sanitizedName); const targetDir = join(projectsDir, sanitizedName);
if (existsSync(targetDir)) { if (existsSync(targetDir)) {
await exec(`git remote set-url origin "${repoUrl}"`, targetDir); try {
await exec('git fetch origin', targetDir); await exec('git rev-parse --git-dir', targetDir);
await exec('git reset --hard origin/main', targetDir); await exec(`git remote set-url origin "${repoUrl}"`, targetDir);
steps.push('Projeto atualizado em /home/projetos/'); 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 { } else {
await exec(`git clone "${repoUrl}" "${targetDir}"`, projectsDir); await exec(`git clone "${repoUrl}" "${targetDir}"`, projectsDir);
steps.push('Projeto clonado em /home/projetos/'); steps.push('Projeto clonado em /home/projetos/');
@@ -1536,10 +1543,17 @@ router.post('/projects/upload', (req, res, next) => {
const projectsDir = '/home/projetos'; const projectsDir = '/home/projetos';
const targetDir = join(projectsDir, repoName); const targetDir = join(projectsDir, repoName);
if (existsSync(targetDir)) { if (existsSync(targetDir)) {
await exec(`git remote set-url origin "${repoUrl}"`, targetDir); try {
await exec('git fetch origin', targetDir); await exec('git rev-parse --git-dir', targetDir);
await exec('git reset --hard origin/main', targetDir); await exec(`git remote set-url origin "${repoUrl}"`, targetDir);
steps.push('Projeto atualizado em /home/projetos/'); 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 { } else {
await exec(`git clone "${repoUrl}" "${targetDir}"`, projectsDir); await exec(`git clone "${repoUrl}" "${targetDir}"`, projectsDir);
steps.push('Projeto clonado em /home/projetos/'); steps.push('Projeto clonado em /home/projetos/');