Exemplo n.º 1
0
  private boolean updateBranch(OpenRepo or, Branch.NameKey destBranch, IdentifiedUser caller)
      throws IntegrationException {
    OpenBranch ob = or.getBranch(destBranch);
    CodeReviewCommit currentTip = ob.getCurrentTip();
    if (Objects.equals(ob.oldTip, currentTip)) {
      if (currentTip != null) {
        logDebug("Branch already at merge tip {}, no update to perform", currentTip.name());
      } else {
        logDebug("Both branch and merge tip are nonexistent, no update");
      }
      return false;
    } else if (currentTip == null) {
      logDebug("No merge tip, no update to perform");
      return false;
    }

    if (RefNames.REFS_CONFIG.equals(ob.update.getName())) {
      logDebug("Loading new configuration from {}", RefNames.REFS_CONFIG);
      try {
        ProjectConfig cfg = new ProjectConfig(or.getProjectName());
        cfg.load(or.repo, currentTip);
      } catch (Exception e) {
        throw new IntegrationException(
            "Submit would store invalid"
                + " project configuration "
                + currentTip.name()
                + " for "
                + or.getProjectName(),
            e);
      }
    }

    ob.update.setRefLogIdent(identifiedUserFactory.create(caller.getAccountId()).newRefLogIdent());
    ob.update.setForceUpdate(false);
    ob.update.setNewObjectId(currentTip);
    ob.update.setRefLogMessage("merged", true);
    try {
      RefUpdate.Result result = ob.update.update(or.rw);
      logDebug(
          "Update of {}: {}..{} returned status {}",
          ob.update.getName(),
          ob.update.getOldObjectId(),
          ob.update.getNewObjectId(),
          result);
      switch (result) {
        case NEW:
        case FAST_FORWARD:
          if (ob.update.getResult() == RefUpdate.Result.FAST_FORWARD) {
            tagCache.updateFastForward(
                destBranch.getParentKey(),
                ob.update.getName(),
                ob.update.getOldObjectId(),
                currentTip);
          }

          if (RefNames.REFS_CONFIG.equals(ob.update.getName())) {
            Project p = or.project.getProject();
            projectCache.evict(p);
            or.project = projectCache.get(p.getNameKey());
            repoManager.setProjectDescription(p.getNameKey(), p.getDescription());
          }

          return true;

        case LOCK_FAILURE:
          throw new IntegrationException("Failed to lock " + ob.update.getName());
        default:
          throw new IOException(ob.update.getResult().name() + '\n' + ob.update);
      }
    } catch (IOException e) {
      throw new IntegrationException("Cannot update " + ob.update.getName(), e);
    }
  }