Optimize cherry pick operation by not re-reading change

Change-Id: If2c307b43788fe9c2efc008fee009334730c4506
This commit is contained in:
David Ostrovsky
2014-11-02 12:24:30 +01:00
committed by David Pursehouse
parent 6842df9ac6
commit 2c01edd5cb
2 changed files with 13 additions and 20 deletions

View File

@@ -24,7 +24,6 @@ import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestModifyView; import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.extensions.webui.UiAction; import com.google.gerrit.extensions.webui.UiAction;
import com.google.gerrit.reviewdb.client.Change; 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.reviewdb.server.ReviewDb;
import com.google.gerrit.server.git.MergeException; import com.google.gerrit.server.git.MergeException;
import com.google.gerrit.server.project.ChangeControl; import com.google.gerrit.server.project.ChangeControl;
@@ -83,11 +82,11 @@ public class CherryPick implements RestModifyView<RevisionResource, CherryPickIn
+ input.destination); + input.destination);
} }
final PatchSet.Id patchSetId = revision.getPatchSet().getId();
try { try {
Change.Id cherryPickedChangeId = cherryPickChange.cherryPick( Change.Id cherryPickedChangeId =
patchSetId, input.message, cherryPickChange.cherryPick(revision.getChange(),
input.destination, refControl); revision.getPatchSet(), input.message, input.destination,
refControl);
return json.format(cherryPickedChangeId); return json.format(cherryPickedChangeId);
} catch (InvalidChangeOperationException e) { } catch (InvalidChangeOperationException e) {
throw new BadRequestException(e.getMessage()); throw new BadRequestException(e.getMessage());

View File

@@ -94,31 +94,25 @@ public class CherryPickChange {
this.mergeUtilFactory = mergeUtilFactory; 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 String message, final String destinationBranch,
final RefControl refControl) throws NoSuchChangeException, final RefControl refControl) throws NoSuchChangeException,
OrmException, MissingObjectException, OrmException, MissingObjectException,
IncorrectObjectTypeException, IOException, IncorrectObjectTypeException, IOException,
InvalidChangeOperationException, MergeException { 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) { if (destinationBranch == null || destinationBranch.length() == 0) {
throw new InvalidChangeOperationException( throw new InvalidChangeOperationException(
"Cherry Pick: Destination branch cannot be null or empty"); "Cherry Pick: Destination branch cannot be null or empty");
} }
Change change = db.get().changes().get(changeId);
Project.NameKey project = change.getProject(); Project.NameKey project = change.getProject();
IdentifiedUser identifiedUser = (IdentifiedUser) currentUser.get(); IdentifiedUser identifiedUser = (IdentifiedUser) currentUser.get();
final Repository git; final Repository git;
try { try {
git = gitManager.openRepository(project); git = gitManager.openRepository(project);
} catch (RepositoryNotFoundException e) { } catch (RepositoryNotFoundException e) {
throw new NoSuchChangeException(changeId, e); throw new NoSuchChangeException(change.getId(), e);
} }
try { try {
@@ -171,9 +165,8 @@ public class CherryPickChange {
List<Change> destChanges = List<Change> destChanges =
db.get().changes() db.get().changes()
.byBranchKey( .byBranchKey(new Branch.NameKey(project, destRef.getName()),
new Branch.NameKey(db.get().changes().get(changeId).getProject(), changeKey).toList();
destRef.getName()), changeKey).toList();
if (destChanges.size() > 1) { if (destChanges.size() > 1) {
throw new InvalidChangeOperationException("Several changes with key " throw new InvalidChangeOperationException("Several changes with key "
@@ -182,13 +175,14 @@ public class CherryPickChange {
} else if (destChanges.size() == 1) { } else if (destChanges.size() == 1) {
// The change key exists on the destination branch. The cherry pick // The change key exists on the destination branch. The cherry pick
// will be added as a new patch set. // will be added as a new patch set.
return insertPatchSet(git, revWalk, destChanges.get(0), cherryPickCommit, return insertPatchSet(git, revWalk, destChanges.get(0),
refControl, identifiedUser); cherryPickCommit, refControl, identifiedUser);
} else { } else {
// Change key not found on destination branch. We can create a new // Change key not found on destination branch. We can create a new
// change. // change.
return createNewChange(git, revWalk, changeKey, project, patchSetId, destRef, return createNewChange(git, revWalk, changeKey, project,
cherryPickCommit, refControl, identifiedUser, change.getTopic()); patch.getId(), destRef, cherryPickCommit, refControl,
identifiedUser, change.getTopic());
} }
} finally { } finally {
revWalk.release(); revWalk.release();