Line data Source code
1 : // Copyright (C) 2020 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.submit; 16 : 17 : import com.google.gerrit.entities.BranchNameKey; 18 : import com.google.gerrit.server.git.CodeReviewCommit; 19 : import com.google.gerrit.server.submit.MergeOpRepoManager.OpenRepo; 20 : import java.io.IOException; 21 : import java.util.HashMap; 22 : import java.util.Map; 23 : import java.util.Optional; 24 : import org.eclipse.jgit.lib.Ref; 25 : 26 : /** 27 : * Current branch tips, taking into account commits created during the submit process as well as 28 : * submodule updates produced by this class. 29 : */ 30 69 : class BranchTips { 31 : 32 69 : private final Map<BranchNameKey, CodeReviewCommit> branchTips = new HashMap<>(); 33 : 34 : /** 35 : * Returns current tip of the branch, taking into account commits created during the submit 36 : * process or submodule updates. 37 : * 38 : * @param branch branch 39 : * @param repo repository to look for the branch if not cached 40 : * @return the current tip. Empty if the branch doesn't exist in the repository 41 : * @throws IOException Cannot access the underlying storage 42 : */ 43 : Optional<CodeReviewCommit> getTip(BranchNameKey branch, OpenRepo repo) throws IOException { 44 : CodeReviewCommit currentCommit; 45 3 : if (branchTips.containsKey(branch)) { 46 1 : currentCommit = branchTips.get(branch); 47 : } else { 48 3 : Ref r = repo.repo.exactRef(branch.branch()); 49 3 : if (r == null) { 50 0 : return Optional.empty(); 51 : } 52 3 : currentCommit = repo.rw.parseCommit(r.getObjectId()); 53 3 : branchTips.put(branch, currentCommit); 54 : } 55 : 56 3 : return Optional.of(currentCommit); 57 : } 58 : 59 : void put(BranchNameKey branch, CodeReviewCommit c) { 60 53 : branchTips.put(branch, c); 61 53 : } 62 : }