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());
}
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));
}
}
}
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();
}
}
}
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();
}
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);
}
Aggregations