Merge pull request #13124 from thingsboard/fix/gateway-dashboard-sync
Improve local repository opening
This commit is contained in:
commit
cb9fd0162e
@ -41,6 +41,8 @@ import org.eclipse.jgit.diff.HistogramDiff;
|
||||
import org.eclipse.jgit.diff.RawText;
|
||||
import org.eclipse.jgit.diff.RawTextComparator;
|
||||
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.ObjectId;
|
||||
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 {
|
||||
GitRepository repository;
|
||||
if (GitRepository.exists(directory.toString())) {
|
||||
repository = GitRepository.open(directory.toFile(), settings);
|
||||
if (fetch) {
|
||||
repository.fetch();
|
||||
}
|
||||
} else {
|
||||
FileUtils.deleteDirectory(directory.toFile());
|
||||
Files.createDirectories(directory);
|
||||
if (settings.isLocalOnly()) {
|
||||
repository = GitRepository.create(settings, directory.toFile());
|
||||
} else {
|
||||
repository = GitRepository.clone(settings, directory.toFile());
|
||||
try {
|
||||
GitRepository repository = GitRepository.open(directory.toFile(), settings);
|
||||
if (fetch) {
|
||||
repository.fetch();
|
||||
}
|
||||
return repository;
|
||||
} catch (RepositoryNotFoundException e) {
|
||||
log.warn("{} not a git repository, reinitializing", directory);
|
||||
} catch (TransportException e) {
|
||||
if (StringUtils.containsIgnoreCase(e.getMessage(), "missing commit")) {
|
||||
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 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user