Search in sources :

Example 21 with CodeReviewCommit

use of com.google.gerrit.server.git.CodeReviewCommit in project gerrit by GerritCodeReview.

the class MergeOneOp method updateRepoImpl.

@Override
public void updateRepoImpl(RepoContext ctx) throws IntegrationConflictException, IOException {
    PersonIdent caller = ctx.getIdentifiedUser().newCommitterIdent(args.serverIdent);
    if (args.mergeTip.getCurrentTip() == null) {
        throw new IllegalStateException("cannot merge commit " + toMerge.name() + " onto a null tip; expected at least one fast-forward prior to" + " this operation");
    }
    CodeReviewCommit merged = args.mergeUtil.mergeOneCommit(caller, args.serverIdent, args.rw, ctx.getInserter(), ctx.getRepoView().getConfig(), args.destBranch, args.mergeTip.getCurrentTip(), toMerge);
    if (args.project.is(BooleanProjectConfig.REJECT_EMPTY_COMMIT) && merged.getTree().equals(merged.getParent(0).getTree())) {
        toMerge.setStatusCode(EMPTY_COMMIT);
        return;
    }
    args.mergeTip.moveTipTo(amendGitlink(merged), toMerge);
}
Also used : PersonIdent(org.eclipse.jgit.lib.PersonIdent) CodeReviewCommit(com.google.gerrit.server.git.CodeReviewCommit)

Example 22 with CodeReviewCommit

use of com.google.gerrit.server.git.CodeReviewCommit in project gerrit by GerritCodeReview.

the class CherryPickChange method cherryPick.

/**
 * This function can be called directly to cherry-pick a change (or commit if sourceChange is
 * null) with a few other parameters that are especially useful for cherry-picking a commit that
 * is the revert-of another change.
 *
 * @param sourceChange Change to cherry pick. Can be null, and then the function will only cherry
 *     pick a commit.
 * @param project Project name
 * @param sourceCommit Id of the commit to be cherry picked.
 * @param input Input object for different configurations of cherry pick.
 * @param dest Destination branch for the cherry pick.
 * @param timestamp the current timestamp.
 * @param revertedChange The id of the change that is reverted. This is used for the "revertOf"
 *     field to mark the created cherry pick change as "revertOf" the original change that was
 *     reverted.
 * @param changeIdForNewChange The Change-Id that the new change of the cherry pick will have.
 * @param idForNewChange The ID that the new change of the cherry pick will have. If provided and
 *     the cherry-pick doesn't result in creating a new change, then
 *     InvalidChangeOperationException is thrown.
 * @return Result object that describes the cherry pick.
 * @throws IOException Unable to open repository or read from the database.
 * @throws InvalidChangeOperationException Parent or branch don't exist, or two changes with same
 *     key exist in the branch. Also thrown when idForNewChange is not null but cherry-pick only
 *     creates a new patchset rather than a new change.
 * @throws UpdateException Problem updating the database using batchUpdateFactory.
 * @throws RestApiException Error such as invalid SHA1
 * @throws ConfigInvalidException Can't find account to notify.
 * @throws NoSuchProjectException Can't find project state.
 */
public Result cherryPick(@Nullable Change sourceChange, Project.NameKey project, ObjectId sourceCommit, CherryPickInput input, BranchNameKey dest, Instant timestamp, @Nullable Change.Id revertedChange, @Nullable ObjectId changeIdForNewChange, @Nullable Change.Id idForNewChange, @Nullable Boolean workInProgress) throws IOException, InvalidChangeOperationException, UpdateException, RestApiException, ConfigInvalidException, NoSuchProjectException {
    IdentifiedUser identifiedUser = user.get();
    try (Repository git = gitManager.openRepository(project);
        // before patch sets are updated.
        ObjectInserter oi = git.newObjectInserter();
        ObjectReader reader = oi.newReader();
        CodeReviewRevWalk revWalk = CodeReviewCommit.newRevWalk(reader)) {
        Ref destRef = git.getRefDatabase().exactRef(dest.branch());
        if (destRef == null) {
            throw new InvalidChangeOperationException(String.format("Branch %s does not exist.", dest.branch()));
        }
        RevCommit baseCommit = getBaseCommit(destRef, project.get(), revWalk, input.base);
        CodeReviewCommit commitToCherryPick = revWalk.parseCommit(sourceCommit);
        if (input.parent <= 0 || input.parent > commitToCherryPick.getParentCount()) {
            throw new InvalidChangeOperationException(String.format("Cherry Pick: Parent %s does not exist. Please specify a parent in" + " range [1, %s].", input.parent, commitToCherryPick.getParentCount()));
        }
        // If the commit message is not set, the commit message of the source commit will be used.
        String commitMessage = Strings.nullToEmpty(input.message);
        commitMessage = commitMessage.isEmpty() ? commitToCherryPick.getFullMessage() : commitMessage;
        String destChangeId = getDestinationChangeId(commitMessage, changeIdForNewChange);
        ChangeData destChange = null;
        if (destChangeId != null) {
            // If "idForNewChange" is not null we must fail, since we are not expecting an already
            // existing change.
            destChange = getDestChangeWithVerification(destChangeId, dest, idForNewChange != null);
        }
        if (changeIdForNewChange != null) {
            // If Change-Id was explicitly provided for the new change, override the value in commit
            // message.
            commitMessage = ChangeIdUtil.insertId(commitMessage, changeIdForNewChange, true);
        } else if (destChangeId == null) {
            // If commit message did not specify Change-Id, generate a new one and insert to the
            // message.
            commitMessage = ChangeIdUtil.insertId(commitMessage, CommitMessageUtil.generateChangeId(), true);
        }
        commitMessage = CommitMessageUtil.checkAndSanitizeCommitMessage(commitMessage);
        CodeReviewCommit cherryPickCommit;
        ProjectState projectState = projectCache.get(dest.project()).orElseThrow(noSuchProject(dest.project()));
        PersonIdent committerIdent = identifiedUser.newCommitterIdent(timestamp, serverTimeZone);
        try {
            MergeUtil mergeUtil;
            if (input.allowConflicts) {
                // allowConflicts requires to use content merge
                mergeUtil = mergeUtilFactory.create(projectState, true);
            } else {
                // use content merge only if it's configured on the project
                mergeUtil = mergeUtilFactory.create(projectState);
            }
            cherryPickCommit = mergeUtil.createCherryPickFromCommit(oi, git.getConfig(), baseCommit, commitToCherryPick, committerIdent, commitMessage, revWalk, input.parent - 1, input.allowEmpty, input.allowConflicts);
            oi.flush();
        } catch (MergeIdenticalTreeException | MergeConflictException e) {
            throw new IntegrationConflictException("Cherry pick failed: " + e.getMessage(), e);
        }
        try (BatchUpdate bu = batchUpdateFactory.create(project, identifiedUser, timestamp)) {
            bu.setRepository(git, revWalk, oi);
            bu.setNotify(resolveNotify(input));
            Change.Id changeId;
            String newTopic = null;
            if (input.topic != null) {
                newTopic = Strings.emptyToNull(input.topic.trim());
            }
            if (newTopic == null && sourceChange != null && !Strings.isNullOrEmpty(sourceChange.getTopic())) {
                newTopic = sourceChange.getTopic() + "-" + dest.shortName();
            }
            if (destChange != null) {
                // The change key exists on the destination branch. The cherry pick
                // will be added as a new patch set.
                changeId = insertPatchSet(bu, git, destChange.notes(), cherryPickCommit, sourceChange, newTopic, input, workInProgress);
            } else {
                // Change key not found on destination branch. We can create a new
                // change.
                changeId = createNewChange(bu, cherryPickCommit, dest.branch(), newTopic, project, sourceChange, sourceCommit, input, revertedChange, idForNewChange, workInProgress);
            }
            bu.execute();
            return Result.create(changeId, cherryPickCommit.getFilesWithGitConflicts());
        }
    }
}
Also used : InvalidChangeOperationException(com.google.gerrit.server.project.InvalidChangeOperationException) CodeReviewRevWalk(com.google.gerrit.server.git.CodeReviewCommit.CodeReviewRevWalk) Change(com.google.gerrit.entities.Change) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) CodeReviewCommit(com.google.gerrit.server.git.CodeReviewCommit) ChangeData(com.google.gerrit.server.query.change.ChangeData) MergeIdenticalTreeException(com.google.gerrit.server.submit.MergeIdenticalTreeException) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) MergeConflictException(com.google.gerrit.extensions.restapi.MergeConflictException) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) PersonIdent(org.eclipse.jgit.lib.PersonIdent) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) MergeUtil(com.google.gerrit.server.git.MergeUtil) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ProjectState(com.google.gerrit.server.project.ProjectState) IntegrationConflictException(com.google.gerrit.server.submit.IntegrationConflictException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 23 with CodeReviewCommit

use of com.google.gerrit.server.git.CodeReviewCommit in project gerrit by GerritCodeReview.

the class CodeReviewCommitTest method checkSerializable_withStatusMessage.

@Test
public void checkSerializable_withStatusMessage() throws Exception {
    CodeReviewCommit commit = new CodeReviewCommit(createCommit());
    commit.setStatusMessage("Status");
    CodeReviewCommit deserializedCommit = serializeAndReadBack(commit);
    assertThat(deserializedCommit).isEqualTo(commit);
    assertThat(deserializedCommit.getStatusMessage().get()).isEqualTo("Status");
}
Also used : CodeReviewCommit(com.google.gerrit.server.git.CodeReviewCommit) Test(org.junit.Test)

Example 24 with CodeReviewCommit

use of com.google.gerrit.server.git.CodeReviewCommit in project gerrit by GerritCodeReview.

the class CodeReviewCommitTest method serializeAndReadBack.

@SuppressWarnings("BanSerializableRead")
private CodeReviewCommit serializeAndReadBack(CodeReviewCommit codeReviewCommit) throws Exception {
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bos)) {
        out.writeObject(codeReviewCommit);
        out.flush();
        try (ByteArrayInputStream fileIn = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream in = new ObjectInputStream(fileIn)) {
            return (CodeReviewCommit) in.readObject();
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) CodeReviewCommit(com.google.gerrit.server.git.CodeReviewCommit) ObjectInputStream(java.io.ObjectInputStream)

Example 25 with CodeReviewCommit

use of com.google.gerrit.server.git.CodeReviewCommit in project gerrit by GerritCodeReview.

the class CodeReviewCommitTest method checkSerializable_emptyStatusMessage.

@Test
public void checkSerializable_emptyStatusMessage() throws Exception {
    CodeReviewCommit commit = new CodeReviewCommit(createCommit());
    CodeReviewCommit deserializedCommit = serializeAndReadBack(commit);
    assertThat(deserializedCommit).isEqualTo(commit);
    assertThat(deserializedCommit.getStatusMessage().isPresent()).isFalse();
}
Also used : CodeReviewCommit(com.google.gerrit.server.git.CodeReviewCommit) Test(org.junit.Test)

Aggregations

CodeReviewCommit (com.google.gerrit.server.git.CodeReviewCommit)45 ArrayList (java.util.ArrayList)13 ObjectId (org.eclipse.jgit.lib.ObjectId)11 RevCommit (org.eclipse.jgit.revwalk.RevCommit)11 IOException (java.io.IOException)10 StorageException (com.google.gerrit.exceptions.StorageException)9 PersonIdent (org.eclipse.jgit.lib.PersonIdent)9 BranchNameKey (com.google.gerrit.entities.BranchNameKey)7 Change (com.google.gerrit.entities.Change)7 PatchSet (com.google.gerrit.entities.PatchSet)7 CodeReviewRevWalk (com.google.gerrit.server.git.CodeReviewCommit.CodeReviewRevWalk)7 ImmutableList (com.google.common.collect.ImmutableList)6 Repository (org.eclipse.jgit.lib.Repository)6 GerritPersonIdent (com.google.gerrit.server.GerritPersonIdent)5 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)5 ProjectState (com.google.gerrit.server.project.ProjectState)5 OpenRepo (com.google.gerrit.server.submit.MergeOpRepoManager.OpenRepo)5 SubmoduleSubscription (com.google.gerrit.entities.SubmoduleSubscription)4 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)4 MergeConflictException (com.google.gerrit.extensions.restapi.MergeConflictException)4