Optimize cherry pick operation by not re-reading change
Change-Id: If2c307b43788fe9c2efc008fee009334730c4506
This commit is contained in:
		
				
					committed by
					
						
						David Pursehouse
					
				
			
			
				
	
			
			
			
						parent
						
							6842df9ac6
						
					
				
				
					commit
					2c01edd5cb
				
			@@ -24,7 +24,6 @@ import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
 | 
			
		||||
import com.google.gerrit.extensions.restapi.RestModifyView;
 | 
			
		||||
import com.google.gerrit.extensions.webui.UiAction;
 | 
			
		||||
import com.google.gerrit.reviewdb.client.Change;
 | 
			
		||||
import com.google.gerrit.reviewdb.client.PatchSet;
 | 
			
		||||
import com.google.gerrit.reviewdb.server.ReviewDb;
 | 
			
		||||
import com.google.gerrit.server.git.MergeException;
 | 
			
		||||
import com.google.gerrit.server.project.ChangeControl;
 | 
			
		||||
@@ -83,11 +82,11 @@ public class CherryPick implements RestModifyView<RevisionResource, CherryPickIn
 | 
			
		||||
          + input.destination);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    final PatchSet.Id patchSetId = revision.getPatchSet().getId();
 | 
			
		||||
    try {
 | 
			
		||||
      Change.Id cherryPickedChangeId = cherryPickChange.cherryPick(
 | 
			
		||||
          patchSetId, input.message,
 | 
			
		||||
          input.destination, refControl);
 | 
			
		||||
      Change.Id cherryPickedChangeId =
 | 
			
		||||
          cherryPickChange.cherryPick(revision.getChange(),
 | 
			
		||||
              revision.getPatchSet(), input.message, input.destination,
 | 
			
		||||
              refControl);
 | 
			
		||||
      return json.format(cherryPickedChangeId);
 | 
			
		||||
    } catch (InvalidChangeOperationException e) {
 | 
			
		||||
      throw new BadRequestException(e.getMessage());
 | 
			
		||||
 
 | 
			
		||||
@@ -94,31 +94,25 @@ public class CherryPickChange {
 | 
			
		||||
    this.mergeUtilFactory = mergeUtilFactory;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public Change.Id cherryPick(final PatchSet.Id patchSetId,
 | 
			
		||||
  public Change.Id cherryPick(Change change, PatchSet patch,
 | 
			
		||||
      final String message, final String destinationBranch,
 | 
			
		||||
      final RefControl refControl) throws NoSuchChangeException,
 | 
			
		||||
      OrmException, MissingObjectException,
 | 
			
		||||
      IncorrectObjectTypeException, IOException,
 | 
			
		||||
      InvalidChangeOperationException, MergeException {
 | 
			
		||||
 | 
			
		||||
    final Change.Id changeId = patchSetId.getParentKey();
 | 
			
		||||
    final PatchSet patch = db.get().patchSets().get(patchSetId);
 | 
			
		||||
    if (patch == null) {
 | 
			
		||||
      throw new NoSuchChangeException(changeId);
 | 
			
		||||
    }
 | 
			
		||||
    if (destinationBranch == null || destinationBranch.length() == 0) {
 | 
			
		||||
      throw new InvalidChangeOperationException(
 | 
			
		||||
          "Cherry Pick: Destination branch cannot be null or empty");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    Change change = db.get().changes().get(changeId);
 | 
			
		||||
    Project.NameKey project = change.getProject();
 | 
			
		||||
    IdentifiedUser identifiedUser = (IdentifiedUser) currentUser.get();
 | 
			
		||||
    final Repository git;
 | 
			
		||||
    try {
 | 
			
		||||
      git = gitManager.openRepository(project);
 | 
			
		||||
    } catch (RepositoryNotFoundException e) {
 | 
			
		||||
      throw new NoSuchChangeException(changeId, e);
 | 
			
		||||
      throw new NoSuchChangeException(change.getId(), e);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    try {
 | 
			
		||||
@@ -171,9 +165,8 @@ public class CherryPickChange {
 | 
			
		||||
 | 
			
		||||
        List<Change> destChanges =
 | 
			
		||||
            db.get().changes()
 | 
			
		||||
                .byBranchKey(
 | 
			
		||||
                    new Branch.NameKey(db.get().changes().get(changeId).getProject(),
 | 
			
		||||
                        destRef.getName()), changeKey).toList();
 | 
			
		||||
                .byBranchKey(new Branch.NameKey(project, destRef.getName()),
 | 
			
		||||
                    changeKey).toList();
 | 
			
		||||
 | 
			
		||||
        if (destChanges.size() > 1) {
 | 
			
		||||
          throw new InvalidChangeOperationException("Several changes with key "
 | 
			
		||||
@@ -182,13 +175,14 @@ public class CherryPickChange {
 | 
			
		||||
        } else if (destChanges.size() == 1) {
 | 
			
		||||
          // The change key exists on the destination branch. The cherry pick
 | 
			
		||||
          // will be added as a new patch set.
 | 
			
		||||
          return insertPatchSet(git, revWalk, destChanges.get(0), cherryPickCommit,
 | 
			
		||||
              refControl, identifiedUser);
 | 
			
		||||
          return insertPatchSet(git, revWalk, destChanges.get(0),
 | 
			
		||||
              cherryPickCommit, refControl, identifiedUser);
 | 
			
		||||
        } else {
 | 
			
		||||
          // Change key not found on destination branch. We can create a new
 | 
			
		||||
          // change.
 | 
			
		||||
          return createNewChange(git, revWalk, changeKey, project, patchSetId, destRef,
 | 
			
		||||
              cherryPickCommit, refControl, identifiedUser, change.getTopic());
 | 
			
		||||
          return createNewChange(git, revWalk, changeKey, project,
 | 
			
		||||
              patch.getId(), destRef, cherryPickCommit, refControl,
 | 
			
		||||
              identifiedUser, change.getTopic());
 | 
			
		||||
        }
 | 
			
		||||
      } finally {
 | 
			
		||||
        revWalk.release();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user