Line data Source code
1 : // Copyright (C) 2021 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.acceptance; 16 : 17 : import com.google.gerrit.exceptions.StorageException; 18 : import com.google.gerrit.server.events.CommitReceivedEvent; 19 : import com.google.gerrit.server.git.GitRepositoryManager; 20 : import com.google.gerrit.server.git.validators.CommitValidationException; 21 : import com.google.gerrit.server.git.validators.CommitValidationListener; 22 : import com.google.gerrit.server.git.validators.CommitValidationMessage; 23 : import com.google.inject.Inject; 24 : import com.google.inject.Singleton; 25 : import java.io.IOException; 26 : import java.util.Collections; 27 : import java.util.List; 28 : import org.eclipse.jgit.lib.ObjectReader; 29 : import org.eclipse.jgit.lib.Repository; 30 : 31 : /** 32 : * Checker that ensures that all Git commits that should be validated are readable using any {@link 33 : * ObjectReader} on the repo. It is easy for users of the JGit API to forget to call {@code flush} 34 : * on ObjectInserter which creates an illegal state for CommitValidators. This safeguard makes sure 35 : * that any functionality tested in acceptance tests got this right. 36 : */ 37 : @Singleton 38 : public class GitObjectVisibilityChecker implements CommitValidationListener { 39 : private final GitRepositoryManager gitRepositoryManager; 40 : 41 : @Inject 42 138 : GitObjectVisibilityChecker(GitRepositoryManager gitRepositoryManager) { 43 138 : this.gitRepositoryManager = gitRepositoryManager; 44 138 : } 45 : 46 : @Override 47 : public List<CommitValidationMessage> onCommitReceived(CommitReceivedEvent receiveEvent) 48 : throws CommitValidationException { 49 : try { 50 101 : try (Repository repo = gitRepositoryManager.openRepository(receiveEvent.getProjectNameKey()); 51 101 : ObjectReader reader = repo.newObjectReader()) { 52 101 : if (!reader.has(receiveEvent.commit)) { 53 0 : throw new IllegalStateException( 54 0 : String.format( 55 : "Commit %s was not visible using a new object reader in the repo. " 56 : + "This creates an illegal state for commit validators. You must flush any ObjectReaders " 57 : + "before performing the ref transaction.", 58 : receiveEvent.commit)); 59 : } 60 : } 61 0 : } catch (IOException e) { 62 0 : throw new StorageException(e); 63 101 : } 64 101 : return Collections.emptyList(); 65 : } 66 : 67 : @Override 68 : public boolean shouldValidateAllCommits() { 69 3 : return true; 70 : } 71 : }