Search in sources :

Example 1 with FixReplacement

use of com.google.gerrit.reviewdb.client.FixReplacement in project gerrit by GerritCodeReview.

the class FixReplacementInterpreter method toTreeModifications.

/**
   * Transforms the given {@code FixReplacement}s into {@code TreeModification}s.
   *
   * @param repository the affected Git repository
   * @param projectState the affected project
   * @param patchSetCommitId the patch set which should be modified
   * @param fixReplacements the replacements which should be applied
   * @return a list of {@code TreeModification}s representing the given replacements
   * @throws ResourceNotFoundException if a file to which one of the replacements refers doesn't
   *     exist
   * @throws ResourceConflictException if the replacements can't be transformed into {@code
   *     TreeModification}s
   */
public List<TreeModification> toTreeModifications(Repository repository, ProjectState projectState, ObjectId patchSetCommitId, List<FixReplacement> fixReplacements) throws ResourceNotFoundException, IOException, ResourceConflictException {
    checkNotNull(fixReplacements, "Fix replacements must not be null");
    Map<String, List<FixReplacement>> fixReplacementsPerFilePath = fixReplacements.stream().collect(Collectors.groupingBy(fixReplacement -> fixReplacement.path));
    List<TreeModification> treeModifications = new ArrayList<>();
    for (Map.Entry<String, List<FixReplacement>> entry : fixReplacementsPerFilePath.entrySet()) {
        TreeModification treeModification = toTreeModification(repository, projectState, patchSetCommitId, entry.getKey(), entry.getValue());
        treeModifications.add(treeModification);
    }
    return treeModifications;
}
Also used : ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) TreeModification(com.google.gerrit.server.edit.tree.TreeModification) Inject(com.google.inject.Inject) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) RawInputUtil(com.google.gerrit.common.RawInputUtil) ChangeFileContentModification(com.google.gerrit.server.edit.tree.ChangeFileContentModification) ProjectState(com.google.gerrit.server.project.ProjectState) FixReplacement(com.google.gerrit.reviewdb.client.FixReplacement) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ObjectId(org.eclipse.jgit.lib.ObjectId) Comment(com.google.gerrit.reviewdb.client.Comment) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult) List(java.util.List) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) FileContentUtil(com.google.gerrit.server.change.FileContentUtil) Map(java.util.Map) Comparator(java.util.Comparator) Repository(org.eclipse.jgit.lib.Repository) Singleton(com.google.inject.Singleton) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) TreeModification(com.google.gerrit.server.edit.tree.TreeModification) Map(java.util.Map)

Example 2 with FixReplacement

use of com.google.gerrit.reviewdb.client.FixReplacement in project gerrit by GerritCodeReview.

the class FixReplacementInterpreterTest method lineSeparatorCanBeChanged.

@Test
public void lineSeparatorCanBeChanged() throws Exception {
    FixReplacement fixReplacement = new FixReplacement(filePath1, new Range(2, 11, 3, 0), "\r");
    mockFileContent(filePath1, "First line\nSecond line\nThird line\n");
    replay(fileContentUtil);
    List<TreeModification> treeModifications = toTreeModifications(fixReplacement);
    assertThatList(treeModifications).onlyElement().asChangeFileContentModification().newContent().isEqualTo("First line\nSecond line\rThird line\n");
}
Also used : TreeModification(com.google.gerrit.server.edit.tree.TreeModification) FixReplacement(com.google.gerrit.reviewdb.client.FixReplacement) Range(com.google.gerrit.reviewdb.client.Comment.Range) Test(org.junit.Test)

Example 3 with FixReplacement

use of com.google.gerrit.reviewdb.client.FixReplacement in project gerrit by GerritCodeReview.

the class FixReplacementInterpreterTest method replacementsMayTouch.

@Test
public void replacementsMayTouch() throws Exception {
    FixReplacement fixReplacement1 = new FixReplacement(filePath1, new Range(1, 6, 2, 7), "modified ");
    FixReplacement fixReplacement2 = new FixReplacement(filePath1, new Range(2, 7, 3, 5), "content");
    mockFileContent(filePath1, "First line\nSecond line\nThird line\n");
    replay(fileContentUtil);
    List<TreeModification> treeModifications = toTreeModifications(fixReplacement1, fixReplacement2);
    assertThatList(treeModifications).onlyElement().asChangeFileContentModification().newContent().isEqualTo("First modified content line\n");
}
Also used : TreeModification(com.google.gerrit.server.edit.tree.TreeModification) FixReplacement(com.google.gerrit.reviewdb.client.FixReplacement) Range(com.google.gerrit.reviewdb.client.Comment.Range) Test(org.junit.Test)

Example 4 with FixReplacement

use of com.google.gerrit.reviewdb.client.FixReplacement in project gerrit by GerritCodeReview.

the class FixReplacementInterpreterTest method replacementsMustNotReferToNotExistingOffsetOfLastLine.

@Test
public void replacementsMustNotReferToNotExistingOffsetOfLastLine() throws Exception {
    FixReplacement fixReplacement = new FixReplacement(filePath1, new Range(3, 0, 3, 11), "modified");
    mockFileContent(filePath1, "First line\nSecond line\nThird line\n");
    replay(fileContentUtil);
    expectedException.expect(ResourceConflictException.class);
    toTreeModifications(fixReplacement);
}
Also used : FixReplacement(com.google.gerrit.reviewdb.client.FixReplacement) Range(com.google.gerrit.reviewdb.client.Comment.Range) Test(org.junit.Test)

Example 5 with FixReplacement

use of com.google.gerrit.reviewdb.client.FixReplacement in project gerrit by GerritCodeReview.

the class FixReplacementInterpreterTest method replacementsDoNotNeedToBeOrderedAccordingToRange.

@Test
public void replacementsDoNotNeedToBeOrderedAccordingToRange() throws Exception {
    FixReplacement fixReplacement1 = new FixReplacement(filePath1, new Range(1, 0, 2, 0), "1st modification\n");
    FixReplacement fixReplacement2 = new FixReplacement(filePath1, new Range(3, 0, 4, 0), "2nd modification\n");
    FixReplacement fixReplacement3 = new FixReplacement(filePath1, new Range(4, 0, 5, 0), "3rd modification\n");
    mockFileContent(filePath1, "First line\nSecond line\nThird line\nFourth line\nFifth line\n");
    replay(fileContentUtil);
    List<TreeModification> treeModifications = toTreeModifications(fixReplacement2, fixReplacement1, fixReplacement3);
    assertThatList(treeModifications).onlyElement().asChangeFileContentModification().newContent().isEqualTo("1st modification\nSecond line\n2nd modification\n3rd modification\nFifth line\n");
}
Also used : TreeModification(com.google.gerrit.server.edit.tree.TreeModification) FixReplacement(com.google.gerrit.reviewdb.client.FixReplacement) Range(com.google.gerrit.reviewdb.client.Comment.Range) Test(org.junit.Test)

Aggregations

FixReplacement (com.google.gerrit.reviewdb.client.FixReplacement)16 Range (com.google.gerrit.reviewdb.client.Comment.Range)13 Test (org.junit.Test)13 TreeModification (com.google.gerrit.server.edit.tree.TreeModification)9 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)3 Comment (com.google.gerrit.reviewdb.client.Comment)3 ArrayList (java.util.ArrayList)3 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 AutoValue (com.google.auto.value.AutoValue)1 MoreObjects (com.google.common.base.MoreObjects)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Strings (com.google.common.base.Strings)1 ListMultimap (com.google.common.collect.ListMultimap)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 Ordering (com.google.common.collect.Ordering)1 HashCode (com.google.common.hash.HashCode)1 Hashing (com.google.common.hash.Hashing)1 Nullable (com.google.gerrit.common.Nullable)1 RawInputUtil (com.google.gerrit.common.RawInputUtil)1