From 07f1eb1675ed9864d84b9b374ed0ff7813e40be7 Mon Sep 17 00:00:00 2001 From: Justin <38869875+justinlampley@users.noreply.github.com> Date: Tue, 19 Apr 2022 17:28:46 -0400 Subject: [PATCH] If git repository is invalid then remove the existing files --- src/api/src/library/FirmwareDownloader/index.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/api/src/library/FirmwareDownloader/index.ts b/src/api/src/library/FirmwareDownloader/index.ts index 52b9d1fc8..28e1c48fb 100644 --- a/src/api/src/library/FirmwareDownloader/index.ts +++ b/src/api/src/library/FirmwareDownloader/index.ts @@ -126,9 +126,20 @@ export class GitFirmwareDownloader implements IFirmwareDownloader { const git = this.getSimpleGit(directory); - const isTargetDirectoryEmpty: boolean = + let isTargetDirectoryEmpty: boolean = (await fs.readdir(directory)).length === 0; + // if directory is not empty and it is not a valid repo, remove the existing files + if (!isTargetDirectoryEmpty && !(await git.checkIsRepo())) { + this.logger.log( + 'Invalid git repository detected, removing the existing files', + { directory } + ); + await fs.rm(directory, { recursive: true, force: true }); + await fs.mkdir(directory, { recursive: true }); + isTargetDirectoryEmpty = true; + } + if (isTargetDirectoryEmpty) { await git.clone(repository, directory, [ '--no-checkout',