use of com.google.gerrit.reviewdb.client.Comment in project gerrit by GerritCodeReview.
the class DeleteCommentRewriter method rewriteCommit.
/**
* Rewrites one commit.
*
* @param originalCommit the original commit to be rewritten.
* @param parentCommit the parent of the new commit.
* @param inserter the {@code ObjectInserter} for the rewrite process.
* @param reader the {@code ObjectReader} for the rewrite process.
* @param putInComments the comments put in by this commit.
* @param deletedComments the comments deleted by this commit.
* @return the {@code objectId} of the new commit.
* @throws IOException
* @throws ConfigInvalidException
*/
private ObjectId rewriteCommit(RevCommit originalCommit, RevCommit parentCommit, ObjectInserter inserter, ObjectReader reader, List<Comment> putInComments, List<Comment> deletedComments) throws IOException, ConfigInvalidException {
RevisionNoteMap<ChangeRevisionNote> revNotesMap = RevisionNoteMap.parse(noteUtil, changeId, reader, NoteMap.read(reader, parentCommit), PUBLISHED);
RevisionNoteBuilder.Cache cache = new RevisionNoteBuilder.Cache(revNotesMap);
for (Comment c : putInComments) {
cache.get(new RevId(c.revId)).putComment(c);
}
for (Comment c : deletedComments) {
cache.get(new RevId(c.revId)).deleteComment(c.key);
}
Map<RevId, RevisionNoteBuilder> builders = cache.getBuilders();
for (Map.Entry<RevId, RevisionNoteBuilder> entry : builders.entrySet()) {
ObjectId objectId = ObjectId.fromString(entry.getKey().get());
byte[] data = entry.getValue().build(noteUtil, noteUtil.getWriteJson());
if (data.length == 0) {
revNotesMap.noteMap.remove(objectId);
} else {
revNotesMap.noteMap.set(objectId, inserter.insert(OBJ_BLOB, data));
}
}
CommitBuilder cb = new CommitBuilder();
cb.setParentId(parentCommit);
cb.setTreeId(revNotesMap.noteMap.writeTree(inserter));
cb.setMessage(originalCommit.getFullMessage());
cb.setCommitter(originalCommit.getCommitterIdent());
cb.setAuthor(originalCommit.getAuthorIdent());
cb.setEncoding(originalCommit.getEncoding());
return inserter.insert(cb);
}
use of com.google.gerrit.reviewdb.client.Comment in project gerrit by GerritCodeReview.
the class DeleteCommentRewriter method rewriteCommitHistory.
@Override
public ObjectId rewriteCommitHistory(RevWalk revWalk, ObjectInserter inserter, ObjectId currTip) throws IOException, ConfigInvalidException, OrmException {
checkArgument(!currTip.equals(ObjectId.zeroId()));
// Walk from the first commit of the branch.
revWalk.reset();
revWalk.markStart(revWalk.parseCommit(currTip));
revWalk.sort(RevSort.REVERSE);
ObjectReader reader = revWalk.getObjectReader();
// The first commit will not be rewritten.
RevCommit newTipCommit = revWalk.next();
Map<String, Comment> parentComments = getPublishedComments(noteUtil, changeId, reader, NoteMap.read(reader, newTipCommit));
boolean rewrite = false;
RevCommit originalCommit;
while ((originalCommit = revWalk.next()) != null) {
NoteMap noteMap = NoteMap.read(reader, originalCommit);
Map<String, Comment> currComments = getPublishedComments(noteUtil, changeId, reader, noteMap);
if (!rewrite && currComments.containsKey(uuid)) {
rewrite = true;
}
if (!rewrite) {
parentComments = currComments;
newTipCommit = originalCommit;
continue;
}
List<Comment> putInComments = getPutInComments(parentComments, currComments);
List<Comment> deletedComments = getDeletedComments(parentComments, currComments);
newTipCommit = revWalk.parseCommit(rewriteCommit(originalCommit, newTipCommit, inserter, reader, putInComments, deletedComments));
parentComments = currComments;
}
return newTipCommit;
}
use of com.google.gerrit.reviewdb.client.Comment in project gerrit by GerritCodeReview.
the class ChangeUpdate method storeRevisionNotes.
/** @return the tree id for the updated tree */
private ObjectId storeRevisionNotes(RevWalk rw, ObjectInserter inserter, ObjectId curr) throws ConfigInvalidException, OrmException, IOException {
if (comments.isEmpty() && pushCert == null) {
return null;
}
RevisionNoteMap<ChangeRevisionNote> rnm = getRevisionNoteMap(rw, curr);
RevisionNoteBuilder.Cache cache = new RevisionNoteBuilder.Cache(rnm);
for (Comment c : comments) {
c.tag = tag;
cache.get(new RevId(c.revId)).putComment(c);
}
if (pushCert != null) {
checkState(commit != null);
cache.get(new RevId(commit)).setPushCertificate(pushCert);
}
Map<RevId, RevisionNoteBuilder> builders = cache.getBuilders();
checkComments(rnm.revisionNotes, builders);
for (Map.Entry<RevId, RevisionNoteBuilder> e : builders.entrySet()) {
ObjectId data = inserter.insert(OBJ_BLOB, e.getValue().build(noteUtil, noteUtil.getWriteJson()));
rnm.noteMap.set(ObjectId.fromString(e.getKey().get()), data);
}
return rnm.noteMap.writeTree(inserter);
}
use of com.google.gerrit.reviewdb.client.Comment in project gerrit by GerritCodeReview.
the class ChangeNotesTest method multipleUpdatesIncludingComments.
@Test
public void multipleUpdatesIncludingComments() throws Exception {
Change c = newChange();
ChangeUpdate update1 = newUpdate(c, otherUser);
String uuid1 = "uuid1";
String message1 = "comment 1";
CommentRange range1 = new CommentRange(1, 1, 2, 1);
Timestamp time1 = TimeUtil.nowTs();
PatchSet.Id psId = c.currentPatchSetId();
RevCommit tipCommit;
try (NoteDbUpdateManager updateManager = updateManagerFactory.create(project)) {
Comment comment1 = newComment(psId, "file1", uuid1, range1, range1.getEndLine(), otherUser, null, time1, message1, (short) 0, "abcd1234abcd1234abcd1234abcd1234abcd1234", false);
update1.setPatchSetId(psId);
update1.putComment(Status.PUBLISHED, comment1);
updateManager.add(update1);
ChangeUpdate update2 = newUpdate(c, otherUser);
update2.putApproval("Code-Review", (short) 2);
updateManager.add(update2);
updateManager.execute();
}
ChangeNotes notes = newNotes(c);
ObjectId tip = notes.getRevision();
tipCommit = rw.parseCommit(tip);
RevCommit commitWithApprovals = tipCommit;
assertThat(commitWithApprovals).isNotNull();
RevCommit commitWithComments = commitWithApprovals.getParent(0);
assertThat(commitWithComments).isNotNull();
try (ChangeNotesRevWalk rw = ChangeNotesCommit.newRevWalk(repo)) {
ChangeNotesParser notesWithComments = new ChangeNotesParser(c.getId(), commitWithComments.copy(), rw, noteUtil, args.metrics);
ChangeNotesState state = notesWithComments.parseAll();
assertThat(state.approvals()).isEmpty();
assertThat(state.publishedComments()).hasSize(1);
}
try (ChangeNotesRevWalk rw = ChangeNotesCommit.newRevWalk(repo)) {
ChangeNotesParser notesWithApprovals = new ChangeNotesParser(c.getId(), commitWithApprovals.copy(), rw, noteUtil, args.metrics);
ChangeNotesState state = notesWithApprovals.parseAll();
assertThat(state.approvals()).hasSize(1);
assertThat(state.publishedComments()).hasSize(1);
}
}
use of com.google.gerrit.reviewdb.client.Comment in project gerrit by GerritCodeReview.
the class ChangeNotesTest method patchLineCommentEmptyFilename.
@Test
public void patchLineCommentEmptyFilename() throws Exception {
Change c = newChange();
ChangeUpdate update = newUpdate(c, otherUser);
PatchSet.Id psId = c.currentPatchSetId();
RevId revId = new RevId("abcd1234abcd1234abcd1234abcd1234abcd1234");
CommentRange range = new CommentRange(1, 2, 3, 4);
Comment comment = newComment(psId, "", "uuid", range, range.getEndLine(), otherUser, null, TimeUtil.nowTs(), "message", (short) 1, revId.get(), false);
update.setPatchSetId(psId);
update.putComment(Status.PUBLISHED, comment);
update.commit();
ChangeNotes notes = newNotes(c);
assertThat(notes.getComments()).isEqualTo(ImmutableListMultimap.of(revId, comment));
}
Aggregations