Class RebaseUtil

java.lang.Object
com.google.gerrit.server.change.RebaseUtil

public class RebaseUtil extends Object
Utility methods related to rebasing changes.
  • Method Details

    • checkCanRebaseOnBehalfOf

      public void checkCanRebaseOnBehalfOf(RevisionResource rsrc, RebaseInput rebaseInput) throws IOException, PermissionBackendException, BadRequestException, ResourceConflictException
      Checks that the uploader has permissions to create a new patch set as the current user which can be used for BatchUpdate to do the rebase on behalf of the uploader.

      The following permissions are required for the uploader:

      • The Read permission that allows to see the change.
      • The Push permission that allows upload.
      • The Add Patch Set permission, required if the change is owned by another user (change owners implicitly have this permission).
      • The Forge Author permission if the patch set that is rebased has a forged author (author != uploader).
      • The Forge Server permission if the patch set that is rebased has the server identity as the author.

      Usually the uploader should have all these permission since they were already required for the original upload, but there is the edge case that the uploader had the permission when doing the original upload and then the permission was revoked.

      Note that patch sets with a forged committer (committer != uploader) can be rebased on behalf of the uploader, even if the uploader doesn't have the Forge Committer permission. This is because on rebase on behalf of the uploader the uploader will become the committer of the new rebased patch set, hence for the rebased patch set the committer is no longer forged (committer == uploader) and hence the Forge Committer permission is not required.

      Note that the Rebase permission is not required for the uploader since the Rebase permission is specifically about allowing a user to do a rebase via the web UI by clicking on the REBASE button and the uploader is not clicking on this button.

      The permissions of the uploader are checked explicitly here so that we can return a 409 Conflict response with a proper error message if they are missing (the error message says that the permission is missing for the uploader). The normal code path also checks these permission but the exception thrown there would result in a 403 Forbidden response and the error message would wrongly look like the caller (i.e. the rebaser) is missing the permission.

      Note that this method doesn't check permissions for the rebaser (aka the impersonating user aka the calling user). Callers should check the permissions for the rebaser before calling this method.

      Parameters:
      rsrc - the revision resource that should be rebased
      rebaseInput - the request input containing options for the rebase
      Throws:
      IOException
      PermissionBackendException
      BadRequestException
      ResourceConflictException
    • verifyRebasePreconditions

      public void verifyRebasePreconditions(org.eclipse.jgit.revwalk.RevWalk rw, ChangeNotes changeNotes, PatchSet patchSet) throws ResourceConflictException, IOException
      Checks whether the given change fulfills all preconditions to be rebased.

      This method does not check whether the calling user is allowed to rebase the change.

      Throws:
      ResourceConflictException
      IOException
    • hasAtLeastOneParent

      public static boolean hasAtLeastOneParent(org.eclipse.jgit.revwalk.RevWalk rw, PatchSet ps) throws IOException
      Throws:
      IOException
    • canRebase

      public boolean canRebase(PatchSet patchSet, BranchNameKey dest, org.eclipse.jgit.lib.Repository git, org.eclipse.jgit.revwalk.RevWalk rw)
    • parseBase

      public RebaseUtil.Base parseBase(RevisionResource rsrc, String base)
    • parseOrFindBaseRevision

      public org.eclipse.jgit.lib.ObjectId parseOrFindBaseRevision(org.eclipse.jgit.lib.Repository git, org.eclipse.jgit.revwalk.RevWalk rw, PermissionBackend permissionBackend, RevisionResource rsrc, RebaseInput rebaseInput, boolean verifyNeedsRebase) throws RestApiException, IOException, PermissionBackendException
      Parse or find the commit onto which a patch set should be rebased.

      If a rebaseInput.base is provided, parse it. Otherwise, finds the latest patch set of the change corresponding to this commit's parent, or the destination branch tip in the case where the parent's change is merged.

      Parameters:
      git - the repository.
      rw - the RevWalk.
      permissionBackend - to check base reading permissions with.
      rsrc - to find the base for
      rebaseInput - to optionally parse the base from.
      verifyNeedsRebase - whether to verify if the change base is not already up to date
      Returns:
      the commit onto which the patch set should be rebased.
      Throws:
      RestApiException - if rebase is not possible.
      IOException - if accessing the repository fails.
      PermissionBackendException - if the user don't have permissions to read the base change.
    • findBaseRevision

      public org.eclipse.jgit.lib.ObjectId findBaseRevision(PatchSet patchSet, BranchNameKey destBranch, org.eclipse.jgit.lib.Repository git, org.eclipse.jgit.revwalk.RevWalk rw, boolean verifyNeedsRebase) throws RestApiException, IOException
      Find the commit onto which a patch set should be rebased.

      This is defined as the latest patch set of the change corresponding to this commit's parent, or the destination branch tip in the case where the parent's change is merged.

      Parameters:
      patchSet - patch set for which the new base commit should be found.
      destBranch - the destination branch.
      git - the repository.
      rw - the RevWalk.
      verifyNeedsRebase - whether to verify if the change base is not already up to date
      Returns:
      the commit onto which the patch set should be rebased.
      Throws:
      RestApiException - if rebase is not possible.
      IOException - if accessing the repository fails.
    • getRebaseOp

      public RebaseChangeOp getRebaseOp(org.eclipse.jgit.revwalk.RevWalk rw, RevisionResource revRsrc, RebaseInput input, org.eclipse.jgit.lib.ObjectId baseRev, IdentifiedUser rebaseAsUser) throws ResourceConflictException, PermissionBackendException, IOException
      Throws:
      ResourceConflictException
      PermissionBackendException
      IOException
    • getRebaseOp

      public RebaseChangeOp getRebaseOp(org.eclipse.jgit.revwalk.RevWalk rw, RevisionResource revRsrc, RebaseInput input, Change.Id baseChange, IdentifiedUser rebaseAsUser) throws ResourceConflictException, PermissionBackendException, IOException
      Throws:
      ResourceConflictException
      PermissionBackendException
      IOException