@Nullable
  private static Pair<String, Integer> getCommitToDownload(@NotNull Project project, @NotNull String serverUrl, boolean forceUpdate) {
    Map<String, Set<String>> availableCommitsPerRemote = CompilerCachesServerClient.getCacheKeysPerRemote(project, serverUrl);
    GitCommitsIterator commitsIterator = new GitCommitsIterator(project, INTELLIJ_REPO_NAME);
    String latestDownloadedCommit = GitRepositoryUtil.getLatestDownloadedCommit();
    String latestBuiltCommit = GitRepositoryUtil.getLatestBuiltMasterCommitId();
    Set<String> availableCommitsForRemote = availableCommitsPerRemote.get(commitsIterator.getRemote());
    if (availableCommitsForRemote == null) {
      LOG.warn("Not found any caches for the remote: " + commitsIterator.getRemote());
      return null;
    }

    int commitsBehind = 0;
    int commitsCountBetweenCompilation = 0;
    String commitToDownload = "";
    boolean latestBuiltCommitFound = false;
    while (commitsIterator.hasNext()) {
      String commitId = commitsIterator.next();
      if (commitId.equals(latestBuiltCommit) && !latestBuiltCommitFound) {
        latestBuiltCommitFound = true;
      }
      if (!latestBuiltCommitFound) {
        commitsCountBetweenCompilation++;
      }
      if (availableCommitsForRemote.contains(commitId) && commitToDownload.isEmpty()) {
        commitToDownload = commitId;
      }
      if (commitToDownload.isEmpty()) {
        commitsBehind++;
      }

      if (latestBuiltCommitFound && !commitToDownload.isEmpty()) break;
    }

    if (!forceUpdate && commitsCountBetweenCompilation == 0) {
      LOG.warn("No new commits since last success compilation");
      return null;
    }
    if (!availableCommitsForRemote.contains(commitToDownload)) {
      LOG.warn("Not found any caches for the latest commits in the branch");
      return null;
    }
    LOG.info("Project contains " + commitsCountBetweenCompilation + " non compiled commits. Cache will be downloaded for " + commitsBehind + " commits before the current master." +
             " The rest commits will be build locally. Commit hash for download: " + commitToDownload);
    if (!forceUpdate && commitsBehind >= commitsCountBetweenCompilation) {
      LOG.info("Caches available for already compiled commit. Skipping download");
      return null;
    }
    if (!forceUpdate && commitToDownload.equals(latestDownloadedCommit)) {
      LOG.info("The system contains up-to-date caches");
      return null;
    }
    if (!forceUpdate && commitToDownload.equals(latestBuiltCommit)) {
      LOG.info("Commit already compiled, thus caches for it won't be download");
      return null;
    }
    return Pair.create(commitToDownload, commitsCountBetweenCompilation);
  }
@Nullable
  private static Pair<String, Integer> getCommitToDownload(@NotNull Project project, @NotNull String serverUrl, boolean forceUpdate)