private RefUpdate getPendingRefUpdate(Branch.NameKey destBranch) throws IntegrationException { if (pendingRefUpdates.containsKey(destBranch)) { logDebug("Access cached open branch {}: {}", destBranch.get(), openBranches.get(destBranch)); return pendingRefUpdates.get(destBranch); } try { RefUpdate branchUpdate = repo.updateRef(destBranch.get()); CodeReviewCommit branchTip; if (branchUpdate.getOldObjectId() != null) { branchTip = rw.parseCommit(branchUpdate.getOldObjectId()); } else if (Objects.equals(repo.getFullBranch(), destBranch.get())) { branchTip = null; branchUpdate.setExpectedOldObjectId(ObjectId.zeroId()); } else { throw new IntegrationException( "The destination branch " + destBranch.get() + " does not exist anymore."); } logDebug("Opened branch {}: {}", destBranch.get(), branchTip); pendingRefUpdates.put(destBranch, branchUpdate); openBranches.put(destBranch, branchTip); return branchUpdate; } catch (IOException e) { throw new IntegrationException("Cannot open branch", e); } }
OpenBranch(OpenRepo or, Branch.NameKey name) throws IntegrationException { this.name = name; try { update = or.repo.updateRef(name.get()); if (update.getOldObjectId() != null) { oldTip = or.rw.parseCommit(update.getOldObjectId()); } else if (Objects.equals(or.repo.getFullBranch(), name.get())) { oldTip = null; update.setExpectedOldObjectId(ObjectId.zeroId()); } else { throw new IntegrationException( "The destination branch " + name + " does not exist anymore."); } } catch (IOException e) { throw new IntegrationException("Cannot open branch " + name, e); } }
private RefUpdate updateBranch(Branch.NameKey destBranch, IdentifiedUser caller) throws IntegrationException { RefUpdate branchUpdate = getPendingRefUpdate(destBranch); CodeReviewCommit branchTip = getBranchTip(destBranch); MergeTip mergeTip = mergeTips.get(destBranch); CodeReviewCommit currentTip = mergeTip != null ? mergeTip.getCurrentTip() : null; if (Objects.equals(branchTip, 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 null; } else if (currentTip == null) { logDebug("No merge tip, no update to perform"); return null; } if (RefNames.REFS_CONFIG.equals(branchUpdate.getName())) { logDebug("Loading new configuration from {}", RefNames.REFS_CONFIG); try { ProjectConfig cfg = new ProjectConfig(destProject.getProject().getNameKey()); cfg.load(repo, currentTip); } catch (Exception e) { throw new IntegrationException( "Submit would store invalid" + " project configuration " + currentTip.name() + " for " + destProject.getProject().getName(), e); } } branchUpdate.setRefLogIdent( identifiedUserFactory.create(caller.getAccountId()).newRefLogIdent()); branchUpdate.setForceUpdate(false); branchUpdate.setNewObjectId(currentTip); branchUpdate.setRefLogMessage("merged", true); try { RefUpdate.Result result = branchUpdate.update(rw); logDebug( "Update of {}: {}..{} returned status {}", branchUpdate.getName(), branchUpdate.getOldObjectId(), branchUpdate.getNewObjectId(), result); switch (result) { case NEW: case FAST_FORWARD: if (branchUpdate.getResult() == RefUpdate.Result.FAST_FORWARD) { tagCache.updateFastForward( destBranch.getParentKey(), branchUpdate.getName(), branchUpdate.getOldObjectId(), currentTip); } if (RefNames.REFS_CONFIG.equals(branchUpdate.getName())) { Project p = destProject.getProject(); projectCache.evict(p); destProject = projectCache.get(p.getNameKey()); repoManager.setProjectDescription(p.getNameKey(), p.getDescription()); } return branchUpdate; case LOCK_FAILURE: throw new IntegrationException("Failed to lock " + branchUpdate.getName()); default: throw new IOException(branchUpdate.getResult().name() + '\n' + branchUpdate); } } catch (IOException e) { throw new IntegrationException("Cannot update " + branchUpdate.getName(), e); } }