Refactor EditTransformer to take new inputs

EditTransformer is the class that we use to identify the edits due to
rebase between 2 commits. These rebase edits are a function of the edits
between these 2 commits, each of the commits and its parent, and the
edits between the parent commits.

The previous implementation of EditTransformer accepted a list of
PatchListEntry(s) corresponding to multiple changed files between 2
commits. This was not optimal, since EditTransformer uses only a small
subset of fields of PatchListEntry.

This change introduces a new entity class "FileEdits" containing only
the fields needed by EditTransformer, namely the list of edits and the
old/new file paths.

New methods are introduced in EditTransformer to use the FileEdits
entities, also some of the methods using the PatchListEntry are
eliminated. PatchListLoader (the old diff cache implementation) is
modified to use the new methods. Subsequent changes will introduce the
git file diff and the file diff caches which should use the new methods.

Change-Id: I3d31180b44d2495c4fa9ee8902f979f23c47cf4b
This commit is contained in:
Youssef Elghareeb
2020-09-02 12:23:21 +02:00
parent 97d2bcdb22
commit e48e9c2558
6 changed files with 230 additions and 67 deletions

View File

@@ -15,12 +15,17 @@
package com.google.gerrit.server.patch;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static java.util.stream.Collectors.toList;
import com.google.common.collect.ImmutableSet;
import com.google.gerrit.entities.Patch;
import com.google.gerrit.server.patch.GitPositionTransformer.FileMapping;
import com.google.gerrit.server.patch.GitPositionTransformer.Mapping;
import com.google.gerrit.server.patch.GitPositionTransformer.Range;
import com.google.gerrit.server.patch.GitPositionTransformer.RangeMapping;
import com.google.gerrit.server.patch.entities.Edit;
import com.google.gerrit.server.patch.entities.FileEdits;
import java.util.List;
/** Mappings derived from diffs. */
public class DiffMappings {
@@ -33,31 +38,47 @@ public class DiffMappings {
return Mapping.create(fileMapping, rangeMappings);
}
private static FileMapping toFileMapping(PatchListEntry patchListEntry) {
switch (patchListEntry.getChangeType()) {
public static Mapping toMapping(FileEdits fileEdits) {
FileMapping fileMapping = FileMapping.forFile(fileEdits.oldPath(), fileEdits.newPath());
ImmutableSet<RangeMapping> rangeMappings = toRangeMappings(fileEdits.edits());
return Mapping.create(fileMapping, rangeMappings);
}
private static FileMapping toFileMapping(PatchListEntry ple) {
return toFileMapping(ple.getChangeType(), ple.getOldName(), ple.getNewName());
}
private static FileMapping toFileMapping(
Patch.ChangeType changeType, String oldName, String newName) {
switch (changeType) {
case ADDED:
return FileMapping.forAddedFile(patchListEntry.getNewName());
return FileMapping.forAddedFile(newName);
case MODIFIED:
case REWRITE:
return FileMapping.forModifiedFile(patchListEntry.getNewName());
return FileMapping.forModifiedFile(newName);
case DELETED:
// Name of deleted file is mentioned as newName.
return FileMapping.forDeletedFile(patchListEntry.getNewName());
return FileMapping.forDeletedFile(newName);
case RENAMED:
case COPIED:
return FileMapping.forRenamedFile(patchListEntry.getOldName(), patchListEntry.getNewName());
return FileMapping.forRenamedFile(oldName, newName);
default:
throw new IllegalStateException("Unmapped diff type: " + patchListEntry.getChangeType());
throw new IllegalStateException("Unmapped diff type: " + changeType);
}
}
private static ImmutableSet<RangeMapping> toRangeMappings(PatchListEntry patchListEntry) {
return patchListEntry.getEdits().stream()
return toRangeMappings(
patchListEntry.getEdits().stream().map(Edit::fromJGitEdit).collect(toList()));
}
private static ImmutableSet<RangeMapping> toRangeMappings(List<Edit> edits) {
return edits.stream()
.map(
edit ->
RangeMapping.create(
Range.create(edit.getBeginA(), edit.getEndA()),
Range.create(edit.getBeginB(), edit.getEndB())))
Range.create(edit.beginA(), edit.endA()),
Range.create(edit.beginB(), edit.endB())))
.collect(toImmutableSet());
}
}