Generalize and extract the transformation logic from EditTransformer
Until now, we needed the transformation logic only for identifying edits due to rebase. Ported comments (I4e96af5ac) will need the same basic algorithm, though, which we don't want to duplicate. There are still some differences (e.g. ported comments requiring comment locations to be transformed and not diff edits) because of which we need a more generalized version. We already account for some of these differences in this change. Further adjustments (e.g. explicit support and handling of deleted files; not-existing ranges) will be done in further changes. Extracting the logic will also allow us to add detailed unit tests for it. Arguably, we could have already had unit tests for EditTransformer. However, construction of all the JGit based inputs like PatchListEntry (which requires a properly filled FileHeader from JGit, which requires further properly filled JGit objects) is hairy. Adding unit tests for GitPositionTransformer is deferred to a later time, though. Even without these tests, we should notice any errors which could be potentially introduced by this change through the extensive tests for edits due to rebase. Change-Id: I4062e6a2274ea71f862ac132421b67826091fc56
This commit is contained in:
52
java/com/google/gerrit/server/patch/DiffMappings.java
Normal file
52
java/com/google/gerrit/server/patch/DiffMappings.java
Normal file
@@ -0,0 +1,52 @@
|
||||
// Copyright (C) 2020 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.server.patch;
|
||||
|
||||
import static com.google.common.collect.ImmutableSet.toImmutableSet;
|
||||
|
||||
import com.google.common.base.MoreObjects;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
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;
|
||||
|
||||
/** Mappings derived from diffs. */
|
||||
public class DiffMappings {
|
||||
|
||||
private DiffMappings() {}
|
||||
|
||||
public static Mapping toMapping(PatchListEntry patchListEntry) {
|
||||
// This is just a direct translation of the former logic in EditTransformer. It doesn't
|
||||
// work for file deletions, though. As file deletions aren't relevant for 'edits due to rebase'
|
||||
// situations, we didn't notice this in the past.
|
||||
// TODO(aliceks): Fix for file deletions in another change.
|
||||
FileMapping fileMapping =
|
||||
FileMapping.create(getOldFilePath(patchListEntry), patchListEntry.getNewName());
|
||||
ImmutableSet<RangeMapping> rangeMappings =
|
||||
patchListEntry.getEdits().stream()
|
||||
.map(
|
||||
edit ->
|
||||
RangeMapping.create(
|
||||
Range.create(edit.getBeginA(), edit.getEndA()),
|
||||
Range.create(edit.getBeginB(), edit.getEndB())))
|
||||
.collect(toImmutableSet());
|
||||
return Mapping.create(fileMapping, rangeMappings);
|
||||
}
|
||||
|
||||
private static String getOldFilePath(PatchListEntry patchListEntry) {
|
||||
return MoreObjects.firstNonNull(patchListEntry.getOldName(), patchListEntry.getNewName());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user