Search in sources :

Example 1 with FixReplacement

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

the class FixCalculatorVariousTest method severalChangesInNonConsecutiveLines.

@Test
public void severalChangesInNonConsecutiveLines() throws Exception {
    FixReplacement replace = new FixReplacement("path", new Range(1, 1, 1, 3), "ABC");
    FixReplacement insert = new FixReplacement("path", new Range(3, 5, 3, 5), "DEFG");
    FixReplacement delete = new FixReplacement("path", new Range(5, 9, 6, 0), "");
    FixResult result = FixCalculator.calculateFix(multilineContent, ImmutableList.of(replace, insert, delete));
    assertThat(result).text().isEqualTo("FABCst line\nSecond line\nThirdDEFG line\nFourth line\nFifth lin");
    assertThat(result).edits().hasSize(3);
    assertThat(result).edits().element(0).isReplace(0, 1, 0, 1);
    assertThat(result).edits().element(0).internalEdits().onlyElement().isReplace(1, 2, 1, 3);
    assertThat(result).edits().element(1).isReplace(2, 1, 2, 1);
    assertThat(result).edits().element(1).internalEdits().onlyElement().isInsert(5, 5, 4);
    assertThat(result).edits().element(2).isReplace(4, 1, 4, 1);
    assertThat(result).edits().element(2).internalEdits().onlyElement().isDelete(9, 2, 9);
}
Also used : FixResult(com.google.gerrit.server.fixes.FixCalculator.FixResult) FixReplacement(com.google.gerrit.entities.FixReplacement) Range(com.google.gerrit.entities.Comment.Range) Test(org.junit.Test)

Example 2 with FixReplacement

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

the class FixCalculatorVariousTest method changesMayTouch.

@Test
public void changesMayTouch() throws Exception {
    FixReplacement firstReplace = new FixReplacement("path", new Range(1, 6, 2, 7), "modified ");
    FixReplacement consecutiveReplace = new FixReplacement("path", new Range(2, 7, 3, 5), "content");
    FixResult result = FixCalculator.calculateFix(multilineContent, ImmutableList.of(firstReplace, consecutiveReplace));
    assertThat(result).text().isEqualTo("First modified content line\nFourth line\nFifth line\n");
    assertThat(result).edits().hasSize(1);
    Edit edit = result.edits.get(0);
    assertThat(edit).isReplace(0, 3, 0, 1);
    // The current code creates two inline edits even though only one would be necessary. It
    // shouldn't make a visual difference to the user and hence we can ignore this.
    assertThat(edit).internalEdits().hasSize(2);
    assertThat(edit).internalEdits().element(0).isReplace(6, 12, 6, 9);
    assertThat(edit).internalEdits().element(1).isReplace(18, 10, 15, 7);
}
Also used : FixResult(com.google.gerrit.server.fixes.FixCalculator.FixResult) Edit(org.eclipse.jgit.diff.Edit) FixReplacement(com.google.gerrit.entities.FixReplacement) Range(com.google.gerrit.entities.Comment.Range) Test(org.junit.Test)

Example 3 with FixReplacement

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

the class FixReplacementInterpreterTest method treeModificationsTargetCorrectFiles.

@Test
public void treeModificationsTargetCorrectFiles() throws Exception {
    FixReplacement fixReplacement = new FixReplacement(filePath1, new Range(1, 6, 3, 2), "Modified content");
    FixReplacement fixReplacement2 = new FixReplacement(filePath1, new Range(3, 5, 3, 5), "Second modification");
    mockFileContent(filePath1, "First line\nSecond line\nThird line\n");
    FixReplacement fixReplacement3 = new FixReplacement(filePath2, new Range(2, 0, 3, 0), "Another modified content");
    mockFileContent(filePath2, "1st line\n2nd line\n3rd line\n");
    CommitModification commitModification = toCommitModification(fixReplacement, fixReplacement3, fixReplacement2);
    List<TreeModification> sortedTreeModifications = getSortedCopy(commitModification.treeModifications());
    assertThatList(sortedTreeModifications).element(0).asChangeFileContentModification().filePaths().containsExactly(filePath1);
    assertThatList(sortedTreeModifications).element(0).asChangeFileContentModification().newContent().startsWith("First");
    assertThatList(sortedTreeModifications).element(1).asChangeFileContentModification().filePaths().containsExactly(filePath2);
    assertThatList(sortedTreeModifications).element(1).asChangeFileContentModification().newContent().startsWith("1st");
}
Also used : CommitModification(com.google.gerrit.server.edit.CommitModification) TreeModification(com.google.gerrit.server.edit.tree.TreeModification) FixReplacement(com.google.gerrit.entities.FixReplacement) Range(com.google.gerrit.entities.Comment.Range) Test(org.junit.Test)

Example 4 with FixReplacement

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

the class FixCalculator method calculateFix.

/**
 * Returns a result of applying fixes to an original content and list of applied edits.
 *
 * @param originalText is a text to which fixes must be applied
 * @param fixReplacements is a list of fixes to be applied
 * @return {@link FixResult}
 * @throws ResourceConflictException if the fixReplacements contains invalid data (for example, if
 *     an item points to an invalid range or if some ranges are intersected).
 */
public static FixResult calculateFix(Text originalText, List<FixReplacement> fixReplacements) throws ResourceConflictException {
    List<FixReplacement> sortedReplacements = new ArrayList<>(fixReplacements);
    sortedReplacements.sort(ASC_RANGE_FIX_REPLACEMENT_COMPARATOR);
    if (!sortedReplacements.isEmpty() && sortedReplacements.get(0).range.startLine <= 0) {
        throw new ResourceConflictException(String.format("Cannot calculate fix replacement for range %s", toString(sortedReplacements.get(0).range)));
    }
    ContentBuilder builder = new ContentBuilder(originalText);
    for (FixReplacement fixReplacement : sortedReplacements) {
        try {
            builder.addReplacement(fixReplacement);
        } catch (IndexOutOfBoundsException e) {
            throw new ResourceConflictException(String.format("Cannot calculate fix replacement for range %s", toString(fixReplacement.range)), e);
        }
    }
    return builder.build();
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ArrayList(java.util.ArrayList) FixReplacement(com.google.gerrit.entities.FixReplacement)

Example 5 with FixReplacement

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

the class FixReplacementInterpreter method toCommitModification.

/**
 * 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 CommitModification toCommitModification(Repository repository, ProjectState projectState, ObjectId patchSetCommitId, List<FixReplacement> fixReplacements) throws BadRequestException, ResourceNotFoundException, IOException, ResourceConflictException {
    requireNonNull(fixReplacements, "Fix replacements must not be null");
    Map<String, List<FixReplacement>> fixReplacementsPerFilePath = fixReplacements.stream().collect(groupingBy(fixReplacement -> fixReplacement.path));
    CommitModification.Builder modificationBuilder = CommitModification.builder();
    for (Map.Entry<String, List<FixReplacement>> entry : fixReplacementsPerFilePath.entrySet()) {
        if (Objects.equals(entry.getKey(), Patch.COMMIT_MSG)) {
            String newCommitMessage = getNewCommitMessage(repository, patchSetCommitId, entry.getValue());
            modificationBuilder.newCommitMessage(newCommitMessage);
        } else {
            TreeModification treeModification = toTreeModification(repository, projectState, patchSetCommitId, entry.getKey(), entry.getValue());
            modificationBuilder.addTreeModification(treeModification);
        }
    }
    return modificationBuilder.build();
}
Also used : ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) Patch(com.google.gerrit.entities.Patch) Inject(com.google.inject.Inject) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) RawInputUtil(com.google.gerrit.common.RawInputUtil) ChangeFileContentModification(com.google.gerrit.server.edit.tree.ChangeFileContentModification) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult) ImmutableList(com.google.common.collect.ImmutableList) FileContentUtil(com.google.gerrit.server.change.FileContentUtil) MagicFile(com.google.gerrit.server.patch.MagicFile) Objects.requireNonNull(java.util.Objects.requireNonNull) Map(java.util.Map) FixReplacement(com.google.gerrit.entities.FixReplacement) TreeModification(com.google.gerrit.server.edit.tree.TreeModification) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ProjectState(com.google.gerrit.server.project.ProjectState) IOException(java.io.IOException) ObjectId(org.eclipse.jgit.lib.ObjectId) Objects(java.util.Objects) CommitModification(com.google.gerrit.server.edit.CommitModification) List(java.util.List) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ObjectReader(org.eclipse.jgit.lib.ObjectReader) Repository(org.eclipse.jgit.lib.Repository) Range(com.google.gerrit.entities.Comment.Range) Singleton(com.google.inject.Singleton) CommitModification(com.google.gerrit.server.edit.CommitModification) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) TreeModification(com.google.gerrit.server.edit.tree.TreeModification) Map(java.util.Map)

Aggregations

FixReplacement (com.google.gerrit.entities.FixReplacement)14 Range (com.google.gerrit.entities.Comment.Range)10 Test (org.junit.Test)8 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)5 FixResult (com.google.gerrit.server.fixes.FixCalculator.FixResult)5 CommitModification (com.google.gerrit.server.edit.CommitModification)4 TreeModification (com.google.gerrit.server.edit.tree.TreeModification)4 Edit (org.eclipse.jgit.diff.Edit)4 ImmutableList (com.google.common.collect.ImmutableList)3 ProjectState (com.google.gerrit.server.project.ProjectState)3 Inject (com.google.inject.Inject)3 Singleton (com.google.inject.Singleton)3 IOException (java.io.IOException)3 List (java.util.List)3 Map (java.util.Map)3 Collectors.groupingBy (java.util.stream.Collectors.groupingBy)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 Change (com.google.gerrit.entities.Change)2 Patch (com.google.gerrit.entities.Patch)2 PatchSet (com.google.gerrit.entities.PatchSet)2