Line data Source code
1 : // Copyright (C) 2017 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.change; 16 : 17 : import com.google.gerrit.entities.PatchSet; 18 : import com.google.gerrit.extensions.restapi.BadRequestException; 19 : import com.google.gerrit.extensions.restapi.ResourceConflictException; 20 : import com.google.gerrit.server.git.PureRevertCache; 21 : import com.google.gerrit.server.notedb.ChangeNotes; 22 : import com.google.inject.Inject; 23 : import com.google.inject.Singleton; 24 : import java.io.IOException; 25 : import java.util.Optional; 26 : import org.eclipse.jgit.errors.InvalidObjectIdException; 27 : import org.eclipse.jgit.lib.ObjectId; 28 : 29 : /** Can check if a change is a pure revert (= a revert with no further modifications). */ 30 : @Singleton 31 : public class PureRevert { 32 : private final PureRevertCache pureRevertCache; 33 : 34 : @Inject 35 146 : PureRevert(PureRevertCache pureRevertCache) { 36 146 : this.pureRevertCache = pureRevertCache; 37 146 : } 38 : 39 : public boolean get(ChangeNotes notes, Optional<String> claimedOriginal) 40 : throws IOException, BadRequestException, ResourceConflictException { 41 15 : PatchSet currentPatchSet = notes.getCurrentPatchSet(); 42 15 : if (currentPatchSet == null) { 43 0 : throw new ResourceConflictException("current revision is missing"); 44 : } 45 15 : if (!claimedOriginal.isPresent()) { 46 14 : return pureRevertCache.isPureRevert(notes); 47 : } 48 : 49 : ObjectId claimedOriginalObjectId; 50 : try { 51 1 : claimedOriginalObjectId = ObjectId.fromString(claimedOriginal.get()); 52 1 : } catch (InvalidObjectIdException e) { 53 1 : throw new BadRequestException("invalid object ID", e); 54 1 : } 55 : 56 1 : return pureRevertCache.isPureRevert( 57 1 : notes.getProjectName(), notes.getCurrentPatchSet().commitId(), claimedOriginalObjectId); 58 : } 59 : }