#58 -- validate git project directory

This commit is contained in:
Anton Romanov 2022-06-21 11:14:13 +04:00
parent a7add46348
commit 312d613d7c

View File

@ -192,6 +192,7 @@ public class GitRepositoryService {
private void cloneOrUpdateRepo(String repositoryUrl, String branchName) throws GitAPIException, IOException { private void cloneOrUpdateRepo(String repositoryUrl, String branchName) throws GitAPIException, IOException {
Git git; Git git;
org.eclipse.jgit.lib.Repository localRepo; org.eclipse.jgit.lib.Repository localRepo;
validateOrClearRepositoryDirectory(getProjectDirectoryFile(repositoryUrl));
if (projectDirExists(getProjectDirectoryFile(repositoryUrl))) { if (projectDirExists(getProjectDirectoryFile(repositoryUrl))) {
localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl)); localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl));
git = new Git(localRepo); git = new Git(localRepo);
@ -210,6 +211,26 @@ public class GitRepositoryService {
git.close(); git.close();
} }
private void validateOrClearRepositoryDirectory(File projectDirectory) {
if (projectDirectory.exists()) {
try {
Git.open(projectDirectory).status().call();
} catch (Exception e) {
deleteDirectory(projectDirectory);
}
}
}
private boolean deleteDirectory(File directoryToBeDeleted) {
File[] allContents = directoryToBeDeleted.listFiles();
if (allContents != null) {
for (File file : allContents) {
deleteDirectory(file);
}
}
return directoryToBeDeleted.delete();
}
private void cloneOrUpdateRepo(String url) throws GitAPIException, IOException { private void cloneOrUpdateRepo(String url) throws GitAPIException, IOException {
cloneOrUpdateRepo(url, null); cloneOrUpdateRepo(url, null);
} }