private void updateSources()

in git-agent/src/main/java/jetbrains/buildServer/buildTriggers/vcs/git/agent/UpdaterImpl.java [251:310]


  private void updateSources() throws VcsException {
    final AgentGitFacade git = myGitFactory.create(myTargetDirectory);
    boolean branchChanged = false;
    removeIndexLock();
    if (GitUtilsAgent.isRegularBranch(myFullBranchName)) {
      String branchName = GitUtilsAgent.getShortBranchName(myFullBranchName);
      Branches branches = git.listBranches(false);
      if (branches.isCurrentBranch(branchName)) {
        removeIndexLock();
        runAndFixIndexErrors(git, new VcsCommand() {
          @Override
          public void call() throws VcsException {
            reset(git).setHard(true).setRevision(myRevision).call();
          }
        });
        git.setUpstream(branchName, GitUtils.createRemoteRef(myFullBranchName)).call();
      } else {
        branchChanged = true;
        if (!branches.contains(branchName)) {
          git.createBranch()
            .setName(branchName)
            .setStartPoint(GitUtils.createRemoteRef(myFullBranchName))
            .setTrack(true)
            .call();
        }
        git.updateRef().setRef(myFullBranchName).setRevision(myRevision).call();
        final String finalBranchName = branchName;
        runAndFixIndexErrors(git, new VcsCommand() {
          @Override
          public void call() throws VcsException {
            checkout(git)
              .setForce(true)
              .setBranch(finalBranchName)
              .setQuiet(isQuietCheckout())
              .setTimeout(myPluginConfig.getCheckoutIdleTimeoutSeconds())
              .call();
          }
        });
        if (branches.contains(branchName)) {
          git.setUpstream(branchName, GitUtils.createRemoteRef(myFullBranchName)).call();
        }
      }
    } else if (GitUtilsAgent.isTag(myFullBranchName)) {
      Ref tag = getRef(myTargetDirectory, myFullBranchName);
      if (tag == null || !tag.getObjectId().name().equals(myRevision)) {
        runAndFixIndexErrors(git, () -> forceCheckout(git, myRevision));
      } else {
        runAndFixIndexErrors(git, () -> forceCheckout(git, myFullBranchName.substring("refs/tags/".length())));
      }
      branchChanged = true;
    } else {
      runAndFixIndexErrors(git, () -> forceCheckout(git, myRevision));
      branchChanged = true;
    }

    doClean(branchChanged);
    if (myRoot.isCheckoutSubmodules()) {
      checkoutSubmodules(myTargetDirectory);
    }
  }