Merge pull request #11849 from thingsboard/fix/git-repo-init
Improve Git repository init
This commit is contained in:
commit
c6636ff6b8
@ -203,7 +203,7 @@ public class DefaultGitRepositoryService implements GitRepositoryService {
|
||||
GitRepository gitRepository = Optional.ofNullable(repositories.get(tenantId))
|
||||
.orElseThrow(() -> new IllegalStateException("Repository is not initialized"));
|
||||
|
||||
if (!Files.exists(Path.of(gitRepository.getDirectory()))) {
|
||||
if (!GitRepository.exists(gitRepository.getDirectory())) {
|
||||
try {
|
||||
return openOrCloneRepository(tenantId, gitRepository.getSettings(), false);
|
||||
} catch (Exception e) {
|
||||
@ -285,12 +285,13 @@ public class DefaultGitRepositoryService implements GitRepositoryService {
|
||||
Path repositoryDirectory = Path.of(repositoriesFolder, settings.isLocalOnly() ? "local_" + settings.getRepositoryUri() : tenantId.getId().toString());
|
||||
|
||||
GitRepository repository;
|
||||
if (Files.exists(repositoryDirectory)) {
|
||||
if (GitRepository.exists(repositoryDirectory.toString())) {
|
||||
repository = GitRepository.open(repositoryDirectory.toFile(), settings);
|
||||
if (fetch) {
|
||||
repository.fetch();
|
||||
}
|
||||
} else {
|
||||
FileUtils.deleteDirectory(repositoryDirectory.toFile());
|
||||
Files.createDirectories(repositoryDirectory);
|
||||
if (settings.isLocalOnly()) {
|
||||
repository = GitRepository.create(settings, repositoryDirectory.toFile());
|
||||
|
||||
@ -21,6 +21,7 @@ import com.google.common.collect.Streams;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@ -75,6 +76,7 @@ import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.KeyPair;
|
||||
import java.security.PublicKey;
|
||||
import java.util.ArrayList;
|
||||
@ -409,6 +411,12 @@ public class GitRepository {
|
||||
return result;
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public static boolean exists(String directory) {
|
||||
File gitDirectory = Path.of(directory, ".git").toFile();
|
||||
return FileUtils.isDirectory(gitDirectory) && !FileUtils.isEmptyDirectory(gitDirectory);
|
||||
}
|
||||
|
||||
private <C extends GitCommand<T>, T> T execute(C command) throws GitAPIException {
|
||||
if (command instanceof TransportCommand transportCommand && authHandler != null) {
|
||||
authHandler.configureCommand(transportCommand);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user