protected void updateLocalMirror()

in git-agent/src/main/java/jetbrains/buildServer/buildTriggers/vcs/git/agent/UpdaterWithMirror.java [90:167]


  protected void updateLocalMirror(File bareRepositoryDir,
                                   CommonURIish fetchUrl,
                                   CommonURIish originalFetchUrl,
                                   Ref... revisions) throws VcsException {
    final boolean isSubmodule = !isRootRepositoryDir(bareRepositoryDir);
    String mirrorDescription = (isSubmodule ? "submodule " : "") + "local mirror of root " + myRoot.getName() + " at " + bareRepositoryDir;
    LOG.info("Update " + mirrorDescription);
    boolean shouldInit = false;
    if (isValidGitRepo(bareRepositoryDir)) {
      removeOrphanedIdxFiles(bareRepositoryDir);
    } else {
      cleanDir(bareRepositoryDir);
      shouldInit = true;
    }
    final AgentGitFacade git = myGitFactory.create(bareRepositoryDir);
    final SSLInvestigator sslInvestigator = getSSLInvestigator(fetchUrl);
    boolean fetchRequired;
    if (shouldInit || !bareRepositoryDir.exists()) {
      LOG.info("Init " + mirrorDescription);
      bareRepositoryDir.mkdirs();
      git.init().setBare(true).call();
      configureRemoteUrl(bareRepositoryDir, fetchUrl);
      sslInvestigator.setCertificateOptions(git);
      fetchRequired = true;
    } else {
      configureRemoteUrl(bareRepositoryDir, fetchUrl);
      sslInvestigator.setCertificateOptions(git);
      fetchRequired = removeOutdatedRefs(bareRepositoryDir, false);
    }

    final AgentCommitLoader commitLoader =
      isSubmodule ?
      AgentCommitLoaderFactory.getCommitLoaderForSubmodule(myRoot, myBuild, bareRepositoryDir, myGitFactory, myPluginConfig, myLogger) :
      AgentCommitLoaderFactory.getCommitLoaderForMirror(myRoot, myBuild, bareRepositoryDir, myGitFactory, myPluginConfig, myLogger);
    try {
      loadCommits(fetchRequired, commitLoader, revisions);
    } catch (VcsException e) {
      VcsException vcsException = e;
      if (shouldRetryFetchAfterRemovingOutadedRefs(vcsException)) {
        try {
          LOG.warnAndDebugDetails("Fetch failed. Removing outdated refs and retrying fetch", e);
          myLogger.warning("Fetch failed. Removing outdated refs and retrying fetch");
          fetchRequired |= removeOutdatedRefs(bareRepositoryDir, true);
          loadCommits(fetchRequired, commitLoader, revisions);
          return;
        } catch (VcsException retryException) {
          vcsException = retryException;
        }
      }

      if (myPluginConfig.isFailOnCleanCheckout() ||
          CommandUtil.isRemoteAccessError(vcsException) ||
          CommandUtil.isCanceledError(vcsException) ||
          vcsException instanceof GitExecTimeout) {
        throw vcsException;
      }

      stopAgentIfNecessary(vcsException);

      if (myPluginConfig.maxRepositorySizeForFsckGiB() <= 0 ||
          commitLoader.getMirrorSizeGiB() >= myPluginConfig.maxRepositorySizeForFsckGiB() ||
          commitLoader.isMirrorValid()) { // git fsck: time-consuming command
        throw vcsException;
      }

      LOG.warnAndDebugDetails("Failed to fetch mirror, will try removing it and cloning from scratch", vcsException);
      if (cleanDir(bareRepositoryDir)) {
        git.init().setBare(true).call();
        configureRemoteUrl(bareRepositoryDir, fetchUrl);
        sslInvestigator.setCertificateOptions(git);
        loadCommits(true, commitLoader, revisions);
      } else {
        LOG.info("Failed to delete repository " + bareRepositoryDir + " after failed checkout, clone repository in another directory");
        myMirrorManager.invalidate(bareRepositoryDir);
        updateLocalMirror(myMirrorManager.getMirrorDir(originalFetchUrl.toString()), fetchUrl, originalFetchUrl, revisions);
      }
    }
  }