Search in sources :

Example 36 with PatchSet

use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.

the class ChangeNotesStateTest method serializePatchSets.

@Test
public void serializePatchSets() throws Exception {
    PatchSet ps1 = PatchSet.builder().id(PatchSet.id(ID, 1)).commitId(ObjectId.fromString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")).uploader(Account.id(2000)).createdOn(cols.createdOn()).build();
    Entities.PatchSet ps1Proto = PatchSetProtoConverter.INSTANCE.toProto(ps1);
    ByteString ps1Bytes = Protos.toByteString(ps1Proto);
    assertThat(ps1Bytes.size()).isEqualTo(66);
    PatchSet ps2 = PatchSet.builder().id(PatchSet.id(ID, 2)).commitId(ObjectId.fromString("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")).uploader(Account.id(3000)).createdOn(cols.lastUpdatedOn()).build();
    Entities.PatchSet ps2Proto = PatchSetProtoConverter.INSTANCE.toProto(ps2);
    ByteString ps2Bytes = Protos.toByteString(ps2Proto);
    assertThat(ps2Bytes.size()).isEqualTo(66);
    assertThat(ps2Bytes).isNotEqualTo(ps1Bytes);
    assertRoundTrip(newBuilder().patchSets(ImmutableMap.of(ps2.id(), ps2, ps1.id(), ps1).entrySet()).build(), ChangeNotesStateProto.newBuilder().setMetaId(SHA_BYTES).setChangeId(ID.get()).setColumns(colsProto).addPatchSet(ps2Proto).addPatchSet(ps1Proto).build());
}
Also used : ByteString(com.google.protobuf.ByteString) PatchSet(com.google.gerrit.entities.PatchSet) Entities(com.google.gerrit.proto.Entities) Test(org.junit.Test)

Example 37 with PatchSet

use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.

the class ChangeEditUtil method byChange.

/**
 * Retrieve edit for a change and the given user.
 *
 * <p>At most one change edit can exist per user and change.
 *
 * @param notes change notes of change to retrieve change edits for.
 * @param user user to retrieve edits as.
 * @return edit for this change for this user, if present.
 * @throws AuthException if this is not a logged-in user.
 * @throws IOException if an error occurs.
 */
public Optional<ChangeEdit> byChange(ChangeNotes notes, CurrentUser user) throws AuthException, IOException {
    if (!user.isIdentifiedUser()) {
        throw new AuthException("Authentication required");
    }
    IdentifiedUser u = user.asIdentifiedUser();
    Change change = notes.getChange();
    try (Repository repo = gitManager.openRepository(change.getProject())) {
        int n = change.currentPatchSetId().get();
        String[] refNames = new String[n];
        for (int i = n; i > 0; i--) {
            refNames[i - 1] = RefNames.refsEdit(u.getAccountId(), change.getId(), PatchSet.id(change.getId(), i));
        }
        Ref ref = repo.getRefDatabase().firstExactRef(refNames);
        if (ref == null) {
            return Optional.empty();
        }
        try (RevWalk rw = new RevWalk(repo)) {
            RevCommit commit = rw.parseCommit(ref.getObjectId());
            PatchSet basePs = getBasePatchSet(notes, ref);
            return Optional.of(new ChangeEdit(change, ref.getName(), commit, basePs));
        }
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) AuthException(com.google.gerrit.extensions.restapi.AuthException) PatchSet(com.google.gerrit.entities.PatchSet) Change(com.google.gerrit.entities.Change) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 38 with PatchSet

use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.

the class ChangeEditUtil method publish.

/**
 * Promote change edit to patch set, by squashing the edit into its parent.
 *
 * @param updateFactory factory for creating updates.
 * @param notes the {@code ChangeNotes} of the change to which the change edit belongs
 * @param user the current user
 * @param edit change edit to publish
 * @param notify Notify handling that defines to whom email notifications should be sent after the
 *     change edit is published.
 */
public void publish(BatchUpdate.Factory updateFactory, ChangeNotes notes, CurrentUser user, ChangeEdit edit, NotifyResolver.Result notify) throws IOException, RestApiException, UpdateException {
    Change change = edit.getChange();
    try (Repository repo = gitManager.openRepository(change.getProject());
        ObjectInserter oi = repo.newObjectInserter();
        ObjectReader reader = oi.newReader();
        RevWalk rw = new RevWalk(reader)) {
        PatchSet basePatchSet = edit.getBasePatchSet();
        if (!basePatchSet.id().equals(change.currentPatchSetId())) {
            throw new ResourceConflictException("only edit for current patch set can be published");
        }
        RevCommit squashed = squashEdit(rw, oi, edit.getEditCommit(), basePatchSet);
        PatchSet.Id psId = ChangeUtil.nextPatchSetId(repo, change.currentPatchSetId());
        PatchSetInserter inserter = patchSetInserterFactory.create(notes, psId, squashed).setSendEmail(!change.isWorkInProgress());
        StringBuilder message = new StringBuilder("Patch Set ").append(inserter.getPatchSetId().get()).append(": ");
        // Previously checked that the base patch set is the current patch set.
        ObjectId prior = basePatchSet.commitId();
        ChangeKind kind = changeKindCache.getChangeKind(change.getProject(), rw, repo.getConfig(), prior, squashed);
        if (kind == ChangeKind.NO_CODE_CHANGE) {
            message.append("Commit message was updated.");
            inserter.setDescription("Edit commit message");
        } else {
            message.append("Published edit on patch set ").append(basePatchSet.number()).append(".");
        }
        try (BatchUpdate bu = updateFactory.create(change.getProject(), user, TimeUtil.now())) {
            bu.setRepository(repo, rw, oi);
            bu.setNotify(notify);
            bu.addOp(change.getId(), inserter.setMessage(message.toString()));
            bu.addOp(change.getId(), new BatchUpdateOp() {

                @Override
                public void updateRepo(RepoContext ctx) throws Exception {
                    ctx.addRefUpdate(edit.getEditCommit().copy(), ObjectId.zeroId(), edit.getRefName());
                }
            });
            bu.execute();
        }
    }
}
Also used : RepoContext(com.google.gerrit.server.update.RepoContext) ObjectId(org.eclipse.jgit.lib.ObjectId) PatchSet(com.google.gerrit.entities.PatchSet) Change(com.google.gerrit.entities.Change) RevWalk(org.eclipse.jgit.revwalk.RevWalk) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) UpdateException(com.google.gerrit.server.update.UpdateException) AuthException(com.google.gerrit.extensions.restapi.AuthException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) LockFailureException(com.google.gerrit.git.LockFailureException) StorageException(com.google.gerrit.exceptions.StorageException) IOException(java.io.IOException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Repository(org.eclipse.jgit.lib.Repository) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) PatchSetInserter(com.google.gerrit.server.change.PatchSetInserter) ObjectReader(org.eclipse.jgit.lib.ObjectReader) RevCommit(org.eclipse.jgit.revwalk.RevCommit) ChangeKind(com.google.gerrit.extensions.client.ChangeKind) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp)

Example 39 with PatchSet

use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.

the class ChangeEditModifier method merge.

private static ObjectId merge(Repository repository, ChangeEdit changeEdit, ObjectId newTreeId) throws IOException, MergeConflictException {
    PatchSet basePatchSet = changeEdit.getBasePatchSet();
    ObjectId basePatchSetCommitId = basePatchSet.commitId();
    ObjectId editCommitId = changeEdit.getEditCommit();
    ThreeWayMerger threeWayMerger = MergeStrategy.RESOLVE.newMerger(repository, true);
    threeWayMerger.setBase(basePatchSetCommitId);
    boolean successful = threeWayMerger.merge(newTreeId, editCommitId);
    if (!successful) {
        throw new MergeConflictException("The existing change edit could not be merged with another tree.");
    }
    return threeWayMerger.getResultTreeId();
}
Also used : MergeConflictException(com.google.gerrit.extensions.restapi.MergeConflictException) ObjectId(org.eclipse.jgit.lib.ObjectId) PatchSet(com.google.gerrit.entities.PatchSet) ThreeWayMerger(org.eclipse.jgit.merge.ThreeWayMerger)

Example 40 with PatchSet

use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.

the class ChangeEditModifier method rebaseEdit.

/**
 * Rebase change edit on latest patch set
 *
 * @param repository the affected Git repository
 * @param notes the {@link ChangeNotes} of the change whose change edit should be rebased
 * @throws AuthException if the user isn't authenticated or not allowed to use change edits
 * @throws InvalidChangeOperationException if a change edit doesn't exist for the specified
 *     change, the change edit is already based on the latest patch set, or the change represents
 *     the root commit
 */
public void rebaseEdit(Repository repository, ChangeNotes notes) throws AuthException, InvalidChangeOperationException, IOException, PermissionBackendException, ResourceConflictException {
    assertCanEdit(notes);
    Optional<ChangeEdit> optionalChangeEdit = lookupChangeEdit(notes);
    if (!optionalChangeEdit.isPresent()) {
        throw new InvalidChangeOperationException(String.format("No change edit exists for change %s", notes.getChangeId()));
    }
    ChangeEdit changeEdit = optionalChangeEdit.get();
    PatchSet currentPatchSet = lookupCurrentPatchSet(notes);
    if (isBasedOn(changeEdit, currentPatchSet)) {
        throw new InvalidChangeOperationException(String.format("Change edit for change %s is already based on latest patch set %s", notes.getChangeId(), currentPatchSet.id()));
    }
    rebase(repository, changeEdit, currentPatchSet);
}
Also used : InvalidChangeOperationException(com.google.gerrit.server.project.InvalidChangeOperationException) PatchSet(com.google.gerrit.entities.PatchSet)

Aggregations

PatchSet (com.google.gerrit.entities.PatchSet)123 Change (com.google.gerrit.entities.Change)61 Test (org.junit.Test)48 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)41 ObjectId (org.eclipse.jgit.lib.ObjectId)35 RevCommit (org.eclipse.jgit.revwalk.RevCommit)29 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)28 Project (com.google.gerrit.entities.Project)25 StorageException (com.google.gerrit.exceptions.StorageException)25 Repository (org.eclipse.jgit.lib.Repository)22 IOException (java.io.IOException)20 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)19 ChangeData (com.google.gerrit.server.query.change.ChangeData)18 HumanComment (com.google.gerrit.entities.HumanComment)16 RevWalk (org.eclipse.jgit.revwalk.RevWalk)16 Inject (com.google.inject.Inject)14 Map (java.util.Map)14 List (java.util.List)13 ImmutableList (com.google.common.collect.ImmutableList)12 AuthException (com.google.gerrit.extensions.restapi.AuthException)11