Merge pull request #13124 from thingsboard/fix/gateway-dashboard-sync

Improve local repository opening
This commit is contained in:
Viacheslav Klimov 2025-04-07 17:28:18 +03:00 committed by GitHub
commit cb9fd0162e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -41,6 +41,8 @@ import org.eclipse.jgit.diff.HistogramDiff;
import org.eclipse.jgit.diff.RawText; import org.eclipse.jgit.diff.RawText;
import org.eclipse.jgit.diff.RawTextComparator; import org.eclipse.jgit.diff.RawTextComparator;
import org.eclipse.jgit.errors.LargeObjectException; import org.eclipse.jgit.errors.LargeObjectException;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.errors.TransportException;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectLoader; import org.eclipse.jgit.lib.ObjectLoader;
@ -147,22 +149,31 @@ public class GitRepository {
} }
public static GitRepository openOrClone(Path directory, RepositorySettings settings, boolean fetch) throws IOException, GitAPIException { public static GitRepository openOrClone(Path directory, RepositorySettings settings, boolean fetch) throws IOException, GitAPIException {
GitRepository repository;
if (GitRepository.exists(directory.toString())) { if (GitRepository.exists(directory.toString())) {
repository = GitRepository.open(directory.toFile(), settings); try {
if (fetch) { GitRepository repository = GitRepository.open(directory.toFile(), settings);
repository.fetch(); if (fetch) {
} repository.fetch();
} else { }
FileUtils.deleteDirectory(directory.toFile()); return repository;
Files.createDirectories(directory); } catch (RepositoryNotFoundException e) {
if (settings.isLocalOnly()) { log.warn("{} not a git repository, reinitializing", directory);
repository = GitRepository.create(settings, directory.toFile()); } catch (TransportException e) {
} else { if (StringUtils.containsIgnoreCase(e.getMessage(), "missing commit")) {
repository = GitRepository.clone(settings, directory.toFile()); log.warn("Couldn't fetch {} due to {}, reinitializing", directory, e.getMessage());
} else {
throw e;
}
} }
} }
return repository;
FileUtils.deleteDirectory(directory.toFile());
Files.createDirectories(directory);
if (settings.isLocalOnly()) {
return GitRepository.create(settings, directory.toFile());
} else {
return GitRepository.clone(settings, directory.toFile());
}
} }
public static void test(RepositorySettings settings, File directory) throws Exception { public static void test(RepositorySettings settings, File directory) throws Exception {